-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathaScan_dev.cpp
More file actions
3730 lines (2822 loc) · 79.1 KB
/
aScan_dev.cpp
File metadata and controls
3730 lines (2822 loc) · 79.1 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
/*
Written by Federico Zambelli federico.zambelli@unimi.it
Distributed under GNU General Public License v3.0
GitHub Repo: https://github.com/Federico77z/aScan
*/
#include "aScan_dev.h"
int main(int argc, char **argv)
{
if(argc == 1)
display_help();
command_line_parser(argc, argv);
map<string, vector<bool> > var_map;
// rnaseq R(&RNA_SEQ_BAM_FILE);
prefetch_variants(VCF_FILE, var_map);
analysis A(&RNA_SEQ_BAM_FILE, &VCF_FILE, >F_FILE, &GENE_NAMES_TABLE_FILE, var_map);
return EXIT_SUCCESS;
}
void prefetch_variants(string& VCF_FILE, map<string, vector<bool> >& var_map)
{
ifstream in(VCF_FILE.c_str());
if(!in)
{
cerr << "\nCan't find VCF file: " << VCF_FILE << endl;
exit(EXIT_FAILURE);
}
cerr << "\nPrefetching VCF file...";
string line;
while(getline(in,line))
{
if(line.empty())
continue;
if(line[0] == '#')
continue;
istringstream str(line);
string chr;
unsigned int pos;
str >> chr >> pos;
pos--;
if(FIX_VCF)
chr.insert(0, "chr");
map<string, vector<bool> >::iterator mi = var_map.find(chr);
if(mi == var_map.end())
mi = var_map.emplace(make_pair(chr, vector<bool>())).first;
if(mi->second.size() < pos + 1)
mi->second.resize(pos + 1, false);
mi->second.at(pos) = true;
}
in.close();
return;
}
void display_help()
{
cerr << "\nSynopsis: aScan --rna rnaseq_bamfile --vcf vcf_file --gtf gtf_file [-nt transcript/gene_correspondence_file] [-p thread_num] [--filter filter_key_word]" << endl;
cerr << "\n--rna rnaseq_bamfile\nBAM file containing RNA-Seq reads mapped to a reference genome. The BAM file does not need to be sorted. Please notice that aScan does not currently support reads mapped to the reference transcriptome." << endl;
cerr << "\n--vcf vcf_file\nVCF file with genomic variants from the same individual as the RNA-Seq data. Please notice that aScan currently does not support multi-VCF files. aScan considers only single nucleotide\nsubstitutions. aScan can work both with phased or unphased VCF files." << endl;
cerr << "\n--gtf gtf_file GTF file with reference transcripts annotation.\nPlease notice that aScan will consider only \"exon\" named features. The attribute column of the GTF file must include a transcript_id value for each exon." << endl;
cerr << "\n-nt, --nametable transcript/gene_correspondence_file.\nA tab-separated tabular file associating transcript IDs (first column) to gene names or gene IDs (second column)." << endl;
cerr << "\n-p threadnum\nThe number of threads to use (default 1). While aScan supports multithreading, the execution speed bottleneck is usually represented by the drive read bandwidth,\nmeaning that normally the benefits of using more than 2 or 3 threads are minimal." << endl;
cerr << "\n--filter filter_key_word\nUsing this option, all the variants flagged with filter_key_word in the FILTER field of the VCF file will be ignored. Use this option to discard low-quality variant calls." << endl;
cerr << endl;
exit(EXIT_SUCCESS);
}
void command_line_parser(int argc, char **argv)
{
for(int i = 1; i < argc; i++)
{
string buf = argv[i];
if(buf == "--rna" || buf == "-r")
{
if(i < argc - 1)
RNA_SEQ_BAM_FILE = argv[++i];
continue;
}
else if(buf == "--vcf" || buf == "-V")
{
if(i < argc - 1)
VCF_FILE = argv[++i];
continue;
}
else if(buf == "-h" || buf == "--help")
{
display_help();
}
else if(buf == "--gtf" || buf == "-g")
{
if(i < argc - 1)
GTF_FILE = argv[++i];
continue;
}
else if(buf == "--texpr" || buf == "-te")
{
if(i < argc - 1)
EXPR_FILE = argv[++i];
continue;
}
else if(buf == "--gexpr" || buf == "-ge")
{
if(i < argc - 1)
GENE_EXPR_FILE = argv[++i];
continue;
}
else if(buf == "--nametable" || buf == "-nt")
{
if(i < argc - 1)
GENE_NAMES_TABLE_FILE = argv[++i];
continue;
}
else if(buf == "-p")
{
if( i < argc - 1)
THREAD_NUM = atoi(argv[++i]);
continue;
}
else if(buf == "--filter" || buf == "-f")
{
if( i < argc - 1)
VCF_FILTER = argv[++i];
continue;
}
else if(buf == "-rep")
{
if( i < argc - 1)
RNAMAP_REP_FILE = argv[++i];
continue;
}
else if(buf == "-v")
{
VERBOSE = true;
continue;
}
else if(buf == "--phase" || buf == "-phase")
{
PHASE_ANALYSIS = true;
continue;
}
else if(buf == "-hq")
{
USE_ONLY_HI_QUALITY_ALIGNMENTS = true;
continue;
}
else if(buf == "--only_first_mate")
{
USE_ONLY_FIRST_MATE = true;
continue;
}
else if(buf == "--fix_vcf")
{
FIX_VCF = true;
continue;
}
/* else if(buf == "-b")
{
if(i < argc - 1)
bedfile = argv[++i];
continue;
}
else if(buf == "-shift")
{
if(i < argc - 1)
SHIFT = atoi(argv[++i]);
if(SHIFT > WARN_MAX_SHIFT)
cerr << "\nWarning: the set shift seems way too big! shift = " << SHIFT << endl;
continue;
}
else if(buf == "-zoom")
{
if(i < argc - 1)
zoomfile = argv[++i];
unlink(zoomfile.c_str());
continue;
}
*/
else
{
cerr << "\n\nUnknown option: " << buf << endl << endl;
exit(EXIT_FAILURE);
}
}
return;
}
// POSITION CLASS
position::position(char C):MULTIPLE_GENE(false), MULTIPLE_GENE_SET(false), PERF_OMO(false), PERF_OMO_SET(false)
{
// static atomic<unsigned int> NATOM[4];
var_ptr = NULL;
p_type = UNKNOWN_EXPR;
static map<unsigned int, vector<boost::dynamic_bitset<> > > bcomb;
// static map<unsigned int, vector<vector<tcomb> > > vcomb;
BCOMB = &bcomb;
// VCOMB = &vcomb;
// COLL_COMB = &coll_comb;
for(unsigned int i = 0; i < ALPHABET_SIZE; i++)
{
// N[i] = 0;
N_ATOM[i].store(0);
}
// assign_nuc(C);
assign_nuc_atomic(C);
return;
}
const short int position::get_phase() const
{
return var_ptr->get_phase();
}
const unsigned int position::get_ref_count() const
{
return N_ATOM[var_ptr->get_ref_int()];
}
const unsigned int position::get_alt_count() const
{
return N_ATOM[var_ptr->get_alt_int()];
}
const unsigned int position::trv_size() const
{
return VPTR.size();
}
/*
position::~position()
{
return;
}
*/
position::position(const position &obj)
{
for(unsigned int i = 0; i < ALPHABET_SIZE; i++)
{
// N[i] = obj.N[i];
N_ATOM[i].store(obj.N_ATOM[i].load());
}
VPTR = obj.VPTR;
var_ptr = obj.var_ptr;
p_type = obj.p_type;
// VCOMB = obj.VCOMB;
BCOMB = obj.BCOMB;
MULTIPLE_GENE = obj.MULTIPLE_GENE;
MULTIPLE_GENE_SET = obj.MULTIPLE_GENE_SET;
PERF_OMO = obj.PERF_OMO;
PERF_OMO_SET = obj.PERF_OMO_SET;
// PV = obj.PV;
return;
}
/*
position::position(const unsigned int *arr)
{
for(unsigned int i = 0; i < ALPHABET_SIZE; i++)
N[i] = arr[i];
return;
}
void position::add(const unsigned int *arr)
{
for(unsigned int i = 0; i < ALPHABET_SIZE; i++)
N[i] += arr[i];
return;
}
*/
/*const unsigned int * position::n_arr()
{
return N;
}*/
/*
inline void position::assign_nuc(char c)
{
switch (c)
{
case 'A':
N[A]++;
break;
case 'C':
N[C]++;
break;
case 'G':
N[G]++;
break;
case 'T':
N[T]++;
break;
default:
break;
}
return;
}
*/
inline void position::assign_nuc_atomic(char c)
{
switch (c)
{
case 'A':
N_ATOM[A]++;
break;
case 'C':
N_ATOM[C]++;
break;
case 'G':
N_ATOM[G]++;
break;
case 'T':
N_ATOM[T]++;
break;
default:
break;
}
return;
}
/*string position::report_s()
{
ostringstream out;
for(unsigned int i = 0; i < ALPHABET_SIZE; i++)
out << N[i] << '\t';
return out.str();
}*/
const string position::report_s_atomic() const
{
ostringstream out;
for(unsigned int i = 0; i < ALPHABET_SIZE; i++)
out << '\t' << N_ATOM[i].load();
return out.str();
}
const string position::report_b_comb()
{
// build_v_comb();
ostringstream out;
/* unsigned int nxcount = 0;
for(unsigned int i = 0; i < VPTR.size(); i++)
if(VPTR.at(i)->get_expr() <= transcript::NO_EXPRESSION)
nxcount++;
*/
// cerr << endl << VPTR.size() << '\t' << nxcount;
auto bi = BCOMB->find(VPTR.size());
// auto vi = VCOMB->find(VPTR.size());
out << '\t' << bi->second.size() << '\t' << VPTR.size() << '\t' << pow(2, VPTR.size());
if(bi->second.size() != pow(2, VPTR.size()))
out << "ERROR!";
// out << "\t---\t" << vi->second.size() << '\t' << VPTR.size() << '\t' << pow(2, VPTR.size());
for(unsigned int i = 0; i < bi->second.size(); i++)
{
out << '\t';
for(unsigned int j = 0; j < bi->second.at(i).size(); j++)
out << bi->second[i][j] << ',';
}
return out.str();
}
void position::add_var_to_tr(unsigned int p) const
{
auto bi = BCOMB->find(VPTR.size());
for(unsigned int i = 0; i < bi->second.at(p).size(); i++)
VPTR.at(i)->add_var_count(bi->second[p][i]);
return;
}
const string position::report_b_comb_at(unsigned int p) const
{
ostringstream out;
auto bi = BCOMB->find(VPTR.size());
if(bi == BCOMB->end())
return out.str();
for(unsigned int i = 0; i < bi->second.at(p).size(); i++)
out << bi->second[p][i] << ',';
return out.str();
}
void position::add_transcript(const transcript *TR)
{
VPTR.push_back(TR);
}
const string position::report_s_transcripts() const
{
ostringstream out;
for(unsigned int i = 0; i < VPTR.size(); i++)
out << *VPTR.at(i)->get_id() << ',';
return out.str();
}
void position::set_var(const variant *V)
{
var_ptr = V;
return;
}
const variant * position::get_var() const
{
return var_ptr;
}
const bool position::tr_marker() const
{
if(VPTR.size() == 1)
return true;
else
return false;
}
const string* position::zygosity_str() const
{
static const vector<string> ZYG = {"RNA_OMOZYG", "RNA_ETEROZYG", "RNA_???"};
int Z = -1;
vector<double> frac(ALPHABET_SIZE, 0);
double sum = 0;
for(unsigned int i = 0; i < ALPHABET_SIZE; i++)
{
frac[i] = N_ATOM[i].load();
sum += N_ATOM[i].load();
}
for(unsigned int i = 0; i < ALPHABET_SIZE; i++)
{
frac[i] /= sum;
if(frac[i] != 0)
Z++;
}
switch (Z)
{
case 0:
return &ZYG[Z];
break;
case 1:
return &ZYG[Z];
break;
default:
return &ZYG[2];
break;
}
}
const int position::get_zygosity() const
{
int Z = -1;
vector<double> frac(ALPHABET_SIZE, 0);
double sum = 0;
for(unsigned int i = 0; i < ALPHABET_SIZE; i++)
{
frac[i] = N_ATOM[i].load();
sum += N_ATOM[i].load();
}
for(unsigned int i = 0; i < ALPHABET_SIZE; i++)
{
frac[i] /= sum;
if(frac[i] != 0)
Z++;
}
switch (Z)
{
case 0:
return OMO_Z;
break;
case 1:
return ETERO_Z;
break;
default:
return UNKNOWN_Z;
break;
}
}
void position::var_diagnostic(unsigned int vcf_z)
{
unsigned int rna_z = get_zygosity();
if(vcf_z == variant::ETERO_Z && rna_z == position::ETERO_Z)
p_type = ETERO_ZYG_EXPR;
else if(vcf_z == variant::ETERO_Z && rna_z == position::OMO_Z)
p_type = ALLELE_SPECIFIC_EXPR;
else if(vcf_z == variant::ALT_OMO_Z && rna_z == position::ETERO_Z)
p_type = RNA_EDIT;
else if(vcf_z == variant::ALT_OMO_Z && rna_z == position::OMO_Z)
p_type = OMOZYG_EXPR;
else
p_type = DUBIOUS_EXPR;
return;
}
const string* position::var_diagnostic_s(unsigned int vcf_z)
{
static const vector<string> D = {"ETERO_ZYG_EXPR", "ALLELE_SPECIFIC_EXPR", "RNA_EDIT", "OMOZYG_EXPR", "DUBIOUS_EXPR", "UNKNOWN_EXPR"};
if(p_type == UNKNOWN_EXPR)
var_diagnostic(vcf_z);
return &D[p_type];
}
const position::Pos_type position::var_diagnostic_p(unsigned int vcf_z)
{
if(p_type == UNKNOWN_EXPR)
var_diagnostic(vcf_z);
return p_type;
}
void position::get_counts(unsigned int p1, unsigned int p2, double &x, double &n, bool &v1p) const
{
if(N_ATOM[p1] >= N_ATOM[p2])
{
x = N_ATOM[p1].load();
n = x + N_ATOM[p2].load();
v1p = false;
}
else
{
x = N_ATOM[p2].load();
n = x + N_ATOM[p1].load();
v1p = true;
}
if(n == x)
PERF_OMO = true;
PERF_OMO_SET = true;
return;
}
double position::compute_probability_gtest(vector<double> &V_V1, vector<double> &V, vector<double> &chi_raw, unsigned int &bgv, unsigned int p1, unsigned int p2) const
{
double x, n;
bool v1p;
get_counts(p1, p2, x, n, v1p);
auto bi = BCOMB->find(VPTR.size()); //BCOMB of VPTR.size() exists since compute_probability_binom is called before compute_probability_gtest
// if(x == n)
if(perf_omo())
{
double p = 1.0 - gsl_ran_binomial_pdf(n, 0.5, n); //IF ONE POSITION IS 0 THAN SWITCH TO BINOMIAL TEST WITH P = 0.5
bgv = 0;
V.push_back(p); //ONLY THE FIRST BOOL VECTOR (0,0,0...0) HAS PROB. P.
chi_raw.push_back(0); // AND CHI 0
}
{
unsigned int i;
if(perf_omo())
i = 1;
else
i = 0;
for(; i < bi->second.size(); i++)
{
double a1, a2;
a1 = V_V1.at(i) * n;
a2 = (1.0 - V_V1.at(i)) * n;
double tmp;
if(perf_omo())
tmp = (x * log(x / a1));
else
tmp = (x * log(x / a1)) + ((n -x) * log((n-x) / a2 ));
// V.push_back((x * log(x / a1)) + ((n -x) * log((n-x) / a2 )));
tmp *= 2;
chi_raw.push_back(tmp);
V.push_back(gsl_cdf_chisq_Q(tmp, 1));
}
}
double max = V.at(0);
bgv = 0;
for(unsigned int i = 1; i < V.size(); i++)
{
if(V.at(i) > max)
{
max = V.at(i);
bgv = i;
}
}
return V.at(bgv);
// return gsl_cdf_chisq_Q(2 * min, 1);
}
void position::compute_probability_binom(vector<double> &PV, vector<double> &V_V1, double *Att, unsigned int p1, unsigned int p2)
{
static constexpr double PAS[2] = {0.99, 0.5};
vector<double> weights;
if(p1 >= ALPHABET_SIZE || p2 >= ALPHABET_SIZE)
{
cerr << "\n\nFATAL ERROR!!";
exit(EXIT_FAILURE);
}
if(VPTR.empty())
return;
auto bi = BCOMB->find(VPTR.size());
if(bi == BCOMB->end())
{
bi = BCOMB->insert(make_pair(VPTR.size(), vector<boost::dynamic_bitset<> >())).first;
build_b_comb(bi);
}
double x, n;
bool v1p;
get_counts(p1, p2, x, n, v1p);
tr_expr_weights(weights);
for(unsigned int i = 0; i < bi->second.size(); i++)
{
double V1 = 0;
for(unsigned int j = 0; j < bi->second.at(i).size(); j++)
{
double frac = weights.at(j);
V1 += frac * PAS[bi->second.at(i)[j]];
}
PV.push_back(gsl_ran_binomial_pdf(x, V1, n));
V_V1.push_back(V1);
}
double max = 0;
unsigned int vpos = 0;
for(unsigned int i = 0; i < PV.size(); i++)
{
if(max < PV.at(i))
{
max = PV.at(i);
vpos = i;
}
}
Att[v1p] = V_V1.at(vpos) * n;
Att[!v1p] = (1 - V_V1.at(vpos)) * n;
return;
}
void position::tr_expr_weights(vector<double> &W) const
{
double sum = 0;
for(auto vi = VPTR.begin(); vi != VPTR.end(); ++vi)
sum += (*vi)->get_expr();
for(auto vi = VPTR.begin(); vi != VPTR.end(); ++vi)
W.push_back((*vi)->get_expr() / sum);
return;
}
const unsigned int position::get_count_at(const unsigned int p) const
{
if(p >= ALPHABET_SIZE)
{
cerr << "\n\nFATAL ERROR!!";
exit(EXIT_FAILURE);
}
return N_ATOM[p].load();
}
const double position::marker_p(unsigned int p1, unsigned int p2, double *rec_p) const
{
if(p1 >= ALPHABET_SIZE || p2 >= ALPHABET_SIZE)
{
cerr << "\n\nFATAL ERROR!!";
exit(EXIT_FAILURE);
}
long double boost_bin_coeff, prob, x, n, rec;
if(N_ATOM[p1] >= N_ATOM[p2])
{
x = N_ATOM[p1].load();
n = x + N_ATOM[p2].load();
}
else
{
x = N_ATOM[p2].load();
n = x + N_ATOM[p1].load();
}
// bin_coeff = tgamma(n + 1) / (tgamma(x + 1) * tgamma(n - x + 1));
// cerr << endl << "bin_coeff " << n << ' ' << x << " = " << bin_coeff << '\t' ;
typedef boost::math::policies::policy<boost::math::policies::overflow_error<boost::math::policies::ignore_error> > my_policy;
boost_bin_coeff = boost::math::binomial_coefficient<long double> (n, x, my_policy());
// cerr << boost_bin_coeff;
prob = boost_bin_coeff * pow(0.5, x) * pow(0.5, n - x);
rec = boost_bin_coeff * pow(0.99, x) * pow(0.01, n - x);
*rec_p = rec;
// cerr << endl << rec << '\t' << *rec_p << endl;
return prob;
}
/*
void position::build_v_comb() //DA ELIMINARE
{
if(VPTR.empty())
return;
auto bi = BCOMB->find(VPTR.size());
if(bi != BCOMB->end())
return;
// map<unsigned int, vector<vector<tcomb> > >::iterator vi;
bi = BCOMB->insert(make_pair(VPTR.size(), vector<boost::dynamic_bitset<> >())).first;
// vi = VCOMB->insert(make_pair(VPTR.size(), vector<vector<tcomb> >())).first;
build_b_comb(bi);
vi->second.resize(bi->second.size());
for(unsigned int i = 0; i < bi->second.size(); i++)
{
for(unsigned int t = 0; t < VPTR.size(); t++)
{
tcomb COMB;
if(bi->second.at(i)[t])
{
COMB.b[0] = PAS[AS_0];
COMB.b[1] = PAS[AS_1];
}
else
{
for(unsigned int k = 0; k < 2; k++)
COMB.b[k] = PAS[ET];
}
vi->second.at(t).push_back(COMB);
}
}
auto ci = COLL_COMB->insert(make_pair(VPTR.size(), vector<tcomb>())).first;
ci->second.resize(bi->second.size());
for(unsigned int i = 0; i < bi->second.size(); i++)
{
ci->second.at(i).b[0] = ci->second.at(i).b[1] = 1;
for(unsigned int j = 0; j < vi->second.at(i).size(); j++)
{
ci->second.at(i).b[0] *= vi->second.at(i).at(j).b[0];
ci->second.at(i).b[1] *= vi->second.at(i).at(j).b[1];
}
}
return;
}
*/
//void position::build_b_comb(vector<bool> &VB, unsigned int l)
void position::build_b_comb(map<unsigned int, vector<boost::dynamic_bitset<> > >::iterator bi)
{
unsigned long int p = pow(2,bi->first);
for(unsigned long int i = 0; i < p; i++)
bi->second.push_back(boost::dynamic_bitset<> (bi->first,i));
return;
}
const bool position::multiple_gene()
{
if(!MULTIPLE_GENE_SET)
set_multiple_gene();
return MULTIPLE_GENE;
}
const bool position::perf_omo() const
{
if(!PERF_OMO_SET)
cerr << "\nWarning, perfect omozygosity requested but never set\n";
return PERF_OMO;
}
void position::set_multiple_gene()
{
if(VPTR.empty())
return;
const string gid = VPTR.at(0)->get_gene_id();
for(unsigned int i = 1; i < VPTR.size(); i++)
{
if(gid != VPTR.at(i)->get_gene_id())
{
MULTIPLE_GENE = true;
break;
}
}
MULTIPLE_GENE_SET = true;
return;
}
void position::pass_prob_values_to_tr(vector<double> &CHI_RAW) const
{
/*
if(!perf_omo())
{
for(auto vi = G_V.cbegin(); vi != G_V.cend(); ++vi)
P_V.push_back(gsl_cdf_chisq_Q(*vi * 2, 1));
}
else
{
for(auto vi = G_V.cbegin(); vi != G_V.cend(); ++vi)
P_V.push_back(*vi);
}
*/
auto bi = BCOMB->find(VPTR.size()); //BCOMB OF APPROPRIATE SIZE ALREADY EXISTENT SINCE THIS RUNS AFTER COMPUTE_PROBABILITY
for(unsigned int i = 0; i < VPTR.size(); i++)
{
double P[2] = {0,0};
for(unsigned int bv = 0; bv < bi->second.size(); bv++)
P[bi->second.at(bv)[i]] += CHI_RAW.at(bv);
/* for(unsigned int j = 0; j < 2; j++)
if(P[j] > 1)
P[j] = 1; //AVOID PROBABLITIES GREATER THAN 1
*/
for(unsigned int j = 0; j < 2; j++)
P[j] = gsl_cdf_chisq_Q(P[j], bi->second.size() / 2);
VPTR.at(i)->add_pos_prob(P);
}
return;
}
// END OF POSITION CLASS
// RNASEQ CLASS
rnaseq::rnaseq(const string *File, map<string, vector<bool> >& Var_map):var_map(Var_map)
{
rna_seq_bam_file = *File;
open_bam();
GMAP.init(refv.size());
FUTV.resize(THREAD_NUM);
busy_vector.resize(THREAD_NUM, false);
if(THREAD_NUM == 1)
build_gmap_from_bam();
else
build_gmap_from_bam_multi();
if(VERBOSE)
GMAP.report(&refv);
GMAP.report_detailed(&refv);
bam.Close();
return;
}
const RefVector* rnaseq::refv_ptr()
{
return &refv;
}
const string rnaseq::get_file_name()
{
return rna_seq_bam_file;
}
const position* rnaseq::get_position_ptr(unsigned int ref_sequence, unsigned int pos) const
{
return GMAP.get_position_ptr(ref_sequence, pos);
}
position* rnaseq::get_position_ptr(unsigned int ref_sequence, unsigned int pos)
{
return GMAP.get_position_ptr(ref_sequence, pos);