forked from abyzovlab/CNVnator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
IO.cpp
463 lines (441 loc) · 14.3 KB
/
IO.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
#include "IO.hh"
#include <algorithm>
const vector<string> IO::Signal = vector<string>({
"RD",
"RD unique",
"RD raw",
"RD l1",
"RD l2",
"RD l3",
"RD partition",
"RD call",
"GC",
"SNP",
"SNP count",
"SNP baf",
"SNP maf",
"SNP likelihood",
"SNP i1",
"SNP i2",
"SNP i3",
"SNP i4",
"SNP likelihood partition",
"SNP maf partition",
"SNP i1 partition",
"SNP likelihood call",
"SNP maf call",
"SNP i1 call",
});
// Distribution use same signal name + " dist"
IO::IO(string _rootfile) : rootfile(_rootfile),file(rootfile,"Update") {
if (file.IsZombie()) {
cerr<<"Can't open file '"<<rootfile<<"'."<<endl;
return;
}
TIterator *it = file.GetListOfKeys()->MakeIterator();
while (TKey *key = (TKey*)it->Next()) {
TObject *obj = key->ReadObj();
if (obj->IsA() != TTree::Class()) {
string name = obj->GetName();
string binstr="bin_";
if (name.compare(0,binstr.size(),binstr)==0)
bin_dir.push_back(name.substr(binstr.size()));
} else {
string chrom = obj->GetName();
string title = obj->GetTitle();
if (chrom == "") continue;
int len;
try {len=stoi(title.substr(title.find(";") + 1));}
catch(...) {len=0;}
string vcfstr="snp_";
string vcfstr_old="vcf_";
if (chrom.compare(0,vcfstr.size(),vcfstr)==0) {
snp_tree.push_back(chrom.substr(vcfstr.size()));
snp_tree_len.push_back(len);
} else {
if (chrom.compare(0,vcfstr_old.size(),vcfstr_old)!=0) {
rd_tree.push_back(chrom);
rd_tree_len.push_back(len);
}
}
}
}
}
IO::~IO() {
file.Close();
}
TString IO::treeName(string chr,string signal) {
if(signal=="RD") return chr;
else if(signal=="SNP") return "snp_"+chr;
cerr << "Unknown type of tree!" << endl;
return "";
}
TString IO::signalName(string chr, int bin, string signal, int unsigned flags) {
TString ret("");
if(signal=="RD") {
ret = "his_rd_p_" + chr + "_";
ret += to_string(bin);
if (flags&FLAG_AT_CORR) ret += "_AT";
if (flags&FLAG_GC_CORR) ret += "_GC";
return ret;
} else if(signal=="RD unique") {
ret = "his_rd_u_" + chr + "_";
ret += to_string(bin);
return ret;
} else if(signal=="RD raw") {
return signalName(chr,bin,"RD",flags)+"_raw";
} else if(signal=="RD l1") {
return signalName(chr,bin,"RD",flags)+"_l1";
} else if(signal=="RD l2") {
return signalName(chr,bin,"RD",flags)+"_l2";
} else if(signal=="RD l3") {
return signalName(chr,bin,"RD",flags)+"_l3";
} else if(signal=="RD partition") {
ret = signalName(chr,bin,"RD",0) + "_partition";
if (flags&FLAG_AT_CORR) ret += "_AT";
if (flags&FLAG_GC_CORR) ret += "_GC";
return ret;
} else if(signal=="RD call") {
return signalName(chr,bin,"RD partition",flags)+"_merge";
} else if(signal=="GC") {
ret = chr + "_gc_";
ret += bin;
return ret;
} else if(signal=="RD dist") {
ret = "rd_p_";
if (flags&FLAG_SEX) ret += "xy_";
if (flags&FLAG_AT_CORR) ret += "AT_";
if (flags&FLAG_AT_CORR) ret += "GC_";
ret += bin;
return ret;
} else if(signal=="SNP") {
return signalName(chr,bin,"SNP count",flags);
} else if(signal=="SNP count") {
ret = "snp_bafc_"+chr+"_";
ret += to_string(bin);
if(flags&FLAG_USEMASK) ret += "_mask";
if(flags&FLAG_USEID) ret += "_id";
if(flags&FLAG_USEHAP) ret += "_hap";
return ret;
} else if(signal=="SNP baf") {
ret = "snp_baf_"+chr+"_";
ret += to_string(bin);
if(flags&FLAG_USEMASK) ret += "_mask";
if(flags&FLAG_USEID) ret += "_id";
if(flags&FLAG_USEHAP) ret += "_hap";
return ret;
} else if(signal=="SNP maf") {
ret = "snp_maf_"+chr+"_";
ret += to_string(bin);
if(flags&FLAG_USEMASK) ret += "_mask";
if(flags&FLAG_USEID) ret += "_id";
if(flags&FLAG_USEHAP) ret += "_hap";
return ret;
} else if(signal=="SNP maf partition") {
ret = "snp_maf_"+chr+"_";
ret += to_string(bin);
if(flags&FLAG_USEMASK) ret += "_mask";
if(flags&FLAG_USEID) ret += "_id";
if(flags&FLAG_USEHAP) ret += "_hap";
return ret+"_partition";
} else if(signal=="SNP maf call") {
ret = "snp_maf_"+chr+"_";
ret += to_string(bin);
if(flags&FLAG_USEMASK) ret += "_mask";
if(flags&FLAG_USEID) ret += "_id";
if(flags&FLAG_USEHAP) ret += "_hap";
return ret+"_call";
} else if(signal=="SNP likelihood") {
ret = "snp_likelihood_"+chr+"_";
ret += to_string(bin);
if(flags&FLAG_USEMASK) ret += "_mask";
if(flags&FLAG_USEID) ret += "_id";
if(flags&FLAG_USEHAP) ret += "_hap";
return ret;
} else if(signal=="SNP likelihood partition") {
ret = "snp_likelihood_"+chr+"_";
ret += to_string(bin);
if(flags&FLAG_USEMASK) ret += "_mask";
if(flags&FLAG_USEID) ret += "_id";
if(flags&FLAG_USEHAP) ret += "_hap";
return ret+"_partition";
} else if(signal=="SNP likelihood call") {
ret = "snp_likelihood_"+chr+"_";
ret += to_string(bin);
if(flags&FLAG_USEMASK) ret += "_mask";
if(flags&FLAG_USEID) ret += "_id";
if(flags&FLAG_USEHAP) ret += "_hap";
return ret+"_call";
} else if(signal=="SNP i1") {
ret = "snp_i1_"+chr+"_";
ret += to_string(bin);
if(flags&FLAG_USEMASK) ret += "_mask";
if(flags&FLAG_USEID) ret += "_id";
if(flags&FLAG_USEHAP) ret += "_hap";
return ret;
} else if(signal=="SNP i1 partition") {
ret = "snp_i1_"+chr+"_";
ret += to_string(bin);
if(flags&FLAG_USEMASK) ret += "_mask";
if(flags&FLAG_USEID) ret += "_id";
if(flags&FLAG_USEHAP) ret += "_hap";
return ret+"_partition";
} else if(signal=="SNP i1 call") {
ret = "snp_i1_"+chr+"_";
ret += to_string(bin);
if(flags&FLAG_USEMASK) ret += "_mask";
if(flags&FLAG_USEID) ret += "_id";
if(flags&FLAG_USEHAP) ret += "_hap";
return ret+"_call";
} else if(signal=="SNP i2") {
ret = "snp_i2_"+chr+"_";
ret += to_string(bin);
if(flags&FLAG_USEMASK) ret += "_mask";
if(flags&FLAG_USEID) ret += "_id";
if(flags&FLAG_USEHAP) ret += "_hap";
return ret;
} else if(signal=="SNP i3") {
ret = "snp_i3_"+chr+"_";
ret += to_string(bin);
if(flags&FLAG_USEMASK) ret += "_mask";
if(flags&FLAG_USEID) ret += "_id";
if(flags&FLAG_USEHAP) ret += "_hap";
return ret;
} else if(signal=="SNP i4") {
ret = "snp_i4_"+chr+"_";
ret += to_string(bin);
if(flags&FLAG_USEMASK) ret += "_mask";
if(flags&FLAG_USEID) ret += "_id";
if(flags&FLAG_USEHAP) ret += "_hap";
return ret;
} else if(signal=="SNP count dist") {
ret = "snp_dist_bafc_"+(chr==""?"":chr+"_");
ret += to_string(bin);
if(flags&FLAG_USEMASK) ret += "_mask";
if(flags&FLAG_USEID) ret += "_id";
if(flags&FLAG_USEHAP) ret += "_hap";
if(flags&FLAG_SEX) ret += "_sex";
return ret;
} else if(signal=="SNP baf dist") {
ret = "snp_dist_baf_"+(chr==""?"":chr+"_");
ret += to_string(bin);
if(flags&FLAG_USEMASK) ret += "_mask";
if(flags&FLAG_USEID) ret += "_id";
if(flags&FLAG_USEHAP) ret += "_hap";
if(flags&FLAG_SEX) ret += "_sex";
return ret;
} else if(signal=="SNP maf dist") {
ret = "snp_dist_maf_"+(chr==""?"":chr+"_");
ret += to_string(bin);
if(flags&FLAG_USEMASK) ret += "_mask";
if(flags&FLAG_USEID) ret += "_id";
if(flags&FLAG_USEHAP) ret += "_hap";
if(flags&FLAG_SEX) ret += "_sex";
return ret;
} else if(signal=="SNP likelihood dist") {
ret = "snp_dist_likelihood_"+(chr==""?"":chr+"_");
ret += to_string(bin);
if(flags&FLAG_USEMASK) ret += "_mask";
if(flags&FLAG_USEID) ret += "_id";
if(flags&FLAG_USEHAP) ret += "_hap";
if(flags&FLAG_SEX) ret += "_sex";
return ret;
} else if(signal=="SNP i1 dist") {
ret = "snp_dist_i1_"+(chr==""?"":chr+"_");
ret += to_string(bin);
if(flags&FLAG_USEMASK) ret += "_mask";
if(flags&FLAG_USEID) ret += "_id";
if(flags&FLAG_USEHAP) ret += "_hap";
if(flags&FLAG_SEX) ret += "_sex";
return ret;
} else if(signal=="SNP i2 dist") {
ret = "snp_dist_i2_"+(chr==""?"":chr+"_");
ret += to_string(bin);
if(flags&FLAG_USEMASK) ret += "_mask";
if(flags&FLAG_USEID) ret += "_id";
if(flags&FLAG_USEHAP) ret += "_hap";
if(flags&FLAG_SEX) ret += "_sex";
return ret;
} else if(signal=="SNP i3 dist") {
ret = "snp_dist_i3_"+(chr==""?"":chr+"_");
ret += to_string(bin);
if(flags&FLAG_USEMASK) ret += "_mask";
if(flags&FLAG_USEID) ret += "_id";
if(flags&FLAG_USEHAP) ret += "_hap";
if(flags&FLAG_SEX) ret += "_sex";
return ret;
} else if(signal=="SNP i4 dist") {
ret = "snp_dist_i4_"+(chr==""?"":chr+"_");
ret += to_string(bin);
if(flags&FLAG_USEMASK) ret += "_mask";
if(flags&FLAG_USEID) ret += "_id";
if(flags&FLAG_USEHAP) ret += "_hap";
if(flags&FLAG_SEX) ret += "_sex";
return ret;
}
return "";
}
int IO::lenChromWithTree(string chr,bool vcf) {
if(!vcf) { for(int i=0;i<rd_tree.size();i++)
if(rd_tree[i]==chr) return rd_tree_len[i];
} else { for(int i=0;i<snp_tree.size();i++)
if(snp_tree[i]==chr) return snp_tree_len[i];
}
return -1;
}
int IO::nChromWithTree(bool vcf) {
if(!vcf) return rd_tree.size();
else return snp_tree.size();
}
int IO::getChromNamesWithTree(string *chrom_names,bool vcf) {
if(!vcf) {
for(int i=0;i<rd_tree.size();i++) chrom_names[i]=rd_tree[i];
return rd_tree.size();
}
else {
for(int i=0;i<snp_tree.size();i++) chrom_names[i]=snp_tree[i];
return snp_tree.size();
}
}
string IO::chromWithTree(int i,bool vcf) {
if(!vcf) return rd_tree[i];
else return snp_tree[i];
}
int IO::chromLenWithTree(int i,bool vcf) {
if(!vcf) return rd_tree_len[i];
else return snp_tree_len[i];
}
TString IO::getDirName(int bin) {
TString ret = "bin_";
ret += bin;
return ret;
}
TTree* IO::getTree(string chr,string signal) {
return (TTree*)file.Get(treeName(chr,signal));
}
TH1* IO::getSignal(string chr, int bin, string signal, unsigned int flags) {
TDirectory *d = NULL;
if (bin > 0) {
TString dir=getDirName(bin);
d = (TDirectory*)file.Get(dir);
if (!d) {
cerr<<"Can't find directory '"<<dir<<"'."<<endl;
return NULL;
}
d->cd();
} else d = &file;
TH1 *his = (TH1*)d->Get(signalName(chr,bin,signal,flags));
if (!his) return NULL;
gROOT->cd();
TH1 *ret = (TH1*)his->Clone(his->GetName());
return ret;
}
TH1* IO::newSignalTH1(string title,string chr, int bin, string signal, unsigned int flags,int res,double min,double max) {
TString h_name = signalName(chr,bin,signal,flags);
TString h_title = title; if(chr!="") {h_title += " "; h_title +=chr;}
TH1 *ret = new TH1D(h_name,h_title,res,min,max);
ret->SetDirectory(0);
return ret;
}
TH2* IO::newSignalTH2(string title,string chr, int bin, string signal, unsigned int flags,int res1,double min1,double max1,int res2,double min2,double max2) {
TString h_name = signalName(chr,bin,signal,flags);
TString h_title = title; if(chr!="") {h_title += " "; h_title +=chr;}
TH2 *ret = new TH2D(h_name,h_title,res1,min1,max1,res2,min2,max2);
ret->SetDirectory(0);
return ret;
}
bool IO::writeHistogramsToBinDir(int bin,TH1 *his1,TH1 *his2,TH1 *his3,TH1 *his4,TH1 *his5,TH1 *his6) {
//TFile file(rootfile,"Update");
//if(file.IsZombie()) {
// cerr << "Can't open file '" << rootfile << "'." <<endl;
// return false;
//}
if(bin>0) {
file.cd();
TString dir_name=getDirName(bin);
TDirectory *dir = (TDirectory*)file.Get(dir_name);
if (!dir) {
cout<<"Making directory "<<dir_name<<" ..."<<endl;
dir = file.mkdir(dir_name);
dir->Write(dir_name);
}
if (!dir) {
cerr<<"Can't find/create directory '"<<dir_name<<"'."<<endl;
return false;
}
dir->cd();
}
if (his1) his1->Write(his1->GetName(),TObject::kOverwrite);
if (his2) his2->Write(his2->GetName(),TObject::kOverwrite);
if (his3) his3->Write(his3->GetName(),TObject::kOverwrite);
if (his4) his4->Write(his4->GetName(),TObject::kOverwrite);
if (his5) his5->Write(his5->GetName(),TObject::kOverwrite);
if (his6) his6->Write(his6->GetName(),TObject::kOverwrite);
//file.Close();
return true;
}
void IO::ls() {
cout << endl << "Root file: " << rootfile << endl << "-------------------" << endl;
if(rd_tree.size()>0) {
cout << "RD TREES:";
for(int i=0;i<rd_tree.size();i++)
cout << ((i==0)?" ":", ") << rd_tree[i];
}
cout << endl << endl;
if(snp_tree.size()>0) {
cout << "SNP TREES:";
for(int i=0;i<snp_tree.size();i++)
cout << ((i==0)?" ":", ") << snp_tree[i];
}
cout << endl << endl;
if(bin_dir.size()>0) {
cout << "DIRECTORIES FOR BIN SIZES:";
for(int i=0;i<bin_dir.size();i++)
cout << ((i==0)?" ":", ") << bin_dir[i];
}
cout << endl << endl;
}
void IO::cptrees(string newrootfile,string *user_chroms,int n_chroms) {
set<string> userchr;
TFile *newfile=new TFile(newrootfile.c_str(),"Recreate");
for(int i=0;i<n_chroms;i++) userchr.insert(user_chroms[i]);
for(int i=0;i<rd_tree.size();i++) if((n_chroms==0)||(userchr.find(rd_tree[i])!=userchr.end())) {
cout << "Copying RD tree for chromosome " << rd_tree[i] << "." << endl;
TTree* t = (TTree*)file.Get(treeName(rd_tree[i],"RD"));
newfile->cd();
TTree* nt=t->CloneTree();
nt->Write(0,TObject::kOverwrite);
}
for(int i=0;i<snp_tree.size();i++) if((n_chroms==0)||(userchr.find(snp_tree[i])!=userchr.end())) {
cout << "Copying SNP tree for chromosome " << snp_tree[i] << "." << endl;
TTree* t = (TTree*)file.Get(treeName(snp_tree[i],"SNP"));
newfile->cd();
TTree* nt=t->CloneTree();
nt->Write(0,TObject::kOverwrite);
}
//Trees with obsolite "vcf_" prefix
TIterator *it = file.GetListOfKeys()->MakeIterator();
while (TKey *key = (TKey*)it->Next()) {
TObject *obj = key->ReadObj();
if (obj->IsA() == TTree::Class()) {
string chrom = obj->GetName();
string title = obj->GetTitle();
if (chrom == "") continue;
int len;
try {len=stoi(title.substr(title.find(";") + 1));}
catch(...) {len=0;}
string vcfstr="vcf_";
if (chrom.compare(0,vcfstr.size(),vcfstr)==0) if((n_chroms==0)||(userchr.find(chrom.substr(4))!=userchr.end())) {
cout << "Copying SNP tree for chromosome " << chrom << " (obsolete name vcf_).";
TTree* t = (TTree*)file.Get(chrom.c_str());
newfile->cd();
TTree* nt=t->CloneTree();
nt->SetName(chrom.replace(0,4,"snp_").c_str());
nt->Write(0,TObject::kOverwrite);
cout << " Tree saved as " << chrom << "." << endl;
}
}
}
newfile->Close();
delete newfile;
}