forked from marbl/Mash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CommandSketch.cpp
171 lines (139 loc) · 4.92 KB
/
CommandSketch.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// Copyright © 2015, Battelle National Biodefense Institute (BNBI);
// all rights reserved. Authored by: Brian Ondov, Todd Treangen,
// Sergey Koren, and Adam Phillippy
//
// See the LICENSE.txt file included with this software for license information.
#include "CommandSketch.h"
#include "Sketch.h"
#include "sketchParameterSetup.h"
#include <iostream>
using std::cerr;
using std::endl;
using std::string;
using std::vector;
namespace mash {
CommandSketch::CommandSketch()
: Command()
{
name = "sketch";
summary = "Create sketches (reduced representations for fast operations).";
description = "Create a sketch file, which is a reduced representation of a sequence or set of sequences (based on min-hashes) that can be used for fast distance estimations. Inputs can be fasta or fastq files (gzipped or not), and \"-\" can be given to read from standard input. Input files can also be files of file names (see -l). For output, one sketch file will be generated, but it can have multiple sketches within it, divided by sequences or files (see -i). By default, the output file name will be the first input file with a '.msh' extension, or 'stdin.msh' if standard input is used (see -o).";
argumentString = "<input> [<input>] ...";
useOption("help");
addOption("list", Option(Option::Boolean, "l", "Input", "List input. Lines in each <input> specify paths to sequence files, one per line.", ""));
addOption("prefix", Option(Option::File, "o", "Output", "Output prefix (first input file used if unspecified). The suffix '.msh' will be appended.", ""));
addOption("id", Option(Option::File, "I", "Sketch", "ID field for sketch of reads (instead of first sequence ID).", ""));
addOption("comment", Option(Option::File, "C", "Sketch", "Comment for a sketch of reads (instead of first sequence comment).", ""));
addOption("counts", Option(Option::Boolean, "M", "Sketch", "Store multiplicity of each k-mer in each sketch.", ""));
useSketchOptions();
}
int CommandSketch::run() const
{
if ( arguments.size() == 0 || options.at("help").active )
{
print();
return 0;
}
int verbosity = 1;//options.at("silent").active ? 0 : options.at("verbose").active ? 2 : 1;
bool list = options.at("list").active;
Sketch::Parameters parameters;
parameters.counts = options.at("counts").active;
if ( sketchParameterSetup(parameters, *(Command *)this) )
{
return 1;
}
for ( int i = 0; i < arguments.size(); i++ )
{
if ( false && hasSuffix(arguments[i], suffixSketch) )
{
cerr << "ERROR: " << arguments[i] << " looks like it is already sketched." << endl;
exit(1);
}
}
Sketch sketch;
uint64_t lengthMax;
double randomChance;
int kMin;
string lengthMaxName;
int warningCount = 0;
vector<string> files;
for ( int i = 0; i < arguments.size(); i++ )
{
if ( list )
{
splitFile(arguments[i], files);
}
else
{
files.push_back(arguments[i]);
}
}
if ( getOption("id").active || getOption("comment").active )
{
if ( files.size() > 1 && ! parameters.reads )
{
cerr << "WARNING: -I and -C will only apply to first sketch" << endl;
}
}
if ( parameters.reads )
{
sketch.initFromReads(files, parameters);
}
else
{
sketch.initFromFiles(files, parameters, verbosity);
}
if ( getOption("id").active )
{
sketch.setReferenceName(0, getOption("id").argument);
}
if ( getOption("comment").active )
{
sketch.setReferenceComment(0, getOption("comment").argument);
}
double lengthThreshold = (parameters.warning * sketch.getKmerSpace()) / (1. - parameters.warning);
for ( int i = 0; i < sketch.getReferenceCount(); i++ )
{
uint64_t length = sketch.getReference(i).length;
if ( length > lengthThreshold )
{
if ( warningCount == 0 || length > lengthMax )
{
lengthMax = length;
lengthMaxName = sketch.getReference(i).name;
randomChance = sketch.getRandomKmerChance(i);
kMin = sketch.getMinKmerSize(i);
}
warningCount++;
}
}
string prefix;
if ( options.at("prefix").argument.length() > 0 )
{
prefix = options.at("prefix").argument;
}
else
{
if ( arguments[0] == "-" )
{
prefix = "stdin";
}
else
{
prefix = arguments[0];
}
}
string suffix = parameters.windowed ? suffixSketchWindowed : suffixSketch;
if ( ! hasSuffix(prefix, suffix) )
{
prefix += suffix;
}
cerr << "Writing to " << prefix << "..." << endl;
sketch.writeToCapnp(prefix.c_str());
if ( warningCount > 0 && ! parameters.reads )
{
warnKmerSize(parameters, *this, lengthMax, lengthMaxName, randomChance, kMin, warningCount);
}
return 0;
}
} // namespace mash