-
Notifications
You must be signed in to change notification settings - Fork 2
/
Genotype_Likelihoods.py
executable file
·700 lines (649 loc) · 37.5 KB
/
Genotype_Likelihoods.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
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
#! /usr/bin/python3
import os
import sys
import gzip
import generics
import numpy as np
import math
import scipy.stats
import random
from statistics import mode
import argparse
class Site:
def __init__(self,chrom,position,reference):
self.chrom = str(chrom)
self.position = int(position)
self.reference = str(reference)
class Reads:
def __init__(self,base,base_quality):
self.base = str(base)
self.base_quality = str(base_quality)
alleles = ['A','C','G','T']
parser = argparse.ArgumentParser()
parser.add_argument("input",help="File containing the list of file names for gzipped mpileup files, mpileup files or bam files to be used in analysis")
parser.add_argument("-p", "--ploidyFile",help="File containing the list of ploidy levels to be used in analysis")
parser.add_argument("-o","--outFolder",help="output folder",default=0)
parser.add_argument("-i","--Inbreeding",help="Inbreeding coefficients for samples e.g 0.1x3,0.2 = 0.1,0.1,0.1,0.2 ")
parser.add_argument("-d","--downsampling",help="Fraction of data to be used in the calculations",default=1)
parser.add_argument("-m","--min_non_major_freq",type=float,help="Set the minimum frequency of non major alleles for bases to be included in the calculations",default=0.2)
parser.add_argument("-q","--min_quality_score",type=int,help="Set the minimum quality score of a read to be included in the calculation",default=1)
parser.add_argument("-dp","--min_global_depth",type=float,help="Set the minimum global depth of a base to be included in calculations",default=0)
parser.add_argument("-dpInd","--min_ind_depth",type=float,help="Set the minimum individual depth of a base to be included in calculations",default=0)
parser.add_argument("-M2","--max_minor2_freq",type=float,help="Set the maximum frequency of third most prolific alleles for bases to be included in the calculations",default=0.1)
parser.add_argument("-M3","--max_minor3_freq",type=float,help="Set the maximum frequency of fourth most prolific alleles for bases to be included in the calculations",default=0.1)
parser.add_argument("-s","--random_seed",type=int,help="Set the random seed to be included in the calculations")
parser.add_argument("-r","--ref_fasta",help="File name of the reference fasta file. Use this if bam files are being used. e.g TAIR10.fa")
args = parser.parse_args()
if args.random_seed:
seed = args.random_seed
random.seed(seed) # set the seed for calculations using random function
print("Seed for calculations using random function is: " + str(seed))
else:
print("Seed is not set.")
if args.ploidyFile:
ploidyFile = args.ploidyFile
list_of_ploidy=[]
with open(ploidyFile,'rb') as ploidyList:
for line in ploidyList:
line = int(line.decode().strip('\n')) # convert bytes into integers
list_of_ploidy.append(line)
ploidy = list_of_ploidy
print("Ploidy levels to be tested in analysis are: " + str(ploidy))
else:
ploidy = [1,2,3,4,5,6] # default ploidy levels
print("Default ploidy levels to be tested in analysis are: " + str(ploidy))
inputs = args.input # input file in form of mpileup, gzipped mpileup or bam
list_of_inputs=[]
fileType = 0 # initial file type
fileTypes = {
1:"bam",
2:"mpileup",
3:"mpileup.gz"
}
with open(inputs,'rb') as filelist:
for line in filelist:
line = line.decode().strip('\n') # convert bytes into strings
if line.endswith(".bam"):
if fileType != 1:
if fileType != 0: # if it is no the first line
sys.exit("Error in file " + line + ". Input file should contain files from the same file type.")
fileType = 1 # bam
list_of_inputs.append(line)
elif line.endswith(".mpileup"):
if fileType != 2:
if fileType != 0:
sys.exit("Error in file " + line + ". Input file should contain files from the same file type.")
fileType = 2 # mpileup
list_of_inputs.append(line)
elif line.endswith(".mpileup.gz"):
if fileType != 3:
if fileType != 0:
sys.exit("Error in file " + line + ". Input file should contain files from the same file type.")
fileType = 3 # mpileup.gz
list_of_inputs.append(line)
else:
sys.exit(line + " file is not supported. Supported file types are '.mpileup', '.mpileup.gz' and '.bam'.")
Nfiles=len(list_of_inputs)
if Nfiles == 1:
print('1 file found')
else:
print('%d files found' %Nfiles)
print(list_of_inputs)
print("File type detected: " + fileTypes[fileType])
extensionLen = {
1:4, #.bam
2:8, #.mpileup
3:11 #.mpileup.gz
}
exl = extensionLen[fileType] # extension length of fileType including dots
outFolder = args.outFolder
if fileType == 1: # bam file
if args.ref_fasta:
refFile = args.ref_fasta
else:
sys.exit("Reference fasta for bam file is not found. Please add your reference file in fasta format by using '-r' parameter and try again.")
for g1 in list_of_inputs: # for every filename
directory = '/'.join(g1.split('/')[:-1])
if len(directory) == 0:
g = "./" + g1
g2 = g1[:-exl]
if outFolder==0:
output = "./" + g2 + ".genolikes.gz"
mpFile = "./" + g2 + ".mpileup"
else:
output = outFolder+'/'.join(g2.split('/')[-1])+".genolikes.gz"
mpFile = outFolder+'/'.join(g2.split('/')[-1])+".mpileup"
else:
g = g1
g2 = g1[:-exl]
if outFolder==0:
output = g2 + ".genolikes.gz"
mpFile = g2 + ".mpileup"
else:
output = outFolder+"/"+g2+".genolikes.gz"
mpFile = outFolder+"/"+g2+".mpileup"
print("Output file is: " + output)
bashCommand = "samtools mpileup -f " + refFile + " " + g1 + " > " + mpFile
try:
os.system(bashCommand)
except OSError as e:
if e.errno == os.errno.ENOENT:
# handle file not found error
sys.exit("'samtools' not found. Please make sure that you have samtools installed.")
else:
# Something else went wrong while trying to run `samtools`
sys.exit("Something went wrong with 'samtools'.")
with open(mpFile) as mp:
first_line = mp.readline()
Data = first_line.strip('\n')
l = Data.split('\t')
NSAMS=int((len(l)-3)/3)
if args.Inbreeding:
inbreed = args.Inbreeding
else:
inbreed = "0x{}".format(str(NSAMS))
# parse inbreeding coeffients
F=[]
temp=inbreed.split(',')
for t in temp:
vals=t.split('x')
if len(vals)==2:
F+=list(np.repeat(float(vals[0]),int(vals[1])))
else:
F+=[vals[0]]
print(F)
downsampling=float(args.downsampling) # fraction of data to be used (0-1].
#Original_sample_number=NSAMS
win=50 # window size for calculating ploidy
phredscale=33
NUMSITES=np.zeros(NSAMS+1,int)
NUMSITES_HWE=np.zeros(NSAMS+1,int)
ExpectedPloidy=[[] for i in range(NSAMS+1)]
Overall_Prob=np.zeros((NSAMS+1,len(ploidy)),float) # (NSAMS+1)xploidies array for probabilities of each ploidy for each sample and overall ploidy probabilities
Overall_Prob_HWE=np.zeros((NSAMS+1,len(ploidy)),float)
delta_prob=np.zeros((NSAMS+1,len(ploidy)),float)
counts=np.zeros((NSAMS+1,len(ploidy)),float)+1 # counts of bases for each ploidy being most likely
base_number=0 # count for bases
list_of_window=[]
list_of_window2=[]
no_bases=0
total_bases=0
for line in mp:
Data=line.strip('\n')# convert bytes into string
l = Data.split('\t')
mySite = Site(str(l[0]),int(l[1]),str(l[2]))
myReads = Reads("","")
# pooled reads for first level filtering (global depth) and estimation of minor/major alleles and allele frequencies
individualDepth = np.zeros(NSAMS,float)
for n in range(NSAMS):
n=n+1
subReads= Reads(l[(n-1)*3+4],l[(n-1)*3+5])
individualDepth[n-1] = len(subReads.base)
myReads.base=str(myReads.base+subReads.base)
myReads.base_quality=str(myReads.base_quality+subReads.base_quality)
# convert to bases
[bases,indexDelN] = generics.convertSyms(myReads,mySite)
myReads=Reads(bases,myReads.base_quality)
if len(myReads.base)!=len(myReads.base_quality):
sys.exit("Conversion not succesful")
# filter by quality
[bases,qualities] = generics.filter(myReads,args.min_quality_score)
myReads=Reads(bases,qualities)
# find all indexes of occurances to be filtered out
index_of_X=[]
index=-1
while True:
index=myReads.base.find('X',index+1)
if index == -1:
break # all occurrences have been found
index_of_X.append(index)
myReads.base=myReads.base.replace('X','')
# remove all corresponding base qualities
count=0
for i in index_of_X:
myReads.base_quality = myReads.base_quality[:i-count] + myReads.base_quality[i+1-count:]
count+=1
globalDepth = len(myReads.base)
if ((globalDepth > args.min_global_depth) & (min(individualDepth)>args.min_ind_depth)):
total_bases+=globalDepth
no_bases+=1
# counts of non-major bases
nonMajorCount = generics.calcNonMajorCounts(myReads)
nonMajorProp = nonMajorCount/len(myReads.base)
# filter the site based on global depth
Set_min_prop = args.min_non_major_freq # minimum proportion of nonMajorCount
if nonMajorProp>Set_min_prop: # remove bases where more that 1-Set_min_prop are major allele i.e monomorphic bases
prob_of_ancestral_allelle_maj=1-nonMajorProp
haploid = generics.calcGenoLogLike1(myReads,mySite)
tri_ref = haploid[4] # retrieve reference value for if the base is not triallilic
haploid=haploid[:4] # remove reference value
# keep reference allele as one possible allele so always assume KeepRef=0
[major,minor,minor2,minor3] = [haploid.index(sorted(haploid,reverse=True)[0]),haploid.index(sorted(haploid,reverse=True)[1]),haploid.index(sorted(haploid,reverse=True)[2]),haploid.index(sorted(haploid,reverse=True)[3])]
# remove sites with >0.1 frequency of minor 2 or minor 3 allele to remove non biallilic sites (0.1 error built in for sequencing error)
minor2_prop=generics.calcAlleleFreq(minor2,myReads)/len(myReads.base) # Calculate allele frequencies of minor2&3 alleles
minor3_prop=generics.calcAlleleFreq(minor3,myReads)/len(myReads.base)
if(minor2_prop<args.max_minor2_freq and minor3_prop<args.max_minor3_freq):
P_bar=0
Q_bar=0
for read in range(len(myReads.base)):
if myReads.base[read]==alleles[major]:
P_bar+=(1-(10**((phredscale-ord(str(myReads.base_quality[read])))/10)))
elif myReads.base[read]==alleles[minor]:
Q_bar+=(1-(10**((phredscale-ord(str(myReads.base_quality[read])))/10)))
P = P_bar/(P_bar+Q_bar) # proportion of major allele weigted by read quality
Q = Q_bar/(P_bar+Q_bar) # proportion of minor allele weigted by read quality
base_number+=1 # count number of SNPs included in data
for n in range(NSAMS):
# retrieve bases for this particular sample
n=n+1
myReads = Reads(l[(n-1)*3+4],l[(n-1)*3+5])
(bases, indexDelN) = generics.convertSyms(myReads,mySite)
myReads = Reads(bases, myReads.base_quality)
# filter by quality
[bases,qualities] = generics.filter(myReads,args.min_quality_score)
myReads=Reads(bases,qualities)
# find all indexes of occurances to be filtered out
index_of_X=[]
index=-1
while True:
index=myReads.base.find('X',index+1)
if index == -1:
break # all occurrences have been found
index_of_X.append(index)
myReads.base=myReads.base.replace('X','')
# remove all corresponding base qualities
count=0
for i in index_of_X:
myReads.base_quality = myReads.base_quality[:i-count] + myReads.base_quality[i+1-count:]
count+=1
major_count=generics.calcAlleleFreq(major,myReads)
minor_count=generics.calcAlleleFreq(minor,myReads)
# take a sample of the bases so that the proportion of data used is as required
if downsampling<1:
data_prop = math.ceil(len(myReads.base)*downsampling) # calculate how many bases to include for proportion of sample
rand_samp = random.sample(range(0,len(myReads.base)),data_prop)
base = ""
qualities = ""
for r in rand_samp:
base+=myReads.base[r]
qualities+=myReads.base_quality[r]
myReads=Reads(base,qualities)
# find sample depth of filtered data
sampleDepth = len(myReads.base)
NUMSITES[0]+=sampleDepth # count the number of reads for each sample
NUMSITES[n]+=sampleDepth
sep="\t"
content=""
content=(mySite.chrom,str(mySite.position),str(n),mySite.reference,str(sampleDepth),alleles[major],alleles[minor],str(major_count),str(minor_count))
content=sep.join(content)
content+="\t"
for ip in ploidy:
Nploid = generics.calcGenoLogLikeN_MajorMinor(ip, myReads, mySite, major, minor)
content2 ="\t".join(map(str,Nploid))
content += content2
content += "\t"
content += "\n"
# write file of genotype likelihoods
with gzip.open(output,'at+') as f:
f.write(content)
# end likelihood calc
# end for sample
# end for if max minor
# end if not filtered for global depth
# end for line
try:
os.remove(mpFile) # remove unnecessary mpileup file
except:
pass
elif fileType == 2: # mpilup file
for g1 in list_of_inputs: # for every filename
directory = '/'.join(g1.split('/')[:-1])
if len(directory) == 0:
g = "./" + g1
g2 = g1[:-exl]
if outFolder==0:
output = "./" + g2 + ".genolikes.gz"
else:
output = outFolder+'/'.join(g2.split('/')[-1])+".genolikes.gz"
else:
g = g1
g2 = g1[:-exl]
if outFolder==0:
output = g2 + ".genolikes.gz"
else:
output = outFolder+"/"+g2+".genolikes.gz"
print("Output file is: " + output)
with open(g) as mp:
first_line = mp.readline()
Data = first_line.strip('\n')
l = Data.split('\t')
NSAMS=int((len(l)-3)/3)
if args.Inbreeding:
inbreed = args.Inbreeding
else:
inbreed = "0x{}".format(str(NSAMS))
# parse inbreeding coeffients
F=[]
temp=inbreed.split(',')
for t in temp:
vals=t.split('x')
if len(vals)==2:
F+=list(np.repeat(float(vals[0]),int(vals[1])))
else:
F+=[vals[0]]
print(F)
downsampling=float(args.downsampling) # fraction of data to be used (0-1].
#Original_sample_number=NSAMS
win=50 # window size for calculating ploidy
phredscale=33
NUMSITES=np.zeros(NSAMS+1,int)
NUMSITES_HWE=np.zeros(NSAMS+1,int)
ExpectedPloidy=[[] for i in range(NSAMS+1)]
Overall_Prob=np.zeros((NSAMS+1,len(ploidy)),float) # (NSAMS+1)xploidies array for probabilities of each ploidy for each sample and overall ploidy probabilities
Overall_Prob_HWE=np.zeros((NSAMS+1,len(ploidy)),float)
delta_prob=np.zeros((NSAMS+1,len(ploidy)),float)
counts=np.zeros((NSAMS+1,len(ploidy)),float)+1 # counts of bases for each ploidy being most likely
base_number=0 # count for bases
list_of_window=[]
list_of_window2=[]
no_bases=0
total_bases=0
for line in mp:
Data=line.strip('\n')# convert bytes into string
l = Data.split('\t')
mySite = Site(str(l[0]),int(l[1]),str(l[2]))
myReads = Reads("","")
# pooled reads for first level filtering (global depth) and estimation of minor/major alleles and allele frequencies
individualDepth = np.zeros(NSAMS,float)
for n in range(NSAMS):
n=n+1
subReads= Reads(l[(n-1)*3+4],l[(n-1)*3+5])
individualDepth[n-1] = len(subReads.base)
myReads.base=str(myReads.base+subReads.base)
myReads.base_quality=str(myReads.base_quality+subReads.base_quality)
# convert to bases
[bases,indexDelN] = generics.convertSyms(myReads,mySite)
myReads=Reads(bases,myReads.base_quality)
if len(myReads.base)!=len(myReads.base_quality):
sys.exit("Conversion not succesful")
# filter by quality
[bases,qualities] = generics.filter(myReads,args.min_quality_score)
myReads=Reads(bases,qualities)
# find all indexes of occurances to be filtered out
index_of_X=[]
index=-1
while True:
index=myReads.base.find('X',index+1)
if index == -1:
break # all occurrences have been found
index_of_X.append(index)
myReads.base=myReads.base.replace('X','')
# remove all corresponding base qualities
count=0
for i in index_of_X:
myReads.base_quality = myReads.base_quality[:i-count] + myReads.base_quality[i+1-count:]
count+=1
globalDepth = len(myReads.base)
if ((globalDepth > args.min_global_depth) & (min(individualDepth)>args.min_ind_depth)):
total_bases+=globalDepth
no_bases+=1
# counts of non-major bases
nonMajorCount = generics.calcNonMajorCounts(myReads)
nonMajorProp = nonMajorCount/len(myReads.base)
# filter the site based on global depth
Set_min_prop = args.min_non_major_freq # minimum proportion of nonMajorCount
if nonMajorProp>Set_min_prop: # remove bases where more that 1-Set_min_prop are major allele i.e monomorphic bases
prob_of_ancestral_allelle_maj=1-nonMajorProp
haploid = generics.calcGenoLogLike1(myReads,mySite)
tri_ref = haploid[4] # retrieve reference value for if the base is not triallilic
haploid=haploid[:4] # remove reference value
# keep reference allele as one possible allele so always assume KeepRef=0
[major,minor,minor2,minor3] = [haploid.index(sorted(haploid,reverse=True)[0]),haploid.index(sorted(haploid,reverse=True)[1]),haploid.index(sorted(haploid,reverse=True)[2]),haploid.index(sorted(haploid,reverse=True)[3])]
# remove sites with >0.1 frequency of minor 2 or minor 3 allele to remove non biallilic sites (0.1 error built in for sequencing error)
minor2_prop=generics.calcAlleleFreq(minor2,myReads)/len(myReads.base) # Calculate allele frequencies of minor2&3 alleles
minor3_prop=generics.calcAlleleFreq(minor3,myReads)/len(myReads.base)
if(minor2_prop<args.max_minor2_freq and minor3_prop<args.max_minor3_freq):
P_bar=0
Q_bar=0
for read in range(len(myReads.base)):
if myReads.base[read]==alleles[major]:
P_bar+=(1-(10**((phredscale-ord(str(myReads.base_quality[read])))/10)))
elif myReads.base[read]==alleles[minor]:
Q_bar+=(1-(10**((phredscale-ord(str(myReads.base_quality[read])))/10)))
P = P_bar/(P_bar+Q_bar) # proportion of major allele weigted by read quality
Q = Q_bar/(P_bar+Q_bar) # proportion of minor allele weigted by read quality
base_number+=1 # count number of SNPs included in data
for n in range(NSAMS):
# retrieve bases for this particular sample
n=n+1
myReads = Reads(l[(n-1)*3+4],l[(n-1)*3+5])
(bases, indexDelN) = generics.convertSyms(myReads,mySite)
myReads = Reads(bases, myReads.base_quality)
# filter by quality
[bases,qualities] = generics.filter(myReads,args.min_quality_score)
myReads=Reads(bases,qualities)
# find all indexes of occurances to be filtered out
index_of_X=[]
index=-1
while True:
index=myReads.base.find('X',index+1)
if index == -1:
break # all occurrences have been found
index_of_X.append(index)
myReads.base=myReads.base.replace('X','')
# remove all corresponding base qualities
count=0
for i in index_of_X:
myReads.base_quality = myReads.base_quality[:i-count] + myReads.base_quality[i+1-count:]
count+=1
major_count=generics.calcAlleleFreq(major,myReads)
minor_count=generics.calcAlleleFreq(minor,myReads)
# take a sample of the bases so that the proportion of data used is as required
if downsampling<1:
data_prop = math.ceil(len(myReads.base)*downsampling) # calculate how many bases to include for proportion of sample
rand_samp = random.sample(range(0,len(myReads.base)),data_prop)
base = ""
qualities = ""
for r in rand_samp:
base+=myReads.base[r]
qualities+=myReads.base_quality[r]
myReads=Reads(base,qualities)
# find sample depth of filtered data
sampleDepth = len(myReads.base)
NUMSITES[0]+=sampleDepth # count the number of reads for each sample
NUMSITES[n]+=sampleDepth
sep="\t"
content=""
content=(mySite.chrom,str(mySite.position),str(n),mySite.reference,str(sampleDepth),alleles[major],alleles[minor],str(major_count),str(minor_count))
content=sep.join(content)
content+="\t"
for ip in ploidy:
Nploid = generics.calcGenoLogLikeN_MajorMinor(ip, myReads, mySite, major, minor)
content2 ="\t".join(map(str,Nploid))
content += content2
content += "\t"
content += "\n"
# write file of genotype likelihoods
with gzip.open(output,'at+') as f:
f.write(content)
# end likelihood calc
# end for sample
# end for if max minor
# end if not filtered for global depth
# end for line
elif fileType == 3: # mpileup.gz file
for g1 in list_of_inputs: # for every filename
directory = '/'.join(g1.split('/')[:-1])
if len(directory) == 0:
g = "./" + g1
g2 = g1[:-exl]
if outFolder==0:
output = "./" + g2 + ".genolikes.gz"
else:
output = outFolder+'/'.join(g2.split('/')[-1])+".genolikes.gz"
else:
g = g1
g2 = g1[:-exl]
if outFolder==0:
output = g2 + ".genolikes.gz"
else:
output = outFolder+"/"+g2+".genolikes.gz"
print("Output file is: " + output)
with gzip.open(g,'rb') as gz:# opens the mpilup.gz file. Use mpileup.read() to display content
first_line = gz.readline()
Data=first_line.decode().strip('\n') # Convert bytes into string
l = Data.split('\t')
NSAMS=int((len(l)-3)/3)
if args.Inbreeding:
inbreed = args.Inbreeding
else:
inbreed = "0x{}".format(str(NSAMS))
# parse inbreeding coeffients
F=[]
temp=inbreed.split(',')
for t in temp:
vals=t.split('x')
if len(vals)==2:
F+=list(np.repeat(float(vals[0]),int(vals[1])))
else:
F+=[vals[0]]
print(F)
downsampling=float(args.downsampling) # fraction of data to be used (0-1].
#Original_sample_number=NSAMS
win=50 # window size for calculating ploidy
phredscale=33
NUMSITES=np.zeros(NSAMS+1,int)
NUMSITES_HWE=np.zeros(NSAMS+1,int)
ExpectedPloidy=[[] for i in range(NSAMS+1)]
Overall_Prob=np.zeros((NSAMS+1,len(ploidy)),float) # (NSAMS+1)xploidies array for probabilities of each ploidy for each sample and overall ploidy probabilities
Overall_Prob_HWE=np.zeros((NSAMS+1,len(ploidy)),float)
delta_prob=np.zeros((NSAMS+1,len(ploidy)),float)
counts=np.zeros((NSAMS+1,len(ploidy)),float)+1 # counts of bases for each ploidy being most likely
base_number=0 # count for bases
list_of_window=[]
list_of_window2=[]
no_bases=0
total_bases=0
for line in gz:
Data=line.decode().strip('\n')# convert bytes into string
l = Data.split('\t')
mySite = Site(str(l[0]),int(l[1]),str(l[2]))
myReads = Reads("","")
# pooled reads for first level filtering (global depth) and estimation of minor/major alleles and allele frequencies
individualDepth = np.zeros(NSAMS,float)
for n in range(NSAMS):
n=n+1
subReads= Reads(l[(n-1)*3+4],l[(n-1)*3+5])
individualDepth[n-1] = len(subReads.base)
myReads.base=str(myReads.base+subReads.base)
myReads.base_quality=str(myReads.base_quality+subReads.base_quality)
# convert to bases
[bases,indexDelN] = generics.convertSyms(myReads,mySite)
myReads=Reads(bases,myReads.base_quality)
if len(myReads.base)!=len(myReads.base_quality):
sys.exit("Conversion not succesful")
# filter by quality
[bases,qualities] = generics.filter(myReads,args.min_quality_score)
myReads=Reads(bases,qualities)
# find all indexes of occurances to be filtered out
index_of_X=[]
index=-1
while True:
index=myReads.base.find('X',index+1)
if index == -1:
break # all occurrences have been found
index_of_X.append(index)
myReads.base=myReads.base.replace('X','')
# remove all corresponding base qualities
count=0
for i in index_of_X:
myReads.base_quality = myReads.base_quality[:i-count] + myReads.base_quality[i+1-count:]
count+=1
globalDepth = len(myReads.base)
if ((globalDepth > args.min_global_depth) & (min(individualDepth)>args.min_ind_depth)):
total_bases+=globalDepth
no_bases+=1
# counts of non-major bases
nonMajorCount = generics.calcNonMajorCounts(myReads)
nonMajorProp = nonMajorCount/len(myReads.base)
# filter the site based on global depth
Set_min_prop = args.min_non_major_freq # minimum proportion of nonMajorCount
if nonMajorProp>Set_min_prop: # remove bases where more that 1-Set_min_prop are major allele i.e monomorphic bases
prob_of_ancestral_allelle_maj=1-nonMajorProp
haploid = generics.calcGenoLogLike1(myReads,mySite)
tri_ref = haploid[4] # retrieve reference value for if the base is not triallilic
haploid=haploid[:4] # remove reference value
# keep reference allele as one possible allele so always assume KeepRef=0
[major,minor,minor2,minor3] = [haploid.index(sorted(haploid,reverse=True)[0]),haploid.index(sorted(haploid,reverse=True)[1]),haploid.index(sorted(haploid,reverse=True)[2]),haploid.index(sorted(haploid,reverse=True)[3])]
# remove sites with >0.1 frequency of minor 2 or minor 3 allele to remove non biallilic sites (0.1 error built in for sequencing error)
minor2_prop=generics.calcAlleleFreq(minor2,myReads)/len(myReads.base) # Calculate allele frequencies of minor2&3 alleles
minor3_prop=generics.calcAlleleFreq(minor3,myReads)/len(myReads.base)
if(minor2_prop<args.max_minor2_freq and minor3_prop<args.max_minor3_freq):
P_bar=0
Q_bar=0
for read in range(len(myReads.base)):
if myReads.base[read]==alleles[major]:
P_bar+=(1-(10**((phredscale-ord(str(myReads.base_quality[read])))/10)))
elif myReads.base[read]==alleles[minor]:
Q_bar+=(1-(10**((phredscale-ord(str(myReads.base_quality[read])))/10)))
P = P_bar/(P_bar+Q_bar) # proportion of major allele weigted by read quality
Q = Q_bar/(P_bar+Q_bar) # proportion of minor allele weigted by read quality
base_number+=1 # count number of SNPs included in data
for n in range(NSAMS):
# retrieve bases for this particular sample
n=n+1
myReads = Reads(l[(n-1)*3+4],l[(n-1)*3+5])
(bases, indexDelN) = generics.convertSyms(myReads,mySite)
myReads = Reads(bases, myReads.base_quality)
# filter by quality
[bases,qualities] = generics.filter(myReads,args.min_quality_score)
myReads=Reads(bases,qualities)
# find all indexes of occurances to be filtered out
index_of_X=[]
index=-1
while True:
index=myReads.base.find('X',index+1)
if index == -1:
break # all occurrences have been found
index_of_X.append(index)
myReads.base=myReads.base.replace('X','')
# remove all corresponding base qualities
count=0
for i in index_of_X:
myReads.base_quality = myReads.base_quality[:i-count] + myReads.base_quality[i+1-count:]
count+=1
major_count=generics.calcAlleleFreq(major,myReads)
minor_count=generics.calcAlleleFreq(minor,myReads)
# take a sample of the bases so that the proportion of data used is as required
if downsampling<1:
data_prop = math.ceil(len(myReads.base)*downsampling) # calculate how many bases to include for proportion of sample
rand_samp = random.sample(range(0,len(myReads.base)),data_prop)
base = ""
qualities = ""
for r in rand_samp:
base+=myReads.base[r]
qualities+=myReads.base_quality[r]
myReads=Reads(base,qualities)
# find sample depth of filtered data
sampleDepth = len(myReads.base)
NUMSITES[0]+=sampleDepth # count the number of reads for each sample
NUMSITES[n]+=sampleDepth
sep="\t"
content=""
content=(mySite.chrom,str(mySite.position),str(n),mySite.reference,str(sampleDepth),alleles[major],alleles[minor],str(major_count),str(minor_count))
content=sep.join(content)
content+="\t"
for ip in ploidy:
Nploid = generics.calcGenoLogLikeN_MajorMinor(ip, myReads, mySite, major, minor)
content2 ="\t".join(map(str,Nploid))
content += content2
content += "\t"
content += "\n"
# write file of genotype likelihoods
with gzip.open(output,'at+') as f:
f.write(content)
# end likelihood calc
# end for sample
# end for if max minor
# end if not filtered for global depth
# end for line