-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathngsLCA.cpp
More file actions
1078 lines (1002 loc) · 41.3 KB
/
Copy pathngsLCA.cpp
File metadata and controls
1078 lines (1002 loc) · 41.3 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
int mod_in[] = {1649555, 1401172, 1582271, 374764, 242716, 1793725, 292451, 38298, 63403, 67357, 163247, 328623, 356150, 502130, 545877, 996989, 996990, 1086724, 1169024, 1576640, 1769757, 1802981, 1811974, 1955118};
int mod_out[] = {1333996, 1333996, 1582270, 1914213, 1917265, 1915309, 263865, 2801, 1916091, 285450, 1762941, 1916091, 157727, 1932322, 376133, 1762939, 1762946, 430531, 1169025, 1247960, 1769758, 1708715, 1708715, 1925741};
#include <htslib/hts.h> // for htsFormat, seq_nt16_str
#include <htslib/sam.h> // for bam1_t, bam1_core_t, sam_write1
#include <signal.h> // for sigaction, sigemptyset
#include <stdint.h> // for uint8_t
#include <strings.h> // for strcasecmp
#include <sys/signal.h> // for sigaction, SIGINT, SIGPIPE, sa_...
#include <time.h> // for time, time_t
#include <zlib.h> // for gzprintf, gzFile
#include <algorithm>
#include <cmath> // for pow
#include <cstdio> // for fprintf, stderr, NULL, FILE
#include <cstdlib> // for free, exit, calloc
#include <cstring> // for strdup, strlen, strcmp
#include <map> // for __map_iterator, operator!=, ope...
#include <vector> // for vector, __vector_base<>::value_...
#include "ngsLCA_cli.h" // for pars, get_pars, pars_free, prin...
#include "profile.h" // for damage, destroy_damage, bam_is_...
#include "shared.h" // for queue, bamRefId2tax, destroy_queue
#include "types.h" // for int2int, int2char, char2int
#include "version.h" // for METADAMAGE_VERSION
extern int SIG_COND; // if we catch signal then quit program nicely
int VERBOSE = 1;
int really_kill = 3;
int2int errmap;
void handler(int s) {
if (VERBOSE)
fprintf(stderr, "\n\t-> Caught SIGNAL (%d): Will try to exit nicely (no more threads are created.\n\t\t\t We will wait for the current threads to finish)\n", s);
if (--really_kill != 3)
fprintf(stderr, "\n\t-> If you really want ./ngsLCA to exit uncleanly ctrl+c: %d more times\n", really_kill + 1);
fflush(stderr);
if (!really_kill)
exit(0);
VERBOSE = 0;
SIG_COND = 0;
}
// bit of a hang and counter intuitive
/*
We associate a intrank to each char*rank, the value will be the intrank for the key which is the lcarank that we use of the classification.
*/
char2int setlevels(int norank2species, char *key, int &value) {
const char *names[48] = {"superkingdom", "domain", "lineage", "kingdom", "subkingdom", "superphylum", "phylum", "subphylum", "superclass", "class", "subclass", "infraclass", "clade", "cohort", "subcohort", "superorder", "order", "suborder", "infraorder", "parvorder", "superfamily", "family", "subfamily", "tribe", "subtribe", "infratribe", "genus", "subgenus", "section", "series", "subseries", "subsection", "species", "species group", "species subgroup", "subspecies", "varietas", "morph", "subvariety", "forma", "forma specialis", "biotype", "genotype", "isolate", "pathogroup", "serogroup", "serotype", "strain"};
int values[48] = {36, 37, 35, 34, 33, 32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
char2int c2i;
value = -1;
char *spec = strdup("species");
for (int i = 0; i < 48; i++) {
c2i[strdup(names[i])] = values[i];
if (strcmp(key, names[i]) == 0) {
value = values[i];
}
}
if (value == -1) {
fprintf(stderr, "\t-> Unknown level defined for -lca_rank: %s\n", key);
exit(0);
}
if (norank2species) {
char2int::iterator it = c2i.find(spec);
if (it == c2i.end()) {
fprintf(stderr, "\t-> Error: iterator reached end in c2i, will exit\n");
exit(1);
}
c2i[strdup("no rank")] = it->second;
}
fprintf(stderr, "\t-> Number of entries with level information: %lu \n", c2i.size());
free(spec);
return c2i;
}
int2int rank2level(int2char &i2c, int norank2species, char *key, int &value) {
char2int c2i = setlevels(norank2species, key, value);
int2int ref2tax;
for (int2char::iterator it = i2c.begin(); it != i2c.end(); it++) {
int key = -1;
char2int::iterator it2 = c2i.find(it->second);
if (it2 != c2i.end())
key = it2->second;
else {
if (strcmp(it->second, "no rank") != 0)
fprintf(stderr, "\t-> Problem finding level for rank: %s\n", it->second);
}
ref2tax[it->first] = key;
}
for (char2int::iterator it = c2i.begin(); it != c2i.end(); it++)
free(it->first);
return ref2tax;
}
// we are threading so we want make a nice signal handler for ctrl+c
void catchkill() {
struct sigaction sa;
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
sa.sa_handler = handler;
sigaction(SIGPIPE, &sa, 0);
sigaction(SIGINT, &sa, 0);
}
int2int specWeight; // Number of reads that map uniquely to a species.
int2int i2i_missing; // contains counter of missing hits for each taxid that doesnt exists in acc2taxid
char2int c2i_missing; // contains counter of missing hits for each taxid that doesnt exists in acc2taxid
void mod_db(int *in, int *out, int2int &parent, int2char &rank, int2char &name_map, int2int *ref2tax) {
int2int::iterator iti;
int2char::iterator itc;
for (int i = 0; i < 24; i++) {
if (parent.count(out[i]) != 1) {
fprintf(stderr, "\t-> Problem \"fixing\" database entries with known issues, consider add --fix_ncbi 0 when running program\n");
exit(0);
}
iti = parent.find(in[i]);
if (iti != parent.end()) {
int oldval_int = iti->second;
parent.erase(iti);
parent[out[i]] = oldval_int;
}
itc = rank.find(in[i]);
if (itc != rank.end()) {
char *oldval_char = itc->second;
rank.erase(itc);
rank[out[i]] = oldval_char;
}
name_map[in[i]] = strdup("satan");//possible leak
for (int2int::iterator it = ref2tax->begin(); it != ref2tax->end(); it++)
if (it->second == in[i])
it->second = out[i];
}
}
int nodes2root(int taxa, int2int &parent) {
int dist = 0;
while (taxa != 1) {
int2int::iterator it = parent.find(taxa);
taxa = it->second;
dist++;
}
return dist;
}
int2int dist2root;
float mean(std::vector<float> &vec) {
if(vec.empty()){
fprintf(stderr,"mean called with empty vector\n");
exit(1);
}
float tmp = vec[0];
for (size_t i = 1; i < vec.size(); i++)
tmp += vec[i];
return tmp / vec.size();
}
int varinfo = 1;
float var(std::vector<float> &vec) {
if (vec.size() <= 1) {
if (varinfo == 1) {
fprintf(stderr, "\t-> Calculation of variance is only defined for >1 datapoints (this message is only printed once)\n");
varinfo = 0;
}
return 0.0;
}
float mea = mean(vec);
float tmp = 0;
for (size_t i = 0; i < vec.size(); i++)
tmp += pow(vec[i] - mea, 2);
return tmp / (vec.size() - 1);
}
// int2int lcadist;
typedef struct {
int nalignments;
std::vector<float> readlengths;
std::vector<float> gccontents;
std::vector<float> dustscores;
std::vector<float> nspecies;
} lcatriplet;
std::map<int, lcatriplet> lcastat;
void adder(int taxid, int readlengths, float gccontent, float dustscore, float nspec) {
std::map<int, lcatriplet>::iterator it = lcastat.find(taxid);
if (it == lcastat.end()) {
lcatriplet tmp;
tmp.nalignments = 1;
tmp.readlengths.push_back(readlengths);
tmp.gccontents.push_back(gccontent);
tmp.dustscores.push_back(dustscore);
tmp.nspecies.push_back(nspec);
lcastat[taxid] = tmp;
} else {
it->second.nalignments = it->second.nalignments + 1;
it->second.readlengths.push_back(readlengths);
it->second.gccontents.push_back(gccontent);
it->second.dustscores.push_back(dustscore);
it->second.nspecies.push_back(nspec);
}
}
int do_lca(std::vector<int> &taxids, int2int &parent) {
// fprintf(stderr,"\t-> [%s] with number of taxids: %lu\n",__func__,taxids.size());
if (taxids.size() == 0) {
fprintf(stderr, "\t-> Error: taxids vector is empty, will exit\n");
exit(1);
}
if (taxids.size() == 1) {
int taxa = taxids[0];
if (parent.count(taxa) == 0) {
fprintf(stderr, "\t-> Problem finding taxaid: %d will skip\n", taxa);
taxids.clear();
return -1;
}
taxids.clear();
return taxa;
}
int2int counter;
for (size_t i = 0; i < taxids.size(); i++) {
int taxa = taxids[i];
while (1) {
// fprintf(stderr,"taxa:%d\n",taxa);
int2int::iterator it = counter.find(taxa);
if (it == counter.end()) {
// fprintf(stderr,"taxa: %d is new will plugin\n",taxa);
counter[taxa] = 1;
} else
it->second = it->second + 1;
it = parent.find(taxa);
if (it == parent.end()) {
int2int::iterator it = errmap.find(taxa);
if (it == errmap.end()) {
fprintf(stderr, "\t-> Problem finding parent of :%d\n", taxa);
errmap[taxa] = 1;
} else
it->second = it->second + 1;
taxids.clear();
return -1;
}
if (taxa == it->second) //<- catch root
break;
taxa = it->second;
}
}
// fprintf(stderr,"counter.size():%lu\n",counter.size());
// now counts contain how many time a node is traversed to the root
int2int dist2root;
for (int2int::iterator it = counter.begin(); it != counter.end(); it++)
if (it->second ==(int) taxids.size())
dist2root[nodes2root(it->first, parent)] = it->first;
for (int2int::iterator it = dist2root.begin(); 0 && it != dist2root.end(); it++)
fprintf(stderr, "%d\t->%d\n", it->first, it->second);
taxids.clear();
if (!dist2root.empty())
return (--dist2root.end())->second;
else {
fprintf(stderr, "\t-> Happens\n");
return -1;
}
}
void print_chain1(kstring_t *kstr, int taxa, int2char &rank, int2char &name_map,char sep) {
int2char::iterator it1 = name_map.find(taxa);
int2char::iterator it2 = rank.find(taxa);
if (it1 == name_map.end() || it2 == rank.end()) {
fprintf(stderr, "taxa: %d name:%s rank:%s doesnt exists will exit\n",
taxa,
it1 != name_map.end() ? it1->second : "<missing-name>",
it2 != rank.end() ? it2->second : "<missing-rank>");
exit(1);
}
ksprintf(kstr, "%c%d:\"%s\":\"%s\"",sep, taxa, it1->second, it2->second);
}
void print_rank(FILE *fp, int taxa, int2char &rank) {
int2char::iterator it2 = rank.find(taxa);
if (it2 == rank.end()) {
fprintf(stderr, "\t-> Error: iterator reached end in rank, will exit\n");
exit(1);
}
fprintf(fp, "taxa: %d rank %s\n", taxa, it2->second);
}
void print_chain(kstring_t *kstr, int taxa, int2int &parent, int2char &rank, int2char &name_map,int donewline) {
int first = 0;
char sep = '\t';
print_chain1(kstr, taxa, rank, name_map,sep);
while (1) {
print_chain1(kstr, taxa, rank, name_map,sep);
int2int::iterator it = parent.find(taxa);
if (it == parent.end()) {
fprintf(stderr, "\t-> Error: iterator reached end in parent, will exit\n");
exit(1);
}
if (taxa == it->second) //<- catch root
break;
taxa = it->second;
if(sep=='\t'){
if(++first > 0)
sep = ';';
}
}
if(donewline)
ksprintf(kstr,"\n");
}
int isuniq(std::vector<int> &vec) {
int ret = 1;
for (size_t i = 1; i < vec.size(); i++)
if (vec[0] != vec[i])
return 0;
return ret;
}
int get_species1(int taxa, int2int &parent, int2char &rank) {
// fprintf(stderr,"\t-> taxa:%d\n",taxa);
int2char::iterator it;
while (1) {
it = rank.find(taxa);
if (it == rank.end())
return -1;
char *val = it->second;
if (val == NULL) {
fprintf(stderr, "\t-> Error: rank value is NULL for taxa:%d, will exit\n", taxa);
exit(1);
}
if (!strcmp(val, "species"))
return taxa;
int2int::iterator pit = parent.find(taxa);
if (pit == parent.end())
return -1;
int next = pit->second;
if (next == taxa)
break;
taxa = next;
}
return taxa;
}
#if 0
int2int get_species(int2int &ref2tax, int2int &parent, int2char &rank, int2char &names, FILE *fp) {
int2int ret;
for (int2int::iterator it = ref2tax.begin(); it != ref2tax.end(); it++) {
// fprintf(stderr,"%d\t%d\n",it->first,it->second);
int asdf = get_species1(it->second, parent, rank);
if (asdf == -1) {
fprintf(fp, "\t-> Removing pair(%d,%d) accnumber:%s since doesnt exists in node list\n", it->first, it->second, names[it->second]);
ref2tax.erase(it);
} else
ret[it->second] = asdf; //
}
return ret;
}
#endif
//
char *make_seq(bam1_t *aln) {
int len = aln->core.l_qseq;
char *qseq = new char[len + 1];
uint8_t *q = bam_get_seq(aln);
for (int i = 0; i < len; i++)
qseq[i] = seq_nt16_str[bam_seqi(q, i)];
qseq[len] = '\0';
// fprintf(stderr,"seq:%s\n",qseq);
// exit(0);
return qseq;
}
// a,c,g,t,n
// A,C,G,T,N
// 0,1,2,3,4
int refToInt[256] = {
0, 1, 2, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, // 15
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, // 31
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, // 47
0, 1, 2, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, // 63
4, 0, 4, 1, 4, 4, 4, 2, 4, 4, 4, 4, 4, 4, 4, 4, // 79
4, 4, 4, 4, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, // 95
4, 0, 4, 1, 4, 4, 4, 2, 4, 4, 4, 4, 4, 4, 4, 4, // 111
4, 4, 4, 4, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, // 127
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, // 143
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, // 159
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, // 175
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, // 191
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, // 207
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, // 223
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, // 239
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4 // 255
};
float gccontent(char *seq) {
int counts[5] = {0, 0, 0, 0, 0};
for (size_t i = 0; i < strlen(seq); i++)
counts[refToInt[(unsigned char)seq[i]]]++;
float gcs = counts[1] + counts[2];
float tot = counts[0] + counts[1] + counts[2] + counts[3];
if (tot == 0)
return 0.0f;
return gcs / tot;
}
float gccontent(bam1_t *aln) {
int counts[5] = {0, 0, 0, 0, 0};
int len = aln->core.l_qseq;
uint8_t *q = bam_get_seq(aln);
for (int i = 0; i < len; i++)
counts[refToInt[(unsigned char)seq_nt16_str[bam_seqi(q, i)]]]++;
float gcs = counts[1] + counts[2];
float tot = counts[0] + counts[1] + counts[2] + counts[3];
if (tot == 0)
return 0.0f;
return gcs / tot;
}
int printonce = 1;
//this function is kept but is not used
void purge(std::vector<int> &taxids, std::vector<int> &specs, std::vector<int> &editdist, std::vector<char> &keep) {
keep.assign(taxids.size(), 1);
if (editdist.empty())
return;
if (printonce == 1)
fprintf(stderr, "\t-> purging taxids oldsize:%lu\n", taxids.size());
if (taxids.size() != specs.size()) {
fprintf(stderr, "\t-> Error: taxids and specs sizes do not match, will exit\n");
exit(1);
}
if (taxids.size() != editdist.size()) {
fprintf(stderr, "\t-> Error: taxids and editdist sizes do not match, will exit\n");
exit(1);
}
std::vector<int> taxids_kept;
std::vector<int> specs_kept;
taxids_kept.reserve(taxids.size());
specs_kept.reserve(specs.size());
const int mylow = *std::min_element(editdist.begin(), editdist.end());
for (size_t i = 0; i < taxids.size(); i++) {
if (editdist[i] <= mylow) {
taxids_kept.push_back(taxids[i]);
specs_kept.push_back(specs[i]);
keep[i] = 1;
} else {
keep[i] = 0;
}
}
if (printonce-- == 1)
fprintf(stderr, "\t-> purging taxids newsize:%lu this info is only printed once\n", taxids_kept.size());
taxids.swap(taxids_kept);
specs.swap(specs_kept);
if(specs.empty()){
fprintf(stderr,"\t-> After purging there is nothing left, will exit\n");
exit(1);
}
}
static size_t count_unique_species(const std::vector<int> &specs, const std::vector<char> &keep, bool apply_purge) {
std::vector<int> species_kept;
species_kept.reserve(specs.size());
for (size_t i = 0; i < specs.size(); i++) {
if (apply_purge && !keep[i])
continue;
species_kept.push_back(specs[i]);
}
if (species_kept.empty())
return 0;
std::sort(species_kept.begin(), species_kept.end());
species_kept.erase(std::unique(species_kept.begin(), species_kept.end()), species_kept.end());
return species_kept.size();
}
static void purge_best_as(queue *myq, std::vector<int> &taxids, std::vector<int> &specs, const char *readname) {
if (taxids.size() != myq->l || specs.size() != myq->l) {
fprintf(stderr, "\t-> Error: taxids/specs/myq sizes do not match in AS purging, will exit\n");
exit(1);
}
bool have_best = false;
int best_as = 0;
for (size_t i = 0; i < myq->l; i++) {
uint8_t *as_tag = bam_aux_get(myq->ary[i], "AS");
if (as_tag == NULL) {
fprintf(stderr, "\t-> Error: --best_as 1 but missing AS tag for read: %s, will exit\n", readname ? readname : "<unknown>");
exit(1);
}
const int as_val = (int)bam_aux2i(as_tag);
if (!have_best || as_val > best_as) {
best_as = as_val;
have_best = true;
}
}
size_t write_idx = 0;
for (size_t i = 0; i < myq->l; i++) {
uint8_t *as_tag = bam_aux_get(myq->ary[i], "AS");
const int as_val = (int)bam_aux2i(as_tag);
if (as_val != best_as)
continue;
if (write_idx != i) {
std::swap(myq->ary[write_idx], myq->ary[i]);
taxids[write_idx] = taxids[i];
specs[write_idx] = specs[i];
}
write_idx++;
}
myq->l = write_idx;
taxids.resize(write_idx);
specs.resize(write_idx);
}
void hts(gzFile fp, samFile *fp_in, int2int &ref2tax, int2int &parent, bam_hdr_t *hdr, int2char &rank, int2char &name_map, int minmapq, int discard, int editMin, int editMax, double scoreLow, double scoreHigh, int maxdust, int minlength, int lca_rank, char *prefix, int howmany, samFile *fp_usedreads, int skipnorank, int2int &rank2level, int nthreads, int weighttype,long maxreads,samFile *fp_famout,int rlens_flat_out,int best_as) {
fprintf(stderr, "[%s] \t-> editMin:%d editmMax:%d scoreLow:%f scoreHigh:%f maxdust:%d minlength:%d discard: %d prefix: %s howmany: %d skipnorank: %d weighttype: %d maxreads: %ld rlens_flat_out: %d best_as: %d\n", __FUNCTION__, editMin, editMax, scoreLow, scoreHigh, maxdust, minlength, discard, prefix, howmany, skipnorank, weighttype,maxreads,rlens_flat_out,best_as);
if (fp_in == NULL) {
fprintf(stderr, "\t-> Error: fp_in is NULL (failed to open input file), will exit\n");
exit(1);
}
damage *dmg = new damage(howmany, nthreads, 13);
bam1_t *aln = bam_init1(); // initialize an alignment
char *last = NULL;
char *seq = NULL;
std::vector<int> taxids;
std::vector<int> specs;
std::vector<int> editdist;
std::vector<char> keep;
queue *myq = init_queue(500);
int lca;
int2int closest_species;
kstring_t *kstr = new kstring_t;
kstr->s = NULL;kstr->l = kstr->m =0;
long nreads = 0;
while (sam_read1(fp_in, hdr, aln) >= 0) {
if(aln->core.l_qseq==0){
fprintf(stderr,"\t-> sequence in read has zero length, assumes corrupt sam/bam/cram will exit\n");
exit(1);
}
if(maxreads!=-1&&nreads>=maxreads)
break;
if (bam_is_unmapped(aln)) {
// fprintf(stderr,"skipping: %s unmapped \n",bam_get_qname(b));
continue;
}
if (bam_is_failed(aln)) {
// fprintf(stderr,"skipping: %s failed: flags=%d \n",bam_get_qname(b),b->core.flag);
continue;
}
char *qname = bam_get_qname(aln);
int chr = aln->core.tid; // contig name (chromosome)
// fprintf(stderr,"%d %d\n",aln->core.qual,minmapq);
static int ntimes = 3;
if (aln->core.qual < minmapq) {
if (ntimes > 0) {
ntimes--;
fprintf(stderr, "Discarding due to low mapq, this message will only be printed three times\n");
}
continue;
}
if (discard > 0 && (aln->core.flag & discard)) {
fprintf(stderr, "Discard: %d coreflag:%d OR %d\n", discard, aln->core.flag, aln->core.flag & discard);
fprintf(stderr, "Discarding due to core flag\n");
continue;
}
if (last == NULL) {
last = strdup(qname);
seq = make_seq(aln);
}
if (minlength != -1 && (aln->core.l_qseq < minlength))
continue;
if (maxdust != -1) {
const int32_t dusts = (int32_t)(0.5 + dust_score_nt16(bam_get_seq(aln), aln->core.l_qseq, 64));
if (dusts > maxdust)
continue;
}
// change of ref
if (strcmp(last, qname) != 0) {
if (taxids.size() > 0) {
if (best_as)
purge_best_as(myq, taxids, specs, last);
// fprintf(stderr,"length of taxids:%lu and other:%lu minedit:%d\n",taxids.size(),editdist.size(),*std::min_element(editdist.begin(),editdist.end()));
size_t size = taxids.size();
if (size != myq->l) {
fprintf(stderr, "\t-> Error: size does not match myq->l, will exit\n");
exit(1);
}
// Legacy read-group purging is disabled: edit distance filtering is per-alignment only.
const bool apply_purge = false;
if (apply_purge)
purge(taxids, specs, editdist, keep);
nreads++;
lca = do_lca(taxids, parent);
// fprintf(stderr,"myq->l: %d\n",myq->l);
if (lca != -1) {
const size_t nused = apply_purge ? taxids.size() : size;
const size_t nspecies = count_unique_species(specs, keep, apply_purge);
int32_t dustscore = 0;
if (myq->l > 0)
dustscore = (int32_t)(0.5 + dust_score_nt16(bam_get_seq(myq->ary[0]), myq->ary[0]->core.l_qseq, 64));
gzprintf(fp, "%s\t%s\t%lu\t%lu\t%lu\t%d\t%f", last, seq, strlen(seq), nused, nspecies, dustscore, gccontent(seq));
print_chain(kstr, lca, parent, rank, name_map,1);
gzwrite(fp,kstr->s,kstr->l);
kstr->l =0;
if ( isuniq(specs)) {
int2int::iterator it = specWeight.find(specs[0]);
if (it == specWeight.end())
specWeight[specs[0]] = 1;
else
it->second = it->second + 1;
}
// lca_rank is integer and is different from minus one
int2int::iterator myit = rank2level.find(lca);
if (myit == rank2level.end()) {
fprintf(stderr, "\t-> Error: iterator reached end in rank2level, will exit\n");
exit(1);
}
//write to family out
if (myit->second != -1 && (myit->second <= 16)) {
//family is 16, see rank2level.txt
for (size_t i = 0; i < myq->l; i++) {
if (apply_purge && !keep[i])
continue;
if(fp_famout){
if (sam_write1(fp_famout, hdr, myq->ary[i]) < 0) {
fprintf(stderr, "\t-> Error: failed to write record with sam_write1, will exit\n");
exit(1);
}
}
}
}
//standard analyses
if (myit->second != -1 && (myit->second <= lca_rank)) {
adder(lca, strlen(seq), gccontent(seq), (float)dustscore, (float)nspecies);
const float dmgw = (weighttype == 0) ? 1.0f : (1.0f / (float)(apply_purge ? nused : myq->l));
//fprintf(stderr,"Looping through alignments we have :%d \n",myq->l);
for (size_t i = 0; i < myq->l; i++) {
if (apply_purge && !keep[i])
continue;
int2int::iterator it2k = ref2tax.find(myq->ary[i]->core.tid);
if (it2k == ref2tax.end()) {
fprintf(stderr, "\t-> Error: iterator reached end in ref2tax, will exit\n");
exit(1);
}
int2char::iterator ititit = rank.find(it2k->second);
if (ititit == rank.end()) {
fprintf(stderr, "\t-> Potential problem no rank for taxid: %d\n", it2k->second);
continue;
}
if (skipnorank == 1 && strcasecmp(ititit->second, "no rank") == 0)
continue;
dmg->damage_analysis(myq->ary[i], it2k->second, dmgw);
if (fp_usedreads){
if (sam_write1(fp_usedreads, hdr, myq->ary[i]) < 0) {
fprintf(stderr, "\t-> Error: failed to write record with sam_write1 (fp_usedreads), will exit\n");
exit(1);
}
}
}
}
}
}
specs.clear();
editdist.clear();
keep.clear();
myq->l = 0;
free(last);
delete[] seq;
last = strdup(qname);
seq = make_seq(aln);
}//of readgroup analyses
// filter by nm
uint8_t *nm = bam_aux_get(aln, "NM");
int thiseditdist = -1;;
if (nm != NULL) {
thiseditdist = (int)bam_aux2i(nm);
// fprintf(stderr,"[%d] nm:%d\t",inc++,val);
if (editMin != -1 && thiseditdist < editMin) {
// Drop only this alignment; keep the rest of the read group.
continue;
} else if (editMax != -1 && thiseditdist > editMax) {
// fprintf(stderr,"continued1\n");
continue;
}
double seqlen = aln->core.l_qseq;
double myscore = 1.0 - (((double)thiseditdist) / seqlen);
// fprintf(stderr," score:%f\t",myscore);
if (myscore > scoreHigh) {
// Drop only this alignment; keep the rest of the read group.
continue;
} else if (myscore < scoreLow) {
// fprintf(stderr,"continued2\n");
continue;
}
}
// See if cloests speciest exists and plug into closests species
int dingdong = -1;//dingdong is obviously the taxid of the closests species
int2int::iterator it = ref2tax.find(chr);
if (it != ref2tax.end()) {
int2int::iterator it2 = closest_species.find(it->second);
if (it2 != closest_species.end())
dingdong = it2->second;
else {
dingdong = get_species1(it->second, parent, rank);
if (dingdong != -1)
closest_species[it->second] = dingdong;
}
// fprintf(stderr,"\t-> closests size:%lu\n",closest_species.size());
}
if (it == ref2tax.end() || dingdong == -1) {
int2int::iterator it2missing = i2i_missing.find(chr);
if (it2missing == i2i_missing.end()) {
fprintf(stderr, "[log] \t-> problem finding id:%d chrname:%s (due to problem with missing entries in nodes.dmp or supporting files)\n", chr, hdr->target_name[chr]);
i2i_missing[chr] = 1;
} else
it2missing->second = it2missing->second + 1;
} else {
if (bam_copy1(myq->ary[myq->l], aln) == NULL) {
fprintf(stderr, "\t-> Error: bam_copy1 returned NULL, will exit\n");
exit(1);
}
myq->l++;
if (myq->l == myq->m)
expand_queue(myq);
taxids.push_back(it->second);
specs.push_back(dingdong);
if(thiseditdist!=-1)
editdist.push_back(thiseditdist);
}
}
if (taxids.size() > 0) {
if (best_as)
purge_best_as(myq, taxids, specs, last);
size_t size = taxids.size();
if (size != myq->l) {
fprintf(stderr, "\t-> Error: size does not match myq->l, will exit\n");
exit(1);
}
// Legacy read-group purging is disabled: edit distance filtering is per-alignment only.
const bool apply_purge = false;
if (apply_purge)
purge(taxids, specs, editdist, keep);
// fprintf(stderr,"ntaxids: %lu\n",taxids.size());
nreads++;
lca = do_lca(taxids, parent);
// fprintf(stderr,"myq->l: %d lca: %d \n",myq->l,lca);
if (lca != -1) {
const size_t nused = apply_purge ? taxids.size() : size;
const size_t nspecies = count_unique_species(specs, keep, apply_purge);
int32_t dustscore = 0;
if (myq->l > 0)
dustscore = (int32_t)(0.5 + dust_score_nt16(bam_get_seq(myq->ary[0]), myq->ary[0]->core.l_qseq, 64));
gzprintf(fp, "%s\t%s\t%lu\t%lu\t%lu\t%d\t%f", last, seq, strlen(seq), nused, nspecies, dustscore, gccontent(seq));
print_chain(kstr, lca, parent, rank, name_map,1);
gzwrite(fp,kstr->s,kstr->l);
kstr->l = 0;
if (isuniq(specs)) {
int2int::iterator it = specWeight.find(specs[0]);
if (it == specWeight.end())
specWeight[specs[0]] = 1;
else
it->second = it->second + 1;
}
// lca_rank is integer and is different from minus one
int2int::iterator myit = rank2level.find(lca);
if (myit == rank2level.end()) {
fprintf(stderr, "\t-> Error: iterator reached end in rank2level, will exit\n");
exit(1);
}
//write to family out
if (myit->second != -1 && (myit->second <= 16)) {
//family is 16, see rank2level.txt
for (size_t i = 0; i < myq->l; i++) {
if (apply_purge && !keep[i])
continue;
if(fp_famout){
if (sam_write1(fp_famout, hdr, myq->ary[i]) < 0) {
fprintf(stderr, "\t-> Error: failed to write record with sam_write1 (fp_famout), will exit\n");
exit(1);
}
}
}
}
if (myit->second != -1 && (myit->second <= lca_rank)) {
adder(lca, strlen(seq), gccontent(seq), (float)dustscore, (float)nspecies);
const float dmgw = (weighttype == 0) ? 1.0f : (1.0f / (float)(apply_purge ? nused : myq->l));
// if(correct_rank(lca_rank,lca,rank,norank2species)){
//fprintf(stderr,"Looping through alignments we have :%d \n",myq->l);
for (size_t i = 0; i < myq->l; i++) {
if (apply_purge && !keep[i])
continue;
// dmg->damage_analysis(myq->ary[i],myq->ary[i]->core.tid);
int2int::iterator ittt = ref2tax.find(myq->ary[i]->core.tid);
if (ittt == ref2tax.end()) {
fprintf(stderr, "\t-> Error: iterator reached end in ref2tax (ittt), will exit\n");
exit(1);
}
int2char::iterator ititit = rank.find(ittt->second);
if (ititit == rank.end()) {
fprintf(stderr, "\t-> Potential problem no rank for taxid: %d\n", ittt->first);
continue;
}
// fprintf(stderr,"uaua: %s taxid: %d\n",ititit->second,ititit->first);
if (skipnorank == 1 && strcasecmp(ititit->second, "no rank") == 0)
continue;
// fprintf(stderr,"uaua\n");
dmg->damage_analysis(myq->ary[i], ittt->second, dmgw);
if (fp_usedreads){
if (sam_write1(fp_usedreads, hdr, myq->ary[i]) < 0) {
fprintf(stderr, "\t-> Error: failed to write record with sam_write1 (fp_usedreads), will exit\n");
exit(1);
}
}
}
}
}
}
fprintf(stderr,"maxreads: %ld nreads: %ld\n",maxreads,nreads);
dmg->bwrite(prefix,rlens_flat_out);
specs.clear();
editdist.clear();
keep.clear();
bam_destroy1(aln);
sam_close(fp_in);
if (seq)
delete[] seq;
if (last)
free(last);
destroy_damage(dmg);
destroy_queue(myq);
free(kstr->s);delete kstr;
return; // 0;
}
void print_ref_rank_species(bam_hdr_t *h, int2int &ref2tax, int2char &names, int2char &rank) {
for (int i = 0; i < h->n_targets; i++) {
fprintf(stdout, "%d\t%s\t%s\n", i, names[ref2tax[i]], rank[ref2tax[i]]);
}
}
int calc_valens(int2int &ref2tax, int2int &parent) {
int2int ret;
for (int2int::iterator it = ref2tax.begin(); it != ref2tax.end(); it++) {
int2int::iterator pit = parent.find(it->second);
if (pit == parent.end() || pit->second == 1)
continue;
// fprintf(stdout,"%d %d\n",pit->first,pit->second);
int2int::iterator rit = ret.find(pit->second);
if (rit == ret.end())
ret[pit->second] = 1;
else
rit->second = rit->second + 1;
}
for (int2int::iterator it = ret.begin(); it != ret.end(); it++)
fprintf(stdout, "%d\t%d\n", it->first, it->second);
return 0;
}
int calc_dist2root(int2int &ref2tax, int2int &parent) {
int2int ret;
for (int2int::iterator it = ref2tax.begin(); it != ref2tax.end(); it++) {
int2int::iterator pit = parent.find(it->second);
if (pit == parent.end() || pit->second == 1)
continue;
// fprintf(stdout,"%d %d\n",pit->first,pit->second);
int2int::iterator rit = ret.find(pit->second);
if (rit == ret.end())
ret[pit->second] = 1;
else
rit->second = rit->second + 1;
}
for (int2int::iterator it = ret.begin(); it != ret.end(); it++)
fprintf(stdout, "%d\t%d\n", it->first, it->second);
return 0;
}
#if 0
int2node makeNodes(int2int &parent){
int2node ret;
for(int2int::iterator it=parent.begin();it!=parent.end();it++){
int2node::iterator it2=ret.find(it->second);
if(it2==ret.end()){
node nd;
nd.up=it->second;
ret[it->first] = nd;
}
}
return ret;
}
#endif
int main_lca(int argc, char **argv) {
htsFormat *dingding2 = (htsFormat *)calloc(1, sizeof(htsFormat));
if (argc == 1 || (argc == 2 && (!strcmp(argv[1], "--help") || !strcmp(argv[1], "-h")))) {
fprintf(stderr, "\nUsage: ./metaDMG-cpp lca [options]\n");
fprintf(stderr, "Core options:\n");
fprintf(stderr, " --bam FILE\n");
fprintf(stderr, " --names FILE\n");
fprintf(stderr, " --nodes FILE\n");
fprintf(stderr, " --acc2tax FILE\n");
fprintf(stderr, "Filtering options:\n");
fprintf(stderr, " --edit_dist_min INT (default: -1 disabled)\n");
fprintf(stderr, " --edit_dist_max INT (default: -1 disabled)\n");
fprintf(stderr, " --sim_score_low FLOAT (default: 0)\n");
fprintf(stderr, " --sim_score_high FLOAT (default: 1)\n");
fprintf(stderr, " --minAni/--min_ani FLOAT alias for --sim_score_low\n");
fprintf(stderr, " --maxAni/--max_ani FLOAT alias for --sim_score_high\n");
fprintf(stderr, " --maxdust INT (0-100, default: -1 disabled)\n");
fprintf(stderr, " --best_as <0|1> (default: 0, keep only best AS per read; ties kept)\n");
fprintf(stderr, " --min_mapq INT (default: 0)\n");
fprintf(stderr, " --min_length INT (default: -1 disabled)\n");
fprintf(stderr, "Output/options:\n");
fprintf(stderr, " --out_prefix STR (default: outnames)\n");
fprintf(stderr, " --lca_rank STR (default: species)\n");
fprintf(stderr, " --threads INT (default: 4)\n");
fprintf(stderr, " --used_reads <0|1> (default: 1)\n");
fprintf(stderr, " --famout <0|1> (default: 1)\n");
fprintf(stderr, "Version: %s\n\n", METADAMAGE_VERSION);
return 0;
}
catchkill();
#if 0
int2int ww = get_weight(argv[1]);
return 0;
#endif
time_t t2 = time(NULL);
pars *p = get_pars(--argc, ++argv);
print_pars(stderr, p);
if (p->maxdust < -1 || p->maxdust > 100) {
fprintf(stderr, "\t-> Error: --maxdust must be -1 (disabled) or between 0 and 100\n");
exit(1);
}
if (p->best_as != 0 && p->best_as != 1) {
fprintf(stderr, "\t-> Error: --best_as must be 0 or 1\n");
exit(1);
}
gzprintf(p->fp1,"#./metaDMG-cpp lca ");
for(int i=0;i<argc;i++)
gzprintf(p->fp1,"%s ",argv[i]);
gzprintf(p->fp1,"\n");
// map of bamref ->taxid
int2int *ref2tax = NULL;
// fprintf(stderr,"p->header: %p\n",p->header);
if (p->header)
ref2tax = bamRefId2tax(p->header, p->acc2taxfile, p->htsfile, errmap, p->tempfolder, p->useDump, p->filteredAcc2taxfile,NULL);
if(ref2tax==NULL){
fprintf(stderr,"\t-> It is a requirement that the input bam/sam/cram contains a header\n");
exit(1);
}
// map of taxid -> taxid
int2int parent;
// map of taxid -> rank
int2char rank;
// map of taxid -> name
// map of parent -> child taxids
int2intvec child;
int2char name_map = parse_names(p->namesfile);
parse_nodes(p->nodesfile, rank, parent, child, 0);
int lca_rank;
// converts each taxid to a intrepresentation of the rank, we call this level
int2int tax2level = rank2level(rank, p->norank2species, p->lca_rank, lca_rank);
// calc_valens(ref2tax,parent);
if (0) {
print_ref_rank_species(p->header, *ref2tax, name_map, rank);
return 0;
}
// closes species (direction of root) for a taxid
// int2int closest_species=get_species(ref2tax,parent,rank,name_map,p->fp3);
// fprintf(stderr,"\t-> Number of items in closest_species map:%lu\n",closest_species.size());
if (p->fixdb) {