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