annotate shm_csr.py @ 41:b8ac74723ab0 draft

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