-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhaltraverse.cpp
380 lines (365 loc) · 16.2 KB
/
haltraverse.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
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
#include <iostream>
#include <cstdlib>
#include <hal.h>
#include <fstream>
#include "./intervaltree/IntervalTree.h"
using namespace std;
using namespace hal;
class HalEdge{
// Holds information about an edge connecting two segments in a HAL graph.
public:
string parentGenome;
string parentSeq;
int parentStart;
int parentEnd;
string childGenome;
string childSeq;
int childStart;
int childEnd;
bool reversed;
set<string> features;
HalEdge(const BottomSegment* parent, const TopSegment* child)
: parentGenome(parent->getGenome()->getName())
, parentSeq(parent->getSequence()->getName())
, parentStart(parent->getStartPosition())
, parentEnd(parent->getEndPosition())
, childGenome(child->getGenome()->getName())
, childSeq(child->getSequence()->getName())
, childStart(child->getStartPosition())
, childEnd(child->getEndPosition())
, reversed(child->getParentReversed())
{}
void print(ostream& os, string feature) const
{
os << parentGenome << "\t";
os << parentSeq << "\t";
os << parentStart << "\t";
os << parentEnd + 1 << "\t";
os << childGenome << "\t";
os << childSeq << "\t";
os << childStart << "\t";
os << childEnd + 1 << "\t";
os << reversed << "\t";
os << feature << endl;
}
};
class HalPath{
// Holds information about a chain of edges connecting two segments in a HAL graph.
public:
vector<HalEdge> edges;
string parent;
string child;
vector<string> features;
};
static void printAllEdges(ostream& os, AlignmentConstPtr alignment);
static void printSubgraphByNode(ostream& os, AlignmentConstPtr alignment, string rootGenome,
map<string, map<string, IntervalTree<string> > > intervalTrees,
float overlapA, float overlapB);
static void printSubgraphByLeaves(ostream& os, AlignmentConstPtr alignment,
map<string, map<string, IntervalTree<string> > > intervalTrees);
static void getIntervals(ostream&os, string bedFile,
map<string, map<string, IntervalTree<string> > >& intervalTrees);
static vector<HalPath> traverseDownRecursive(ostream& os, AlignmentConstPtr alignment,
BottomSegmentIteratorPtr parent, HalPath parentPath,
float overlapA, float overlapB);
static void traverseUpRecursive(ostream& os, AlignmentConstPtr alignment,
TopSegmentIteratorPtr child,
map<string, set<HalEdge> >& edges);
int main(int argc, char** argv)
{
CLParserPtr optionsParser = hdf5CLParserInstance();
optionsParser->setDescription("Print HAL graph edges that connect user-specified intervals in two or more child genomes through the user-specified root genome");
optionsParser->addArgument("halFile", "path to hal file to analyze");
optionsParser->addOption("bed", "BED file of intervals "
"in bed file", "\"\"");
optionsParser->addOption("root", "Name of root genome "
, "\"\"");
string path;
string bedFile;
string rootGenome;
try
{
optionsParser->parseOptions(argc, argv);
path = optionsParser->getArgument<string>("halFile");
bedFile = optionsParser->getOption<string>("bed");
rootGenome = optionsParser->getOption<string>("root");
size_t optCount = 0;
if (bedFile != "\"\"") ++optCount;
if (optCount > 1)
{
throw hal_exception("hmm");
}
}
catch(exception& e)
{
cerr << e.what() << endl;
optionsParser->printUsage(cerr);
exit(1);
}
try
{
AlignmentConstPtr alignment = openHalAlignmentReadOnly(path, optionsParser);
if (bedFile != "\"\"" && rootGenome != "\"\"")
{
map<string, map<string, IntervalTree<string> > > featureTrees;
getIntervals(cout, bedFile, featureTrees);
printSubgraphByNode(cout, alignment, rootGenome, featureTrees, 1.0, 0.0);
//printSubgraphByLeaves(cout, alignment, featureTrees);
}
else
{
printAllEdges(cout, alignment);
}
}
catch(hal_exception& e)
{
cerr << "hal exception caught: " << e.what() << endl;
return 1;
}
catch(exception& e)
{
cerr << "Exception caught: " << e.what() << endl;
return 1;
}
return 0;
}
void printAllEdges(ostream& os, AlignmentConstPtr alignment)
{
// Print all edges in the HAL graph encoded by alignment.
set<const Genome*> genomes;
const Genome* root = alignment->openGenome(alignment->getRootName());
getGenomesInSubTree(root, genomes);
for (set<const Genome*>::iterator i = genomes.begin(); i != genomes.end();
++i)
{
TopSegmentIteratorPtr top;
TopSegmentIteratorConstPtr topEnd;
BottomSegmentIteratorPtr bot;
const Genome* parent = (*i)->getParent();
top = (*i)->getTopSegmentIterator();
topEnd = (*i)->getTopSegmentEndIterator();
if (parent == NULL)
{
continue;
}
bot = parent->getBottomSegmentIterator();
while(top != topEnd)
{
if (top->hasParent())
{
bot->toParent(top);
HalEdge edge = HalEdge(bot->getBottomSegment(), top->getTopSegment());
edge.print(os, "");
}
top->toRight();
}
}
}
void printSubgraphByNode(ostream& os, AlignmentConstPtr alignment, string rootGenome,
map<string, map<string, IntervalTree<string> > > intervalTrees,
float overlapA, float overlapB)
{
// Print all edges connected to a given node.
// Node is root right now, but plan to generalize soon.
const Genome* root = alignment->openGenome(rootGenome);
vector<string> leafNames = alignment->getLeafNamesBelow(alignment->getRootName());
// Only output paths that span this set of leaves through root.
set<string> leafSet(leafNames.begin(), leafNames.end());
BottomSegmentIteratorPtr bot = root->getBottomSegmentIterator();
BottomSegmentIteratorConstPtr botEnd = root->getBottomSegmentEndIterator();
while (bot != botEnd)
{
vector<HalPath> paths;
vector<HalPath> outputPaths;
HalPath path;
path.parent = root->getName();
paths = traverseDownRecursive(os, alignment, bot, path, overlapA, overlapB);
bot->toRight();
// Set of child genomes containing a target feature that is linked to root via edges in a HalPath.
set<string> leaves;
for (vector<HalPath>::iterator i = paths.begin(); i != paths.end(); ++i)
{
// Get terminal edge in the HalPath.
HalEdge lastEdge = *((*i).edges.end() - 1);
// Get interval tree of target features in the sequence at the terminus of HalPath.
IntervalTree<string> targetFeatures;
if (intervalTrees.find(lastEdge.childGenome) != intervalTrees.end())
{
if (intervalTrees[lastEdge.childGenome].find(lastEdge.childSeq) != intervalTrees[lastEdge.childGenome].end())
{
targetFeatures = intervalTrees[lastEdge.childGenome][lastEdge.childSeq];
}
}
// Get vector of target features that overlap with the top segment of the terminal edge.
vector<Interval<string> > features;
targetFeatures.findOverlapping(lastEdge.childStart, lastEdge.childEnd, features);
// Assign vector of feature names to HalPath.
for (vector<Interval<string> >::iterator feature = features.begin(); feature != features.end(); ++feature)
{
(*i).features.push_back((*feature).value);
}
// Don't consider HalPaths that don't overlap with a target feature.
if (features.size() > 0)
{
leaves.insert(lastEdge.childGenome);
outputPaths.push_back(*i);
}
}
// Only print HalPaths that connect target features in more than one genome.
if (leaves.size() > 1)
{
for (vector<HalPath>::iterator i = outputPaths.begin(); i != outputPaths.end(); ++i)
{
for (vector<HalEdge>::iterator j = (*i).edges.begin(); j != (*i).edges.end(); ++j)
{
for (vector<string>::iterator f = (*i).features.begin(); f != (*i).features.end(); ++f)
{
(*j).print(os, *f);
}
}
}
}
}
}
void printSubgraphByLeaves(ostream& os, AlignmentConstPtr alignment,
map<string, map<string, IntervalTree<string> > > intervalTrees)
{
// Print all edges connected to any one of a set of leaves.
// Function is unfinished--doesn't do anything yet!
set<const Genome*> genomes;
const Genome* root = alignment->openGenome(alignment->getRootName());
// Get leaf genomes to iterate over.
getGenomesInSubTree(root, genomes);
for (set<const Genome*>::iterator i = genomes.begin(); i != genomes.end(); ++i)
{
os << (*i)->getName() << endl;
TopSegmentIteratorPtr top = (*i)->getTopSegmentIterator();
TopSegmentIteratorConstPtr topEnd = (*i)->getTopSegmentEndIterator();
while (top != topEnd)
{
top->toRight();
}
}
}
void getIntervals(ostream&os, string bedFile,
map<string, map<string, IntervalTree<string> > >& intervalTrees)
{
ifstream fin(bedFile.c_str());
string genomeName;
string seq;
int start;
int end;
string feature;
map<string, map<string, vector<Interval<string> > > > intervalVectors;
while (fin >> genomeName >> seq >> start >> end >> feature)
{
end = end - 1;
intervalVectors[genomeName][seq].push_back(Interval<string>(start, end, feature));
}
map<string, map<string, vector<Interval<string> > > >::iterator i = intervalVectors.begin();
while (i != intervalVectors.end())
{
map<string, vector<Interval<string> > >::iterator j = i->second.begin();
while (j != i->second.end())
{
intervalTrees[i->first][j->first] = IntervalTree<string>(j->second);
++j;
}
++i;
}
}
vector<HalPath> traverseDownRecursive(ostream& os, AlignmentConstPtr alignment,
BottomSegmentIteratorPtr parent,
HalPath parentPath,
float overlapA, float overlapB)
{
vector<HalPath> paths;
// Get names of the child genomes.
vector<string> children = alignment->getChildNames(parent->getBottomSegment()->getGenome()->getName());
for (vector<string>::iterator i = children.begin(); i != children.end(); ++i)
{
const Genome* childGenome = alignment->openGenome(*i);
// Make iterator for top segments in child genome.
TopSegmentIteratorPtr top = childGenome->getTopSegmentIterator();
TopSegmentIteratorConstPtr topEnd = childGenome->getTopSegmentEndIterator();
// If parent has a child segment in this genome, move top iterator to it.
if (parent->hasChildG(childGenome))
{
top->toChildG(parent, childGenome);
}
else
{
// Otherwise set top iterator to end.
top = topEnd;
}
while (top != topEnd)
{
HalEdge edge = HalEdge(parent->getBottomSegment(), top->getTopSegment());
// Make iterator for bottom segments in child genome.
BottomSegmentIteratorPtr nextParent;
BottomSegmentIteratorConstPtr nextParentEnd;
nextParentEnd = childGenome->getBottomSegmentEndIterator();
// If top segment has a parse edge to a bottom segment, move bottom iterator to it.
if (top->hasParseDown())
{
nextParent = childGenome->getBottomSegmentIterator(top->getBottomParseIndex());
}
else
{
// Otherwise set iterator to end.
nextParent = nextParentEnd;
// Recursion stops here for top segment, so add terminal path to the vector to return.
HalPath terminalPath = parentPath;
terminalPath.child = childGenome->getName();
terminalPath.edges.push_back(edge);
paths.push_back(terminalPath);
}
while (nextParent != nextParentEnd)
{
int overlap = nextParent->getBottomSegment()->getEndPosition() - top->getTopSegment()->getStartPosition();
int lenB = nextParent->getBottomSegment()->getLength();
// Require some minimum overlap with the next bottom segment to continue the recursion.
if (float(overlap) / float(lenB) >= overlapB)
{
HalPath nextParentPath = parentPath;
nextParentPath.child = childGenome->getName();
nextParentPath.edges.push_back(edge);
// Search recursively for more edges connected to child bottom segment.
vector<HalPath> childPaths = traverseDownRecursive(os, alignment, nextParent, nextParentPath, overlapA, overlapB);
// Gather recursion output.
paths.insert(paths.end(), childPaths.begin(), childPaths.end());
}
// Move the bottom segment one to the right.
nextParent->toRight();
if (nextParent->getTopParseIndex() != top->getTopSegment()->getArrayIndex())
{
// If the start coordinate of the bottom segment isn't contained in the top,
// then no more overlaps exist.
break;
}
}
if (top->getTopSegment()->hasNextParalogy())
{
// Move top to next paralogous segment in child genome.
top->toNextParalogy();
if (top->getTopSegment()->isCanonicalParalog())
{
// Only visit the `canonical` paralog once (otherwise we go in a circle).
break;
}
}
else
{
// If no paralogs exist, move on.
break;
}
}
}
return paths;
}
void traverseUpRecursive(ostream& os, AlignmentConstPtr alignment,
TopSegmentIteratorPtr child,
map<string, set<HalEdge> >& edges)
{
// Function is unfinished--doesn't do anything yet!
}