-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphase_sperm.py
More file actions
130 lines (120 loc) · 4.63 KB
/
phase_sperm.py
File metadata and controls
130 lines (120 loc) · 4.63 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
import sys
import copy
import pysam
import bisect
# parameters
minNumOfSperm = 2
numOfPhased = 5 # on each side to search
snpFold = 4 # >80% of nearby phased SNPs of each sperm must agree
spermFold = 3 # >75% of voted sperm must agree
# read IO locations from arguments
inputVcfFile = sys.argv[1]
inputDraftFile = open(sys.argv[2],"r")
chrName = sys.argv[3]
numOfSperm=99
childId=numOfSperm+1
fatherId=numOfSperm+0
motherId=numOfSperm+2
# load already phased SNPs
sys.stderr.write('reading phased SNPs\n')
phasedData = {}
for inputDraftLine in inputDraftFile:
inputDraftLineData = inputDraftLine.strip().split()
if inputDraftLineData[0][3:] != chrName: # assuming phased file has "chr1" style naming
continue
phasedData[int(inputDraftLineData[1])] = [inputDraftLineData[2],inputDraftLineData[3]]
# open VCF file
bcf_in = pysam.VariantFile(inputVcfFile)
vcfSamples = bcf_in.header.samples
childSample = vcfSamples[childId]
fatherSample = vcfSamples[fatherId]
motherSample = vcfSamples[motherId]
spermSamples = []
for spermId in range(numOfSperm):
spermSamples.append(vcfSamples[spermId])
# build draft haplotypes based on phased SNPs
sys.stderr.write('using phased SNPs to determine sperm haplotypes\n')
draftLoci = []
draftHaplotypes = []
for rec in bcf_in.fetch():
if rec.pos not in phasedData:
continue # only use phased SNPs
phasedNucleotides = phasedData[rec.pos]
phasedHaplotype = []
for spermSample in spermSamples:
spermHaplotype = -1 # unknown
if rec.samples[spermSample]["GT"][0] != None:
spermNucleotide = rec.alleles[rec.samples[spermSample]["GT"][0]]
if spermNucleotide == phasedNucleotides[0]:
spermHaplotype = 0
elif spermNucleotide == phasedNucleotides[1]:
spermHaplotype = 1
phasedHaplotype.append(spermHaplotype)
draftLoci.append(rec.pos)
draftHaplotypes.append(phasedHaplotype)
#print rec.pos, phasedHaplotype
draftLoci, draftHaplotypes = zip(*sorted(zip(draftLoci, draftHaplotypes)))
# start phasing
phasingCounter = 0
sys.stderr.write('using sperm haplotypes to phase unknown SNPs\n')
for rec in bcf_in.fetch():
childGenotype = rec.samples[childSample]["GT"]
#fatherGenotype = rec.samples[fatherSample]["GT"]
#motherGenotype = rec.samples[motherSample]["GT"]
vcfAlleles = rec.alleles
spermGenotypes = []
if childGenotype[0] == childGenotype[1]:
continue # only use het
if rec.pos in phasedData:
continue # only study unknown
leftGenotype = childGenotype[0]
rightGenotype = childGenotype[1]
spermVotes = [0, 0] # left=pat vs. left=mat
for spermId in range(numOfSperm):
spermGenotype = rec.samples[spermSamples[spermId]]["GT"][0]
if spermGenotype == leftGenotype:
spermSide = 0
elif spermGenotype == rightGenotype:
spermSide = 1
else:
continue
siteVotes = [0, 0]
posId = bisect.bisect_left(draftLoci, rec.pos)
counter = 0
for i in range(posId-1, 0, -1):
if draftHaplotypes[i][spermId] < 0:
continue
if draftHaplotypes[i][spermId] == spermSide:
siteVotes[0] += 1
else:
siteVotes[1] += 1
counter += 1
if counter >= numOfPhased:
break
counter = 0
for i in range(posId, len(draftLoci), 1):
if draftHaplotypes[i][spermId] < 0:
continue
if draftHaplotypes[i][spermId] == spermSide:
siteVotes[0] += 1
else:
siteVotes[1] += 1
counter += 1
if counter >= numOfPhased:
break
if siteVotes[0] > snpFold * siteVotes[1]: # > 80% of SNPs agree on a haplotype
spermVotes[0] += 1
elif siteVotes[1] > snpFold * siteVotes[0]:
spermVotes[1] += 1
if spermVotes[0] + spermVotes[1] < minNumOfSperm: # needs at least two sperms cover
continue
if spermVotes[0] > spermFold * spermVotes[1]: # needs > 3x majority vote
phasingCounter += 1
sys.stdout.write(chrName+'\t'+str(rec.pos)+'\t'+vcfAlleles[leftGenotype]+'\t'+vcfAlleles[rightGenotype]+'\n')
if phasingCounter % 1000 == 0:
sys.stderr.write('phased '+str(phasingCounter)+' SNPs\n')
elif spermVotes[1] > spermFold * spermVotes[0]: # needs > 3x majority vote
phasingCounter += 1
sys.stdout.write(chrName+'\t'+str(rec.pos)+'\t'+vcfAlleles[rightGenotype]+'\t'+vcfAlleles[leftGenotype]+'\n')
if phasingCounter % 1000 == 0:
sys.stderr.write('phased '+str(phasingCounter)+' SNPs\n')