-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathMinia.cpp
More file actions
296 lines (242 loc) · 12.1 KB
/
Copy pathMinia.cpp
File metadata and controls
296 lines (242 loc) · 12.1 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/*****************************************************************************
* GATB : Genome Assembly Tool Box
* Copyright (C) 2014 INRIA
* Authors: R.Chikhi, G.Rizk, E.Drezen
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*****************************************************************************/
/********************************************************************************/
// We include required definitions
/********************************************************************************/
#include <Minia.hpp>
#include <NodeSelector.hpp>
#include <fstream>
using namespace std;
/********************************************************************************/
#define DEBUG(a) //a
/********************************************************************************/
static const char* STR_TRAVERSAL_KIND = "-traversal";
static const char* STR_STARTER_KIND = "-starter";
static const char* STR_CONTIG_MAX_LEN = "-contig-max-len";
static const char* STR_BFS_MAX_DEPTH = "-bfs-max-depth";
static const char* STR_BFS_MAX_BREADTH = "-bfs-max-breadth";
static const char* STR_NO_LENGTH_CUTOFF = "-no-length-cutoff";
static const char* STR_FASTA_LINE_SIZE = "-fasta-line";
static const char* progressFormat0 = "Assembly ";
/*********************************************************************
** METHOD :
** PURPOSE :
** INPUT :
** OUTPUT :
** RETURN :
** REMARKS :
*********************************************************************/
Minia::Minia () : Tool ("minia")
{
// reinit the parser to get rid of options added by the Tool class, as we'll add them again in the Graph parser
setParser (new OptionsParser ("minia"));
/** We add options specific to Minia (most important at the end). */
OptionsParser* assemblyParser = new OptionsParser ("assembly");
assemblyParser->push_front (new OptionOneParam (STR_FASTA_LINE_SIZE, "number of nucleotides per line in fasta output (0 means one line)", false, "0"));
assemblyParser->push_front (new OptionOneParam (STR_BFS_MAX_BREADTH, "maximum breadth for BFS", false, "0" ));
assemblyParser->push_front (new OptionOneParam (STR_BFS_MAX_DEPTH, "maximum depth for BFS", false, "0" ));
assemblyParser->push_front (new OptionOneParam (STR_CONTIG_MAX_LEN, "maximum length for contigs", false, "0" ));
assemblyParser->push_front (new OptionOneParam (STR_STARTER_KIND, "starting node ('best', 'simple')", false, "best" ));
assemblyParser->push_front (new OptionOneParam (STR_TRAVERSAL_KIND, "traversal type ('contig', 'unitig')", false, "contig" ));
assemblyParser->push_front (new OptionNoParam (STR_NO_LENGTH_CUTOFF, "turn off length cutoff of 2*k in output sequences", false));
assemblyParser->push_front (new OptionOneParam (STR_URI_INPUT, "input reads (fasta/fastq/compressed)", false));
assemblyParser->push_front (new OptionOneParam (STR_URI_GRAPH, "input graph file (hdf5)", false));
getParser()->push_back (assemblyParser);
// when we input reads, dbgh5 is executed, so its options are needed here
IOptionsParser* graphParser = Graph::getOptionsParser(false, true);
// we hide the STR_URI_INPUT option, otherwise we would have it twice
if (IOptionsParser* p = graphParser->getParser(STR_URI_INPUT)) { p->setVisible(false); }
getParser()->push_back(graphParser, 1);
}
/*********************************************************************
** METHOD :
** PURPOSE :
** INPUT :
** OUTPUT :
** RETURN :
** REMARKS :
*********************************************************************/
void Minia::execute ()
{
Graph graph;
if (getInput()->get(STR_URI_GRAPH) != 0)
{
graph = Graph::load (getInput()->getStr(STR_URI_GRAPH));
}
else if (getInput()->get(STR_URI_INPUT) != 0)
{
graph = Graph::create (getInput());
}
else
{
throw OptionFailure (getParser(), "Specifiy -graph or -in");
}
/** We build the contigs. */
assemble (graph);
/** We gather some statistics. */
getInfo()->add (1, getTimeInfo().getProperties("time"));
}
/*********************************************************************
** METHOD :
** PURPOSE :
** INPUT :
** OUTPUT :
** RETURN :
** REMARKS :
*********************************************************************/
void Minia::assemble (const Graph& graph)
{
TIME_INFO (getTimeInfo(), "assembly");
string output = (getInput()->get(STR_URI_OUTPUT) ?
getInput()->getStr(STR_URI_OUTPUT) :
System::file().getBaseName (
(getInput()->get(STR_URI_INPUT) ? getInput()->getStr(STR_URI_INPUT) :
getInput()->getStr(STR_URI_GRAPH))
)
)+ ".contigs.fa";
/** We setup default values if needed. */
if (getInput()->getInt (STR_CONTIG_MAX_LEN) == 0) { getInput()->setInt (STR_CONTIG_MAX_LEN, Traversal::defaultMaxLen); }
if (getInput()->getInt (STR_BFS_MAX_DEPTH) == 0) { getInput()->setInt (STR_BFS_MAX_DEPTH, Traversal::defaultMaxDepth); }
if (getInput()->getInt (STR_BFS_MAX_BREADTH) == 0) { getInput()->setInt (STR_BFS_MAX_BREADTH, Traversal::defaultMaxBreadth); }
/** We create the output bank. Note that we could make this a little bit prettier
* => possibility to save the contigs in specific output format (other than fasta). */
IBank* outputBank = new BankFasta (output);
LOCAL (outputBank);
/** We set the fasta line size. */
BankFasta::setDataLineSize (getInput()->getInt (STR_FASTA_LINE_SIZE));
/** We get an iterator over the branching nodes. */
ProgressGraphIterator<BranchingNode,ProgressTimerAndSystem> itBranching (graph.iterator<BranchingNode>(), progressFormat0);
/** We create the Terminator. */
BranchingTerminator terminator (graph);
/** We create the starting node selector according to the user choice. */
INodeSelector* starter = NodeSelectorFactory::singleton().create (getInput()->getStr(STR_STARTER_KIND), graph, terminator);
LOCAL (starter);
/** We create the Traversal instance according to the user choice. */
Traversal* traversal = Traversal::create (
getInput()->getStr(STR_TRAVERSAL_KIND),
graph,
terminator,
getInput()->getInt (STR_CONTIG_MAX_LEN),
getInput()->getInt (STR_BFS_MAX_DEPTH),
getInput()->getInt (STR_BFS_MAX_BREADTH)
);
LOCAL (traversal);
Path consensusRight;
Path consensusLeft;
u_int64_t nbContigs = 0;
u_int64_t nbSmallContigs = 0;
u_int64_t totalNt = 0;
u_int64_t maxContigLen = 0;
u_int64_t maxContigLenLeft = 0;
u_int64_t maxContigLenRight = 0;
bool isNoLengthCutoff = getParser()->saw(STR_NO_LENGTH_CUTOFF);
Sequence seq (Data::ASCII);
/** We loop over the branching nodes. */
for (itBranching.first(); !itBranching.isDone(); itBranching.next())
{
DEBUG ((cout << endl << "-------------------------- " << graph.toString (itBranching.item()) << " -------------------------" << endl));
Node startingNode;
// keep looping while a starting kmer is available from this kmer
// everything will be marked during the traversal()'s
while (starter->select (itBranching.item(), startingNode) == true)
{
/** We compute right and left extensions of the starting node. */
int lenRight = traversal->traverse (startingNode, DIR_OUTCOMING, consensusRight);
int lenLeft = traversal->traverse (graph.reverse(startingNode), DIR_OUTCOMING, consensusLeft);
int lenTotal = graph.getKmerSize() + lenRight + lenLeft;
/** We keep this contig if its size is long enough. */
if (lenTotal >= 2*graph.getKmerSize()+1 || isNoLengthCutoff)
{
/** We create the contig sequence. */
buildSequence (graph, startingNode, lenTotal, nbContigs, consensusRight, consensusLeft, seq);
/** We add the sequence into the output bank. */
outputBank->insert (seq);
nbContigs += 1;
totalNt += lenTotal;
traversal->commit_stats();
if (lenTotal > maxContigLen) { maxContigLen = lenTotal; }
if (lenLeft > maxContigLenLeft) { maxContigLenLeft = lenLeft; }
if (lenRight > maxContigLenRight) { maxContigLenRight = lenRight; }
}
else
{
traversal->revert_stats();
nbSmallContigs++;
}
} /* end of while (starter->select() */
} /* end of for (itBranching.first() */
/** We add the input parameters to the global properties. */
getInfo()->add (1, getInput());
/** We gather some statistics. */
getInfo()->add (1, "stats");
getInfo()->add (2, "traversal", "%s", traversal->getName().c_str());
getInfo()->add (2, "start_selector", "%s", starter->getName().c_str());
getInfo()->add (2, "nb_contigs", "%d", nbContigs);
getInfo()->add (2, "nb_small_contigs_discarded","%d", nbSmallContigs);
getInfo()->add (2, "nt_assembled", "%ld", totalNt);
getInfo()->add (2, "max_length", "%d", maxContigLen);
getInfo()->add (2, "max_length_left", "%d", maxContigLenLeft);
getInfo()->add (2, "max_length_right", "%d", maxContigLenRight);
getInfo()->add (2, "debugging traversal stats");
getInfo()->add (2, "large breadth", "%d", traversal->final_stats.couldnt_traverse_bubble_breadth);
getInfo()->add (2, "large depth", "%d", traversal->final_stats.couldnt_traverse_bubble_depth);
getInfo()->add (2, "marked kmer inside traversal", "%d", traversal->final_stats.couldnt_because_marked_kmer);
getInfo()->add (2, "traversal ends with dead-ends", "%d", traversal->final_stats.couldnt_find_extension);
getInfo()->add (2, "in-branching large depth", "%d", traversal->final_stats.couldnt_inbranching_depth);
getInfo()->add (2, "in-branching large breadth", "%d", traversal->final_stats.couldnt_inbranching_breadth);
getInfo()->add (2, "in-branching other", "%d", traversal->final_stats.couldnt_inbranching_other);
getInfo()->add (2, "couldn't validate consensuses", "%d", traversal->final_stats.couldnt_validate_consensuses);
}
/*********************************************************************
** METHOD :
** PURPOSE :
** INPUT :
** OUTPUT :
** RETURN :
** REMARKS :
*********************************************************************/
void Minia::buildSequence (
const Graph& graph,
const Node& startingNode,
size_t length,
size_t nbContigs,
const Path& consensusRight,
const Path& consensusLeft,
Sequence& seq
)
{
/** Shortcuts. */
Data& data = seq.getData();
size_t lenRight = consensusRight.size();
size_t lenLeft = consensusLeft.size ();
/** We set the sequence comment. */
stringstream ss1;
ss1 << nbContigs << "__len__" << length;
seq._comment = ss1.str();
/** We set the data length. */
seq.getData().resize (length);
size_t idx=0;
/** We dump the left part. */
for (size_t i=0; i<lenLeft; i++) { data[idx++] = ascii (reverse(consensusLeft [lenLeft-i-1])); }
/** We dump the starting node. */
string node = graph.toString (startingNode);
for (size_t i=0; i<node.size(); i++) { data[idx++] = node[i]; }
/** We dump the right part. */
for (size_t i=0; i<lenRight; i++) { data[idx++] = ascii (consensusRight[i]); }
}