annotate mutation_analysis.py @ 0:8a5a2abbb870 draft default tip

Uploaded
author davidvanzessen
date Mon, 29 Aug 2016 05:36:10 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
1 from __future__ import division
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
2 from collections import defaultdict
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
3 import re
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
4 import argparse
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
5
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
6 parser = argparse.ArgumentParser()
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
7 parser.add_argument("--input",
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
8 help="The '7_V-REGION-mutation-and-AA-change-table' and '10_V-REGION-mutation-hotspots' merged together, with an added 'best_match' annotation")
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
9 parser.add_argument("--genes", help="The genes available in the 'best_match' column")
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
10 parser.add_argument("--includefr1", help="Should the mutation/nucleotides in the FR1 region be included?")
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
11 parser.add_argument("--output", help="Output file")
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
12
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
13 args = parser.parse_args()
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
14
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
15 infile = args.input
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
16 genes = str(args.genes).split(",")
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
17 print "includefr1 =", args.includefr1
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
18 include_fr1 = True if args.includefr1 == "yes" else False
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
19 outfile = args.output
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
20
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
21 genedic = dict()
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
22
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
23 mutationdic = dict()
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
24 mutationMatcher = re.compile("^(.)(\d+).(.),?(.)?(\d+)?.?(.)?(.?.?.?.?.?)?")
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
25 NAMatchResult = (None, None, None, None, None, None, '')
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
26 linecount = 0
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
27
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
28 IDIndex = 0
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
29 best_matchIndex = 0
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
30 fr1Index = 0
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
31 cdr1Index = 0
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
32 fr2Index = 0
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
33 cdr2Index = 0
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
34 fr3Index = 0
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
35 first = True
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
36 IDlist = []
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
37 mutationList = []
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
38 mutationListByID = {}
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
39 cdr1LengthDic = {}
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
40 cdr2LengthDic = {}
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
41
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
42 with open(infile, 'r') as i:
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
43 for line in i:
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
44 if first:
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
45 linesplt = line.split("\t")
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
46 IDIndex = linesplt.index("Sequence.ID")
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
47 best_matchIndex = linesplt.index("best_match")
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
48 fr1Index = linesplt.index("FR1.IMGT")
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
49 cdr1Index = linesplt.index("CDR1.IMGT")
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
50 fr2Index = linesplt.index("FR2.IMGT")
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
51 cdr2Index = linesplt.index("CDR2.IMGT")
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
52 fr3Index = linesplt.index("FR3.IMGT")
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
53 cdr1LengthIndex = linesplt.index("CDR1.IMGT.length")
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
54 cdr2LengthIndex = linesplt.index("CDR2.IMGT.length")
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
55 first = False
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
56 continue
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
57 linecount += 1
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
58 linesplt = line.split("\t")
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
59 ID = linesplt[IDIndex]
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
60 genedic[ID] = linesplt[best_matchIndex]
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
61 try:
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
62 if linesplt[fr1Index] != "NA":
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
63 mutationdic[ID + "_FR1"] = [mutationMatcher.match(x).groups() for x in linesplt[fr1Index].split("|") if x] if include_fr1 else []
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
64 else:
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
65 mutationdic[ID + "_FR1"] = []
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
66 mutationdic[ID + "_CDR1"] = [mutationMatcher.match(x).groups() for x in linesplt[cdr1Index].split("|") if x] if linesplt[cdr1Index] != "NA" else []
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
67 mutationdic[ID + "_FR2"] = [mutationMatcher.match(x).groups() for x in linesplt[fr2Index].split("|") if x] if linesplt[fr2Index] != "NA" else []
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
68 mutationdic[ID + "_CDR2"] = [mutationMatcher.match(x).groups() for x in linesplt[cdr2Index].split("|") if x] if linesplt[cdr2Index] != "NA" else []
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
69 mutationdic[ID + "_FR2-CDR2"] = mutationdic[ID + "_FR2"] + mutationdic[ID + "_CDR2"]
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
70 mutationdic[ID + "_FR3"] = [mutationMatcher.match(x).groups() for x in linesplt[fr3Index].split("|") if x] if linesplt[fr3Index] != "NA" else []
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
71 except e:
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
72 print linesplt
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
73 print linecount
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
74 print e
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
75 mutationList += mutationdic[ID + "_FR1"] + mutationdic[ID + "_CDR1"] + mutationdic[ID + "_FR2"] + mutationdic[ID + "_CDR2"] + mutationdic[ID + "_FR3"]
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
76 mutationListByID[ID] = mutationdic[ID + "_FR1"] + mutationdic[ID + "_CDR1"] + mutationdic[ID + "_FR2"] + mutationdic[ID + "_CDR2"] + mutationdic[ID + "_FR3"]
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
77
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
78 cdr1Length = linesplt[cdr1LengthIndex]
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
79 cdr2Length = linesplt[cdr2LengthIndex]
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
80
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
81 cdr1LengthDic[ID] = int(cdr1Length) if cdr1Length != "X" else 0
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
82 cdr2LengthDic[ID] = int(cdr2Length) if cdr2Length != "X" else 0
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
83
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
84 IDlist += [ID]
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
85
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
86 AALength = (int(max(mutationList, key=lambda i: int(i[4]) if i[4] else 0)[4]) + 1) # [4] is the position of the AA mutation, None if silent
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
87 if AALength < 60:
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
88 AALength = 64
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
89
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
90 AA_mutation = [0] * AALength
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
91 AA_mutation_dic = {"ca": AA_mutation[:], "cg": AA_mutation[:], "cm": AA_mutation[:], "un": AA_mutation[:]}
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
92 AA_mutation_empty = AA_mutation[:]
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
93
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
94 aa_mutations_by_id_file = outfile[:outfile.rindex("/")] + "/aa_id_mutations.txt"
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
95 with open(aa_mutations_by_id_file, 'w') as o:
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
96 o.write("ID\tbest_match\t" + "\t".join([str(x) for x in range(1,AALength)]) + "\n")
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
97 for ID in mutationListByID.keys():
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
98 AA_mutation_for_ID = AA_mutation_empty[:]
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
99 for mutation in mutationListByID[ID]:
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
100 if mutation[4]:
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
101 AA_mutation_position = int(mutation[4])
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
102 AA_mutation[AA_mutation_position] += 1
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
103 AA_mutation_for_ID[AA_mutation_position] += 1
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
104 clss = genedic[ID][:2]
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
105 AA_mutation_dic[clss][AA_mutation_position] += 1
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
106 o.write(ID + "\t" + genedic[ID] + "\t" + "\t".join([str(x) for x in AA_mutation_for_ID[1:]]) + "\n")
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
107
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
108
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
109
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
110 #absent AA stuff
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
111 absentAACDR1Dic = defaultdict(list)
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
112 absentAACDR1Dic[5] = range(29,36)
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
113 absentAACDR1Dic[6] = range(29,35)
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
114 absentAACDR1Dic[7] = range(30,35)
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
115 absentAACDR1Dic[8] = range(30,34)
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
116 absentAACDR1Dic[9] = range(31,34)
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
117 absentAACDR1Dic[10] = range(31,33)
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
118 absentAACDR1Dic[11] = [32]
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
119
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
120 absentAACDR2Dic = defaultdict(list)
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
121 absentAACDR2Dic[0] = range(55,65)
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
122 absentAACDR2Dic[1] = range(56,65)
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
123 absentAACDR2Dic[2] = range(56,64)
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
124 absentAACDR2Dic[3] = range(57,64)
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
125 absentAACDR2Dic[4] = range(57,63)
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
126 absentAACDR2Dic[5] = range(58,63)
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
127 absentAACDR2Dic[6] = range(58,62)
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
128 absentAACDR2Dic[7] = range(59,62)
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
129 absentAACDR2Dic[8] = range(59,61)
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
130 absentAACDR2Dic[9] = [60]
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
131
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
132 absentAA = [len(IDlist)] * (AALength-1)
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
133 for k, cdr1Length in cdr1LengthDic.iteritems():
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
134 for c in absentAACDR1Dic[cdr1Length]:
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
135 absentAA[c] -= 1
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
136
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
137 for k, cdr2Length in cdr2LengthDic.iteritems():
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
138 for c in absentAACDR2Dic[cdr2Length]:
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
139 absentAA[c] -= 1
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
140
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
141
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
142 aa_mutations_by_id_file = outfile[:outfile.rindex("/")] + "/absent_aa_id.txt"
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
143 with open(aa_mutations_by_id_file, 'w') as o:
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
144 o.write("ID\tcdr1length\tcdr2length\tbest_match\t" + "\t".join([str(x) for x in range(1,AALength)]) + "\n")
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
145 for ID in IDlist:
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
146 absentAAbyID = [1] * (AALength-1)
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
147 cdr1Length = cdr1LengthDic[ID]
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
148 for c in absentAACDR1Dic[cdr1Length]:
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
149 absentAAbyID[c] -= 1
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
150
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
151 cdr2Length = cdr2LengthDic[ID]
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
152 for c in absentAACDR2Dic[cdr2Length]:
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
153 absentAAbyID[c] -= 1
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
154 o.write(ID + "\t" + str(cdr1Length) + "\t" + str(cdr2Length) + "\t" + genedic[ID] + "\t" + "\t".join([str(x) for x in absentAAbyID]) + "\n")
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
155
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
156 if linecount == 0:
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
157 print "No data, exiting"
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
158 with open(outfile, 'w') as o:
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
159 o.write("RGYW (%)," + ("0,0,0\n" * len(genes)))
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
160 o.write("WRCY (%)," + ("0,0,0\n" * len(genes)))
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
161 o.write("WA (%)," + ("0,0,0\n" * len(genes)))
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
162 o.write("TW (%)," + ("0,0,0\n" * len(genes)))
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
163 import sys
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
164
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
165 sys.exit()
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
166
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
167 hotspotMatcher = re.compile("[actg]+,(\d+)-(\d+)\((.*)\)")
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
168 RGYWCount = {}
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
169 WRCYCount = {}
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
170 WACount = {}
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
171 TWCount = {}
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
172
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
173 #IDIndex = 0
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
174 ataIndex = 0
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
175 tatIndex = 0
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
176 aggctatIndex = 0
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
177 atagcctIndex = 0
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
178 first = True
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
179 with open(infile, 'r') as i:
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
180 for line in i:
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
181 if first:
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
182 linesplt = line.split("\t")
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
183 ataIndex = linesplt.index("X.a.t.a")
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
184 tatIndex = linesplt.index("t.a.t.")
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
185 aggctatIndex = linesplt.index("X.a.g.g.c.t..a.t.")
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
186 atagcctIndex = linesplt.index("X.a.t..a.g.c.c.t.")
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
187 first = False
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
188 continue
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
189 linesplt = line.split("\t")
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
190 gene = linesplt[best_matchIndex]
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
191 ID = linesplt[IDIndex]
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
192 if ID == "ca2":
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
193 print linesplt
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
194 RGYW = [(int(x), int(y), z) for (x, y, z) in
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
195 [hotspotMatcher.match(x).groups() for x in linesplt[aggctatIndex].split("|") if x]]
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
196 WRCY = [(int(x), int(y), z) for (x, y, z) in
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
197 [hotspotMatcher.match(x).groups() for x in linesplt[atagcctIndex].split("|") if x]]
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
198 WA = [(int(x), int(y), z) for (x, y, z) in
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
199 [hotspotMatcher.match(x).groups() for x in linesplt[ataIndex].split("|") if x]]
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
200 TW = [(int(x), int(y), z) for (x, y, z) in
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
201 [hotspotMatcher.match(x).groups() for x in linesplt[tatIndex].split("|") if x]]
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
202 RGYWCount[ID], WRCYCount[ID], WACount[ID], TWCount[ID] = 0, 0, 0, 0
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
203
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
204 mutationList = (mutationdic[ID + "_FR1"] if include_fr1 else []) + mutationdic[ID + "_CDR1"] + mutationdic[
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
205 ID + "_FR2"] + mutationdic[ID + "_CDR2"] + mutationdic[ID + "_FR3"]
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
206 for mutation in mutationList:
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
207 frm, where, to, AAfrm, AAwhere, AAto, junk = mutation
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
208 mutation_in_RGYW = any([(start <= int(where) <= end) for (start, end, region) in RGYW])
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
209 mutation_in_WRCY = any([(start <= int(where) <= end) for (start, end, region) in WRCY])
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
210 mutation_in_WA = any([(start <= int(where) <= end) for (start, end, region) in WA])
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
211 mutation_in_TW = any([(start <= int(where) <= end) for (start, end, region) in TW])
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
212
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
213 in_how_many_motifs = sum([mutation_in_RGYW, mutation_in_WRCY, mutation_in_WA, mutation_in_TW])
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
214
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
215 if in_how_many_motifs > 0:
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
216 RGYWCount[ID] += (1.0 * int(mutation_in_RGYW)) / in_how_many_motifs
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
217 WRCYCount[ID] += (1.0 * int(mutation_in_WRCY)) / in_how_many_motifs
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
218 WACount[ID] += (1.0 * int(mutation_in_WA)) / in_how_many_motifs
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
219 TWCount[ID] += (1.0 * int(mutation_in_TW)) / in_how_many_motifs
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
220
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
221
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
222 def mean(lst):
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
223 return (float(sum(lst)) / len(lst)) if len(lst) > 0 else 0.0
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
224
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
225
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
226 def median(lst):
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
227 lst = sorted(lst)
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
228 l = len(lst)
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
229 if l == 0:
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
230 return 0
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
231 if l == 1:
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
232 return lst[0]
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
233
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
234 l = int(l / 2)
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
235
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
236 if len(lst) % 2 == 0:
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
237 return float(lst[l] + lst[(l - 1)]) / 2.0
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
238 else:
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
239 return lst[l]
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
240
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
241 funcs = {"mean": mean, "median": median, "sum": sum}
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
242
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
243 directory = outfile[:outfile.rfind("/") + 1]
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
244 value = 0
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
245 valuedic = dict()
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
246
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
247 for fname in funcs.keys():
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
248 for gene in genes:
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
249 with open(directory + gene + "_" + fname + "_value.txt", 'r') as v:
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
250 valuedic[gene + "_" + fname] = float(v.readlines()[0].rstrip())
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
251 with open(directory + "all_" + fname + "_value.txt", 'r') as v:
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
252 valuedic["total_" + fname] = float(v.readlines()[0].rstrip())
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
253
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
254
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
255 def get_xyz(lst, gene, f, fname):
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
256 x = int(round(f(lst)))
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
257 y = valuedic[gene + "_" + fname]
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
258 z = str(round(x / float(y) * 100, 1)) if y != 0 else "0"
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
259 return (str(x), str(y), z)
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
260
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
261 dic = {"RGYW": RGYWCount, "WRCY": WRCYCount, "WA": WACount, "TW": TWCount}
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
262 arr = ["RGYW", "WRCY", "WA", "TW"]
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
263
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
264 geneMatchers = {gene: re.compile("^" + gene + ".*") for gene in genes}
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
265
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
266 for fname in funcs.keys():
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
267 func = funcs[fname]
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
268 foutfile = outfile[:outfile.rindex("/")] + "/hotspot_analysis_" + fname + ".txt"
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
269 with open(foutfile, 'w') as o:
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
270 for typ in arr:
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
271 o.write(typ + " (%)")
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
272 curr = dic[typ]
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
273 for gene in genes:
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
274 geneMatcher = geneMatchers[gene] #re.compile("^" + gene + ".*") #recompile every loop....
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
275 if valuedic[gene + "_" + fname] is 0:
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
276 o.write(",0,0,0")
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
277 else:
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
278 x, y, z = get_xyz([curr[x] for x in [y for y, z in genedic.iteritems() if geneMatcher.match(z)]], gene, func, fname)
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
279 o.write("," + x + "," + y + "," + z)
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
280
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
281 x, y, z = get_xyz([y for x, y in curr.iteritems() if not genedic[x].startswith("unmatched")], "total", func, fname)
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
282 o.write("," + x + "," + y + "," + z + "\n")
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
283
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
284
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
285 # for testing
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
286 seq_motif_file = outfile[:outfile.rindex("/")] + "/motif_per_seq.txt"
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
287 with open(seq_motif_file, 'w') as o:
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
288 o.write("ID\tRGYWC\tWRCY\tWA\tTW\n")
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
289 for ID in IDlist:
8a5a2abbb870 Uploaded
davidvanzessen
parents:
diff changeset
290 o.write(ID + "\t" + str(round(RGYWCount[ID], 2)) + "\t" + str(round(WRCYCount[ID], 2)) + "\t" + str(round(WACount[ID], 2)) + "\t" + str(round(TWCount[ID], 2)) + "\n")