-
Notifications
You must be signed in to change notification settings - Fork 3
/
tmerge.cpp
350 lines (329 loc) · 12.6 KB
/
tmerge.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
#include "tmerge.h"
#include "commons.h"
#include <string>
#include <sstream>
#include <stdlib.h>
#include <cstring>
bool check_id(std::string& line, std::string id_tag){
std::stringstream *line_stream = new std::stringstream(line);
std::string col;
// first tag should have already been checked
std::getline(*line_stream,col,'\t');
// check if ID == SAMPLE
std::getline(*line_stream,col,'\t');
if(strcmp(col.c_str(),id_tag.c_str())!=0){
delete line_stream;
return false;
}
delete line_stream;
return true;
}
bool check_id_full(sam_hdr_t *hdr,std::string tag1,std::string tag2){
bool found_line = false;
int line_pos = 0;
std::string line;
while(true){
kstring_t str = KS_INITIALIZE;
if (sam_hdr_find_line_pos(hdr, tag1.c_str(), line_pos, &str)!=0) {
break;
}
else{
// parse line to check if tb tag is present
line = std::string(str.s);
bool ret = check_id(line,tag2.c_str());
if(ret){
found_line=true;
}
line_pos++;
}
ks_free(&str);
}
return found_line;
}
void TInputFiles::addFile(const char* fn) {
GStr sfn(fn);
if (sfn!="-" && fileExists(fn)<2) {
GError("Error: input file %s cannot be found!\n",
fn);
}
freaders.Add(new TSamReader(fn));
}
bool TInputFiles::addSam(GSamReader* r, int fidx) {
//requirement: all files must have the same number of SQ entries in the same order!
kstring_t hd_line = KS_INITIALIZE;
int res = sam_hdr_find_hd(r->header(), &hd_line);
if (res < 0) GError("Error: failed to get @HD line from header!\n");
//check for SO:coordinate
kstring_t str = KS_INITIALIZE;
if (sam_hdr_find_tag_hd(r->header(), "SO", &str)
|| !str.s
|| strcmp(str.s, "coordinate") )
GError("Error: %s file not coordinate-sorted!\n", r->fileName());
ks_free(&hd_line);
bool tb_file=false; //was this file a product of TieBrush? (already merged)
res=sam_hdr_find_tag_id(r->header(), "PG", "PN", "TieBrush", "VN", &str);
if (res<-1) GError("Error: failed to lookup @PG line in header!\n");
if (res==0) {
tb_file=true;
#ifdef _DEBUG
GMessage("DEBUG info: %s is a TieBrush (merged) file.\n", freaders[fidx]->fname.chars());
#endif
}
if (mHdr==NULL) { //first file
headerfilename = r->fileName();
headerfiletbMerged = tb_file;
mHdr=sam_hdr_dup(r->header());
}
else { //check if this file has the same SQ entries in the same order
//if it has more seqs, make it the main header
int r_numrefs=sam_hdr_nref(r->header());
int m_nrefs=sam_hdr_nref(mHdr);
bool swapHdr=(r_numrefs>m_nrefs);
sam_hdr_t *loHdr = swapHdr ? mHdr : r->header();
sam_hdr_t *hiHdr = swapHdr ? r->header() : mHdr;
int loNum = swapHdr ? m_nrefs : r_numrefs;
//if (r_numrefs!=m_nrefs)
// GError("Error: file %s has different number of reference sequences (%d)!\n", r->fileName(), r_numrefs);
for (int i = 0; i < loNum; ++i) {
str.l = 0;
res = sam_hdr_find_tag_pos(loHdr, "SQ", i, "SN", &str);
if (res < 0)
GError("Error: failed to get @SQ SN #%d from header\n", i + 1);
int m_tid = sam_hdr_name2tid(hiHdr, str.s);
if (m_tid < -1)
GError("Error: unexpected ref lookup failure (%s)!\n", str.s);
if (m_tid < 0)
GError("Error: ref %s not seen before!\n", str.s);
int r_tid = sam_hdr_name2tid(loHdr, str.s);
if (r_tid != m_tid)
GError("Error: ref %s from file %s does not have the expected id#!", str.s, r->fileName());
}
if (swapHdr) {
sam_hdr_destroy(mHdr);
headerfilename = r->fileName();
headerfiletbMerged = tb_file;
mHdr=sam_hdr_dup(r->header());
}
}
ks_free(&str);
freaders[fidx]->samreader=r;
freaders[fidx]->tbMerged=tb_file;
if (fidx==freaders.Count()-1) { // last samreader entry
// add any currently existing ID:SAMPLE lines to the maps
load_hdr_samples(mHdr,this->headerfilename,this->headerfiletbMerged,true);
for(int fi=0;fi<this->freaders.Count();fi++){ // add metadata about the files being collapsed and the index of each of them
if(std::strcmp(this->freaders[fi]->fname.chars(),this->headerfilename.c_str())==0){ // this is the file which contributed header to the merged result - can safely
continue;
}
else{
load_hdr_samples(this->freaders[fi]->samreader->header(),this->freaders[fi]->fname.chars(),this->freaders[fi]->tbMerged,false);
}
}
// now that we have a full list of samples - we can add them to the header
for(auto& ls : this->lineno2sample){ // sorted order of the map by line number
if(this->headerfiletbMerged && std::get<3>(ls.second)){ // if donor - can skip since already in the header
continue;
}
int res_rg = sam_hdr_add_line(mHdr, "CO", ("SAMPLE:"+std::get<0>(ls.second)).c_str(),NULL);
if(res_rg==-1){
std::cerr<<"unable to complete adding CO tags for file names"<<std::endl;
exit(-1);
}
}
sam_hdr_add_pg(mHdr, "TieBrush",
"VN", pg_ver, "CL", pg_args.chars(), NULL);
// sam_hdr_rebuild(mHdr); -- is this really needed?
}
return tb_file;
}
void TInputFiles::load_hdr_samples(sam_hdr_t* hdr,std::string filename,bool tbMerged,bool donor){
int sample_line_pos = 0;
if(tbMerged){
bool found_line = false;
int line_pos = 0;
std::string line;
while(true){
kstring_t str = KS_INITIALIZE;
if (sam_hdr_find_line_pos(hdr,"CO", line_pos, &str)!=0) {
break;
}
else{
line = std::string(str.s);
bool ret = get_sample_from_line(line);
if(ret){
found_line=true;
this->s2l_it = this->sample2lineno.insert(std::make_pair(line,std::make_tuple(this->max_sample_id,sample_line_pos,filename,donor)));
if(!this->s2l_it.second){ // not inserted
std::cerr<<"duplicate entries detected"<<std::endl;
exit(-1);
}
this->lineno2sample.insert(std::make_pair(this->max_sample_id,std::make_tuple(line,sample_line_pos,filename,donor)));
sample_line_pos++;
this->max_sample_id++;
}
line_pos++;
}
ks_free(&str);
}
if(!found_line){
std::cerr<<"Collapsed file does not have any CO: lines in the header"<<std::endl;
exit(-1);
}
}
else{ // no sample was found - need to add current name to the header
this->s2l_it = this->sample2lineno.insert(std::make_pair(get_full_path(filename),std::make_tuple(this->max_sample_id,sample_line_pos,"",donor)));
if(!this->s2l_it.second){ // not inserted
std::cerr<<"duplicate entries detected"<<std::endl;
exit(-1);
}
this->lineno2sample.insert(std::make_pair(this->max_sample_id,std::make_tuple(get_full_path(filename),sample_line_pos,"",donor)));
sample_line_pos++;
this->max_sample_id++;
}
}
bool TInputFiles::get_sample_from_line(std::string& line){ // returns true if is sample pg line
std::stringstream *line_stream = new std::stringstream(line);
std::string col;
// make sure it's CO - comment line
std::getline(*line_stream,col,'\t');
if(std::strcmp(col.c_str(),"@CO")!=0){
delete line_stream;
return false;
}
// check if ID == SAMPLE
std::getline(*line_stream,col,':');
if(std::strcmp(col.c_str(),"SAMPLE")!=0){
delete line_stream;
return false;
}
std::getline(*line_stream,col,'\t');
line = col;
delete line_stream;
return true;
}
// adds a line to the header which tells whether the file has been processed with tiebrush before
bool TInputFiles::add_tb_tag_if_not_exists(sam_hdr_t *hdr){ // returns true if the tb_tag already existed and false if a new one was created
// check if already exists
bool found_tb_tag_line = false;
int line_pos = 0;
std::string line;
while(true){
kstring_t str = KS_INITIALIZE;
if (sam_hdr_find_line_pos(hdr, "PG", line_pos, &str)!=0) {
break;
}
else{
// parse line to check if tb tag is present
line = std::string(str.s);
bool ret = check_id(line,"ID:TB_TAG");
if(ret){
if(found_tb_tag_line){
std::cerr<<"multiple tb tag lines found"<<std::endl;
exit(-1);
}
else{
found_tb_tag_line=true;
}
}
line_pos++;
}
ks_free(&str);
}
if(!found_tb_tag_line){ // no tb_tag line - can now append the tag line
int res_rg = sam_hdr_add_line(mHdr, "PG", "ID","TB_TAG",
"VL","1",
"XN","1",NULL);
if(res_rg==-1){
std::cerr<<"unable to complete adding pg tags for file names"<<std::endl;
exit(-1);
}
return false;
}
return true;
}
// removed all lines with matching tags
void TInputFiles::delete_all_hdr_with_tag(sam_hdr_t *hdr,std::string tag1, std::string tag2){
int line_pos = 0;
std::string line;
while(true){
kstring_t str = KS_INITIALIZE;
if (sam_hdr_find_line_pos(hdr, tag1.c_str(), line_pos, &str)!=0) {
break;
}
else{
// parse line to check if tb tag is present
line = std::string(str.s);
bool ret = check_id(line,tag2.c_str());
if(ret){ // found line - remove
int ret_rm = sam_hdr_remove_line_pos(hdr,"PG",line_pos);
if(ret_rm!=0){
std::cerr<<"could not find requested header line"<<std::endl;
exit(-1);
}
}
line_pos++;
}
ks_free(&str);
}
}
// todo: merge header PG tags
int TInputFiles::start(){
if (this->freaders.Count()==1) {
//special case, if it's only one file it might be a list of file paths
GStr& fname= this->freaders.First()->fname;
//try to open it as a SAM/BAM/CRAM
htsFile* hf=hts_open(fname.chars(), "r");
if (hf==NULL || hf->format.category!=sequence_data) {
//must be a list of file paths
if (hf) hts_close(hf);
FILE* flst=fopen(fname.chars(),"r");
if (flst==NULL) GError("Error: could not open input file %s!\n",
fname.chars());
char* line=NULL;
int lcap=5000;
GMALLOC(line, lcap);
freaders.Clear();
while (fgetline(line,lcap,flst)) {
GStr s(line);
s.trim();
if (s.length()<2 || s[0]=='#') continue; //skip comments/header in the list file, if any
if (!fileExists(s.chars()))
GError("Error: cannot find alignment file %s !\n",s.chars());
freaders.Add(new TSamReader(s.chars()));
} //for each line in the list file
GFREE(line);
fclose(flst);
}
else { // single alignment file
hts_close(hf);
}
}
for (int i=0;i<freaders.Count();++i) {
GSamReader* samrd=new GSamReader(freaders[i]->fname.chars(),
SAM_QNAME|SAM_FLAG|SAM_RNAME|SAM_POS|SAM_CIGAR|SAM_AUX);
bool tb_merged=addSam(samrd, i); //merge SAM headers etc.
GSamRecord* brec=samrd->next();
if (brec)
recs.Add(new TInputRecord(brec, i, tb_merged));
}
return freaders.Count();
}
TInputRecord* TInputFiles::next() {
//must free old current record first
delete crec;
crec=NULL;
if (recs.Count()>0) {
crec=recs.Pop();//lowest coordinate
GSamRecord* rnext=freaders[crec->fidx]->samreader->next();
if (rnext)
recs.Add(new TInputRecord(rnext,crec->fidx, crec->tbMerged));
//return crec->brec;
return crec;
}
else return NULL;
}
void TInputFiles::stop() {
for (int i=0;i<freaders.Count();++i) {
freaders[i]->samreader->bclose();
}
}