-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvariantQual.py
executable file
·184 lines (170 loc) · 6.83 KB
/
variantQual.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
#!/usr/bin/env python
import sys
def usage():
if len(sys.argv) < 2:
print("Usage: <program> <inputFile> ")
sys.exit(0)
def make_geneDict():
geneDict = {}
with open("/home/mrood/scripts/importantFiles/PerGeneSummary.txt", 'r') as dat:
for line in dat:
line = line.strip().split('\t')
start = int(line[4])
stop = int(line[5])
gene = line[0]
geneDict[gene] = [start,stop,line[1],line[7]]
return geneDict
def make_sampList(inputFile):
"""From an input file extract all the samples to be processed"""
samples = []
with open(inputFile, 'r') as inFile:
for i, line in enumerate(inFile):
if i > 0:
line = line.strip().split('\t')
samples.append(line[0])
print("{0} samples to be processed.".format(len(samples)))
return samples
def make_snpList(samples):
"""Make a set of all SNPs called in the subsampled pileups for a given sample and write them to file"""
for samp in samples:
outFileName = samp + '.sub.snps'
snps = set()
for i in range(1,10):
snpFileName = samp + '_mq20_rand{i}.bygene.snps'.format(i=i)
#print("Adding snps from {0}".format(snpFileName))
with open(snpFileName, 'r') as snpFile:
for i, line in enumerate(snpFile):
line = line.strip().split('\t')
if len(line) > 1:
try:
pos = int(line[1])
#print pos
snps.add(pos)
except ValueError:
print("ValueError encountered in {0}".format(snpFileName))
print i
with open(outFileName, 'w') as outFile:
for var in snps:
outFile.write(str(var) + '\n')
def extract_pileup(samp):
snps = set()
snpFileName = samp + '.sub.snps'
print snpFileName
with open(snpFileName, 'r') as snpFile:
for line in snpFile:
line = line.strip()
snps.add(int(line))
print("There are {0} snps for {1}".format(len(snps),snpFileName))
pileupFileName = '/home/mrood/WH-BH/rawData/' + samp + '_Q20_filtered.pileup'
outFileName = samp + '_sub_snps.pileup'
print('Reading {0}'.format(pileupFileName))
with open(pileupFileName, 'r') as pileupFile, open(outFileName, 'w') as outFile:
for line in pileupFile:
line = line.strip().split('\t')
pos = int(line[1])
if pos in snps:
outFile.write("\t".join(line) + '\n')
def extract_baseq(samp):
snps = set()
snpFileName = samp + '.sub.snps'
print snpFileName
with open(snpFileName, 'r') as snpFile:
for line in snpFile:
line = line.strip()
snps.add(int(line))
print("There are {0} snps for {1}".format(len(snps),snpFileName))
baseqFileName = samp + '.baseq.ext'
outFileName = samp + 'baseq.snps'
print('Reading {0}'.format(baseqFileName))
with open(baseqFileName, 'r') as baseqFile, open(outFileName, 'w') as outFile:
next(baseqFile)
for line in baseqFile:
line = line.strip().split('\t')
pos = int(line[1])
if pos in snps:
outFile.write("\t".join(line) + '\n')
def make_catDict():
catDict = {}
with open("/home/mrood/scripts/importantFiles/TBGeneSet.txt", 'r') as catFile:
for line in catFile:
line = line.strip().split('\t')
cat = line[0]
catDict[cat] = [i.replace("c","") for i in line[2].split()]
return catDict
def annotate_snp(samp, geneDict, catDict):
inFile = samp + 'baseq.snps'
snpDict = {}
with open(inFile, 'r') as infile:
for line in infile:
line = line.strip().split('\t')
snp = int(line[1])
maf = float(line[7])/float(line[3])
snpDict[snp] = [line, maf, [], []]
for gene in geneDict:
if snp >= geneDict[gene][0] and snp <= geneDict[gene][1]:
snpDict[snp][2].append(gene)
outFile = samp + 'baseq.snps.gene'
for snp in snpDict:
if len(snpDict[snp][2]) < 1:
snpDict[snp][3].append(["yes"] + ["no"]*32) #yes for 'all'
else:
for i, geneX in enumerate(snpDict[snp][2]):
gene = geneX.replace("c", "")
if i == 0:
catList = ["yes"] #yes for 'all'
if i > 0:
catList = ["no"] #only count each snp once for 'all'
for cat in sorted(catDict, key=catDict.get):
if gene in catDict[cat]:
catList.append("yes")
else:
catList.append("no")
snpDict[snp][3].append(catList)
outFile = samp + 'baseq.snps.ann'
with open(outFile, 'w') as outfile:
outfile.write('chrom\tpos\tref\treads_all\treads_pp\t\
matches\tmatches_pp\tmismatches\tmismatches_pp\t\
rms_baseq\trms_baseq_pp\trms_baseq_matches\trms_baseq_matches_pp\t\
rms_baseq_mismatches\trms_baseq_mismatches_pp\talt_allele_freq\t\
gene\tall\tTL.info\tTraSH.invitro\tTraSH.noness\tTL.CHP\t\
TL.cell\tTL.inter\tTL.reg\tTL.vir\tTL.LIP\tTraSH.invivo\t\
COG.Q\tTIM\tCOG.S\tCOG.O\tCOG.K\tCOG.F\tCOG.L\tCOG.V\tCOG.I\t\
COG.R\tCOG.J\tCOG.G\tCOG.P\tCOG.C\tCOG.M\tCOG.T\tCOG.E\tCOG.A\t\
COG.H\tCOG.D\tCOG.U\tCOG.N\n') #header
for snp in snpDict:
for i, gene in enumerate(snpDict[snp][2]):
outfile.write('%s\t%f\t%s\t%s\n' %
("\t".join(snpDict[snp][0]),
snpDict[snp][1],
snpDict[snp][2][i],
"\t".join(snpDict[snp][3][i]))
)
return snpDict
def make_d():
d = {}
with open("/home/mrood/WH-BH/data/mq20_masterEdit.txt", 'r')\
as infile:
header = []
for i, line in enumerate(infile):
line = line.strip()
if i == 0:
linefield = line.split('\t')
for variable in linefield:
d[variable] = {}
header.append(variable)
else:
linefield = line.split('\t')
for pos, item in enumerate(linefield):
d[header[pos]][linefield[0]] = linefield[pos]
return d
def add_TD(samples, d):
for samp in samples:
inFile = samp + 'baseq.snps.ann'
outFile = inFile + '.td'
with open(inFile, 'r') as infile, open(outFile, 'w') as outfile:
for i, line in enumerate(infile):
line = line.strip().split()
if i == 0:
outfile.write("\t".join(line) + '\t' + "TD" + '\n')
else:
outfile.write("\t".join(line) + '\t' + str(d[samp+'.tajD'][line[16]]) + '\n')