-
Notifications
You must be signed in to change notification settings - Fork 16
/
libcorrect.cpp
366 lines (328 loc) · 10.5 KB
/
libcorrect.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
#include <iostream>
#include <algorithm>
#include <map>
#include <string>
#include <cstring>
#include <fstream>
#include <cmath>
#include <queue>
#include <numeric>
#include <unordered_map>
#include "cmdline/cmdline.h"
using namespace std;
class BedRecord
// Information that we extract from a BED file
{
public:
string contig; // Name of the contig
int start; // start position of read in the contig (leftmost coordinate)
int end; // end position of read in the contig (rightmost coordinate)
char strand; //+ forward - reverse
BedRecord () {}
BedRecord(string contig, int start, int end, char strand);
};
BedRecord :: BedRecord(string contig, int start, int end, char strand)
{
this->contig = contig;
this->start = start;
this->end = end;
this->strand = strand;
}
//change readnames to ids
map<string, BedRecord> first_in_pair;
map<string, BedRecord> second_in_pair;
char* getCharExpr(string s) // TODO: this can probably be rewritten neater
{
char *a=new char[s.size()+1];
a[s.size()]=0;
memcpy(a,s.c_str(),s.size());
return a;
}
void parse_bed(string path)
// parse a BED file. File path provided as parameter.
// populates class variables
{
ifstream bedfile(getCharExpr(path));
string line;
unordered_map<string,int> seen;
while(getline(bedfile,line))
{
// Assumes that we are processing the first 6 fields in a BED file
// 1 - contig name
// 2 - start coordinate of read alignment
// 3 - end coordinate of read alignment
// 4 - read name
// 5 - alignment quality (ignored)
// 6 - strand (+/-)
// All other fields are ignored
string contig, read;
char strand;
int start,end,flag;
istringstream iss(line);
iss >> contig >> start >> end >> read >> flag >> strand;
BedRecord rec(contig,start,end,strand);
// Here we identify which reads are paired
// The assumption is that the read name ends with /1 for the forward read and /2 for the reverse read
// TODO: Better way of keeping track of forward/reverse - this only works for one naming convention
// TODO: Using maps may be inefficient - a sorting-based approach may be preferred.
if(read[read.length()-2] == '/')
{
if(read[read.length() -1 ] == '1')
{
first_in_pair[read.substr(0,read.length()-2)] = rec;
}
else
{
second_in_pair[read.substr(0,read.length()-2)] = rec;
}
}
else // here we appear to process a type of file where the forward and
// reverse reads have the same name (presumably after some pre-processing
// of the BED file.
// TODO: this should be replaced with a better way of handling forward/reverse reads
{
if(seen.find(read) == seen.end()) // TODO: this is inefficient
{
first_in_pair[read] = rec;
seen[read] = true;
}
else
{
second_in_pair[read] = rec;
}
}
}
}
class LibRecord
{
public:
string lib_id;
string read_1;
string read_2;
string format;
double mean;
double stdev;
double maximum;
double minimum;
string orientation;
LibRecord() {}
LibRecord(string lib_id, string read_1, string read_2, string format, double mean, double stdev,double maximum, double minimum, string orientation);
};
LibRecord :: LibRecord(string lib_id, string read_1, string read_2, string format, double mean, double stdev,double maximum, double minimum, string orientation)
{
this->lib_id = lib_id;
this->read_1 = read_1;
this->read_2 = read_2;
this->format = format;
this->mean = mean;
this->stdev = stdev;
this->maximum = maximum;
this->minimum = minimum;
this->orientation = orientation;
}
map<string, int> contig2length;
map<string, int> contig2bases;
map<string, int> contig2reads;
void get_contig_length(string file)
{
ifstream lenfile(getCharExpr(file));
string line;
while(getline(lenfile,line))
{
istringstream iss(line);
string contig;
int len;
iss >> contig >> len;
contig2length[contig] = len;
}
}
map<string,string> getFastqSequences(string file)
// Parse fastq file into memory
{
map<string, string> ret;
ifstream fastqfile(getCharExpr(file));
string line,seqname,seq;
bool prevlineseqname = false;
while(getline(fastqfile,line))
{
if(line[0] == '@')
{
seqname = line.substr(1);
int space = int(seqname.find(" "));
seqname = seqname.substr(0,space);
prevlineseqname = true;
continue;
}
if(prevlineseqname == true)
{
seq = line;
ret[seqname] = seq;
prevlineseqname = false;
}
}
return ret;
}
// given start/end coordinates of two reads, compute the implied fragment/insert size
int get_insert_size(int start1, int end1, int start2, int end2)
{
if(start1 <= start2)
{
return end2 - start1 + 1;
}
else
{
return end1 - start2 + 1;
}
}
// Given a pair of contigs and the coordinates of the forward and reverse reads of a pair within them
// compute the size of the gap between the adjacent ends of the contigs
// Parameters:
// mean - mean library size
// start1, end1 - start and end of first mate in contig 1
// start2, end2 - start and end of second mate in contig 2
// ctg1_length, ctg2_length - lengths of the two contigs
// orientation - which ends of the contigs are adjacent:
// BB - the beginning of contigs are adjacent, or contig 1 is reverse and 2 is forward
// EE - the ends of the contigs are adjacent - contig 1 is forward and 2 is reverse
// EB - both contigs are forward
// BE - both contigs are reverse
double estimate_distance(double mean, int start1, int end1, int start2, int end2, int ctg1_length, int ctg2_length, string orientation)
{
int offset1,offset2;
if(orientation == "EB")
// c1 c2
// -----> ----->
{
offset1 = ctg1_length - start1;
offset2 = end2;
}
//Need to work out BB and EE properly with reasoning
if(orientation == "BB")
// c1 c2
// <----- ----->
{
offset1 = end1;
offset2 = end2;
}
if(orientation == "EE")
// c1 c2
// -----> <-----
{
offset1 = ctg1_length - start1;
offset2 = ctg2_length - start2;
}
if(orientation == "BE")
// c1 c2
// <----- <-----
{
offset1 = end1;
offset2 = ctg2_length - start2;
}
return mean - offset2 - offset1; // mean size of the gap between corresponding contig ends
}
int main(int argc, char* argv[])
{
cmdline ::parser pr;
//pr.add<string>("lib_info",'l',"file containing information about library",true,"");
// TODO: accept library info from command line rather than computing here
pr.add<string>("alignment_info",'a',"alignment of read to assembled contigs in bed format",true,"");
pr.add<string>("contig_file",'d',"file containing length of contigs",true,"");
pr.add<string>("coverage_file",'x',"file to output coverage of contigs",true,"");
// TODO: set up a separate procedure to compute library size and coverage
pr.add<int>("length_cutoff",'c',"length cutoff on contigs to be used for scaffolding",false,500);
pr.add<string>("output",'o',"output file",true,"");
pr.parse_check(argc,argv);
get_contig_length(pr.get<string>("contig_file"));
vector<LibRecord> libraries;
string line;
int threshold = pr.get<int>("length_cutoff");
parse_bed(pr.get<string>("alignment_info"));
vector<int> insert_sizes;
cerr<<"Size of First Map = "<<first_in_pair.size()<<endl;
cerr<<"Size of Second Map = "<<second_in_pair.size()<<endl;
map<string,BedRecord> :: iterator it;
// here we compute length of mate-pairs based on reads mapped to the same contig
// TODO: move to a separate module/executable
for(it = first_in_pair.begin(); it != first_in_pair.end();++it)
{
string read = it->first;
BedRecord first = it->second;
if(second_in_pair.find(read) != second_in_pair.end()) // TODO: this is inefficient
{
BedRecord second = second_in_pair[read];
if(first.contig == second.contig)
{
if(contig2reads.find(first.contig) == contig2reads.end()) // TODO: this is inefficient
{
contig2reads[first.contig] = 0;
}
contig2reads[first.contig] += 1;
int insert_size = get_insert_size(first.start, first.end, second.start, second.end);
insert_sizes.push_back(insert_size);
}
}
}
// Next few lines compute mean and standard deviation for libraries
// TODO: move to a different executable.
// TODO: must account for end-of-contig effects
double sum = std::accumulate(insert_sizes.begin(), insert_sizes.end(), 0.0);
double mean = sum / insert_sizes.size();
cerr<<"Sum = "<<sum<<endl;
cerr<<"Size = "<<insert_sizes.size()<<endl;
std::vector<double> diff(insert_sizes.size());
std::transform(insert_sizes.begin(), insert_sizes.end(), diff.begin(), std::bind2nd(std::minus<double>(), mean));
double sq_sum = std::inner_product(diff.begin(), diff.end(), diff.begin(), 0.0);
double stdev = std::sqrt(sq_sum / insert_sizes.size());
cerr<<"Mean = "<<mean<<endl;
cerr<<"Stdev = "<<stdev<<endl;
//Here we calculate fragment coverage for each contig (why?)
ofstream covfile(getCharExpr(pr.get<string>("coverage_file")));
for(map<string,int> :: iterator it = contig2reads.begin(); it != contig2reads.end(); ++it)
{
int len = contig2length[it->first];
double coverage = it->second * 1.0 * mean / len;
covfile<<it->first<<"\t"<<coverage<<endl;
}
//calculate links between contigs based on mate pair information, iterate through maps of mate pairs and find links
ofstream ofile(getCharExpr(pr.get<string>("output")));
for(it = first_in_pair.begin(); it != first_in_pair.end(); ++it)
{
BedRecord first = it->second;
string firstcontigend, secondcontigend;
if(second_in_pair.find(it->first) != second_in_pair.end()) // TODO: this is inefficient
{
BedRecord second = second_in_pair[it->first];
if(contig2length[first.contig] <= threshold || contig2length[second.contig] <= threshold)
{
// skip contigs that are too short
continue;
}
if(first.contig != second.contig) // only process mates that link contigs
{
if(first.strand == '+' && second.strand == '+')
{
firstcontigend = "E";
secondcontigend = "E";
}
if(first.strand == '+' && second.strand == '-')
{
firstcontigend = "E";
secondcontigend = "B";
}
if(first.strand == '-' && second.strand == '+')
{
firstcontigend = "B";
secondcontigend = "E";
}
if(first.strand == '-' && second.strand == '-')
{
firstcontigend = "B";
secondcontigend = "B";
}
double dist = estimate_distance(mean,first.start,first.end,second.start,second.end,contig2length[first.contig],contig2length[second.contig],firstcontigend+secondcontigend);
ofile << first.contig<<"\t"<<firstcontigend<<"\t"<<second.contig<<"\t"<<secondcontigend<<"\t"<<dist<<"\t"<<stdev<<endl;
}
}
}
return 0;
}