~ubuntu-branches/ubuntu/quantal/genometools/quantal-backports

« back to all changes in this revision

Viewing changes to src/tools/gt_seqfilter.c

  • Committer: Package Import Robot
  • Author(s): Sascha Steinbiss
  • Date: 2012-07-09 14:10:23 UTC
  • Revision ID: package-import@ubuntu.com-20120709141023-juuu4spm6chqsf9o
Tags: upstream-1.4.1
ImportĀ upstreamĀ versionĀ 1.4.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
  Copyright (c) 2008-2010 Gordon Gremme <gremme@zbh.uni-hamburg.de>
 
3
  Copyright (c) 2008      Center for Bioinformatics, University of Hamburg
 
4
 
 
5
  Permission to use, copy, modify, and distribute this software for any
 
6
  purpose with or without fee is hereby granted, provided that the above
 
7
  copyright notice and this permission notice appear in all copies.
 
8
 
 
9
  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 
10
  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 
11
  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 
12
  ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 
13
  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 
14
  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 
15
  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 
16
*/
 
17
 
 
18
#include <string.h>
 
19
#include "core/bioseq_iterator.h"
 
20
#include "core/fasta.h"
 
21
#include "core/ma.h"
 
22
#include "core/outputfile.h"
 
23
#include "core/option_api.h"
 
24
#include "core/undef_api.h"
 
25
#include "tools/gt_seqfilter.h"
 
26
 
 
27
typedef struct {
 
28
  unsigned long minlength,
 
29
                maxlength,
 
30
                maxseqnum,
 
31
                width;
 
32
  GtOutputFileInfo *ofi;
 
33
  GtFile *outfp;
 
34
} SeqFilterArguments;
 
35
 
 
36
static void* gt_seqfilter_arguments_new(void)
 
37
{
 
38
  SeqFilterArguments *arguments = gt_calloc(1, sizeof *arguments);
 
39
  arguments->ofi = gt_outputfileinfo_new();
 
40
  return arguments;
 
41
}
 
42
 
 
43
static void gt_seqfilter_arguments_delete(void *tool_arguments)
 
44
{
 
45
  SeqFilterArguments *arguments = tool_arguments;
 
46
  if (!arguments) return;
 
47
  gt_file_delete(arguments->outfp);
 
48
  gt_outputfileinfo_delete(arguments->ofi);
 
49
  gt_free(arguments);
 
50
}
 
51
 
 
52
static GtOptionParser* gt_seqfilter_option_parser_new(void *tool_arguments)
 
53
{
 
54
  SeqFilterArguments *arguments = tool_arguments;
 
55
  GtOption *option;
 
56
  GtOptionParser *op;
 
57
  gt_assert(arguments);
 
58
 
 
59
  op = gt_option_parser_new("[option ...] [sequence_file ...]",
 
60
                            "Filter the given sequence_file(s) and show the "
 
61
                            "results on stdout.");
 
62
 
 
63
  /* -minlength */
 
64
  option = gt_option_new_ulong("minlength",
 
65
                               "set minimum length a sequence must "
 
66
                               "have to pass the filter", &arguments->minlength,
 
67
                               GT_UNDEF_ULONG);
 
68
  gt_option_parser_add_option(op, option);
 
69
 
 
70
  /* -maxlength */
 
71
  option = gt_option_new_ulong("maxlength", "set maximum length a sequence can "
 
72
                               "have to pass the filter", &arguments->maxlength,
 
73
                               GT_UNDEF_ULONG);
 
74
  gt_option_parser_add_option(op, option);
 
75
 
 
76
  /* -maxseqnum */
 
77
  option = gt_option_new_ulong("maxseqnum", "set the maximum number of "
 
78
                               "sequences which can pass the filter",
 
79
                               &arguments->maxseqnum, GT_UNDEF_ULONG);
 
80
  gt_option_parser_add_option(op, option);
 
81
 
 
82
  /* -width */
 
83
  option = gt_option_new_width(&arguments->width);
 
84
  gt_option_parser_add_option(op, option);
 
85
 
 
86
  gt_outputfile_register_options(op, &arguments->outfp, arguments->ofi);
 
87
 
 
88
  return op;
 
89
}
 
90
 
 
91
static int gt_seqfilter_runner(int argc, const char **argv, int parsed_args,
 
92
                               void *tool_arguments, GtError *err)
 
93
{
 
94
  SeqFilterArguments *arguments = tool_arguments;
 
95
  GtBioseqIterator *bsi;
 
96
  GtBioseq *bioseq;
 
97
  unsigned long i;
 
98
  unsigned long long passed = 0, filtered = 0, num_of_sequences = 0;
 
99
  int had_err = 0;
 
100
 
 
101
  gt_error_check(err);
 
102
  gt_assert(tool_arguments);
 
103
 
 
104
  bsi = gt_bioseq_iterator_new(argc - parsed_args, argv + parsed_args);
 
105
 
 
106
  while (!(had_err = gt_bioseq_iterator_next(bsi, &bioseq, err)) && bioseq) {
 
107
    for (i = 0; i < gt_bioseq_number_of_sequences(bioseq); i++) {
 
108
      if ((arguments->minlength == GT_UNDEF_ULONG ||
 
109
           gt_bioseq_get_sequence_length(bioseq, i) >= arguments->minlength) &&
 
110
          (arguments->maxlength == GT_UNDEF_ULONG ||
 
111
           gt_bioseq_get_sequence_length(bioseq, i) <= arguments->maxlength) &&
 
112
          (arguments->maxseqnum == GT_UNDEF_ULONG ||
 
113
           passed + 1 <= arguments->maxseqnum)) {
 
114
        gt_fasta_show_entry(gt_bioseq_get_description(bioseq, i),
 
115
                            gt_bioseq_get_sequence(bioseq, i),
 
116
                            gt_bioseq_get_sequence_length(bioseq, i),
 
117
                            arguments->width, arguments->outfp);
 
118
        passed++;
 
119
      }
 
120
      else
 
121
        filtered++;
 
122
      num_of_sequences++;
 
123
    }
 
124
    gt_bioseq_delete(bioseq);
 
125
  }
 
126
 
 
127
  /* show statistics */
 
128
  if (!had_err) {
 
129
    gt_assert(passed + filtered == num_of_sequences);
 
130
    fprintf(stderr, "# %llu out of %llu sequences have been removed (%.3f%%)\n",
 
131
            filtered, num_of_sequences,
 
132
            ((double) filtered / num_of_sequences) * 100.0);
 
133
  }
 
134
 
 
135
  gt_bioseq_iterator_delete(bsi);
 
136
 
 
137
  return had_err;
 
138
}
 
139
 
 
140
GtTool* gt_seqfilter(void)
 
141
{
 
142
  return gt_tool_new(gt_seqfilter_arguments_new,
 
143
                     gt_seqfilter_arguments_delete,
 
144
                     gt_seqfilter_option_parser_new,
 
145
                     NULL,
 
146
                     gt_seqfilter_runner);
 
147
}