0
|
1 import re
|
|
2 import argparse
|
2
|
3 import time
|
|
4 starttime= int(time.time() * 1000)
|
0
|
5
|
|
6 parser = argparse.ArgumentParser()
|
|
7 parser.add_argument("--input", help="The 1_Summary file from an IMGT zip file")
|
4
|
8 parser.add_argument("--output", help="The annotated summary output file")
|
0
|
9
|
|
10 args = parser.parse_args()
|
|
11
|
|
12 infile = args.input
|
|
13 #infile = "test_VH-Ca_Cg_25nt/1_Summary_test_VH-Ca_Cg_25nt_241013.txt"
|
4
|
14 output = args.output
|
0
|
15 #outfile = "identified.txt"
|
|
16
|
|
17 dic = dict()
|
|
18 total = 0
|
|
19
|
4
|
20
|
0
|
21 first = True
|
4
|
22 IDIndex = 0
|
|
23 seqIndex = 0
|
|
24
|
0
|
25 with open(infile, 'r') as f: #read all sequences into a dictionary as key = ID, value = sequence
|
4
|
26 for line in f:
|
|
27 total += 1
|
|
28 if first:
|
|
29 linesplt = line.split("\t")
|
|
30 IDIndex = linesplt.index("Sequence ID")
|
|
31 seqIndex = linesplt.index("Sequence")
|
|
32 first = False
|
|
33 continue
|
|
34 linesplt = line.split("\t")
|
|
35 ID = linesplt[IDIndex]
|
|
36 if len(linesplt) < 28: #weird rows without a sequence
|
|
37 dic[ID] = ""
|
|
38 else:
|
|
39 dic[ID] = linesplt[seqIndex]
|
0
|
40
|
|
41 #lambda/kappa reference sequence
|
|
42 searchstrings = {"ca": "catccccgaccagccccaaggtcttcccgctgagcctctgcagcacccagccagatgggaacgtggtcatcgcctgcctggtccagggcttcttcccccaggagccactcagtgtgacctggagcgaaag",
|
|
43 "cg": "ctccaccaagggcccatcggtcttccccctggcaccctcctccaagagcacctctgggggcacagcggccctgggctgcctggtcaaggactacttccccgaaccggtgacggtgtcgtggaactcaggcgccctgaccagcggcgtgcacaccttcc",
|
|
44 "cm": "gggagtgcatccgccccaacccttttccccctcgtctcctgtgagaattccc"}
|
|
45
|
|
46 compiledregex = {"ca": [],
|
|
47 "cg": [],
|
|
48 "cm": []}
|
|
49
|
|
50 #lambda/kappa reference sequence variable nucleotides
|
|
51 ca1 = {38: 't', 39: 'g', 48: 'a', 49: 'g', 51: 'c', 68: 'a', 73: 'c'}
|
|
52 ca2 = {38: 'g', 39: 'a', 48: 'c', 49: 'c', 51: 'a', 68: 'g', 73: 'a'}
|
|
53 cg1 = {0: 'c', 33: 'a', 38: 'c', 44: 'a', 54: 't', 56: 'g', 58: 'g', 66: 'g', 132: 'c'}
|
|
54 cg2 = {0: 'c', 33: 'g', 38: 'g', 44: 'g', 54: 'c', 56: 'a', 58: 'a', 66: 'g', 132: 't'}
|
|
55 cg3 = {0: 't', 33: 'g', 38: 'g', 44: 'g', 54: 't', 56: 'g', 58: 'g', 66: 'g', 132: 'c'}
|
|
56 cg4 = {0: 't', 33: 'g', 38: 'g', 44: 'g', 54: 'c', 56: 'a', 58: 'a', 66: 'c', 132: 'c'}
|
|
57
|
|
58 #reference sequences are cut into smaller parts of 'chunklength' length, and with 'chunklength' / 2 overlap
|
|
59 chunklength = 8
|
|
60
|
|
61 #create the chunks of the reference sequence with regular expressions for the variable nucleotides
|
|
62 for i in range(0, len(searchstrings["ca"]) - chunklength, chunklength / 2):
|
|
63 pos = i
|
|
64 chunk = searchstrings["ca"][i:i+chunklength]
|
|
65 result = ""
|
|
66 varsInResult = 0
|
|
67 for c in chunk:
|
|
68 if pos in ca1.keys():
|
|
69 varsInResult += 1
|
|
70 result += "[" + ca1[pos] + ca2[pos] + "]"
|
|
71 else:
|
|
72 result += c
|
|
73 pos += 1
|
|
74 compiledregex["ca"].append((re.compile(result), varsInResult))
|
|
75
|
|
76 for i in range(0, len(searchstrings["cg"]) - chunklength, chunklength / 2):
|
|
77 pos = i
|
|
78 chunk = searchstrings["cg"][i:i+chunklength]
|
|
79 result = ""
|
|
80 varsInResult = 0
|
|
81 for c in chunk:
|
|
82 if pos in cg1.keys():
|
|
83 varsInResult += 1
|
|
84 result += "[" + "".join(set([cg1[pos], cg2[pos], cg3[pos], cg4[pos]])) + "]"
|
|
85 else:
|
|
86 result += c
|
|
87 pos += 1
|
|
88 compiledregex["cg"].append((re.compile(result), varsInResult))
|
|
89
|
|
90 for i in range(0, len(searchstrings["cm"]) - chunklength, chunklength / 2):
|
|
91 compiledregex["cm"].append((re.compile(searchstrings["cm"][i:i+chunklength]), False))
|
|
92
|
|
93
|
|
94
|
|
95 def removeAndReturnMaxIndex(x): #simplifies a list comprehension
|
|
96 m = max(x)
|
|
97 index = x.index(m)
|
|
98 x[index] = 0
|
|
99 return index
|
|
100
|
|
101
|
|
102 start_location = dict()
|
|
103 hits = dict()
|
|
104 alltotal = 0
|
|
105 for key in compiledregex.keys(): #for ca/cg/cm
|
|
106 regularexpressions = compiledregex[key] #get the compiled regular expressions
|
|
107 for ID in dic.keys()[0:]: #for every ID
|
|
108 if ID not in hits.keys(): #ensure that the dictionairy that keeps track of the hits for every gene exists
|
|
109 hits[ID] = {"ca_hits": 0, "cg_hits": 0, "cm_hits": 0, "ca1": 0, "ca2": 0, "cg1": 0, "cg2": 0, "cg3": 0, "cg4": 0}
|
|
110 currentIDHits = hits[ID]
|
|
111 seq = dic[ID]
|
|
112 lastindex = 0
|
|
113 start = [0] * len(seq)
|
|
114 for i, regexp in enumerate(regularexpressions): #for every regular expression
|
5
|
115 relativeStartLocation = lastindex - (chunklength / 2) * i
|
|
116 if relativeStartLocation < 0 or relativeStartLocation >= len(seq):
|
|
117 break
|
0
|
118 regex, hasVar = regexp
|
|
119 matches = regex.finditer(seq[lastindex:])
|
|
120 for match in matches: #for every match with the current regex, only uses the first hit
|
|
121 lastindex += match.start()
|
5
|
122 print ID
|
|
123 print lastindex
|
|
124 print chunklength
|
|
125 print i
|
|
126 print seq[lastindex:]
|
|
127 print start
|
|
128 print len(seq)
|
|
129 print relativeStartLocation
|
|
130 print "-------------------"
|
|
131 start[relativeStartLocation] += 1
|
0
|
132 if hasVar: #if the regex has a variable nt in it
|
|
133 chunkstart = chunklength / 2 * i #where in the reference does this chunk start
|
|
134 chunkend = chunklength / 2 * i + chunklength #where in the reference does this chunk end
|
|
135 if key == "ca": #just calculate the variable nt score for 'ca', cheaper
|
|
136 currentIDHits["ca1"] += len([1 for x in ca1 if chunkstart <= x < chunkend and ca1[x] == seq[lastindex + x - chunkstart]])
|
|
137 currentIDHits["ca2"] += len([1 for x in ca2 if chunkstart <= x < chunkend and ca2[x] == seq[lastindex + x - chunkstart]])
|
|
138 elif key == "cg": #just calculate the variable nt score for 'cg', cheaper
|
|
139 currentIDHits["cg1"] += len([1 for x in cg1 if chunkstart <= x < chunkend and cg1[x] == seq[lastindex + x - chunkstart]])
|
|
140 currentIDHits["cg2"] += len([1 for x in cg2 if chunkstart <= x < chunkend and cg2[x] == seq[lastindex + x - chunkstart]])
|
|
141 currentIDHits["cg3"] += len([1 for x in cg3 if chunkstart <= x < chunkend and cg3[x] == seq[lastindex + x - chunkstart]])
|
|
142 currentIDHits["cg4"] += len([1 for x in cg4 if chunkstart <= x < chunkend and cg4[x] == seq[lastindex + x - chunkstart]])
|
|
143 else: #key == "cm" #no variable regions in 'cm'
|
|
144 pass
|
|
145 break #this only breaks when there was a match with the regex, breaking means the 'else:' clause is skipped
|
|
146 else: #only runs if there were no hits
|
|
147 continue
|
|
148 #print "found ", regex.pattern , "at", lastindex, "adding one to", (lastindex - chunklength / 2 * i), "to the start array of", ID, "gene", key, "it's now:", start[lastindex - chunklength / 2 * i]
|
|
149 currentIDHits[key + "_hits"] += 1
|
4
|
150 start_location[ID + "_" + key] = str([(removeAndReturnMaxIndex(start) + 1) for x in range(5) if len(start) > 0 and max(start) > 1])
|
0
|
151 #start_location[ID + "_" + key] = str(start.index(max(start)))
|
|
152
|
|
153
|
|
154 chunksInCA = len(compiledregex["ca"])
|
|
155 chunksInCG = len(compiledregex["cg"])
|
|
156 chunksInCM = len(compiledregex["cm"])
|
|
157 requiredChunkPercentage = 0.7
|
|
158 varsInCA = float(len(ca1.keys()) * 2)
|
|
159 varsInCG = float(len(cg1.keys()) * 2) + 1
|
|
160 varsInCM = 0
|
|
161 requiredVarPercentage = 0.7
|
|
162
|
4
|
163
|
|
164 first = True
|
|
165 with open(infile, 'r') as f: #read all sequences into a dictionary as key = ID, value = sequence
|
|
166 with open(output, 'w') as o:
|
|
167 for line in f:
|
|
168 total += 1
|
|
169 if first:
|
|
170 o.write(line.rstrip() + "\tbest_match\tnt_hit_percentage\tchunk_hit_percentage\tstart_locations\n")
|
|
171 first = False
|
0
|
172 continue
|
4
|
173 linesplt = line.split("\t")
|
|
174 if linesplt[2] == "No results":
|
|
175 pass
|
|
176 ID = linesplt[1]
|
|
177 currentIDHits = hits[ID]
|
|
178 possibleca = float(len(compiledregex["ca"]))
|
|
179 possiblecg = float(len(compiledregex["cg"]))
|
|
180 possiblecm = float(len(compiledregex["cm"]))
|
|
181 cahits = currentIDHits["ca_hits"]
|
|
182 cghits = currentIDHits["cg_hits"]
|
|
183 cmhits = currentIDHits["cm_hits"]
|
|
184 if cahits > cghits and cahits > cmhits: #its a ca gene
|
|
185 ca1hits = currentIDHits["ca1"]
|
|
186 ca2hits = currentIDHits["ca2"]
|
|
187 if ca1hits > ca2hits:
|
|
188 o.write(line.rstrip() + "\tca1\t" + str(int(ca1hits / varsInCA * 100)) + "\t" + str(int(cahits / possibleca * 100)) + "\t" + start_location[ID + "_ca"] + "\n")
|
|
189 else:
|
|
190 o.write(line.rstrip() + "\tca2\t" + str(int(ca2hits / varsInCA * 100)) + "\t" + str(int(cahits / possibleca * 100)) + "\t" + start_location[ID + "_ca"] + "\n")
|
|
191 elif cghits > cahits and cghits > cmhits: #its a cg gene
|
|
192 cg1hits = currentIDHits["cg1"]
|
|
193 cg2hits = currentIDHits["cg2"]
|
|
194 cg3hits = currentIDHits["cg3"]
|
|
195 cg4hits = currentIDHits["cg4"]
|
|
196 if cg1hits > cg2hits and cg1hits > cg3hits and cg1hits > cg4hits: #cg1 gene
|
|
197 o.write(line.rstrip() + "\tcg1\t" + str(int(cg1hits / varsInCG * 100)) + "\t" + str(int(cghits / possiblecg * 100)) + "\t" + start_location[ID + "_cg"] + "\n")
|
|
198 elif cg2hits > cg1hits and cg2hits > cg3hits and cg2hits > cg4hits: #cg2 gene
|
|
199 o.write(line.rstrip() + "\tcg2\t" + str(int(cg2hits / varsInCG * 100)) + "\t" + str(int(cghits / possiblecg * 100)) + "\t" + start_location[ID + "_cg"] + "\n")
|
|
200 elif cg3hits > cg1hits and cg3hits > cg2hits and cg3hits > cg4hits: #cg3 gene
|
|
201 o.write(line.rstrip() + "\tcg3\t" + str(int(cg3hits / varsInCG * 100)) + "\t" + str(int(cghits / possiblecg * 100)) + "\t" + start_location[ID + "_cg"] + "\n")
|
|
202 else: #cg4 gene
|
|
203 o.write(line.rstrip() + "\tcg3\t" + str(int(cg4hits / varsInCG * 100)) + "\t" + str(int(cghits / possiblecg * 100)) + "\t" + start_location[ID + "_cg"] + "\n")
|
|
204 else: #its a cm gene
|
|
205 o.write(line.rstrip() + "\tcm\t0\t" + str(int(cmhits / possiblecm * 100)) + "\t" + start_location[ID + "_cg"] + "\n")
|
0
|
206
|
2
|
207 print "Time: %i" % (int(time.time() * 1000) - starttime)
|
0
|
208
|
|
209
|
|
210
|
|
211
|
|
212
|
|
213
|
|
214
|