-
Notifications
You must be signed in to change notification settings - Fork 0
/
sam-overlap-rp.C
367 lines (338 loc) · 7.79 KB
/
sam-overlap-rp.C
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
/*
100 Chr1 21089763 21089921 Chr1 21090056 21090218 3 F, R, INSERTION 380.333333333333 UNBAL 478 13 10 101,204,3,34,41,48,89,96,97,G13,
output
Chr1 21089921 21090056 INSERTION 0 1 0 0 0 0 0 1 1 1 0 1 0 ....
126 accessions, if presented 1; otherwise 0;
vector<string> accessionVect; //input file containing 126 accession names
construct map<string, bool> acc;
output header
for each line in conserv.txt file //representative conserv.txt file
parse line into fields
call getAccPresent()
output selected fields
for each elem in accessionVector //output file
if(acc[elem] == true)
print "\t" 1
else
print "\t" 0
getAccPresent( map<string, bool> & acc, string col1, string field)
{
set each member of acc to be false
if(acc.count(col1) == 0) //a new accession not in the input file
cerr << new accession
exit(1)
acc[col1] = 'true';
parse field into a list
for each member m in the list
if(acc.count(col1) == 0)
cerr << new accession
exit(1)
acc[m] = 'true'
}
*/
#include <iostream>
#include <string>
#include <fstream>
#include <cstring>
#include <cctype>
#include <cstdlib>
#include <set>
#include <vector>
#include <sstream>
#include <map>
using namespace std;
struct intChar
{
int num;
char mid; //'M' 'I' 'D'
};
void readFile(const char * argv, ifstream & input);
void writeFile(const char * argv, ofstream & output);
void getFieldContent(vector<string> & fields, char del, string line);
void getFieldContent2(vector<string> & fields, string del, string line);
bool isSAMheader(const char *f);
bool isCigarMID_Read(string cigar);
bool parseCigar(string cigar, vector<intChar> & vC);
int parseReadMap(string line, string & res);
string int2str(int n);
int main(int argc, char *argv[])
{
if(argc != 3)
{
cerr << argv[0] << " SAM-file-ip(1) output(2) " << endl;
return 1;
}
ifstream input;
ofstream output;
string line, res;
vector<string> lineFields;
int parseRes;
writeFile(argv[2], output);
readFile(argv[1], input);
getline(input, line);
while(!input.eof())
{
parseRes = parseReadMap(line, res);
if(parseRes == 1)
output << line << endl;
else if(parseRes == -1)
output << res << endl;
else;
getline(input, line);
}
input.close();
output.close();
return 0;
}
void readFile(const char * argv, ifstream & input)
{
input.open(argv, ios::in);
if(!input)
{
cerr << argv << " can not be opened for reading.\n";
exit(1);
}
}
void writeFile(const char * argv, ofstream & output)
{
output.open(argv, ios::out);
if(!output)
{
cerr << argv << " can not be opened for writing.\n";
exit(1);
}
}
void getFieldContent(vector<string> & fields, char del, string line)
{
vector<int> pos;
string str;
int len, size;
fields.clear();
for(int i = 0; i < line.length(); i++)
{
if(del == line[i])
pos.push_back(i);
}
if(pos.size() > 0)
{
len = pos[0];
str = line.substr(0, len);
fields.push_back(str);
for(int i = 0; i < pos.size() - 1; i++)
{
len = pos[i+1] - pos[i] - 1;
str = line.substr(pos[i] + 1, len);
fields.push_back(str);
}
size = pos.size();
{
str = line.substr(pos[size-1] + 1);
fields.push_back(str);
}
}
else
{
fields.push_back(line);
}
}
void getFieldContent2(vector<string> & fields, string del, string line)
{
vector<int> pos;
string str;
int len, size;
fields.clear();
for(int i = 0; i < line.length(); i++)
{
if(del.find(line[i]) != string::npos)
pos.push_back(i);
}
if(pos.size() > 0)
{
len = pos[0];
str = line.substr(0, len);
if(len > 0)
fields.push_back(str);
for(int i = 0; i < pos.size() - 1; i++)
{
len = pos[i+1] - pos[i] - 1;
if(len > 0)
{
str = line.substr(pos[i] + 1, len);
fields.push_back(str);
}
}
size = pos.size();
if(pos[size-1] < line.length() - 1) //not at the end of line
{
str = line.substr(pos[size-1] + 1);
fields.push_back(str);
}
}
else
{
fields.push_back(line);
}
}
bool isSAMheader(const char *f)
{
bool res = false;
if(strlen(f) == 3)
if(f[0] == '@')
if(isalpha(f[1]) && isalpha(f[2]))
res = true;
return res;
}
bool isCigarMID_Read(string cigar)
{
string chars = "0123456789MID";
for(int i = 0; i < cigar.length(); i++)
{
if(chars.find(cigar[i]) == string::npos) //cigar contains non \dMID characters
{
return false;
}
}
return true;
}
bool parseCigar(string cigar, vector<intChar> & vC)
{
string numOfOp = "";
intChar cigarPair;
vC.clear();
if(isCigarMID_Read(cigar))
{
for(int i = 0; i < cigar.length(); i++)
{
if(isdigit(cigar[i]))
{
numOfOp += cigar[i];
}
else //MID
{
cigarPair.num = atoi(numOfOp.c_str());
cigarPair.mid = cigar[i]; //M I D
vC.push_back(cigarPair);
numOfOp = "";
}
}
return true;
}
else
return false;
}
//SAM header line, return 1;
//read line, cannot recognize cigar string, read kept, return 1;
//read; cigar recognized; no overlapping, return 1;
//read; cigar recognized; overlappiong; return -1 and a new line string
//if entire read is trimmed, return 0
int parseReadMap(string line, string & res)
{
res = "";
vector<string> fields;
bool bo;
vector<intChar> vC;
int a, prev, ic, start, mateStart, keptBases;
string newCigar = "";
getFieldContent( fields, '\t', line);
if(fields.size() == 0)
return 1;
if(isSAMheader(fields[0].c_str()))
return 1;
//read line
if(fields.size() < 11)
return 1;
if(atoi(fields[8].c_str()) <= 0) //read in downstream
return 1;
bo = parseCigar(fields[5], vC);
if(!bo)//bo == false, cannot recognize cigar string
return 1;
//cigar recognized; read upstream
a = 0;
for(int i = 0; i < vC.size(); i++)
{
if(vC[i].mid == 'M' || vC[i].mid == 'D')
a += vC[i].num;
}
start = atoi(fields[3].c_str());
mateStart = atoi(fields[7].c_str());
if(!(start - 1 + a >= mateStart))
return 1; //two reads no overlapping
a = 0; prev = 0;
keptBases = 0;
newCigar = "";
for(int i = 0; i < vC.size(); i++)
{
prev = a;
if(vC[i].mid == 'M')
{
a += vC[i].num; //101M; 30M+2D; curr extension L from start
if(start + a >= mateStart) //reached mate start position
{
ic = mateStart - start - prev;
newCigar = newCigar + int2str(ic) + 'M';
keptBases += ic;
break;
}
else
{
newCigar = newCigar + int2str(vC[i].num) + 'M';
keptBases += vC[i].num;
}
}
if(vC[i].mid == 'D')
{
a += vC[i].num; //101M; 30M+2D; curr extension L from start
if(start + a >= mateStart) //reached mate start position
{
ic = mateStart - start - prev;
break;
}
else
{
newCigar = newCigar + int2str(vC[i].num) + 'D';
}
}
if(vC[i].mid == 'I')
{
if(start + a >= mateStart) //reached mate start position
{
ic = mateStart - start - prev;
break;
}
else
{
newCigar = newCigar + int2str(vC[i].num) + 'I';
keptBases += vC[i].num;
}
}
} //end for(int i = 0; i < vC.size(); i++)
if(keptBases == 0)
return 0;
res = "";
for(int i = 0; i < fields.size(); i++)
{
string temps;
if(i == 5)
{
temps = newCigar;
}
else if (i == 9 || i == 10)
{
temps = fields[i].substr(0, keptBases);
}
else
temps = fields[i];
res += temps;
if(i != fields.size() -1 )
res += "\t";
}
cout << fields[0] << '\t' << fields[3] << '\t' << fields[5] << '\t' << fields[7] << '\t';
cout << newCigar << '\t' << keptBases << endl;
return -1;
}
string int2str(int n)
{
stringstream ss;
ss << n;
return ss.str();
}