-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSift.cpp
More file actions
217 lines (178 loc) · 6.73 KB
/
Sift.cpp
File metadata and controls
217 lines (178 loc) · 6.73 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
#include <fstream>
#include <algorithm>
#include "main.h"
#include "BowtieEntry.h"
#include "FastaEntry.h"
#include "Read.h"
void clear_bowtie_vector(vector<BowtieEntry*> & btv) {
for(unsigned int i = 0; i < btv.size(); i++) {
delete btv.at(i);
}
btv.clear();
}
int Sift_main(int argc, char * argv[]) {
char * bowtiefile1 = argv[1];
char * bowtiefile2 = argv[2];
char * fastqfile1 = argv[3];
char * fastqfile2 = argv[4];
int offset = atoi(argv[5]);
bool bustfiles;
char * bustfile1 = NULL;
char * bustfile2 = NULL;
if(argc == 8){
bustfiles = true;
bustfile1 = argv[6];
bustfile2 = argv[7];
} else {
bustfiles = false;
}
cout << "Bowtiefile1 " << bowtiefile1 << endl;
cout << "Bowtiefile2 " << bowtiefile2 << endl;
cout << "fastqfile1 " << fastqfile1 << endl;
cout << "fastqfile2 " << fastqfile2 << endl;
cout << "bustfile1 " << bustfile1 << endl;
cout << "bustfile2 " << bustfile2 << endl;
cout << "offset " << offset << endl;
ifstream bowtiestream1(bowtiefile1);
ifstream bowtiestream2(bowtiefile2);
ifstream fastqstream1(fastqfile1);
ifstream fastqstream2(fastqfile2);
ifstream buststream1;
ifstream buststream2;
if(bustfiles) {
buststream1.open(bustfile1);
buststream2.open(bustfile2);
}
char outfile[250];
strcpy(outfile, fastqfile1);
ofstream mapping_distance_stream(strcat(outfile, ".mapdist"));
strcpy(outfile, fastqfile1);
ofstream discordant_mapping_stream(strcat(outfile, ".discord"));
strcpy(outfile, fastqfile1);
ofstream unmapped_fasta_stream(strcat(outfile, ".unmapped.fasta"));
Read read1(offset);
Read read2(offset);
FastaEntry bust1;
FastaEntry bust2;
string cur_bust1_id = "";
string cur_bust2_id = "";
string cur_bowtie1_id = "";
string cur_bowtie2_id = "";
vector<BowtieEntry*> bowtieentries1;
vector<BowtieEntry*> bowtieentries2;
BowtieEntry * bte1 = new BowtieEntry(offset);
BowtieEntry * bte2 = new BowtieEntry(offset);
int iter_counter = 0;
while(fastqstream1 >> read1 && fastqstream2 >> read2) {
++iter_counter;
if(iter_counter % 1000000 == 0) {
cout << "Working on read " << iter_counter << endl;
}
if(bustfiles){
while(cur_bust1_id.compare(read1.id) < 0 && buststream1 >> bust1){
cur_bust1_id = bust1.id;
}
while(cur_bust2_id.compare(read2.id) < 0 && buststream2 >> bust2){
cur_bust2_id = bust2.id;
}
}
if(cur_bust1_id == read1.id || cur_bust2_id == read2.id) {
continue;
}
clear_bowtie_vector(bowtieentries1);
clear_bowtie_vector(bowtieentries2);
if(bte1->read_id == read1.id){ //See if there's a good match from the last iteration
bowtieentries1.push_back(bte1);
bte1 = new BowtieEntry(offset);
}
while(cur_bowtie1_id.compare(read1.id) <= 0 && bowtiestream1 >> *bte1) {
if(bte1->read_id == read1.id){
bowtieentries1.push_back(bte1);
bte1 = new BowtieEntry(offset);
}
cur_bowtie1_id = bte1->read_id;
}
if(bte2->read_id == read2.id){ //See if there's a good match from the last iteration
// cout << "Adding bte2 to bowtieentries2 " << bte1 << endl;
bowtieentries2.push_back(bte2);
bte2 = new BowtieEntry(offset);
}
while(cur_bowtie2_id.compare(read2.id) <= 0 && bowtiestream2 >> *bte2) {
if(bte2->read_id == read2.id){
bowtieentries2.push_back(bte2);
bte2 = new BowtieEntry(offset);
}
cur_bowtie2_id = bte2->read_id;
}
// cout << "Bowtie entries have been read in." << endl;
// cout << "Current BT entries are: " << cur_bowtie1_id << " " << cur_bowtie2_id << endl;
if(bowtieentries1.size() > 0 && bowtieentries2.size() > 0){ //If both ends of the mate pair have mappings
vector<string> genes1;
vector<string> genes2;
// See if there is an overlap between the mapped genes for each side.
for(unsigned int i = 0; i < bowtieentries1.size(); i++) {
genes1.push_back(bowtieentries1.at(i)->mapped_gene());
}
for(unsigned int i = 0; i < bowtieentries2.size(); i++) {
genes2.push_back(bowtieentries2.at(i)->mapped_gene());
}
sort(genes1.begin(), genes1.end());
sort(genes2.begin(), genes2.end());
for(unsigned int i = 0; i < genes1.size(); i++) {
}
for(unsigned int i = 0; i < genes2.size(); i++) {
}
vector<string> g_intersection(genes1.size() + genes2.size());
vector<string>::iterator end_it;
end_it = set_intersection(genes1.begin(), genes1.end(), genes2.begin(), genes2.end(), g_intersection.begin());
if (int(end_it - g_intersection.begin()) > 0){ //There is at least one concordant mapping
// Write out distances between mappings on the same transcript, if there are any.
for(unsigned int i = 0; i < bowtieentries1.size(); i++){
for(unsigned int j = 0; j < bowtieentries2.size(); j++){
if(bowtieentries1.at(i)->mapped_transcript() == bowtieentries2.at(j)->mapped_transcript()) {
mapping_distance_stream << abs(bowtieentries1.at(i)->position - bowtieentries2.at(j)->position) << endl;
}
}
}
} else { //Only discordant mappings
// cout << "Only discordant" << endl;
// Write out discordant mappings
for(unsigned int i = 0; i < bowtieentries1.size(); i++){
for(unsigned int j = 0; j < bowtieentries2.size(); j++){
vector<string> transcripts (2);
vector<string> genes (2);
vector<int> positions (2);
if(bowtieentries1.at(i)->mapped_transcript().compare(bowtieentries2.at(j)->mapped_transcript()) < 0) {
discordant_mapping_stream << bowtieentries1.at(i)->mapped_transcript() << "\t";
discordant_mapping_stream << bowtieentries2.at(j)->mapped_transcript() << "\t";
discordant_mapping_stream << bowtieentries1.at(i)->mapped_gene() << "\t";
discordant_mapping_stream << bowtieentries2.at(j)->mapped_gene() << "\t";
discordant_mapping_stream << bowtieentries1.at(i)->position << "\t";
discordant_mapping_stream << bowtieentries2.at(j)->position << endl;
} else if (bowtieentries1.at(i)->mapped_transcript().compare(bowtieentries2.at(j)->mapped_transcript()) > 0) {
discordant_mapping_stream << bowtieentries2.at(j)->mapped_transcript() << "\t";
discordant_mapping_stream << bowtieentries1.at(i)->mapped_transcript() << "\t";
discordant_mapping_stream << bowtieentries2.at(j)->mapped_gene() << "\t";
discordant_mapping_stream << bowtieentries1.at(i)->mapped_gene() << "\t";
discordant_mapping_stream << bowtieentries2.at(j)->position << "\t";
discordant_mapping_stream << bowtieentries1.at(i)->position << endl;
} else {
cout << "THE PROGRAM IS BROKEN!" << endl;
exit(1);
}
}
}
}
}
else if (bowtieentries1.size() == 0 && bowtieentries2.size() == 0){
read1.write_as_fasta(unmapped_fasta_stream);
read2.write_as_fasta(unmapped_fasta_stream);
}
}
// Get valgrind clean:
delete bte1;
delete bte2;
clear_bowtie_vector(bowtieentries1);
clear_bowtie_vector(bowtieentries2);
return 0;
}