forked from deweylab/RSEM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extractRef.cpp
318 lines (252 loc) · 7.91 KB
/
extractRef.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
#include<cstdio>
#include<cstring>
#include<cctype>
#include<cstdlib>
#include<fstream>
#include<sstream>
#include<map>
#include<vector>
#include<algorithm>
#include "utils.h"
#include "my_assert.h"
#include "GTFItem.h"
#include "Transcript.h"
#include "Transcripts.h"
using namespace std;
struct ChrInfo {
string name;
size_t len;
ChrInfo(const string& name, size_t len) {
this->name = name;
this->len = len;
}
bool operator< (const ChrInfo& o) const {
return name < o.name;
}
};
int M;
vector<GTFItem> items;
vector<string> seqs;
vector<int> starts; // used to generate .grp
map<string, vector<int> > sn2tr; // map from seqname to transcripts
map<string, vector<int> >::iterator iter;
vector<ChrInfo> chrvec;
Transcripts transcripts;
char groupF[STRLEN], tiF[STRLEN], refFastaF[STRLEN];
char chromListF[STRLEN];
bool hasMappingFile;
char mappingFile[STRLEN];
map<string, string> mi_table; // mapping info table
map<string, string>::iterator mi_iter; //mapping info table's iterator
void loadMappingInfo(char* mappingF) {
ifstream fin(mappingF);
string line, key, value;
general_assert(fin.is_open(), "Cannot open " + cstrtos(mappingF) + "! It may not exist.");
mi_table.clear();
while (getline(fin, line)) {
line = cleanStr(line);
if (line[0] == '#') continue;
istringstream strin(line);
strin>>value>>key;
mi_table[key] = value;
}
fin.close();
}
bool buildTranscript(int sp, int ep) {
int cur_s, cur_e; // current_start, current_end
vector<Interval> vec;
string transcript_id = items[sp].getTranscriptID();
string gene_id = items[sp].getGeneID();
char strand = items[sp].getStrand();
string seqname = items[sp].getSeqName();
string left = items[sp].getLeft();
vec.clear();
cur_s = cur_e = -1;
for (int i = sp; i <= ep; i++) {
int start = items[i].getStart();
int end = items[i].getEnd();
general_assert(strand == items[i].getStrand(), "According to the GTF file given, a transcript has exons from different orientations!");
general_assert(seqname == items[i].getSeqName(), "According to the GTF file given, a transcript has exons on multiple chromosomes!");
if (cur_e + 1 < start) {
if (cur_s > 0) vec.push_back(Interval(cur_s, cur_e));
cur_s = start;
}
cur_e = (cur_e < end ? end : cur_e);
}
if (cur_s > 0) vec.push_back(Interval(cur_s, cur_e));
transcripts.add(Transcript(transcript_id, gene_id, seqname, strand, vec, left));
return true;
}
void parse_gtf_file(char* gtfF) {
ifstream fin(gtfF);
string line, tid, gid;
GTFItem item;
general_assert(fin.is_open(), "Cannot open " + cstrtos(gtfF) + "! It may not exist.");
int cnt = 0;
items.clear();
while (getline(fin, line)) {
if (line[0] == '#') continue; // if this line is comment, jump it
item.parse(line);
string feature = item.getFeature();
if (feature == "exon") {
if (item.getStart() > item.getEnd()) {
printf("Warning: exon's start position is larger than its end position! This exon is discarded.\n");
printf("\t%s\n\n", line.c_str());
}
else if (item.getStart() < 1) {
printf("Warning: exon's start position is less than 1! This exon is discarded.\n");
printf("\t%s\n\n", line.c_str());
}
else {
if (hasMappingFile) {
tid = item.getTranscriptID();
mi_iter = mi_table.find(tid);
general_assert(mi_iter != mi_table.end(), "Mapping Info is not correct, cannot find " + tid + "'s gene_id!");
gid = mi_iter->second;
item.setGeneID(gid);
}
items.push_back(item);
}
}
++cnt;
if (verbose && cnt % 200000 == 0) { printf("Parsed %d lines\n", cnt); }
}
fin.close();
sort(items.begin(), items.end());
int sp = 0, ep; // start pointer, end pointer
int nItems = items.size();
sn2tr.clear();
while (sp < nItems) {
tid = items[sp].getTranscriptID();
ep = sp + 1;
while (ep < nItems && items[ep].getTranscriptID() == tid) ep++;
ep--;
buildTranscript(sp, ep);
int sid = transcripts.getM();
const Transcript& transcript = transcripts.getTranscriptAt(sid);
iter = sn2tr.find(transcript.getSeqName());
if (iter == sn2tr.end()) {
vector<int> vec(1, sid);
sn2tr[transcript.getSeqName()] = vec;
}
else {
iter->second.push_back(sid);
}
sp = ep + 1;
}
items.clear();
M = transcripts.getM();
general_assert(M > 0, "The reference contains no transcripts!");
if (verbose) { printf("Parsing gtf File is done!\n"); }
}
char check(char c) {
general_assert(isalpha(c), "Sequence contains unknown letter '" + ctos(c) + "'!");
if (isupper(c) && c != 'A' && c != 'C' && c != 'G' && c != 'T') c = 'N';
if (islower(c) && c != 'a' && c != 'c' && c != 'g' && c != 't') c = 'n';
return c;
}
void shrink() {
int curp = 0;
for (int i = 1; i <= M; i++)
if (seqs[i] == "") {
const Transcript& transcript = transcripts.getTranscriptAt(i);
printf("Warning: Cannot extract transcript %s's sequence since the chromosome it locates, %s, is absent!\n", transcript.getTranscriptID().c_str(), transcript.getSeqName().c_str());
}
else {
++curp;
transcripts.move(i, curp);
if (i > curp) seqs[curp] = seqs[i];
}
printf("%d transcripts are extracted and %d transcripts are omitted.\n", curp, M - curp);
transcripts.setM(curp);
M = transcripts.getM();
general_assert(M > 0, "The reference contains no transcripts!");
starts.clear();
string curgid = "", gid;
for (int i = 1; i <= M; i++) {
gid = transcripts.getTranscriptAt(i).getGeneID();
if (curgid != gid) {
starts.push_back(i);
curgid = gid;
}
}
starts.push_back(M + 1);
}
void writeResults(char* refName) {
int s;
ofstream fout;
sprintf(groupF, "%s.grp", refName);
sprintf(tiF, "%s.ti", refName);
sprintf(refFastaF, "%s.transcripts.fa", refName);
sprintf(chromListF, "%s.chrlist", refName);
fout.open(groupF);
s = starts.size();
for (int i = 0; i < s; i++) fout<<starts[i]<<endl;
fout.close();
if (verbose) { printf("Group File is generated!\n"); }
transcripts.writeTo(tiF);
if (verbose) { printf("Transcript Information File is generated!\n"); }
fout.open(chromListF);
s = chrvec.size();
for (int i = 0; i < s; i++) {
fout<<chrvec[i].name<<'\t'<<chrvec[i].len<<endl;
}
fout.close();
if (verbose) { printf("Chromosome List File is generated!\n"); }
fout.open(refFastaF);
for (int i = 1; i <= M; i++) {
fout<<">"<<transcripts.getTranscriptAt(i).getTranscriptID()<<endl;
fout<<seqs[i]<<endl;
}
fout.close();
if (verbose) { printf("Extracted Sequences File is generated!\n"); }
}
int main(int argc, char* argv[]) {
if (argc < 6 || ((hasMappingFile = atoi(argv[4])) && argc < 7)) {
printf("Usage: rsem-extract-reference-transcripts refName quiet gtfF hasMappingFile [mappingFile] chromosome_file_1 [chromosome_file_2 ...]\n");
exit(-1);
}
verbose = !atoi(argv[2]);
if (hasMappingFile) {
loadMappingInfo(argv[5]);
}
parse_gtf_file(argv[3]);
ifstream fin;
string line, gseq, seqname;
chrvec.clear();
seqs.clear();
seqs.resize(M + 1, "");
int start = hasMappingFile ? 6 : 5;
for (int i = start; i < argc; i++) {
fin.open(argv[i]);
general_assert(fin.is_open(), "Cannot open " + cstrtos(argv[i]) + "! It may not exist.");
getline(fin, line);
while ((fin) && (line[0] == '>')) {
istringstream strin(line.substr(1));
strin>>seqname;
gseq = "";
while((getline(fin, line)) && (line[0] != '>')) {
gseq += line;
}
size_t len = gseq.length();
assert(len > 0);
for (size_t j = 0; j < len; j++) gseq[j] = check(gseq[j]);
iter = sn2tr.find(seqname);
if (iter == sn2tr.end()) continue;
chrvec.push_back(ChrInfo(seqname, len));
vector<int>& vec = iter->second;
int s = vec.size();
for (int j = 0; j < s; j++) {
assert(vec[j] > 0 && vec[j] <= M);
transcripts.getTranscriptAt(vec[j]).extractSeq(gseq, seqs[vec[j]]);
}
}
fin.close();
if (verbose) { printf("%s is processed!\n", argv[i]); }
}
sort(chrvec.begin(), chrvec.end());
shrink();
if (verbose) { printf("Extracting sequences is done!\n"); }
writeResults(argv[1]);
return 0;
}