-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmllib.cpp
More file actions
297 lines (292 loc) · 7.13 KB
/
mllib.cpp
File metadata and controls
297 lines (292 loc) · 7.13 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
297
#include<iostream>
#include "ml.h"
using namespace std;
using chroms = map<string,chromPair>;
using ccov = vector<int>;
using vq = vector<qord>;
///////////////////////////////////////////////////////
mI liftCords(mI & cm,mI & mi)
{
int refC = mi.x1;
int ci = 0; //ci is minus i
int found = 0;
qord temp;
bool oneFound = 0;//whether at least one if statement has been hit
mI tempmi,liftmi;//stores the positions as mI
tempmi.rn = cm.rn;
liftmi.rn = cm.rn;
liftmi.x1 = cm.x1;
liftmi.x2 = cm.x2;
liftmi.qn = mi.qn;
liftmi.y1 = 0;
liftmi.y2 = 0;
if(mi.y1 < mi.y2 ) //both are on the same strand
{
int qC = mi.y1;
while(refC<mi.x2+1)
{
//cout<<cm.rn<<'\t'<<cm.x1<<'\t'<<cm.x2<<'\t'<<mi.x1<<'\t'<<mi.x2<<'\t'<<mi.y1<<'\t'<<mi.y2<<endl;
ci = refC * (-1);
if((find(mi.mv.begin(),mi.mv.end(),refC) == mi.mv.end()) && (find(mi.mv.begin(),mi.mv.end(),ci) == mi.mv.end())) //if this position does not have a indel
{
refC++;
qC++;
temp.cord = qC-1;
tempmi.x1 = refC-1;
tempmi.x2 = refC-1;
tempmi.y1 = qC-1;
tempmi.y2 = qC-1;
tempmi.qn = mi.qn;
}
if((find(mi.mv.begin(),mi.mv.end(),refC) != mi.mv.end()) && (find(mi.mv.begin(),mi.mv.end(),ci) == mi.mv.end())) //position has insertion
{
refC++;
tempmi.x1 = refC-1;
tempmi.x2 = refC-1;
tempmi.y1 = qC-1;
tempmi.y2 = qC-1;
tempmi.qn = mi.qn;
}
if((find(mi.mv.begin(),mi.mv.end(),refC) == mi.mv.end()) && (find(mi.mv.begin(),mi.mv.end(),ci) != mi.mv.end())) //position has deletion
{
refC++;
qC++;
tempmi.x1 = refC-1;
tempmi.x2 = refC-1;
tempmi.y1 = qC-1;
tempmi.y2 = qC-1;
tempmi.qn = mi.qn;
qC++;
found = returnIndex(mi.mv,ci);//get the index
mi.mv[found] = 0;//reset the deletion start
found++;
while((mi.mv[found] == -1) && (found<mi.mv.size()))
{
qC++;
found++;
}
//refC++;//increment refC to move to the next
}
if((cm.x1 == tempmi.x1) && (liftmi.y1 ==0))//if liftmi hasn't been changed yet
{
liftmi.y1 = tempmi.y1;
}
if((cm.x2 == tempmi.x2) && (liftmi.y2 ==0))
{
liftmi.y2 = tempmi.y2;
}
}
}
if(mi.y1 > mi.y2 )//if two are on different strands
{
int qC = mi.y1; //y1 is bigger than y2
while(refC<mi.x2+1)
{
ci = refC * (-1);
if((find(mi.mv.begin(),mi.mv.end(),refC) == mi.mv.end()) && (find(mi.mv.begin(),mi.mv.end(),ci) == mi.mv.end())) //if this position does not have a indel
{
refC++;
qC--;
tempmi.x1 = refC-1;
tempmi.x2 = refC-1;
tempmi.y1 = qC+1;
tempmi.y2 = qC+1;
tempmi.qn = mi.qn;
}
if((find(mi.mv.begin(),mi.mv.end(),refC) != mi.mv.end()) && (find(mi.mv.begin(),mi.mv.end(),ci) == mi.mv.end())) //position has insertion
{
refC++;
tempmi.x1 = refC-1;
tempmi.x2 = refC-1;
tempmi.y1 = qC+1;
tempmi.y2 = qC+1;
tempmi.qn = mi.qn;
}
if((find(mi.mv.begin(),mi.mv.end(),refC) == mi.mv.end()) && (find(mi.mv.begin(),mi.mv.end(),ci) != mi.mv.end())) //position has deletion
{
refC++;
qC--;
tempmi.x1 = refC-1;
tempmi.x2 = refC-1;
tempmi.y1 = qC+1;
tempmi.y2 = qC+1;
tempmi.qn = mi.qn;
qC--;
found = returnIndex(mi.mv,ci);//get the index
mi.mv[0] = 0;//reset the deletion start
found++;
while((mi.mv[found] == -1) && (found<mi.mv.size()))
{
qC--;
found++;
}
}
//cout<<tempmi.rn<<'\t'<<tempmi.x1<<'\t'<<tempmi.x2<<'\t'<<tempmi.qn<<'\t'<<tempmi.y1<<'\t'<<tempmi.y2<<endl;
if((cm.x1 == tempmi.x1) && (liftmi.y1 ==0))//if liftmi hasn't been changed yet
{
liftmi.y1 = tempmi.y1;
}
if((cm.x2 == tempmi.x2) && (liftmi.y2 ==0))
{
liftmi.y2 = tempmi.y2;
}
}
}
return liftmi;
}
////////////////////////////////////////////////////
int returnIndex(vector<int> & vi, int delPos)
{
int i =0;
while(( vi[i] != delPos) && (i<vi.size()))
{
i++;
}
return i;
}
///////////////////////////////////////////////////
void findMum(vector<mI> & mums,vector<mI> & cm,ofstream & fout, char & c, map<string,string> & rseq, map<string,string> & qseq)
{
unsigned int i =0;
mI tempmi;
//vector<mI> vmi;
for(unsigned int j=0;j<cm.size();j++)
{
while((!(cm[j].x2 < mums[i].x1)) && (i<mums.size()))
{
//cout<<"scan\t"<<cm[j].rn<<'\t'<<cm[j].x1<<'\t'<<cm[j].x2<<'\t'<<mums[i].rn<<'\t'<<mums[i].x1<<'\t'<<mums[i].x2<<'\t'<<i<<endl;
if(!(cm[j].x2 < mums[i].x1) && !(cm[j].x1 > mums[i].x2))
{
tempmi = mums[i];
//vmi.push_back(tempmi);
writeLift(tempmi,cm[j],fout,c,rseq,qseq);
//cout<<"query\t"<<cm[j].rn<<'\t'<<cm[j].x1<<'\t'<<cm[j].x2<<'\t'<<mums[i].rn<<'\t'<<mums[i].x1<<'\t'<<mums[i].x2<<'\t'<<i<<endl;
}
++i;
}
i = 0;//reset i
}
// return vmi;
}
//////////////////////////////////////////////////////
void writeLift(mI & vmi,mI & cm,ofstream & fout, char & c, map<string,string> & rseq, map<string,string> & qseq)
{
mI tempmi,mi;
if(c=='s')//if sensitive mode is used
{
cm.x1 = max(cm.x1,vmi.x1);
cm.x2 = min(cm.x2,vmi.x2);
}
tempmi = liftCords(cm,vmi);
if((tempmi.y1 != 0) && (tempmi.y2 !=0))
{
if(vmi.y1 < vmi.y2)//forward strand
{
fout<<tempmi.rn<<'\t'<<tempmi.x1<<'\t'<<tempmi.x2<<'\t'<<tempmi.qn<<'\t'<<tempmi.y1<<'\t'<<tempmi.y2<<'\t'<<rseq[tempmi.rn].substr(tempmi.x1-1,max(tempmi.x2-tempmi.x1,1))<<'\t'<<qseq[tempmi.qn].substr(tempmi.y1-1,max(tempmi.y2-tempmi.y1,1))<<endl;
}
if(vmi.y1 > vmi.y2)//reverse strand
{
fout<<tempmi.rn<<'\t'<<tempmi.x1<<'\t'<<tempmi.x2<<'\t'<<tempmi.qn<<'\t'<<tempmi.y1<<'\t'<<tempmi.y2<<'\t'<<rseq[tempmi.rn].substr(tempmi.x1-1,max(tempmi.x2-tempmi.x1,1))<<'\t'<<revcom(qseq[tempmi.qn].substr(tempmi.y2-1,max(tempmi.y1-tempmi.y2,1)))<<endl;
}
}
if(tempmi.y1 ==0)
{
fout<<tempmi.rn<<'\t'<<tempmi.x1<<'\t'<<tempmi.x2<<'\t'<<tempmi.qn<<'\t'<<"NA"<<'\t'<<tempmi.y2<<endl;
}
if(tempmi.y2 == 0)
{
fout<<tempmi.rn<<'\t'<<tempmi.x1<<'\t'<<tempmi.x2<<'\t'<<tempmi.qn<<'\t'<<tempmi.y1<<'\t'<<"NA"<<endl;
}
}
/////////////////////////////////////////////////
vector<string> splitField(string & str, char c)
{
size_t pos =1, pos1 =0;
vector<string> vs;
string tempstr;
while(pos1 <str.size())
{
pos1 = str.find(c,pos);
if(pos1 < str.size())
{
tempstr = str.substr(pos-1,pos1-pos+1);
if(tempstr[0] == c)
{
tempstr = tempstr.substr(1); //remove the preceding delimiter
}
pos = pos1+1;
vs.push_back(tempstr);
}
}
tempstr = str.substr(pos);
vs.push_back(tempstr);
return vs;
}
/////////////////////////////////////////////////
void readFasta(ifstream & fin, map<string,string> & seq)
{
string str,name;
while(getline(fin,str))
{
if(str[0] == '>')//fasta header
{
name = str.substr(1);
}
else
{
seq[name].append(str);
}
}
}
//////////////////////////////////////////////////
string revcom(string seq) //return the complementary sequence
{
string revseq;
char N,c;
for(int i=seq.size()-1;i>-1;i--)
{
N = seq[i];
if(N == 'N')
{
c = 'N';
}
if(N == 'n')
{
c = 'n';
}
if(N == 'A')
{
c = 'T';
}
if(N == 'a')
{
c = 't';
}
if(N == 'T')
{
c = 'A';
}
if(N == 't')
{
c = 'a';
}
if(N == 'G')
{
c = 'C';
}
if(N == 'g')
{
c = 'c';
}
if(N == 'C')
{
c = 'G';
}
if(N == 'c')
{
c = 'g';
}
revseq.push_back(c);
}
return revseq;
}