-
Notifications
You must be signed in to change notification settings - Fork 7
/
Genotyper.hpp
2213 lines (1982 loc) · 63.7 KB
/
Genotyper.hpp
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
#ifndef _MOURISL_GENOTYPER
#define _MOURISL_GENOTYPER
#include <set>
#include <math.h>
#include "SeqSet.hpp"
#include "defs.h"
#include "KmerCount.hpp"
#include "SimpleVector.hpp"
#define GENETYPE_KIR 0
#define GENETYPE_HLA 1
struct _alleleInfo
{
int majorAlleleIdx ;
int geneIdx ; // which gene this allele belongs to
int alleleRank ; // -1 not select, 0-first allele, 1-second alelle
int genotypeQuality ; // assignment quality.
double abundance ;
int equivalentClass ; // the class id for the alleles with the same set of read alignment
double ecAbundance ; // the abundance for the equivalent class.
int missingCoverage ;
bool whitelist ;
} ;
struct _readGroupInfo
{
double count ; // number of reads this group contains.
} ;
struct _ecInfo
{
int length ;
int missingCoverage ;
} ; // combining the information of allels
struct _readAssignment
{
int alleleIdx ;
int start, end ;
float weight ;
float qual ;
float adjustWeight ; // The weight used for tie breaking
bool operator<(const struct _readAssignment &b) const
{
return alleleIdx < b.alleleIdx ;
}
} ;
class Genotyper
{
private:
// field type: 0 - default
// 1 - the digits up until the exon stage (before intronic stage)
void ParseAlleleName(char *allele, char *gene, char *majorAllele, int fieldsType = 0)
{
int i, j ;
int parseType = 1 ; // 1-no delimits (KIR); 2-with dimit(HLA)
int fieldsLength = alleleDigitUnits;
char delimiter = '\0' ;
strcpy(gene, allele) ;
strcpy(majorAllele, allele) ;
if (fieldsLength == -1)
{
fieldsLength = 3 ;
for (i = 0 ; allele[i] ; ++i)
if (allele[i] == ':')
{
delimiter = ':' ;
parseType = 2 ;
}
if (fieldsType == 0)
{
fieldsLength = 3 ;
}
else if (fieldsType >= 1)
{
if (parseType == 1)
fieldsLength = 5 ;
else if (parseType == 2)
fieldsLength = 3 ;
}
}
if (alleleDelimiter != '\0')
{
delimiter = alleleDelimiter ;
parseType = 2 ;
}
if (parseType == 1)
{
for (i = 0 ; allele[i] ; ++i)
{
if (allele[i] == '*')
break ;
}
gene[i] = '\0' ;
for (j = 0 ; j <= fieldsLength && allele[i + j] ; ++j)
;
majorAllele[i + j] = '\0' ;
}
else if (parseType == 2)
{
for (i = 0 ; allele[i] ; ++i)
{
if (allele[i] == '*')
break ;
}
gene[i] = '\0' ;
int k = 0 ;
for (j = i ; allele[j] ; ++j)
{
if (allele[j] == delimiter)
{
++k ;
if (k >= fieldsLength)
break ;
}
}
majorAllele[j] = '\0' ;
}
}
bool IsAlleleSameInExon(char *nameA, char *nameB)
{
char geneName[50];
char exonAlleleNameA[50];
char exonAlleleNameB[50];
ParseAlleleName(nameA, geneName, exonAlleleNameA, 1) ;
ParseAlleleName(nameB, geneName, exonAlleleNameB, 1) ;
return strcmp(exonAlleleNameA, exonAlleleNameB) == 0;
}
static bool CompSortPairByBDec( const struct _pair &p1, const struct _pair &p2 )
{
if (p1.b != p2.b)
return p2.b < p1.b ;
return p1.a < p2.a ;
}
static bool CompSortPairIntDoubleBDec( const struct _pairIntDouble &p1, const struct _pairIntDouble &p2 )
{
if (p2.b != p1.b)
return p2.b < p1.b ;
return p1.a < p2.a ;
}
static bool CompSortDoubleDec(const double &a, const double &b)
{
return b < a ;
}
bool IsAssignedReadTheSame(const std::vector<struct _pair> &l1, const std::vector<struct _pair> &l2)
{
int cnt1 = l1.size() ;
int cnt2 = l2.size() ;
int i ;
if (cnt1 != cnt2)
return false ;
// The read id in each vector should be sorted
for (i = 0 ; i < cnt1 ; ++i)
{
if (l1[i].a != l2[i].a
|| readAssignments[l1[i].a][l1[i].b].qual != readAssignments[l2[i].a][l2[i].b].qual)
return false ;
}
return true ;
}
bool IsReadAssignmentTheSame(const std::vector<struct _readAssignment> &a1, const std::vector<struct _readAssignment> &a2)
{
int cnt1 = a1.size() ;
int cnt2 = a2.size() ;
int i ;
if (cnt1 != cnt2)
return false ;
// The read id in each vector should be sorted
for (i = 0 ; i < cnt1 ; ++i)
{
if (a1[i].alleleIdx != a2[i].alleleIdx
|| a1[i].qual != a2[i].qual)
return false ;
}
return true ;
}
bool IsReadsInAlleleIdxOptimal(const std::vector<struct _pair> &readsInAllele, int k)
{
if (readAssignments[readsInAllele[k].a][readsInAllele[k].b].qual == 1)
return true ;
return false ;
}
double ReadAssignmentWeight(const struct _fragmentOverlap &o)
{
double ret = 1 ;
double similarity = o.similarity ; //o.overlap1.similarity ;
//if (o.hasMatePair && o.overlap2.similarity < similarity)
// similarity = o.overlap2.similarity ;
double segment = (1 - refSet.GetRefSeqSimilarity()) / 4.0 ;
if (segment < 0.01)
segment = 0.01;
if (similarity < 1 - 3 * segment)
ret = 0.01 ;
else if (similarity < 1 - 2 * segment)
ret = 0.1 ;
else if (similarity < 1 - segment)
ret = 0.5 ;
//else if (similarity < 1)
// ret = 0.5 ;
//if (pairedEndData && !o.hasMatePair)
// ret *= 0.9 ;
if (o.hasN)
ret /= 10.0 ;
return ret ;
}
int Rand()
{
return randomSeed = (48271 * randomSeed) & 0x7fffffff ;
}
double alnorm ( double x, bool upper )
//****************************************************************************80
//
// Purpose:
//
// ALNORM computes the cumulative density of the standard normal distribution.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 17 January 2008
//
// Author:
//
// Original FORTRAN77 version by David Hill.
// C++ version by John Burkardt.
//
// Reference:
//
// David Hill,
// Algorithm AS 66:
// The Normal Integral,
// Applied Statistics,
// Volume 22, Number 3, 1973, pages 424-427.
//
// Parameters:
//
// Input, double X, is one endpoint of the semi-infinite interval
// over which the integration takes place.
//
// Input, bool UPPER, determines whether the upper or lower
// interval is to be integrated:
// .TRUE. => integrate from X to + Infinity;
// .FALSE. => integrate from - Infinity to X.
//
// Output, double ALNORM, the integral of the standard normal
// distribution over the desired interval.
//
{
double a1 = 5.75885480458;
double a2 = 2.62433121679;
double a3 = 5.92885724438;
double b1 = -29.8213557807;
double b2 = 48.6959930692;
double c1 = -0.000000038052;
double c2 = 0.000398064794;
double c3 = -0.151679116635;
double c4 = 4.8385912808;
double c5 = 0.742380924027;
double c6 = 3.99019417011;
double con = 1.28;
double d1 = 1.00000615302;
double d2 = 1.98615381364;
double d3 = 5.29330324926;
double d4 = -15.1508972451;
double d5 = 30.789933034;
double ltone = 7.0;
double p = 0.398942280444;
double q = 0.39990348504;
double r = 0.398942280385;
bool up;
double utzero = 18.66;
double value;
double y;
double z;
up = upper;
z = x;
if ( z < 0.0 )
{
up = !up;
z = - z;
}
if ( ltone < z && ( ( !up ) || utzero < z ) )
{
if ( up )
{
value = 0.0;
}
else
{
value = 1.0;
}
return value;
}
y = 0.5 * z * z;
if ( z <= con )
{
value = 0.5 - z * ( p - q * y
/ ( y + a1 + b1
/ ( y + a2 + b2
/ ( y + a3 ))));
}
else
{
value = r * exp ( - y )
/ ( z + c1 + d1
/ ( z + c2 + d2
/ ( z + c3 + d3
/ ( z + c4 + d4
/ ( z + c5 + d5
/ ( z + c6 ))))));
}
if ( !up )
{
value = 1.0 - value;
}
return value;
}
double EMupdate(double *ecAbundance0, double *ecAbundance1, double *ecReadCount, const std::vector<std::vector<struct _pairID> > &readGroupToAlleleEc, const SimpleVector<struct _readGroupInfo> readGroupInfo, const struct _ecInfo *ecInfo)
{
int ecCnt = equivalentClassToAlleles.size() ;
int rgCnt = readGroupToAlleleEc.size() ;
int i, j ;
// E-step: find the expected number of reads
memset(ecReadCount, 0, sizeof(double) * ecCnt) ;
for (i = 0 ; i < rgCnt ; ++i)
{
double psum = 0 ;
int size = readGroupToAlleleEc[i].size() ;
for (j = 0 ; j < size ; ++j)
{
int ecIdx = readGroupToAlleleEc[i][j].a ;
//double qual = readGroupToAlleleEc[i][j].b ;
//psum += ecAbundance0[ecIdx] / ecLength[ecIdx] * qual ;
//if (ecAbundance0[ecIdx] >= 0)
double adjust = 1.0 / (ecInfo[ecIdx].missingCoverage + 1) ;
adjust = 1 ;
psum += ecAbundance0[ecIdx] * adjust ;
}
if (psum == 0)
psum = 1 ;
for (j = 0 ; j < size ; ++j)
{
int ecIdx = readGroupToAlleleEc[i][j].a ;
//double qual = readGroupToAlleleEc[i][j].b ;
//ecReadCount[ecIdx] += readGroupInfo[i].count * (ecAbundance0[ecIdx] * qual / ecLength[ecIdx] / psum) ;
double adjust = 1.0 / (ecInfo[ecIdx].missingCoverage + 1) ;
adjust = 1 ;
ecReadCount[ecIdx] += readGroupInfo[i].count * (ecAbundance0[ecIdx] * adjust / psum) ;
}
}
// M-step: recompute the abundance
double diffSum = 0 ;
double normalization = 0 ;
for (i = 0 ; i < ecCnt ; ++i)
normalization += ecReadCount[i] / ecInfo[i].length ;
for (i = 0 ; i < ecCnt ; ++i)
{
double tmp = ecReadCount[i] / ecInfo[i].length / normalization ;
//printf("%d %s %d: %lf %lf %lf. %lf\n", i, refSet.GetSeqName(equivalentClassToAlleles[i][0]), equivalentClassToAlleles[i].size(), tmp, ecReadCount[i], ecLength[i], ecAbundance[i]) ;
diffSum += ABS(tmp - ecAbundance0[i]) ;
ecAbundance1[i] = tmp ;
}
return diffSum ;
}
// Compute the update coeffcient alpha in the SQUREEM paper
double SQUAREMalpha(double *t0, double *t1, double *t2, int n)
{
int i ;
double sqrSumR = 0 ;
double sqrSumV = 0 ;
for (i = 0 ; i < n ; ++i)
{
sqrSumR += (t1[i] - t0[i]) * (t1[i] - t0[i]) ;
sqrSumV += (t2[i] - 2 * t1[i] + t0[i]) * (t2[i] - 2 * t1[i] + t0[i]) ;
}
if (sqrSumV == 0)
return -1 ;
return -sqrt(sqrSumR) / sqrt(sqrSumV) ;
}
int readCnt ;
int totalReadCnt ;
int maxAssignCnt ;
std::vector< std::vector<struct _pair> > readsInAllele ; // a-read idx. b-the index withint the readAssignment[a]
std::vector< std::vector<struct _readAssignment> > readAssignments ; // Coalesce reads assigned to the same alleles
std::map<int, std::vector<int> > readAssignmentsFingerprintToIdx ;
std::vector< std::vector<struct _readAssignment> > allReadAssignments ;
std::vector< std::vector<int> > equivalentClassToAlleles ;
std::vector< std::vector<struct _pair> > selectedAlleles ; // a-allele name, b-which allele (0,1)
// variables for allele, majorAllele and genes
char *geneBuffer ;
char *majorAlleleBuffer ;
SimpleVector<struct _alleleInfo> alleleInfo ;
std::map<std::string, int> majorAlleleNameToIdx ;
std::map<std::string, int> geneNameToIdx ;
std::vector<int> majorAlleleSize ; // number of alleles in the major allele
std::vector<std::string> geneIdxToName ;
std::vector<std::string> majorAlleleIdxToName ;
int geneCnt ;
int majorAlleleCnt ;
int alleleCnt ;
// variables for abundance
SimpleVector<double> geneAbundance ;
SimpleVector<double> majorAlleleAbundance ;
SimpleVector<double> geneMaxMajorAlleleAbundance ;
int64_t randomSeed ;
int geneType ; // not used actually
bool pairedEndData ;
// variables for filter
double filterFrac ;
double filterCov ;
double crossGeneRate ;
double **geneSimilarity ;
int alleleDigitUnits ;
char alleleDelimiter ;
int readLength ;
public:
SeqSet refSet ;
Genotyper(int kmerLength):refSet(kmerLength)
{
geneBuffer = new char[256] ;
majorAlleleBuffer = new char[256] ;
alleleCnt = majorAlleleCnt = geneCnt = readCnt = 0 ;
maxAssignCnt = 2000 ;
randomSeed = 17 ;
geneType = GENETYPE_KIR ;
pairedEndData = true ;
filterFrac = 0.15 ;
filterCov = 1.0 ;
crossGeneRate = 0.04 ;
geneSimilarity = NULL ;
readLength = 0 ;
alleleDigitUnits = -1 ;
alleleDelimiter = '\0' ;
}
~Genotyper()
{
delete[] geneBuffer ;
delete[] majorAlleleBuffer ;
int i ;
for (i = 0 ; i < geneCnt ; ++i)
delete[] geneSimilarity[i] ;
delete[] geneSimilarity ;
}
void SetGeneType(int g)
{
geneType = g ;
}
void SetFilterFrac(double f)
{
filterFrac = f ;
}
void SetFilterCov(double c)
{
filterCov = c ;
}
void SetReadLength(int rl)
{
readLength = rl ;
}
void SetCrossGeneRate(double r)
{
crossGeneRate = r ;
}
void SetAlleleNameStructure(int n, char d)
{
alleleDigitUnits = n ;
alleleDelimiter = d ;
}
void InitAlleleInfo()
{
int i, j, k ;
alleleCnt = refSet.Size() ;
alleleInfo.ExpandTo(alleleCnt) ;
for ( i = 0 ; i < alleleCnt ; ++i )
{
char *allele = refSet.GetSeqName(i) ;
ParseAlleleName( allele, geneBuffer, majorAlleleBuffer ) ;
std::string sGene(geneBuffer) ;
std::string sMajorAllele(majorAlleleBuffer) ;
if (geneNameToIdx.find(sGene) == geneNameToIdx.end())
{
geneNameToIdx[sGene] = geneCnt ;
geneIdxToName.push_back(sGene) ;
++geneCnt ;
}
if (majorAlleleNameToIdx.find(sMajorAllele) == majorAlleleNameToIdx.end())
{
majorAlleleNameToIdx[sMajorAllele] = majorAlleleCnt ;
majorAlleleIdxToName.push_back(sMajorAllele) ;
majorAlleleSize.push_back(0);
++majorAlleleCnt ;
}
alleleInfo[i].abundance = 0 ;
alleleInfo[i].geneIdx = geneNameToIdx[sGene] ;
alleleInfo[i].majorAlleleIdx = majorAlleleNameToIdx[sMajorAllele] ;
alleleInfo[i].alleleRank = -1 ;
alleleInfo[i].abundance = 0 ;
alleleInfo[i].genotypeQuality = -1 ;
alleleInfo[i].whitelist = true ;
majorAlleleSize[ alleleInfo[i].majorAlleleIdx ] += refSet.GetSeqWeight(i) ;
}
// Compute gene similarity
geneSimilarity = new double*[geneCnt] ;
KmerCount *kmerProfiles = new KmerCount[geneCnt];
for (i = 0 ; i < geneCnt ; ++i)
{
geneSimilarity[i] = new double[geneCnt] ;
// Select the lexcographically smallest
int minTag = -1 ;
for (j = 0 ; j < alleleCnt ; ++j)
{
if (alleleInfo[j].geneIdx != i)
continue ;
if (minTag == -1 || strcmp(refSet.GetSeqConsensus(j), refSet.GetSeqConsensus(minTag) ) < 0)
minTag = j ;
}
kmerProfiles[i].AddCount(refSet.GetSeqConsensus(minTag)) ;
}
/*for (i = 0 ; i < geneCnt ; ++i)
{
geneSimilarity[i][i] = 1.0 ;
for (j = i + 1 ; j < geneCnt ; ++j)
{
geneSimilarity[i][j] = geneSimilarity[j][i] = kmerProfiles[i].GetCountSimilarityJaccard( kmerProfiles[j] ) ;
printf("%s %s %lf\n", geneIdxToName[i].c_str(), geneIdxToName[j].c_str(), geneSimilarity[i][j]) ;
}
}*/
for (i = 0 ; i < geneCnt ; ++i)
{
for (j = 0 ; j < geneCnt ; ++j)
{
if (i == j)
{
geneSimilarity[i][i] = 1.0 ;
continue ;
}
geneSimilarity[i][j] = kmerProfiles[i].GetCountSimilarity( kmerProfiles[j] ) ;
//printf("%s %s %lf\n", geneIdxToName[i].c_str(), geneIdxToName[j].c_str(), geneSimilarity[i][j]) ;
}
}
delete[] kmerProfiles ;
// Adjust allele effective length
std::map< int, std::vector<int> > geneIdxToAlleleIdx ;
for (i = 0 ; i < geneCnt ; ++i)
geneIdxToAlleleIdx[i] = std::vector<int>() ;
for (i = 0 ; i < alleleCnt ; ++i)
geneIdxToAlleleIdx[ alleleInfo[i].geneIdx ].push_back(i) ;
for (i = 0 ; i < geneCnt ; ++i)
{
SimpleVector<int> lens ;
std::vector<int> &alleleIds = geneIdxToAlleleIdx[i] ;
int size = alleleIds.size() ;
lens.ExpandTo(size) ;
for (j = 0 ; j < size ; ++j)
lens[j] = refSet.GetSeqEffectiveLen( alleleIds[j] ) ;
std::sort(lens.BeginAddress(), lens.EndAddress()) ;
int lenMode = 0 ;
int max = 0 ;
for (j = 0 ; j < size ; )
{
for (k = j ; k < size ; ++k)
if (lens[k] != lens[j])
break ;
if (k - j > max)
{
max = k - j ;
lenMode = lens[j] ;
}
j = k ;
}
const int largeDeletion = 500 ;
for (j = 0 ; j < size ; ++j)
{
if (refSet.GetSeqEffectiveLen( alleleIds[j] ) < lenMode - largeDeletion)
{
refSet.SetSeqEffectiveLen(alleleIds[j], lenMode) ;
//printf("%s changed\n", majorAlleleIdxToName[ alleleInfo[ alleleIds[j] ].majorAlleleIdx].c_str()) ;
}
}
}
}
void SetAlleleWhitelist(FILE *fpAlleleWhitelist)
{
char alleleName[256] ;
int i ;
for (i = 0 ; i < alleleCnt ; ++i)
alleleInfo[i].whitelist = false ;
std::set<int> selectedMajorAlleles ;
while (fscanf(fpAlleleWhitelist, "%s", alleleName) != EOF)
{
ParseAlleleName(alleleName, geneBuffer, majorAlleleBuffer) ;
std::string sMajorAllele(majorAlleleBuffer) ;
if (majorAlleleNameToIdx.find( sMajorAllele ) != majorAlleleNameToIdx.end() )
selectedMajorAlleles.insert( majorAlleleNameToIdx[sMajorAllele] ) ;
}
for (i = 0 ; i < alleleCnt ; ++i)
{
if (selectedMajorAlleles.find( alleleInfo[i].majorAlleleIdx ) != selectedMajorAlleles.end() )
alleleInfo[i].whitelist = true ;
}
}
void InitRefSet(char *filename)
{
int i, j ;
//refSet.InputRefFa(filename) ;
std::map<std::string, int> usedSeq ;
ReadFiles fa ;
fa.AddReadFile( filename, false ) ;
while ( fa.Next() )
{
std::string seq( fa.seq );
if (usedSeq.find(seq) != usedSeq.end())
{
refSet.UpdateSeqWeight( usedSeq[seq], 1) ;
}
else
{
usedSeq[seq] = refSet.InputRefSeq(fa.id, fa.seq, 1, true, fa.comment);
}
}
refSet.UpdateDnaSeqWeight() ;
InitAlleleInfo() ;
}
void InitRefSet(char *filename, const std::map<std::string, int> &selectedAlleles)
{
int i, j ;
//refSet.InputRefFa(filename) ;
std::map<std::string, int> usedSeq ;
ReadFiles fa ;
fa.AddReadFile( filename, false ) ;
while ( fa.Next() )
{
if (selectedAlleles.find(fa.id) == selectedAlleles.end())
continue ;
std::string seq( fa.seq );
if (usedSeq.find(seq) != usedSeq.end())
{
refSet.UpdateSeqWeight( usedSeq[seq], 1) ;
}
else
{
usedSeq[seq] = refSet.InputRefSeq(fa.id, fa.seq, 1, true, fa.comment);
}
}
refSet.UpdateDnaSeqWeight() ;
InitAlleleInfo() ;
}
void InitReadAssignments(int totalReadCnt, int maxAssignCnt)
{
maxAssignCnt = maxAssignCnt ;
readCnt = 0 ;
allReadAssignments.resize(totalReadCnt) ;
readAssignments.clear() ;
readAssignmentsFingerprintToIdx.clear() ;
readsInAllele.resize(alleleCnt) ;
int i ;
for (i = 0 ; i < totalReadCnt ; ++i)
allReadAssignments[i].clear() ;
for (i = 0 ; i < alleleCnt ; ++i)
readsInAllele[i].clear() ;
this->totalReadCnt = totalReadCnt ;
}
void SetReadAssignments(int readId, const std::vector<struct _fragmentOverlap> &assignment)
{
int i ;
int assignmentCnt = assignment.size() ;
allReadAssignments[readId].clear() ;
if (maxAssignCnt > 0 && assignmentCnt > maxAssignCnt)
return ;
/*for (i = 1 ; i < assignmentCnt ; ++i)
if ( alleleInfo[assignment[i].seqIdx].geneIdx != alleleInfo[assignment[i - 1].seqIdx].geneIdx)
return ;*/
double weightFactor = 1.0 ;
/*for (i = 1 ; i < assignmentCnt ; ++i)
if (assignment[i].overlap1.matchCnt != assignment[i - 1].overlap1.matchCnt ||
assignment[i].overlap2.matchCnt != assignment[i - 1].overlap2.matchCnt)
{
weightFactor = 0.1 ;
return;
break ;
}*/
for (i = 0 ; i < assignmentCnt ; ++i)
{
if (refSet.IsFragmentSpanSeparator(assignment[i]))
return;
}
double adjustFactor = 1.0 ;
double maxSimilarity = 0 ;
for (i = 0 ; i < assignmentCnt ; ++i)
{
if (assignment[i].similarity > maxSimilarity)
maxSimilarity = assignment[i].similarity ;
/*if (assignment[i].matchCnt != assignment[i - 1].matchCnt)
{
adjustFactor = 0 ;
break ;
}*/
}
if (maxSimilarity < 1)
adjustFactor = 0.25 ;
for (i = 0; i < assignmentCnt; ++i)
{
struct _readAssignment na ;
if (!alleleInfo[ assignment[i].seqIdx ].whitelist)
continue ;
na.alleleIdx = assignment[i].seqIdx ;
na.start = assignment[i].seqStart ;
na.end = assignment[i].seqEnd ;
na.weight = ReadAssignmentWeight(assignment[i]);
na.qual = assignment[i].qual ;
na.adjustWeight = adjustFactor * na.weight ;
allReadAssignments[readId].push_back(na) ;
}
}
// Should only be called on the reads without coalescing
std::vector<struct _readAssignment> GetReadAssignments(int readId)
{
return allReadAssignments[readId] ;
}
// Coalesce the [begin,end] all reads to the read assignment
int CoalesceReadAssignments(int begin, int end)
{
int i, j, k ;
int ret = 0 ;
for (i = begin ; i <= end && i < totalReadCnt ; ++i)
{
const int FINGERPRINT_MAX = 20000003 ;
int size = allReadAssignments[i].size() ;
if (size == 0)
continue ;
++ret ;
std::sort(allReadAssignments[i].begin(), allReadAssignments[i].end()) ;
int fingerprint = 0 ;
for (j = 0 ; j < size ; ++j)
{
k = allReadAssignments[i][j].alleleIdx ;
fingerprint = (fingerprint * (int64_t)alleleCnt + k) % FINGERPRINT_MAX ;
}
int addTo = -1 ;
if (readAssignmentsFingerprintToIdx.find(fingerprint) == readAssignmentsFingerprintToIdx.end())
addTo = -1 ;
else
{
std::vector<int> assignmentsIdx = readAssignmentsFingerprintToIdx[fingerprint] ;
int idxSize = assignmentsIdx.size() ;
addTo = -1 ;
for (j = 0 ; j < idxSize ; ++j)
{
if (IsReadAssignmentTheSame(allReadAssignments[i],
readAssignments[ assignmentsIdx[j] ] ) )
{
addTo = assignmentsIdx[j] ;
break ;
}
}
}
if (addTo == -1)
{
readAssignments.push_back( allReadAssignments[i] ) ;
readAssignmentsFingerprintToIdx[fingerprint].push_back(readCnt) ;
++readCnt ;
}
else
{
for (j = 0 ; j < size ; ++j)
{
if (allReadAssignments[i][j].qual == 1)
{
if (allReadAssignments[i][j].start < readAssignments[addTo][j].start)
readAssignments[addTo][j].start = allReadAssignments[i][j].start ;
if (allReadAssignments[i][j].end < readAssignments[addTo][j].end)
readAssignments[addTo][j].end = allReadAssignments[i][j].start ;
}
readAssignments[addTo][j].weight += allReadAssignments[i][j].weight ;
readAssignments[addTo][j].adjustWeight += allReadAssignments[i][j].adjustWeight ;
// The read assignment the same test makes sure they have the same quality
}
}
}
// Release the memory space for allReadAssignments
for (i = begin ; i <= end && i < totalReadCnt ; ++i)
{
std::vector<struct _readAssignment>().swap(allReadAssignments[i]) ;
}
return ret ;
}
// Build the read in allele list
// Ret: the number of read got assigned
int FinalizeReadAssignments()
{
int i, j ;
int ret = 0 ;
for (i = 0 ; i < readCnt ; ++i)
{
int assignmentCnt = readAssignments[i].size() ;
//std::sort(readAssignments[i].begin(), readAssignments[i].end()) ;
if (assignmentCnt > 0)
++ret ;
for (j = 0; j < assignmentCnt; ++j)
{
struct _pair np ;
np.a = i ;
np.b = j ;
readsInAllele[readAssignments[i][j].alleleIdx].push_back(np) ;
}
}
BuildAlleleEquivalentClass() ;
for (i = 0 ; i < alleleCnt ; ++i)
{
alleleInfo[i].missingCoverage = refSet.GetSeqMissingBaseCoverage(i, 0.01) ;
//printf("%d %s %d\n", i, refSet.GetSeqName(i), alleleMissingCoverage[i]) ;
}
return ret ;
}
double GetAverageReadAssignmentCnt()
{
int i ;
double sum = 0 ;
double cnt = 0 ;
for (i = 0 ; i < readCnt ; ++i)
{
if (readAssignments[i].size() > 0)
{
sum += readAssignments[i].size() ;
++cnt ;
}
}
return sum / cnt ;
}
void SetAlleleAbundance(double *ecReadCount, struct _ecInfo *ecInfo)
{
int i, j, k ;
int ecCnt = equivalentClassToAlleles.size();
if (ecReadCount != NULL)
{
for (i = 0 ; i < alleleCnt ; ++i)
alleleInfo[i].abundance = alleleInfo[i].ecAbundance = 0 ;
for (i = 0 ; i < ecCnt ; ++i)
{
int size = equivalentClassToAlleles[i].size() ;
double abund = 0 ;
//k = equivalentClassToAlleles[i][0] ;
//printf("%d %d %s %lf %d %d\n", i, k, refSet.GetSeqName(k), emEcReadCount[j][i], refSet.GetSeqConsensusLen(k),readsInAllele[k].size()) ;
abund += ecReadCount[i] ;
//printf("%lf\n", abund) ;
abund = abund / ecInfo[i].length * 1000.0 ; // FPK
for (j = 0 ; j < size ; ++j)
{
k = equivalentClassToAlleles[i][j] ;
//alleleInfo[k].abundance = ecAbundance[i] / size * effectiveReadCnt ;
//alleleInfo[k].ecAbundance = ecAbundance[i] * effectiveReadCnt ;
alleleInfo[k].abundance = abund / size ;
alleleInfo[k].ecAbundance = abund ;
//printf("%d %d %s %lf %d %d\n", i, k, refSet.GetSeqName(k), (double)abund, refSet.GetSeqConsensusLen(k),readsInAllele[k].size()) ;
}
//printf("%lf %lf\n", ecAbundance[i], ecAbundance[i] * effectiveReadCnt) ;
}
}
// Set major allele and gene abundances
// Init other useful abundance data
geneAbundance.ExpandTo(geneCnt) ;
geneAbundance.SetZero(0, geneCnt) ;
majorAlleleAbundance.ExpandTo(majorAlleleCnt) ;
majorAlleleAbundance.SetZero(0, majorAlleleCnt) ;
geneMaxMajorAlleleAbundance.ExpandTo(geneCnt) ;
geneMaxMajorAlleleAbundance.SetZero(0, geneCnt) ;
for (i = 0 ; i < alleleCnt ; ++i)
{
majorAlleleAbundance[ alleleInfo[i].majorAlleleIdx ] += alleleInfo[i].abundance ;
geneAbundance[ alleleInfo[i].geneIdx ] += alleleInfo[i].abundance ;
/*if (alleleInfo[i].abundance > geneMaxMajorAlleleAbundance[ alleleInfo[i].geneIdx ])
{
geneMaxMajorAlleleAbundance[ alleleInfo[i].geneIdx ] = alleleInfo[i].abundance ;
}*/
}
for (i = 0 ; i < alleleCnt ; ++i)
{
double abund = majorAlleleAbundance[ alleleInfo[i].majorAlleleIdx ] ;
if (abund > geneMaxMajorAlleleAbundance[ alleleInfo[i].geneIdx ])
geneMaxMajorAlleleAbundance[ alleleInfo[i].geneIdx ] = abund ;
}
}
void InitAlleleAbundance(FILE *fp)
{
int i, j ;
char buffer[256] ;
double abundance ;
double count ;
int tmp ;
std::map<std::string, int> refNameToIdx ;