forked from Arkarachai/STR-FM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
heteroprob.py
199 lines (171 loc) · 6.15 KB
/
heteroprob.py
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
### import libraries ###
import sys
import collections, math
import heapq
import itertools
### basic function ###
def permuterepeat(n,rlist):
f = math.factorial
nfac=f(n)
rfaclist=[f(i) for i in rlist]
for rfac in rfaclist:
nfac=nfac/rfac
return nfac
def nCr(n,r):
f = math.factorial
return f(n) / f(r) / f(n-r)
def averagelist(a,b,expectedlevelofminor):
product=[]
for i in range(len(a)):
product.append((1-expectedlevelofminor)*a[i]+expectedlevelofminor*b[i])
return product
def complement_base(read):
collect=''
for i in read:
if i.upper()=='A':
collect+='T'
elif i.upper()=='T':
collect+='A'
elif i.upper()=='C':
collect+='G'
elif i.upper()=='G':
collect+='C'
return collect
def makeallpossible(read):
collect=[]
for i in range(len(read)):
tmp= read[i:]+read[:i]
collect.append(tmp)
collect.append(complement_base(tmp))
return collect
def motifsimplify(base):
'''str--> str
'''
motiflength=len(base)
temp=list(set(ALLMOTIF[motiflength]).intersection(set(makeallpossible(base))))
return temp[0]
def majorallele(seq):
binseq=list(set(seq))
binseq.sort(reverse=True) # highly mutate mode
#binseq.sort() # majority mode
storeform=''
storevalue=0
for i in binseq:
if seq.count(i)>storevalue:
storeform=i
storevalue=seq.count(i)
return int(storeform)
### decide global parameter ###
COORDINATECOLUMN=1
ALLELECOLUMN=2
MOTIFCOLUMN=3
inputname=sys.argv[1]
errorprofile=sys.argv[2]
EXPECTEDLEVELOFMINOR=float(sys.argv[3])
if EXPECTEDLEVELOFMINOR >0.5:
try:
errorexpectcontribution=int('a')
except Exception, eee:
print eee
stop_err("Expected contribution of minor allele must be at least 0 and not more than 0.5")
MINIMUMMUTABLE=0 ###1.2*(1.0/(10**8)) #http://www.ncbi.nlm.nih.gov/pubmed/22914163 Kong et al 2012
## Fixed global variable
ALLREPEATTYPE=[1,2,3,4]
ALLREPEATTYPENAME=['mono','di','tri','tetra']
monomotif=['A','C']
dimotif=['AC','AG','AT','CG']
trimotif=['AAC','AAG','AAT','ACC','ACG','ACT','AGC','AGG','ATC','CCG']
tetramotif=['AAAC','AAAG','AAAT','AACC','AACG','AACT','AAGC','AAGG','AAGT','AATC','AATG','AATT',\
'ACAG','ACAT','ACCC','ACCG','ACCT','ACGC','ACGG','ACGT','ACTC','ACTG','AGAT','AGCC','AGCG','AGCT',\
'AGGC','AGGG','ATCC','ATCG','ATGC','CCCG','CCGG','AGTC']
ALLMOTIF={1:monomotif,2:dimotif,3:trimotif,4:tetramotif}
monorange=range(5,60)
dirange=range(6,60)
trirange=range(9,60)
tetrarange=range(12,80)
ALLRANGE={1:monorange,2:dirange,3:trirange,4:tetrarange}
#########################################
######## Prob calculation sector ########
#########################################
def multinomial_prob(majorallele,STRlength,motif,probdatabase):
'''int,int,str,dict-->int
### get prob for each STRlength to be generated from major allele
'''
#print (majorallele,STRlength,motif)
prob=probdatabase[len(motif)][motif][majorallele][STRlength]
return prob
################################################
######## error model database sector ###########
################################################
## structure generator
errormodeldatabase={1:{},2:{},3:{},4:{}}
sumbymajoralleledatabase={1:{},2:{},3:{},4:{}}
for repeattype in ALLREPEATTYPE:
for motif in ALLMOTIF[repeattype]:
errormodeldatabase[repeattype][motif]={}
sumbymajoralleledatabase[repeattype][motif]={}
for motifsize1 in ALLRANGE[repeattype]:
errormodeldatabase[repeattype][motif][motifsize1]={}
sumbymajoralleledatabase[repeattype][motif][motifsize1]=0
for motifsize2 in ALLRANGE[repeattype]:
errormodeldatabase[repeattype][motif][motifsize1][motifsize2]=MINIMUMMUTABLE
#print errormodeldatabase
## read database
## get read count for each major allele
fd=open(errorprofile)
lines=fd.readlines()
for line in lines:
temp=line.strip().split('\t')
t_major=int(temp[0])
t_count=int(temp[2])
motif=temp[3]
sumbymajoralleledatabase[len(motif)][motif][t_major]+=t_count
fd.close()
##print sumbymajoralleledatabase
## get probability
fd=open(errorprofile)
lines=fd.readlines()
for line in lines:
temp=line.strip().split('\t')
t_major=int(temp[0])
t_read=int(temp[1])
t_count=int(temp[2])
motif=temp[3]
if sumbymajoralleledatabase[len(motif)][motif][t_major]>0:
errormodeldatabase[len(motif)][motif][t_major][t_read]=t_count/(sumbymajoralleledatabase[len(motif)][motif][t_major]*1.0)
#errormodeldatabase[repeattype][motif][t_major][t_read]=math.log(t_count/(sumbymajorallele[t_major]*1.0))
#else:
# errormodeldatabase[repeattype][motif][t_major][t_read]=0
fd.close()
#print errormodeldatabase
#print math.log(100,10)
#########################################
######## input reading sector ###########
#########################################
fd = open(inputname)
##fd=open('sampleinput_C.txt')
lines=fd.xreadlines()
for line in lines:
i_read=[]
i2_read=[]
temp=line.strip().split('\t')
i_coordinate=temp[COORDINATECOLUMN-1]
i_motif=motifsimplify(temp[MOTIFCOLUMN-1])
i_read=temp[ALLELECOLUMN-1].split(',')
i_read=map(int,i_read)
depth=len(i_read)
heteromajor1=int(temp[6])
heteromajor2=int(temp[7])
### calculate the change to detect combination (using error profile)
heterozygous_collector=0
alist=[multinomial_prob(heteromajor1,x,i_motif,errormodeldatabase)for x in i_read]
blist=[multinomial_prob(heteromajor2,x,i_motif,errormodeldatabase)for x in i_read]
ablist=averagelist(alist,blist,EXPECTEDLEVELOFMINOR)
if 0 in ablist:
continue
heterozygous_collector=reduce(lambda y, z: y*z,ablist )
### prob of combination (using multinomial distribution)
frequency_distribution=[len(list(group)) for key, group in itertools.groupby(i_read)]
## print frequency_distribution
expandbypermutation=permuterepeat(depth,frequency_distribution)
print line.strip()+'\t'+str(heterozygous_collector)+'\t'+str(expandbypermutation)+'\t'+str(expandbypermutation*heterozygous_collector)+'\t'+str(depth)