comparison gene_identification.py @ 0:8a5a2abbb870 draft default tip

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