-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmetaDMG.cpp
More file actions
1435 lines (1306 loc) · 51.4 KB
/
metaDMG.cpp
File metadata and controls
1435 lines (1306 loc) · 51.4 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
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
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// gpl thorfinn@binf.ku.dk
#include <getopt.h> // for optarg, getopt_long, optind, option
#include <htslib/bgzf.h> // for bgzf_read, bgzf_close, bgzf_open, BGZF
#include <htslib/hts.h> // for htsFormat, hts_opt_add, htsFile, hts_opt
#include <htslib/sam.h> // for sam_hdr_read, bam_hdr_destroy, sam_hdr_t...
#include <strings.h> // for strcasecmp
#include <time.h> // for clock, time, clock_t, time_t
#include <zlib.h> // for gzprintf, gzclose, gzgets, gzopen, Z_NULL
#include <climits>
#include <cassert> // for assert
#include <cstdio> // for fprintf, NULL, stderr, stdout, fopen
#include <cstdlib> // for atoi, exit, free, atof, calloc, malloc
#include <cstring> // for strdup, strcmp, strtok, strlen, strncpy
#include <map> // for __map_iterator, map, operator!=, map<>::...
#include <vector> // for vector
#include "main_pmd.h" // for main_pmd
#include "main_dfit.h" // for main_pmd
#include "Aggregate_stat.h" // for main_pmd
#include "ngsLCA.h" // for mean, var, gccontent, print_chain
#include "profile.h" // for mydataD, mydata2, load_bdamage3, (anonym...
#include "shared.h" // for parse_names, parse_nodes
#include "types.h" // for int2intvec, int2int
#include "version.h" // for METADAMAGE_VERSION
#include "merge_bdamage.h" //for merging multiple bdmage files
#ifdef __REGRESSION__
#include "regression.h" // for main_regression
#endif
htsFormat *dingding2 = (htsFormat *)calloc(1, sizeof(htsFormat));
typedef std::map<int, char *> int2char;
int usage_getdamage(FILE *fp) {
fprintf(fp, "\nUsage: metadamage getdamage [options] <in.bam>|<in.sam>|<in.cram>\n");
fprintf(fp, "\nExample: ./metaDMG-cpp getdamage -l 10 -p 5 --threads 8 ../data/subs.sam\nOptions:\n");
fprintf(fp, " -n/--threads\t\t number of threads used for reading/writing (default: 4)\n");
fprintf(fp, " -f/--fasta\t\t reference genome (required with CRAM)\n");
fprintf(fp, " -l/--min_length\t minimum read length (default: 35)\n");
fprintf(fp, " -p/--print_length\t number of base pairs from read termini to estimate damage (default: 5)\n");
fprintf(fp, " -r/--run_mode\t\t 0: global estimate (default)\n\t\t\t 1: damage patterns will be calculated for each chr/scaffold contig\n");
fprintf(fp, " -i/--ignore_errors\t continue analyses even if there are errors.\n");
fprintf(fp, " -o/--out_prefix\t output prefix (default: meta)\n");
return 1;
}
double *getval(std::map<int, double *> &retmap, int2intvec &child, int taxid, int howmany) {
// fprintf(stderr,"getval\t%d\t%d\n",taxid,howmany);
std::map<int, double *>::iterator it = retmap.find(taxid);
if (it != retmap.end()) {
// fprintf(stderr,"has found: %d in retmap\n",it->first);
#if 0
fprintf(stderr,"val\t%d",taxid);
for(int i=0;i<3*howmany+1;i++)
fprintf(stderr,"\t%f",it->second[i]);
fprintf(stderr,"\n");
#endif
return it->second;
}
double *ret = new double[3 * howmany + 1];
for (int i = 0; i < 3 * howmany + 1; i++)
ret[i] = 0.0;
if (child.size() > 0) { // if we have supplied -nodes
int2intvec::iterator it2 = child.find(taxid);
if (it2 != child.end()) {
std::vector<int> &avec = it2->second;
for (size_t i = 0; i < avec.size(); i++) {
// fprintf(stderr,"%d/%d %d\n",i,avec.size(),avec[i]);
double *tmp = getval(retmap, child, avec[i], howmany);
for (int i = 0; i < 3 * howmany + 1; i++)
ret[i] += tmp[i];
}
}
}
retmap[taxid] = ret;
return ret;
}
int INT_WARN = 1;
//apparantly there is an issue when data is not only leaf.
std::map<int,mydataD> getval_full_norec(std::map<int, mydataD> &retmap, int2int &parent, int howmany) {
// fprintf(stderr,"getval\t%d\t%d\n",taxid,howmany);
std::map<int, mydataD> results;
//funky modern syntax below
//loop over all entries in retmap. lizard king
for (const auto &[taxid, data] : retmap) {
int current = taxid;
// fprintf(stderr,"taxid: %d\n",taxid);
while (true) {
// fprintf(stderr,"current: %d\n",current);
mydataD &target = results[current]; //<- will call constructor if doesnt exists. magic magic
if (!target.fwD) {//if it doesnt exists then allocate. Just like fw.D!=NULL
target.fwD = new double[16 * howmany]();
target.bwD = new double[16 * howmany]();
target.nal = 0;
}
target.nal += data.nal;
if (target.nal > INT_MAX && INT_WARN) {
fprintf(stderr, "\t-> Potential issue, sum of alignment counts exceeds INT_MAX\n");
INT_WARN = 0;
}
for (int i = 0; i < 16 * howmany; ++i) {
target.fwD[i] += data.fwD[i];
target.bwD[i] += data.bwD[i];
}
auto it = parent.find(current);
// fprintf(stderr,"second: %d\n",it->second);
if (it == parent.end()) break;
//break if up is same as current. That only happens with root that has tqxid=1
if(current == it->second)
break;
current = it->second;
}
}
return results;
}
int mywarn =1;
mydata2 getval_stats_unknownfunctionality(std::map<int, mydata2> &retmap, int2intvec &child, int taxid) {
if(mywarn>0){
fprintf(stderr,"PAS PAA SATAN\n");
mywarn--;
}
// fprintf(stderr,"getval\t%d\t%d\n",taxid,howmany);
std::map<int, mydata2>::iterator it = retmap.find(taxid);
if (it != retmap.end()) {
// fprintf(stderr,"has found: %d in retmap\n",it->first);
#if 0
fprintf(stderr,"val\t%d",taxid);
for(int i=0;i<3*howmany+1;i++)
fprintf(stderr,"\t%f",it->second[i]);
fprintf(stderr,"\n");
#endif
return it->second;
}
mydata2 ret;
ret.nreads = 0;
ret.data = new double[4];
ret.data[0] = ret.data[1] = ret.data[2] = ret.data[3] = 0;
if (child.size() > 0) { // if we have supplied -nodes
int2intvec::iterator it2 = child.find(taxid);
if (it2 != child.end()) {
std::vector<int> &avec = it2->second;
for (size_t i = 0; i < avec.size(); i++) {
mydata2 tmp = getval_stats_unknownfunctionality(retmap, child, avec[i]);
ret.nreads += tmp.nreads;
}
for (size_t i = 0; i < avec.size(); i++) {
// fprintf(stderr,"%d/%d %d\n",i,avec.size(),avec[i]);
mydata2 tmp = getval_stats_unknownfunctionality(retmap, child, avec[i]);
if (tmp.nreads == 0)
continue;
double a = tmp.nreads;
double b = ret.nreads;
double scal = a / b;
// fprintf(stderr,"a: %f b: %f scal: %f\n",a,b,scal);
for (int i = 0; i < 4; i++)
ret.data[i] += tmp.data[i] * scal;
}
}
}
retmap[taxid] = ret;
return ret;
}
int main_getdamage(int argc, char **argv) {
if (argc == 1)
return usage_getdamage(stderr);
// int MAXLENGTH = 256;
int minLength = 35;
int printLength = 5;
char *refName = NULL;
char *fname = NULL;
int runmode = 0; // this means one species, runmode=1 means multi species
htsFile *fp = NULL;
char *onam = strdup("meta");
int nthreads = 4;
int ignore_errors = 0;
// fix thesepro
static struct option lopts[] = {
{"threads", required_argument, 0, 'n'},
{"fasta", required_argument, 0, 'f'},
{"min_length", required_argument, 0, 'l'},
{"print_length", required_argument, 0, 'p'},
{"run_mode", required_argument, 0, 'r'},
{"ignore_errors", no_argument, &ignore_errors, 1},
{"out_prefix", required_argument, 0, 'o'},
{"help", no_argument, 0, 'h'},
{NULL, 0, NULL, 0}};
int c;
while ((c = getopt_long(argc, argv,
"n:f:l:p:r:o:h",
lopts, NULL)) >= 0) {
switch (c) {
case 'n':
nthreads = atoi(optarg);
break;
case 'f':
refName = strdup(optarg);
break;
case 'l':
minLength = atoi(optarg);
break;
case 'p':
printLength = atoi(optarg);
break;
case 'r':
runmode = atoi(optarg);
break;
case 'o':
free(onam);
onam = strdup(optarg);
break;
case 'h':
return usage_getdamage(stdout);
default:
fprintf(stderr, "Never here: %s\n", optarg);
break;
}
}
if (optind < argc)
fname = strdup(argv[optind]);
fprintf(stderr, "\t-> ./metaDMG-cpp refName: %s min_length: %d print_length: %d run_mode: %d out_prefix: %s nthreads: %d ignore_errors: %d\n", refName, minLength, printLength, runmode, onam, nthreads, ignore_errors);
if (fname == NULL) {
usage_getdamage(stderr);
return 0;
}
if (refName) {
int lenlen = 10 + strlen(refName) + 1;
char *ref = (char *)malloc(lenlen);
snprintf(ref,lenlen, "reference=%s", refName);
hts_opt_add((hts_opt **)&dingding2->specific, ref);
free(ref);
}
if ((fp = sam_open_format(fname, "r", dingding2)) == NULL) {
fprintf(stderr, "[%s] nonexistant file: %s\n", __FUNCTION__, fname);
exit(0);
}
bam1_t *b = bam_init1();
bam_hdr_t *hdr = sam_hdr_read(fp);
int checkIfSorted(char *str);
if(checkIfSorted(hdr->text)){
fprintf(stderr, "Input alignment file is not sorted.");
if(!ignore_errors)
return 1;
}
int ret;
damage *dmg = new damage(printLength, nthreads, 0);
int skipper[4] = {3, 3, 3, 3};
std::map<int, std::vector<float> > gcconts;
std::map<int, std::vector<float> > seqlens;
while (((ret = sam_read1(fp, hdr, b))) >= 0) {
if (bam_is_unmapped(b)) {
if (skipper[0])
fprintf(stderr, "skipping: %s unmapped, this msg is printed: %d times more\n", bam_get_qname(b), --skipper[0]);
continue;
}
if (bam_is_failed(b)) {
if (skipper[1])
fprintf(stderr, "skipping: %s failed: flags=%d, this msg is printed: %d times more\n", bam_get_qname(b), b->core.flag, --skipper[1]);
continue;
}
if (b->core.l_qseq < minLength) {
if (skipper[2])
fprintf(stderr, "skipping: %s too short, this msg is printed %d times more \n", bam_get_qname(b), --skipper[2]);
continue;
}
dmg->damage_analysis(b, runmode != 0 ? b->core.tid : 0, 1);
float mygc = gccontent(b);
float mylen = b->core.l_qseq;
int whichref = 0;
if (runmode == 1)
whichref = b->core.tid;
std::map<int, std::vector<float> >::iterator it = gcconts.find(whichref);
if (it == gcconts.end()) {
std::vector<float> tmp1;
tmp1.push_back(mygc);
gcconts[whichref] = tmp1;
std::vector<float> tmp2;
tmp2.push_back(mylen);
seqlens[whichref] = tmp2;
} else {
if(it->second.size()<1000000)
it->second.push_back(mygc);
it = seqlens.find(whichref);
assert(it != seqlens.end());
if(it->second.size()<1000000)
it->second.push_back(mylen);
}
}
dmg->printit(stdout, printLength);
dmg->write(onam, runmode == 1 ? hdr : NULL);
dmg->bwrite(onam);
// write stat
char buf[1024];
snprintf(buf, 1024, "%s.stat.gz", onam);
fprintf(stderr, "\t-> Outputting overall statistic in file: \"%s\"\n", buf);
gzFile fpstat = NULL;
assert((fpstat = gzopen(buf, "wb")) != NULL);
gzprintf(fpstat,"taxid\tnreads\tmean_len\tvar_len\tmean_gc\tvar_gc\tlca\trank\n");
for (std::map<int, std::vector<float> >::iterator it = gcconts.begin(); it != gcconts.end(); it++) {
std::map<int, triple>::iterator it2 = dmg->assoc.find(it->first);
assert(it2 != dmg->assoc.end());
std::map<int, std::vector<float> >::iterator it3 = seqlens.find(it->first);
assert(it3 != seqlens.end());
if (0)
gzprintf(fpstat, "%d\t%lu\t%f\t%f\t%f\t%f\tNA\tNA\n", it->first, it2->second.nreads, mean(it3->second), var(it3->second), mean(it->second), var(it->second));
else
gzprintf(fpstat, "%s\t%lu\t%f\t%f\t%f\t%f\tNA\tNA\n", sam_hdr_tid2name(hdr, it->first), it2->second.nreads, mean(it3->second), var(it3->second), mean(it->second), var(it->second));
}
gzclose(fpstat);
sam_hdr_destroy(hdr);
bam_destroy1(b);
sam_close(fp);
destroy_damage(dmg);
free(fname);
free(onam);
return 0;
}
int main_index(int argc, char **argv) {
char *infile = argv[1];
fprintf(stderr, "infile: %s\n", infile);
char onam[strlen(infile) + 20];
snprintf(onam,strlen(infile) + 20, "%s.idx", infile);
fprintf(stderr, "outfile: %s\n", onam);
FILE *fp = NULL;
if (((fp = fopen(onam, "wb"))) == NULL) {
fprintf(stderr, "Problem opening file\n");
return 0;
}
fclose(fp);
return 0;
}
int main_print(int argc, char **argv) {
if (argc == 1) {
fprintf(stderr, "./metaDMG-cpp print file.bdamage.gz [-names names.gz -bam file.bam -ctga -countout -nodes -howmany -r -doOld -nodes]\n");
return 0;
}
char *infile = NULL;
char *inbam = NULL;
char *infile_nodes = NULL;
char *infile_names = NULL;
int ctga = 0; // only print ctga errors
int search = -1;
int countout = 0;
int howmany = 15;
int doold = 1;
while (*(++argv)) {
if (strcasecmp("-names", *argv) == 0)
infile_names = strdup(*(++argv));
else if (strcasecmp("-bam", *argv) == 0)
inbam = strdup(*(++argv));
else if (strcasecmp("-r", *argv) == 0)
search = atoi(*(++argv));
else if (strcasecmp("-howmany", *argv) == 0)
howmany = atoi(*(++argv));
else if (strcasecmp("-ctga", *argv) == 0)
ctga = 1;
else if (strcasecmp("-doOld", *argv) == 0)
doold = 1;
else if (strcasecmp("-countout", *argv) == 0)
countout = 1;
else if (strcasecmp("-nodes", *argv) == 0)
infile_nodes = strdup(*(++argv));
else
infile = strdup(*argv);
}
fprintf(stderr, "infile: %s inbam: %s search: %d ctga: %d countout: %d nodes: %s names: %s howmany: %d\n", infile, inbam, search, ctga, countout, infile_nodes, infile_names, howmany);
assert(infile);
int2char name_map;
if (infile_names != NULL)
name_map = parse_names(infile_names);
// map of taxid -> taxid
int2int parent;
// map of taxid -> rank
int2char rank;
// map of parent -> child taxids
int2intvec child;
if (infile_nodes != NULL)
parse_nodes(infile_nodes, rank, parent, child, 1);
if (search != -1 && doold == 0) {
std::map<int, double *> retmap = load_bdamage3(infile, howmany);
double *dbl = getval(retmap, child, search, howmany);
double dbldbl[3 * howmany + 1]; // 3 because ct,ga,other
dbldbl[0] = dbl[0];
for (int i = 0; i < 3 * howmany; i++)
dbldbl[i + 1] = dbl[1 + i] / dbl[0];
fprintf(stdout, "%d\t%.0f", search, dbldbl[0]);
for (int i = 0; i < 3 * howmany; i++)
fprintf(stdout, "\t%f", dbldbl[1 + i]);
fprintf(stdout, "\n");
return 0;
}
BGZF *bgfp = NULL;
samFile *samfp = NULL;
bam_hdr_t *hdr = NULL;
if (((bgfp = bgzf_open(infile, "r"))) == NULL) {
fprintf(stderr, "Could not open input bdamage.gz file: %s\n", infile);
return 1;
}
if (inbam != NULL) {
if (((samfp = sam_open_format(inbam, "r", NULL))) == NULL) {
fprintf(stderr, "Could not open input BAM file: %s\n", inbam);
return 1;
}
if (((hdr = sam_hdr_read(samfp))) == NULL) {
fprintf(stderr, "Could not read header for: %s\n", inbam);
return 1;
}
}
int printlength;
assert(sizeof(int) == bgzf_read(bgfp, &printlength, sizeof(int)));
fprintf(stderr, "\t-> printlength(howmany) from inputfile: %d\n", printlength);
int ref_nreads[2];
if (ctga == 0) {
if (hdr != NULL)
fprintf(stdout, "Reference\tNalignments\tDirection\tPos\tAA\tAC\tAG\tAT\tCA\tCC\tCG\tCT\tGA\tGC\tGG\tGT\tTA\tTC\tTG\tTT\n");
else if (infile_names != NULL)
fprintf(stdout, "FunkyName\tNalignments\tDirection\tPos\tAA\tAC\tAG\tAT\tCA\tCC\tCG\tCT\tGA\tGC\tGG\tGT\tTA\tTC\tTG\tTT\n");
else
fprintf(stdout, "taxid\tNalignments\tDirection\tPos\tAA\tAC\tAG\tAT\tCA\tCC\tCG\tCT\tGA\tGC\tGG\tGT\tTA\tTC\tTG\tTT\n");
} else {
}
float data[16];
while (1) {
int nread = bgzf_read(bgfp, ref_nreads, 2 * sizeof(int));
double ctgas[2 * printlength];
if (nread == 0)
break;
assert(nread == 2 * sizeof(int));
for (int at = 0; at < printlength; at++) {
assert(16 * sizeof(float) == bgzf_read(bgfp, data, sizeof(float) * 16));
if (at > howmany)
continue;
if (search == -1 || search == ref_nreads[0]) {
if (ctga == 0) {
if (hdr != NULL)
fprintf(stdout, "%s\t%d\t5\'\t%d", hdr->target_name[ref_nreads[0]], ref_nreads[1], at);
else if (infile_names != NULL) {
int2char::iterator itt = name_map.find(ref_nreads[0]);
if (itt == name_map.end()) {
fprintf(stderr, "\t-> Problem finding taxid: \'%d' in namedatabase: \'%s\'\n", ref_nreads[0], infile_names);
exit(0);
}
fprintf(stdout, "\"%s\"\t%d\t5\'\t%d", itt->second, ref_nreads[1], at);
} else
fprintf(stdout, "%d\t%d\t5\'\t%d", ref_nreads[0], ref_nreads[1], at);
} else {
if (at == 0)
fprintf(stdout, "%d\t%d", ref_nreads[0], ref_nreads[1]);
}
if (countout == 1) {
for (int i = 0; i < 16; i++)
fprintf(stdout, "\t%f", data[i]);
fprintf(stdout, "\n");
} else {
float flt[16];
for (int i = 0; i < 4; i++) {
double tsum = 0;
for (int j = 0; j < 4; j++) {
tsum += data[i * 4 + j];
flt[i * 4 + j] = data[i * 4 + j];
}
if (tsum == 0) tsum = 1;
for (int j = 0; j < 4; j++)
flt[i * 4 + j] /= tsum;
}
if (ctga == 0) {
for (int j = 0; j < 16; j++)
fprintf(stdout, "\t%f", flt[j]);
fprintf(stdout, "\n");
} else
ctgas[at] = flt[7];
}
}
}
if (search == -1 || search == ref_nreads[0]) {
if (ctga == 1) {
for (int i = 0; i < printlength; i++)
fprintf(stdout, "\t%f", ctgas[i]);
}
}
for (int at = 0; at < printlength; at++) {
assert(16 * sizeof(int) == bgzf_read(bgfp, data, sizeof(float) * 16));
if (at > howmany)
continue;
if (search == -1 || search == ref_nreads[0]) {
if (ctga == 0) {
if (hdr != NULL)
fprintf(stdout, "%s\t%d\t3\'\t%d", hdr->target_name[ref_nreads[0]], ref_nreads[1], at);
else if (infile_names != NULL) {
int2char::iterator itt = name_map.find(ref_nreads[0]);
if (itt == name_map.end()) {
fprintf(stderr, "\t-> Problem finding taxid: \'%d' in namedatabase: \'%s\'\n", ref_nreads[0], infile_names);
exit(0);
}
fprintf(stdout, "\"%s\"\t%d\t3\'\t%d", itt->second, ref_nreads[1], at);
} else
fprintf(stdout, "%d\t%d\t3\'\t%d", ref_nreads[0], ref_nreads[1], at);
}
if (countout == 1) {
for (int i = 0; i < 16; i++)
fprintf(stdout, "\t%f",data[i]);
fprintf(stdout, "\n");
} else {
float flt[16];
for (int i = 0; i < 4; i++) {
double tsum = 0;
for (int j = 0; j < 4; j++) {
tsum += data[i * 4 + j];
flt[i * 4 + j] = data[i * 4 + j];
}
if (tsum == 0) tsum = 1;
for (int j = 0; j < 4; j++)
flt[i * 4 + j] /= tsum;
}
if (ctga == 0) {
for (int j = 0; j < 16; j++)
fprintf(stdout, "\t%f", flt[j]);
fprintf(stdout, "\n");
} else
ctgas[at + printlength] = flt[8];
}
}
}
if (search == -1 || search == ref_nreads[0]) {
if (ctga == 1) {
for (int i = 0; i < printlength; i++)
fprintf(stdout, "\t%f", ctgas[printlength + i]);
fprintf(stdout, "\n");
}
}
}
//cleanup
for(int2char::iterator it=name_map.begin();it!=name_map.end();it++)
free(it->second);
for(int2char::iterator it=rank.begin();it!=rank.end();it++)
free(it->second);
if (bgfp)
bgzf_close(bgfp);
if (hdr)
bam_hdr_destroy(hdr);
if (samfp)
sam_close(samfp);
if(infile)
free(infile);
if(infile_nodes)
free(infile_nodes);
if(infile_names)
free(infile_names);
if(inbam)
free(inbam);
return 0;
}
int main_print2(int argc, char **argv) {
if (argc == 1) {
fprintf(stderr, "./metaDMG-cpp print2 file.bdamage.gz [-acc2tax file.gz -bam file.bam -ctga -countout -nodes -howmany -r -doOld -nodes]\n");
return 0;
}
char *infile = NULL;
char *inbam = NULL;
char *acc2tax = NULL;
int ctga = 0; // only print ctga errors
int search = -1;
int countout = 0;
char *infile_nodes = NULL;
int howmany = 15;
int doold = 0;
while (*(++argv)) {
if (strcasecmp("-acc2tax", *argv) == 0)
acc2tax = strdup(*(++argv));
else if (strcasecmp("-bam", *argv) == 0)
inbam = strdup(*(++argv));
else if (strcasecmp("-r", *argv) == 0)
search = atoi(*(++argv));
else if (strcasecmp("-howmany", *argv) == 0)
howmany = atoi(*(++argv));
else if (strcasecmp("-ctga", *argv) == 0)
ctga = 1;
else if (strcasecmp("-doOld", *argv) == 0)
doold = 1;
else if (strcasecmp("-countout", *argv) == 0)
countout = 1;
else if (strcasecmp("-nodes", *argv) == 0)
infile_nodes = strdup(*(++argv));
else
infile = strdup(*argv);
}
fprintf(stderr, "infile: %s inbam: %s names: %s search: %d ctga: %d countout: %d nodes: %s\n", infile, inbam, acc2tax, search, ctga, countout, infile_nodes);
assert(infile);
int2char name_map;
if (acc2tax != NULL)
name_map = parse_names(acc2tax);
// map of taxid -> taxid
int2int parent;
// map of taxid -> rank
int2char rank;
// map of parent -> child taxids
int2intvec child;
if (infile_nodes != NULL)
parse_nodes(infile_nodes, rank, parent, child, 1);
if (search != -1 && doold == 0) {
std::map<int, double *> retmap = load_bdamage3(infile, howmany);
double *dbl = getval(retmap, child, search, howmany);
double dbldbl[3 * howmany + 1]; // 3 because ct,ga,other
dbldbl[0] = dbl[0];
for (int i = 0; i < 3 * howmany; i++)
dbldbl[i + 1] = dbl[1 + i] / dbl[0];
fprintf(stdout, "%d\t%.0f", search, dbldbl[0]);
for (int i = 0; i < 3 * howmany; i++)
fprintf(stdout, "\t%f", dbldbl[1 + i]);
fprintf(stdout, "\n");
return 0;
}
BGZF *bgfp = NULL;
samFile *samfp = NULL;
bam_hdr_t *hdr = NULL;
if (((bgfp = bgzf_open(infile, "r"))) == NULL) {
fprintf(stderr, "Could not open input bdamage.gz file: %s\n", infile);
return 1;
}
if (inbam != NULL) {
if (((samfp = sam_open_format(inbam, "r", NULL))) == NULL) {
fprintf(stderr, "Could not open input BAM file: %s\n", inbam);
return 1;
}
if (((hdr = sam_hdr_read(samfp))) == NULL) {
fprintf(stderr, "Could not read header for: %s\n", inbam);
return 1;
}
}
int printlength;
assert(sizeof(int) == bgzf_read(bgfp, &printlength, sizeof(int)));
fprintf(stderr, "\t-> printlength(howmany) from inputfile: %d\n", printlength);
int ref_nreads[2];
char *type_name = NULL;
if (hdr != NULL)
type_name = strdup("Reference");
else if (acc2tax != NULL)
type_name = strdup("FunkyName");
else
type_name = strdup("taxid");
if (ctga == 0) {
fprintf(stdout, "%s\tNalignments\tDirection\tPos\tAA\tAC\tAG\tAT\tCA\tCC\tCG\tCT\tGA\tGC\tGG\tGT\tTA\tTC\tTG\tTT\n", type_name);
} else {
fprintf(stdout, "%s\tNalignment", type_name);
for (int i = 0; i < howmany; i++)
fprintf(stdout, "\tCT_%d", i);
for (int i = 0; i < howmany; i++)
fprintf(stdout, "\tGA_%d", i);
fprintf(stdout, "\n");
}
int data[16];
while (1) {
int nread = bgzf_read(bgfp, ref_nreads, 2 * sizeof(int));
double ctgas[2 * printlength];
if (nread == 0)
break;
fprintf(stderr, "ref: %d nreads: %d\n", ref_nreads[0], ref_nreads[1]);
assert(nread == 2 * sizeof(int));
for (int at = 0; at < printlength; at++) {
assert(16 * sizeof(int) == bgzf_read(bgfp, data, sizeof(int) * 16));
if ((at + 1) > howmany)
continue;
if (search == -1 || search == ref_nreads[0]) {
if (ctga == 0) {
if (hdr != NULL)
fprintf(stdout, "%s\t%d\t5\'\t%d", hdr->target_name[ref_nreads[0]], ref_nreads[1], at);
else if (acc2tax != NULL) {
int2char::iterator itt = name_map.find(ref_nreads[0]);
if (itt == name_map.end()) {
fprintf(stderr, "\t-> Problem finding taxid: \'%d' in namedatabase: \'%s\'\n", ref_nreads[0], acc2tax);
exit(0);
}
fprintf(stdout, "\"%s\"\t%d\t5\'\t%d", itt->second, ref_nreads[1], at);
} else
fprintf(stdout, "%d\t%d\t5\'\t%d", ref_nreads[0], ref_nreads[1], at);
} else {
if (at == 0)
fprintf(stdout, "%d\t%d", ref_nreads[0], ref_nreads[1]);
}
if (countout == 1) {
for (int i = 0; i < 16; i++)
fprintf(stdout, "\t%d", data[i]);
fprintf(stdout, "\n");
} else {
float flt[16];
for (int i = 0; i < 4; i++) {
double tsum = 0;
for (int j = 0; j < 4; j++) {
tsum += data[i * 4 + j];
flt[i * 4 + j] = data[i * 4 + j];
}
if (tsum == 0) tsum = 1;
for (int j = 0; j < 4; j++)
flt[i * 4 + j] /= tsum;
}
if (ctga == 0) {
for (int j = 0; j < 16; j++)
fprintf(stdout, "\t%f", flt[j]);
fprintf(stdout, "\n");
} else
ctgas[at] = flt[7];
}
}
}
if (search == -1 || search == ref_nreads[0]) {
if (ctga == 1) {
for (int i = 0; i < howmany; i++)
fprintf(stdout, "\t%f", ctgas[i]);
}
}
for (int at = 0; at < printlength; at++) {
assert(16 * sizeof(int) == bgzf_read(bgfp, data, sizeof(int) * 16));
if (at + 1 > howmany)
continue;
if (search == -1 || search == ref_nreads[0]) {
if (ctga == 0) {
if (hdr != NULL)
fprintf(stdout, "%s\t%d\t3\'\t%d", hdr->target_name[ref_nreads[0]], ref_nreads[1], at);
else if (acc2tax != NULL) {
int2char::iterator itt = name_map.find(ref_nreads[0]);
if (itt == name_map.end()) {
fprintf(stderr, "\t-> Problem finding taxid: \'%d' in namedatabase: \'%s\'\n", ref_nreads[0], acc2tax);
exit(0);
}
fprintf(stdout, "\"%s\"\t%d\t3\'\t%d", itt->second, ref_nreads[1], at);
} else
fprintf(stdout, "%d\t%d\t3\'\t%d", ref_nreads[0], ref_nreads[1], at);
}
if (countout == 1) {
for (int i = 0; i < 16; i++)
fprintf(stdout, "\t%d", data[i]);
fprintf(stdout, "\n");
} else {
float flt[16];
for (int i = 0; i < 4; i++) {
double tsum = 0;
for (int j = 0; j < 4; j++) {
tsum += data[i * 4 + j];
flt[i * 4 + j] = data[i * 4 + j];
}
if (tsum == 0) tsum = 1;
for (int j = 0; j < 4; j++)
flt[i * 4 + j] /= tsum;
}
if (ctga == 0) {
for (int j = 0; j < 16; j++)
fprintf(stdout, "\t%f", flt[j]);
fprintf(stdout, "\n");
} else
ctgas[at + printlength] = flt[8];
}
}
}
if (search == -1 || search == ref_nreads[0]) {
if (ctga == 1) {
for (int i = 0; i < howmany; i++)
fprintf(stdout, "\t%f", ctgas[printlength + i]);
fprintf(stdout, "\n");
}
}
}
//clean up
for(int2char::iterator it=name_map.begin();it!=name_map.end();it++)
free(it->second);
for(int2char::iterator it=rank.begin();it!=rank.end();it++)
free(it->second);
if (bgfp)
bgzf_close(bgfp);
if (hdr)
bam_hdr_destroy(hdr);
if (samfp)
sam_close(samfp);
if(type_name)
free(type_name);
if(infile_nodes)
free(infile_nodes);
if(infile)
free(infile);
if(inbam)
free(inbam);
if(acc2tax)
free(acc2tax);
return 0;
}
int main_merge(int argc, char **argv) {
fprintf(stderr, "./metaDMG-cpp merge file.lca file.bdamage.gz [-names file.gz -bam file.bam -howmany 5 -nodes trestructure.gz]\n");
if (argc <= 2)
return 0;
char *infile_lca = argv[1];
char *infile_bdamage = argv[2];
char *infile_nodes = NULL;
char *inbam = NULL;
char *acc2tax = NULL;
int howmany = 5;
++argv;
while (*(++argv)) {
if (strcasecmp("-names", *argv) == 0)
acc2tax = strdup(*(++argv));
if (strcasecmp("-bam", *argv) == 0)
inbam = strdup(*(++argv));
if (strcasecmp("-howmany", *argv) == 0)
howmany = atoi(*(++argv));
if (strcasecmp("-nodes", *argv) == 0)
infile_nodes = strdup(*(++argv));
}
fprintf(stderr, "infile_lca: %s infile_bdamage: %s nodes: %s\n", infile_lca, infile_bdamage, infile_nodes);
// map of taxid -> taxid
int2int parent;
// map of taxid -> rank
int2char rank;
// map of parent -> child taxids
int2intvec child;
if (infile_nodes != NULL)
parse_nodes(infile_nodes, rank, parent, child, 1);
std::map<int, double *> retmap = load_bdamage3(infile_bdamage, howmany);
// fprintf(stderr,"retmap.size():%lu\n",retmap.size());
int2char name_map;
if (acc2tax != NULL)
name_map = parse_names(acc2tax);
BGZF *bgfp = NULL;
samFile *samfp = NULL;
bam_hdr_t *hdr = NULL;
if (inbam != NULL) {
if (((bgfp = bgzf_open(inbam, "r"))) == NULL) {
fprintf(stderr, "Could not open input BAM file: %s\n", inbam);
return 1;
}
}
if (inbam != NULL) {
if (((samfp = sam_open_format(inbam, "r", NULL))) == NULL) {
fprintf(stderr, "Could not open input BAM file: %s\n", inbam);
return 1;
}
if (((hdr = sam_hdr_read(samfp))) == NULL) {
fprintf(stderr, "Could not read header for: %s\n", inbam);
return 1;
}
}
gzFile fp = Z_NULL;
fp = gzopen(infile_lca, "rb");
assert(fp != Z_NULL);
char buf[4096];
char orig[4096];
gzgets(fp, buf, 4096); // skipheader
buf[strlen(buf) - 1] = '\0';
fprintf(stderr, "%s: VERSION:%s\n", buf, METADAMAGE_VERSION);
float presize = retmap.size();
while (gzgets(fp, buf, 4096)) {
strncpy(orig, buf, 4096);
// fprintf(stderr,"buf: %s\n",buf);
char *tok = strtok(buf, "\t\n ");
int taxid = atoi(strtok(NULL, ":"));
// fprintf(stderr,"taxid: %d\n",taxid);
double *dbl = getval(retmap, child, taxid, howmany);
double dbldbl[3 * howmany + 1]; // 3 because ct,ga,other
dbldbl[0] = dbl[0];
for (int i = 0; i < 3 * howmany; i++)
dbldbl[i + 1] = dbl[1 + i] / dbl[0];
orig[strlen(orig) - 1] = '\0';
fprintf(stdout, "%s\t%d:%.0f", orig, taxid, dbldbl[0]);
for (int i = 0; i < 3 * howmany; i++)
fprintf(stdout, "\t%f", dbldbl[1 + i]);
fprintf(stdout, "\n");
}
float postsize = retmap.size();
fprintf(stderr, "\t-> pre: %f post:%f grownbyfactor: %f\n", presize, postsize, postsize / presize);
if (bgfp)
bgzf_close(bgfp);
if (hdr)
bam_hdr_destroy(hdr);
if (samfp)
sam_close(samfp);
if (fp != Z_NULL)
gzclose(fp);
return 0;
}
int2int getlcadist(char *fname) {
// fprintf(stderr,"fname: %s\n",fname);
int2int lcadist;
int tlen = strlen(fname) + 10;
char tmp[tlen];
snprintf(tmp, tlen, "%s.stat", fname);
fprintf(stderr, "tmp: %s\n", tmp);
FILE *fp = NULL;
fp = fopen(tmp, "rb");
assert(fp != NULL);
char buf[4096];
while (fgets(buf, 4096, fp)) {
int key = atoi(strtok(buf, "\t\n "));
int val = atoi(strtok(NULL, "\t\n "));
lcadist[key] = val;
}
fprintf(stderr, "\t-> Done reading: %lu entries from file: \'%s\'\n", lcadist.size(), tmp);
return lcadist;
}
std::map<int, double *> getcsv(char *fname) {
std::map<int, double *> ret;
FILE *fp = NULL;
fp = fopen(fname, "rb");
assert(fp != NULL);
char buf[4096];
assert(fgets(buf, 4096, fp) != NULL); // skip header
while (fgets(buf, 4096, fp)) {
int key = atoi(strtok(buf, "\t\n, "));
double *valval = new double[2];
valval[0] = atof(strtok(NULL, "\t\n, "));
valval[1] = atof(strtok(NULL, "\t\n, "));
ret[key] = valval;
}
fprintf(stderr, "\t-> Done reading: %lu entries from file: \'%s\'\n", ret.size(), fname);
return ret;
}
int main_merge2(int argc, char **argv) {
fprintf(stderr, "./metaDMG-cpp merge2 file.lca file.bdamage.gz christian.csv [-names file.gz -bam file.bam -howmany 5 -nodes trestructure.gz]\n");
if (argc <= 3)
return 0;