/
fixed_len_trim.cpp
96 lines (78 loc) · 3.09 KB
/
fixed_len_trim.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include "common.h"
struct ModifyStringOptions
{
CharString inputFileName;
CharString outputFileName;
int length;
};
seqan::ArgumentParser::ParseResult parseCommandLine(ModifyStringOptions & options, int argc, char const ** argv)
{
seqan::ArgumentParser parser("fixed_len_trim");
addOption(parser, seqan::ArgParseOption("i", "input-file", "Path to the input file. Supported input: fq, fq.gz, fastq, fastq.gz, fasta, fasta.gz, fa and fa.gz.", seqan::ArgParseArgument::INPUT_FILE, "IN"));
setRequired(parser, "input-file");
setShortDescription(parser, "Methylation Tools");
setVersion(parser, "0.0.1");
setDate(parser, "July 2016");
addUsageLine(parser, "-i [input file] -o [output file] -l [trim length]");
addOption(parser, seqan::ArgParseOption("l", "length", "Length to trim to.",seqan::ArgParseArgument::INTEGER, "INT"));
setRequired(parser, "l");
addOption(parser, seqan::ArgParseOption("o", "output-file", "Path to the output file. You must include a file extension. Supported output types: fq, fastq, fasta and fa.", seqan::ArgParseArgument::OUTPUT_FILE, "OUT"));
setRequired(parser, "o");
addDescription(parser, "Trims your fasta files to a fixed length.");
seqan::ArgumentParser::ParseResult res = seqan::parse(parser, argc, argv);
// Extract options ONLY if the args are parsed correctly
if (res != seqan::ArgumentParser::PARSE_OK)
return res;
getOptionValue(options.inputFileName, parser, "input-file");
getOptionValue(options.length, parser, "length");
getOptionValue(options.outputFileName, parser, "output-file");
return seqan::ArgumentParser::PARSE_OK;
}
int main(int argc, char const ** argv)
{
//parse our options
ModifyStringOptions options;
seqan::ArgumentParser::ParseResult res = parseCommandLine(options, argc, argv);
// If parsing was not successful then exit with code 1 if there were errors.
// Otherwise, exit with code 0 (e.g. help was printed).
if (res != seqan::ArgumentParser::PARSE_OK)
return res == seqan::ArgumentParser::PARSE_ERROR;
SeqFileIn seqFileIn;
if (!open(seqFileIn, toCString(options.inputFileName)))
{
std::cerr << "ERROR: Could not open the file.\n";
return 1;
}
CharString id;
Dna5String seq;
CharString qual;
SeqFileOut seqFileOut;
if (!open(seqFileOut, toCString(options.outputFileName)))
{
std::cerr << "ERROR: Could not open the file.\n";
return 1;
}
while(!atEnd(seqFileIn))
{
try
{
readRecord(id, seq, qual, seqFileIn);
}
catch (Exception const & e)
{
std::cout << "ERROR: " << e.what() << std::endl;
return 1;
}
if(length(seq) >= options.length)
{
String<Dna> dnaSeq;
resize(dnaSeq, options.length, Exact());
assign(dnaSeq, seq, Limit());
CharString dnaQual;
resize(dnaQual, options.length, Exact());
assign(dnaQual, qual, Limit());
writeRecord(seqFileOut, id, dnaSeq, dnaQual);
}
}
return 0;
}