2
Copyright (c) 2008-2011 Gordon Gremme <gremme@zbh.uni-hamburg.de>
3
Copyright (c) 2011 Giorgio Gonnella <gonnella@zbh.uni-hamburg.de>
4
Copyright (c) 2008-2011 Center for Bioinformatics, University of Hamburg
6
Permission to use, copy, modify, and distribute this software for any
7
purpose with or without fee is hereby granted, provided that the above
8
copyright notice and this permission notice appear in all copies.
10
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19
#include "core/bioseq.h"
20
#include "core/fasta.h"
21
#include "core/fileutils_api.h"
23
#include "core/outputfile.h"
24
#include "core/option_api.h"
25
#include "core/progressbar.h"
26
#include "core/seqiterator_sequence_buffer.h"
27
#include "core/string_distri.h"
28
#include "core/unused_api.h"
29
#include "extended/md5set.h"
30
#include "tools/gt_sequniq.h"
33
bool seqit, verbose, rev;
34
unsigned long width, nofseqs;
35
GtOutputFileInfo *ofi;
39
static void* gt_sequniq_arguments_new(void)
41
GtSequniqArguments *arguments = gt_calloc((size_t)1, sizeof *arguments);
42
arguments->ofi = gt_outputfileinfo_new();
46
static void gt_sequniq_arguments_delete(void *tool_arguments)
48
GtSequniqArguments *arguments = tool_arguments;
49
if (!arguments) return;
50
gt_file_delete(arguments->outfp);
51
gt_outputfileinfo_delete(arguments->ofi);
55
static GtOptionParser* gt_sequniq_option_parser_new(void *tool_arguments)
57
GtSequniqArguments *arguments = tool_arguments;
59
GtOption *seqit_option, *verbose_option, *width_option, *rev_option,
63
op = gt_option_parser_new("[option ...] sequence_file [...] ",
64
"Filter out repeated sequences in given in given "
68
seqit_option = gt_option_new_bool("seqit", "use sequence iterator",
69
&arguments->seqit, false);
70
gt_option_is_development_option(seqit_option);
71
gt_option_parser_add_option(op, seqit_option);
74
nofseqs_option = gt_option_new_ulong("nofseqs", "number of sequences "
75
"(improves efficiency)\ndefault: unspecified",
76
&arguments->nofseqs, 0);
77
gt_option_is_development_option(nofseqs_option);
78
gt_option_hide_default(nofseqs_option);
79
gt_option_parser_add_option(op, nofseqs_option);
82
rev_option = gt_option_new_bool("rev", "filter out also sequences whose "
83
"reverse complement is identical to a sequence already output",
84
&arguments->rev, false);
85
gt_option_parser_add_option(op, rev_option);
88
verbose_option = gt_option_new_verbose(&arguments->verbose);
89
gt_option_parser_add_option(op, verbose_option);
92
width_option = gt_option_new_width(&arguments->width);
93
gt_option_parser_add_option(op, width_option);
95
gt_outputfile_register_options(op, &arguments->outfp, arguments->ofi);
97
/* option implications */
98
gt_option_imply(verbose_option, seqit_option);
100
gt_option_parser_set_min_args(op, 1U);
104
static int gt_sequniq_runner(int argc, const char **argv, int parsed_args,
105
void *tool_arguments, GtError *err)
107
GtSequniqArguments *arguments = tool_arguments;
108
unsigned long long duplicates = 0, num_of_sequences = 0;
113
gt_assert(arguments);
114
md5set = gt_md5set_new(arguments->nofseqs);
115
if (!arguments->seqit) {
119
for (i = parsed_args; !had_err && i < argc; i++) {
120
if (!(bs = gt_bioseq_new(argv[i], err)))
123
GtMD5SetStatus retval;
124
for (j = 0; j < gt_bioseq_number_of_sequences(bs) && !had_err; j++) {
125
retval = gt_md5set_add_sequence(md5set,
126
gt_bioseq_get_sequence(bs, j),
127
gt_bioseq_get_sequence_length(bs, j),
128
arguments->rev, err);
129
if (retval == GT_MD5SET_NOT_FOUND)
130
gt_fasta_show_entry(gt_bioseq_get_description(bs, j),
131
gt_bioseq_get_sequence(bs, j),
132
gt_bioseq_get_sequence_length(bs, j),
133
arguments->width, arguments->outfp);
134
else if (retval != GT_MD5SET_ERROR)
140
gt_bioseq_delete(bs);
145
GtSeqIterator *seqit;
148
const GtUchar *sequence;
152
files = gt_str_array_new();
153
for (i = parsed_args; i < argc; i++)
154
gt_str_array_add_cstr(files, argv[i]);
155
totalsize = gt_files_estimate_total_size(files);
156
seqit = gt_seqiterator_sequence_buffer_new(files, err);
160
if (arguments->verbose) {
161
gt_progressbar_start(gt_seqiterator_getcurrentcounter(seqit,
164
(unsigned long long) totalsize);
167
GtMD5SetStatus retval;
168
if ((gt_seqiterator_next(seqit, &sequence, &len, &desc, err)) != 1)
171
retval = gt_md5set_add_sequence(md5set, (const char*) sequence, len,
172
arguments->rev, err);
173
if (retval == GT_MD5SET_NOT_FOUND)
174
gt_fasta_show_entry(desc, (const char*) sequence, len,
175
arguments->width, arguments->outfp);
176
else if (retval != GT_MD5SET_ERROR)
182
if (arguments->verbose)
183
gt_progressbar_stop();
184
gt_seqiterator_delete(seqit);
186
gt_str_array_delete(files);
189
/* show statistics */
191
fprintf(stderr, "# %lu out of %lu sequences have been removed (%.3f%%)\n",
192
(unsigned long)duplicates, (unsigned long)num_of_sequences,
193
((double) duplicates / (double)num_of_sequences) * 100.0);
196
gt_md5set_delete(md5set);
200
GtTool* gt_sequniq(void)
202
return gt_tool_new(gt_sequniq_arguments_new,
203
gt_sequniq_arguments_delete,
204
gt_sequniq_option_parser_new,