-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodify_genome_with_small_variants.cpp
695 lines (649 loc) · 20.3 KB
/
modify_genome_with_small_variants.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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
/*
* Author: Chen Sun([email protected])
*
*
* Modify: 9/15/2014
* Using a fast algorithm that search all sv, not all site of genome, the running time is worth, haven't debug yet, the usable code is high_confident, which use old algorithm
* 10/15/2014
* Already debuged, this is the up to date version
* */
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstring>
#include <map>
#include <ctime>
#include <vector>
#include <string>
#include <string.h>
#include <sstream>
using namespace std;
#define OLDLEN 400000000
#define NEWLEN 400000000
#define TESTLEN 2000
void dsptime()
{
time_t nowtime;
nowtime = time(NULL); //get int time number
struct tm * ptm=localtime(&nowtime); //get system time
cout << ptm->tm_mon+1 << "/" << ptm->tm_mday << "/"<< ptm->tm_year+1900 << "," ;
cout << ptm->tm_hour << ":" << ptm->tm_min << ":" << ptm->tm_sec <<" " << endl;
}
/*split function*/
std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems) {
std::stringstream ss(s);
std::string item;
while (std::getline(ss, item, delim)) {
if (! item.empty()){
elems.push_back(item);
}
}
return elems;
}
std::vector<std::string> split(const std::string &s, char delim) {
std::vector<std::string> elems;
split(s, delim, elems);
return elems;
}
string readRef(char* filename, char* & firstLine, int & refLength, int & lineLength)
{
ifstream refFile;
refFile.open(filename);
//char * refSequence = (char*) calloc(OLDLEN , sizeof(char));
string sequenceString="";
if (!refFile.good())
{
cout << "Error in open reference file!" << endl;
}
//int lineNum = 0;
while(refFile != NULL){
string string_line;
getline(refFile, string_line, '\n');
//lineNum ++;
char* line=const_cast<char*>(string_line.c_str());
if(line[0] == '>')
{
cout << "firstLine" << line << endl;
strcpy(firstLine,line);
cout<<firstLine << endl;
continue;
}
if (lineLength == 0) lineLength = strlen(line);
//refSequence = strcat(refSequence, line);
sequenceString += string_line;
//cout << lineNum << endl;
}
refFile.close();
//char * refSequence = const_cast<char*>(sequenceString.c_str());
//refLength = strlen(refSequence);
refLength = sequenceString.size();
//cout << lineLength << endl;
//cout << refSequence << endl;
return sequenceString;
}
void writeRef(char* filename, char* firstLine, string refSequence, int refLength, int lineLength)
{
ofstream outFile;
outFile.open(filename);
outFile << firstLine << endl;
int markLength = 0;
//char * outSequence = (char*) calloc(lineLength+1, sizeof(char));
string outSequence = "";
while(markLength + lineLength < refLength)
{
//strncpy(outSequence, refSequence+markLength, lineLength);
outSequence = refSequence.substr(markLength, lineLength);
outFile << outSequence << endl;
markLength += lineLength;
}
if(markLength < refLength)
{
//char* tempSequence = (char*) calloc(refLength - markLength+1, sizeof(char));
//strncpy(tempSequence, refSequence+markLength, refLength-markLength);
string tempSequence = refSequence.substr(markLength, refLength-markLength);
outFile << tempSequence << endl ;
}
outFile.close();
}
bool checkInfo(string info){
//check from info
vector<string> infoVector = split(info, ';');
for(int i = 0; i < infoVector.size(); i++){
string section = infoVector[i];
vector<string> sectionSplit = split(section, '=');
if(sectionSplit.size() == 2){
if(sectionSplit[0] == "AB"){
if(sectionSplit[1] != "0") return false;
//else cout << "AB= " << sectionSplit[1] << endl;
}
if(sectionSplit[0] == "AF"){
if(sectionSplit[1] != "1") return false;
//else cout << "AF= " << sectionSplit[1] << endl;
}
}else{
cout << "Warning: Error when check vcf info";
for(int i = 0; i < sectionSplit.size(); i++){
cout << sectionSplit[i] << endl;
}
}
}
return true;
}
void readSnp(char* filename, map<int, string> & position_ref, map<int, string> & position_alt, char* highConfidentFilename, char* lowConfidentFilename)
{
ofstream highConfidentFile;
highConfidentFile.open(highConfidentFilename);
ofstream lowConfidentFile;
lowConfidentFile.open(lowConfidentFilename);
//outFile << "position" << "\t" << "ref" << "\t" << "alt" << "\t" << "abScore" << endl;
ifstream snpFile;
snpFile.open(filename);
if (!snpFile.good())
{
cout << "Error: Fail to open snp file " << filename << endl;
}
char* columns[31];
while (snpFile != NULL)
{
int section_index = 0;
string string_line;
getline(snpFile, string_line, '\n');
char* line = const_cast<char*>(string_line.c_str());
string holdLine = line;
if (line[0] == '#')
{
highConfidentFile << line << endl;
lowConfidentFile << line << endl;
continue;
}
//cout << "breakpoint 1.1" << endl;
char* section = strtok(line, "\t");
while (section != NULL)
{
columns[section_index] = section;
section = strtok(NULL, "\t");
section_index++;
}
//cout << "breakpoint 1.2 " << section_index <<endl;
//cout << line << endl;
if (section_index == 0){ continue; }
if (section_index < 5)
{
cout << "Error: vcf format error, please check your vcf file" << endl;
return;
}
int position = atoi(columns[1]);
position--; // vcf is 1 based
//position *= -1;
string ref = columns[3];
string alt = columns[4];
int qual = atoi(columns[5]);
string info = columns[7];
if(qual <= 20){
lowConfidentFile << holdLine << endl;
continue;
}
//split alt
//cout << alt << "\t" << info << endl;
char* alt_charStar = const_cast<char*>(alt.c_str());
char* alleles[20];
int allele_index = 0;
char* alt_section = strtok(alt_charStar, ",");
while (alt_section != NULL)
{
alleles[allele_index] = alt_section;
alt_section = strtok(NULL, "\t");
allele_index++;
}
if (allele_index > 1) {
lowConfidentFile << holdLine << endl;
continue;
}
if(!checkInfo(info)) {
//cout << "Error: detecting low confident snp";
lowConfidentFile << holdLine << endl;
continue;
}
//split info
// char* info_charStar = const_cast<char*>(info.c_str());
// char* infoSet[200];
// int info_index = 0;
// // in reality, we should traverse all info, but here we know the first is AB
// char* info_section = strtok(info_charStar, ";");
// while (info_section != NULL)
// {
// infoSet[info_index] = info_section;
// info_section = strtok(NULL, ";");
// info_index++;
// }
//
// if (info_index < 4)
// {
// cout << "Error: not enough info sections." << endl;
// continue;
// }
//
// char* singleInfo = infoSet[0];
// int abScore = -1;
// //cout << singleInfo << endl;
// if (singleInfo[0] == 'A' && singleInfo[1] == 'B' && singleInfo[2] == '='){
// char* abSign = strtok(singleInfo, "=");
// char* abScore_charStar = strtok(NULL, "=");
// abScore = atoi(abScore_charStar);
// if (abScore != 0) continue;
// }
//
// char* afInfo = infoSet[3];
// int afScore = -1;
// if (afInfo[0] == 'A' && afInfo[1] == 'F' && afInfo[2] == '='){
// char* afSign = strtok(afInfo, "=");
// char* afScore_charStar = strtok(NULL, "=");
// afScore = atoi(afScore_charStar);
// if (afScore != 1) continue;
// }
//outFile << position << "\t" << ref << "\t" << alt << endl;
highConfidentFile << holdLine << endl;
position_ref.insert(pair<int, string>(position, ref));
position_alt.insert(pair<int, string>(position, alleles[0])); //alt_section will be NULL finally
}
snpFile.close();
}
// this function no longer used
void readSnp_old_version(char* filename, map<int, string> & position_ref, map<int, string> & position_alt)
{
ofstream outFile;
outFile.open("qualifiedSNP.txt");
outFile << "position" << "\t" << "ref" << "\t" << "alt" << "\t" << "abScore" << endl;
ifstream snpFile;
snpFile.open(filename);
if (!snpFile.good())
{
cout << "Error: Fail to open snp file " << filename << endl;
}
char* columns[31];
while (snpFile != NULL)
{
int section_index = 0;
string string_line;
getline(snpFile, string_line, '\n');
char* line = const_cast<char*>(string_line.c_str());
if (line[0] == '#')
{
continue;
}
//cout << "breakpoint 1.1" << endl;
char* section = strtok(line, "\t");
while (section != NULL)
{
columns[section_index] = section;
section = strtok(NULL, "\t");
section_index++;
}
//cout << "breakpoint 1.2 " << section_index <<endl;
//cout << line << endl;
if (section_index == 0){ continue; }
if (section_index < 5)
{
cout << "Error: vcf format error, please check your vcf file" << endl;
return;
}
int position = atoi(columns[1]);
position--; // vcf is 1 based
//position *= -1;
string ref = columns[3];
string alt = columns[4];
string info = columns[7];
//split alt
//cout << alt << "\t" << info << endl;
char* alt_charStar = const_cast<char*>(alt.c_str());
char* alleles[20];
int allele_index = 0;
char* alt_section = strtok(alt_charStar, ",");
while (alt_section != NULL)
{
alleles[allele_index] = alt_section;
alt_section = strtok(NULL, "\t");
allele_index++;
}
if (allele_index > 1) continue;
//split info
char* info_charStar = const_cast<char*>(info.c_str());
char* infoSet[200];
int info_index = 0;
// in reality, we should traverse all info, but here we know the first is AB
char* info_section = strtok(info_charStar, ";");
while (info_section != NULL)
{
infoSet[info_index] = info_section;
info_section = strtok(NULL, ";");
info_index++;
}
if (info_index < 4)
{
cout << "Error: not enough info sections." << endl;
continue;
}
char* singleInfo = infoSet[0];
int abScore = -1;
//cout << singleInfo << endl;
if (singleInfo[0] == 'A' && singleInfo[1] == 'B' && singleInfo[2] == '='){
char* abSign = strtok(singleInfo, "=");
char* abScore_charStar = strtok(NULL, "=");
abScore = atoi(abScore_charStar);
if (abScore != 0) continue;
}
char* afInfo = infoSet[3];
int afScore = -1;
if (afInfo[0] == 'A' && afInfo[1] == 'F' && afInfo[2] == '='){
char* afSign = strtok(afInfo, "=");
char* afScore_charStar = strtok(NULL, "=");
afScore = atoi(afScore_charStar);
if (afScore != 1) continue;
}
outFile << position << "\t" << ref << "\t" << alt << "\t" << afScore << endl;
position_ref.insert(pair<int, string>(position, ref));
position_alt.insert(pair<int, string>(position, alleles[0])); //alt_section will be NULL finally
}
snpFile.close();
}
map<int, int> read_del_vcf(char* filename){
map<int, int> delMap_start_end;
ifstream vcfFile;
vcfFile.open(filename);
if (!vcfFile.good())
{
cout << "Error: Fail to open snp file " << filename << endl;
}
//char* columns[31];
while (vcfFile != NULL)
{
int section_index = 0;
string string_line;
getline(vcfFile, string_line, '\n');
//cout << string_line << endl;
//const string conservedLine = string_line;
char* line = const_cast<char*>(string_line.c_str());
if (line[0] == '#')
{
//headInfo.push_back(string_line);
continue;
}
vector<string> columns = split(string_line, '\t');
if (columns.size() == 0){
cout << "Error: no section in a line." << endl;
continue;
}
if (columns.size() < 7)
{
cout << "Error: vcf format error, please check your vcf file" << endl;
continue;
}
//cout << conservedLine << endl;
int startPosition = atoi(columns[1].c_str());
startPosition--; // vcf is 1 based
//position *= -1;
string quality = columns[6];
if (quality.compare("PASS") != 0){
//cout << quality << endl;
continue;
}
string info = columns[7];
vector<string> infoSet = split(info, ';');
if (infoSet.size() < 4)
{
cout << "Error: not enough info sections." << endl;
continue;
}
string singleInfo = infoSet[6];
int endPosition = -1;
int length = atoi(infoSet[7].c_str());
if (singleInfo.substr(0,3) == "END"){
vector<string> endInfoSet = split(singleInfo, '=');
endPosition = atoi(endInfoSet[1].c_str());
endPosition -= 2;
}
else{
cout << "Error in reading end position of SV." << singleInfo.substr(0,3) << endl;
}
delMap_start_end.insert(pair<int, int>(startPosition, endPosition));
//delMap_start_info.insert(pair<int, string>(startPosition, string_line)); //alt_section will be NULL finally
}
vcfFile.close();
cout << "After Read: " << delMap_start_end.size() << endl;
return delMap_start_end;
}
char* repairGenome(string refSequence, int refLength, map<int, string> position_ref, map<int, string> position_alt)
{
char* donorSequence = (char*)calloc(NEWLEN, sizeof(char));
int j = 0;
for(int i = 0; i < refLength; i++,j++)
{
//cout << i << endl;
if(j >= NEWLEN)
{
cout << "Error: donor genome index out of range" << endl;
}
if (position_ref.count(i)){
//cout << i << " " << position_ref[i] << " " << position_alt[i] << endl;
int refNum = strlen(position_ref[i].c_str());
int altNum = strlen(position_alt[i].c_str());
if (refNum == 1 && altNum == 1){
donorSequence[j] = position_alt[i][0];
continue;
}
donorSequence = strcat(donorSequence, position_alt[i].c_str());
if (refNum > 1)
{
i += (refNum - 1);
}
if (altNum > 1)
{
j += (altNum - 1);
}
}else
{
donorSequence[j] = refSequence[i];
}
}
cout << "about to return donor genome" << endl;
return donorSequence;
}
char* quickRepairing(string refSequence, int refLength, map<int, string> position_ref, map<int, string>position_alt){
char* donorSequence = (char*)calloc(NEWLEN, sizeof(char));
int startPosition = 0;
int endPosition = refSequence.size() - 1;
//cout << "quick repairing, INDEL num: " << position_ref.size() << endl;
//int index = 0;
for(map<int, string>::iterator it = position_ref.begin(); it != position_ref.end(); it++){
//index ++ ;
//cout << index << endl;
int position = it->first;
int refNum = strlen(position_ref[position].c_str());
int altNum = strlen(position_alt[position].c_str());
//cout << position << "\t" << position_ref[position] << "\t" << position_alt[position] << endl;
string subString = refSequence.substr(startPosition, position - startPosition);
//cout << startPosition << "\t" << position-startPosition << "\t" << subString << endl;
char* subCharStar = const_cast<char*>(subString.c_str());
strcat(donorSequence, subCharStar);
//if (refNum == 1 && altNum == 1)
//{
// donorSequence[j] = position_alt[position][0];
//}else{
strcat(donorSequence, position_alt[position].c_str());
//}
startPosition = position + refNum;
}
if(startPosition < endPosition)
{
string subString = refSequence.substr(startPosition, endPosition - startPosition+1);
//cout << startPosition << "\t" << endPosition-startPosition+1 << "\t" << subString << endl;
strcat(donorSequence, subString.c_str());
}
return donorSequence;
}
// del info here is 0-based
char* chopDelSection(string refSequence, map<int, int> delMap_start_end){
char* donorSequence = (char*)calloc(NEWLEN, sizeof(char));
int startPosition = 0;
int endPosition = refSequence.size() - 1;
for(map<int, int>::iterator it = delMap_start_end.begin(); it != delMap_start_end.end(); it++){
int delStart = it->first;
int delEnd = it->second;
if (delStart != 0){
string subString = refSequence.substr(startPosition, delStart - startPosition);
char* subCharStar = const_cast<char*>(subString.c_str());
strcat(donorSequence, subCharStar);
}
startPosition = delEnd+1;
}
if(startPosition < endPosition){
string subString = refSequence.substr(startPosition, endPosition - startPosition+1);
strcat(donorSequence, subString.c_str());
}
return donorSequence;
}
//in the future, we may write a generous function to deal with
/*
string getDonor(string refSequence, int refLength, int & donorLength, map<int, string> position_ref, map<int, string> position_alt)
{
string donorSequence = "";
//cout << refSequence.size();
for(map<int, string>::iterator it = position_ref.begin(); it != position_ref.end(); it++)
{
int position = it->first;
int real_position = position * -1;
string ref = position_ref[position];
string alt = position_alt[position];
int refNum = ref.size();
int altNum = alt.size();
//cout << real_position << " " << ref << " " << alt << endl;
donorSequence = refSequence.substr(0, real_position) + alt;
donorSequence += refSequence.substr(real_position+refNum, refLength - real_position - refNum);
}
donorLength = donorSequence.size();
//cout << donorLength << endl;
return donorSequence;
}
*/
int usage(char* command){
cout << "\n";
cout << "\tPlease cite out paper:" << endl;
cout << "Usage:" << endl;
cout << "\t" << command << " -r reference -d donor {-s snp_file, -l deletion_file} -h high_confident_vcf -w low_confident_vcf" << endl;
cout << "\t reference and donor are mandatory. snp file and deletion file are alternative, you can offer both or just one of them." << endl;
cout << "\t snp is in vcf format." << endl;
cout << "\t deletion is now in vcf file format, using Delly's standard." << endl;
}
int test(){
string info = "AB=0.24;ABP=17.6895;AC=1;AF=0.5;AN=2;AO=6;CIGAR=1X;DP=25;DPB=25;DPRA=0;EPP=16.0391;EPPR=4.03889;GTI=0;LEN=1;MEANALT=1;MQM=23;MQMR=33.7368;NS=1";
cout << checkInfo(info) << endl;
}
int main(int argc, char* argv[])
{
//test();
//return 0;
if(argc < 2){
usage(argv[0]);
return 0;
}
char refFilename[3000]={' '};
char donorFilename[3000]={' '};
char snpFilename[3000]={' '};
bool snpModification = false;
char delFilename[3000]={' '};
bool delModification = false;
char highConfidentFilename[3000]={' '};
char lowConfidentFilename[3000]={' '};
char blankString[10]={' '};
for(int i = 1; i < argc-1; i++)
{
if (!strcmp(argv[i],"-r")){
strcpy(refFilename, argv[++i]);
}else if(! strcmp(argv[i], "-d")){
strcpy(donorFilename, argv[++i]);
}else if(! strcmp(argv[i], "-s")){
strcpy(snpFilename, argv[++i]);
snpModification = true;
}else if(! strcmp(argv[i], "-l")){
strcpy(delFilename, argv[++i]);
delModification = true;
}else if(! strcmp(argv[i], "-h")){
strcpy(highConfidentFilename, argv[++i]);
}else if(! strcmp(argv[i], "-w")){
strcpy(lowConfidentFilename, argv[++i]);
}
}
char * firstLine = (char*)calloc(50, sizeof(char));
int refLength = 0;
int lineLength = 0;
bool donorFileExist = true;
bool highConfidentExist = true;
bool lowConfidentExist = true;
if (!strcmp(donorFilename, blankString)) {
donorFileExist = false;
cout << "Error: must have a donor file name"<<endl;
}
if (!strcmp(highConfidentFilename, blankString)){
highConfidentExist = false;
cout << "Error: must have a high confident vcf file name"<<endl;
}
if (!strcmp(lowConfidentFilename, blankString)){
lowConfidentExist = false;
cout << "Error: must have a low confident vcf file name"<<endl;
}
if(!donorFileExist or ! highConfidentExist or !lowConfidentExist){
usage(argv[0]);
return 0;
}
cout << "Start reading reference file..." ;
dsptime();
//char * refSequence = (char*)calloc()
string refSequence = readRef(refFilename, firstLine, refLength, lineLength);
cout << "End reading reference file.";
dsptime();
int donorLength = 0;
string donorSequence = "";
if (snpModification){
map<int, string> position_ref;
map<int, string> position_alt;
cout << "Start reading snp file...";
dsptime();
readSnp(snpFilename, position_ref, position_alt, highConfidentFilename, lowConfidentFilename);
cout << "End reading snp file.";
dsptime();
//int donorLength = 0;
cout << "Start creating donor genome...";
dsptime();
//donorSequence = quickRepairing(refSequence, refLength, position_ref, position_alt);
donorSequence = repairGenome(refSequence, refLength, position_ref, position_alt);
cout << "End creating donor genome.";
dsptime();
}
//cout << refSequence << endl;
//cout << refSequence.size() << endl;
if (delModification){
cout << "Start reading del vcf file...";
dsptime();
map<int, int> delMap_start_end = read_del_vcf(delFilename);
//cout << delMap_start_end.size() << endl;
cout << "End reading del vcf file.";
dsptime();
cout << "Start chop del section...";
dsptime();
if (snpModification){
donorSequence = chopDelSection(donorSequence, delMap_start_end);
}else {
donorSequence = chopDelSection(refSequence, delMap_start_end);
}
cout << "End chop del section.";
dsptime();
}
//cout << donorSequence << endl;
cout << "Start write donor genome...";
dsptime();
donorLength = donorSequence.length();
//writeRef(donorFilename, firstLine, refSequence, refLength, lineLength);
writeRef(donorFilename, firstLine, donorSequence, donorLength, lineLength);
cout << "End write donor genome.";
dsptime();
}