-
Notifications
You must be signed in to change notification settings - Fork 15
/
bam_consensus.py
executable file
·59 lines (46 loc) · 1.22 KB
/
bam_consensus.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
#!/usr/bin/python
import sys
import operator
print "bam_consensus.py VariationFile"
try:
varfile = sys.argv[1]
except:
varfile = raw_input("Introduce Variation File: ")
vardata = open(varfile).readlines()
header = vardata[0]
samples = header.split()
keys = {0:"N",1:"N",2:"A",3:"C",4:"T",5:"G"}
out = open("output.txt", "w")
genes_dict = {}
for line in vardata[2:]:
data = line.split()
gene = data[0]
if gene in genes_dict:
genes_dict[gene].append(line)
else:
genes_dict[gene] = [line]
for m in range(0,len(samples)):
begin = 2+(m*6)
end = 2+(m*6)+6
for gene in genes_dict:
seq = []
g_lines = genes_dict[gene]
for g_line in g_lines:
data = g_line.split()
nums = [int(x) for x in data[begin:end]]
nums_dict = {}
for n in range(0,6):
nums_dict[n] = nums[n]
sorted_nums = sorted(nums_dict.items(), key=operator.itemgetter(1))
nucleotide = sorted_nums[5][0]
abundance = sorted_nums[5][1]
if abundance == 0:
nucleotide = 0 # nucleotide is an "N"
base = keys[nucleotide]
seq.append(base)
out.write(">%s_%s\n" % (gene,samples[m]))
# for el in seq_dict:
# out.write(">%s_%s\n" % (samples[m],el))
out.write("".join(seq))
out.write("\n")
out.close()