annotate trams.py @ 4:bd5692103d5b draft

Uploaded
author rreumerman
date Fri, 05 Apr 2013 05:00:40 -0400
parents
children 8de0ffc2166f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
1 #################
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
2 transl_table = 11
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
3 intro_message = ''' +------------------------------------------------------------------+
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
4 | Tool for Rapid Annotation of Microbial SNPs (TRAMS): a simple |
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
5 | program for rapid annotation of genomic variation in prokaryotes |
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
6 | |
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
7 | Developed by: Richard A. Reumerman, Paul R. Herron, |
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
8 | Paul A. Hoskisson and Vartul Sangal |
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
9 +------------------------------------------------------------------+\n'''
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
10 #################
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
11
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
12 import sys
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
13 import time
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
14 start = time.clock()
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
15
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
16 # Command line files: SNP REF REF-TYPE ANNOT OVERL SUM;
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
17 if len(sys.argv) < 7:
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
18 exit("\nNot enough arguments given.\nUsage: TRAMS_Galaxy.py [SNP.] [REF.] [ANNOT.] [OVERL.] [SUM.]")
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
19 try:
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
20 file_snps = open(sys.argv[1], "rU")
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
21 except IOError as e:
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
22 exit("Error trying to open '"+sys.argv[1]+"': {1}".format(e.errno, e.strerror))
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
23 try:
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
24 file_ref = open(sys.argv[2], "rU")
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
25 except IOError as e:
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
26 exit("Error trying to open '"+sys.argv[2]+"': {1}".format(e.errno, e.strerror))
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
27
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
28 filetype_reference = sys.argv[3]
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
29
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
30 try:
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
31 file_out = open(sys.argv[4], "w")
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
32 except IOError as e:
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
33 exit("Error trying to open '"+sys.argv[4]+"': {1}".format(e.errno, e.strerror))
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
34 try:
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
35 file_overlap = open(sys.argv[5], "w")
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
36 except IOError as e:
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
37 exit("Error trying to open '"+sys.argv[5]+"': {1}".format(e.errno, e.strerror))
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
38 try:
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
39 file_summary = open(sys.argv[6], "w")
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
40 except IOError as e:
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
41 exit("Error trying to open '"+sys.argv[6]+"': {1}".format(e.errno, e.strerror))
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
42
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
43 import Bio
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
44 from Bio import SeqIO, SeqFeature
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
45 from Bio.SeqRecord import SeqRecord
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
46 from Bio.Seq import Seq
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
47 from Bio.Alphabet import generic_dna, IUPAC
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
48 from Bio.Data import CodonTable
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
49
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
50 modules_loaded = time.clock()
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
51
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
52 def non_coding_calc(gene, pos = 0):
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
53 '''This function takes a pseudogene and returns the number of bases
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
54 located in between the sub-features before 'pos'. Returns 0 if 'pseudo' = False.
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
55 Input: {start, subfeats, pseudo}, pos (default = 0)'''
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
56 if not gene['pseudo']: return 0
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
57
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
58 non_coding_bases = 0
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
59 prev_subfeat_end = gene['start']
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
60 if gene['strand'] == -1:
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
61 for subfeature in gene['subfeats']:
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
62 if subfeature.location._start.position < pos:
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
63 prev_subfeat_end = subfeature.location._end.position
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
64 continue
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
65 non_coding_bases += (subfeature.location._start.position - prev_subfeat_end)
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
66 prev_subfeat_end = subfeature.location._end.position
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
67 else:
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
68 for subfeature in gene['subfeats']:
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
69 non_coding_bases += (subfeature.location._start.position - prev_subfeat_end)
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
70 prev_subfeat_end = subfeature.location._end.position
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
71 if prev_subfeat_end >= pos and pos != 0: break
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
72
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
73 return non_coding_bases
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
74
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
75
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
76 def region_calc(bounds,length):
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
77 regions = []
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
78 lastend=i=0
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
79 while i < len(bounds):
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
80 if bounds[i]['start'] > lastend:# Intergenic region present;
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
81 regions.append([lastend,bounds[i]['start'],-1])
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
82 lastend = bounds[i]['start']
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
83 else:
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
84 regions.append([bounds[i]['start'],bounds[i]['end'],i])
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
85 if bounds[i]['end'] > lastend:
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
86 lastend = bounds[i]['end']
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
87 i += 1
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
88
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
89 if regions[-1][1] < length:# Final tail of genome;
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
90 regions.append([lastend,length,-1])
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
91
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
92 return regions
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
93
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
94
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
95 def overlap_calc(bounds):
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
96 '''This function takes an array of feature starts and ends and
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
97 returns an array of starts and ends of all overlapping regions.
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
98 Input: [{start,end}]'''
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
99 i = 0
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
100 overlaps = []
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
101 while i < len(bounds) - 1:
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
102 for downstr in bounds[i+1:]:
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
103 if downstr[0] < bounds[i][1]:# Features overlap;
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
104 if downstr[1] < bounds[i][1]:# Complete overlap;
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
105 overlaps.append([downstr[0],downstr[1],bounds[i][2],downstr[2],[0,0]])
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
106 else:# Partial overlap;
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
107 overlaps.append([downstr[0],bounds[i][1],bounds[i][2],downstr[2],[0,0]])
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
108 else:# No use looking further;
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
109 break
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
110
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
111 i += 1
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
112
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
113 return overlaps
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
114
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
115
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
116 def match_feature(bounds,pos,prev=0):
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
117 '''This function checks if a position is located inside a feature and
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
118 returns the feature's number if found or -1 if none is found.
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
119 Input: {start,end},pos,prev_feat (default = 0)'''
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
120 for i in range(prev, len(bounds)):
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
121 if (pos >= bounds[i]['start']) and (pos < bounds[i]['end']):
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
122 return i
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
123 elif pos < bounds[i]['start']:# No use looking further
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
124 return -1
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
125
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
126 return -1
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
127
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
128
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
129 def write_output(line,target=file_out):
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
130 '''This function takes the 2 dimensional array containing all the SNP
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
131 data. It contains an array of information on the feature and an array
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
132 for each strain for which SNPs are given.
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
133 Input: [[pos],[ref],[cells],[cells],etc]'''
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
134 target.write('\n'+str(line[0][0]))
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
135 for cell in line[1]:
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
136 target.write('\t'+str(cell))
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
137 for strain in line[2:]:
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
138 target.write('\t')
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
139 for cell in strain:
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
140 target.write('\t'+str(cell))
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
141
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
142 target.flush()
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
143
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
144
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
145 def new_codon_calc(ref_codon, new_base, pos_in_cod):
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
146 return str(ref_codon[0:pos_in_cod-1]+new_base+ref_codon[pos_in_cod:len(ref_codon)])
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
147
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
148
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
149 def mut_type_check(ref_res, ref_codon, pos_in_gene, new_base, new_codon):
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
150 if str(new_codon).lower() == str(ref_codon).lower():
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
151 return ['','','','']
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
152 new_residue = Seq(new_codon).translate(table=transl_table)
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
153 if str(new_residue) == str(ref_res):
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
154 mut_type = 'synonymous'
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
155 elif (pos_in_gene / 3) < 1 and str(ref_codon).upper() in CodonTable.unambiguous_dna_by_id[transl_table].start_codons:# position 0,1 or 2 and SNP is in start codon;
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
156 if str(new_codon).upper() in CodonTable.unambiguous_dna_by_id[transl_table].start_codons: mut_type = 'nonsynonymous'
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
157 else: mut_type = 'nonstart'
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
158 elif str(new_residue) == '*': mut_type = 'nonsense'
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
159 elif str(ref_res) == '*': mut_type = 'nonstop'
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
160 else: mut_type = 'nonsynonymous'
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
161
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
162 return [mut_type,new_base,new_codon,new_residue]
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
163
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
164
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
165 def codon_process(codon):
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
166 '''This function processes a codon. It loops through it 3 times,
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
167 once to determine which is the highest position mutated, once to
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
168 fill in the cells for the output file and once to output all lines.
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
169 Input: [empty,start_pos,[line1],[line2],[line3],strand]
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
170 It also uses global variable strain_nr'''
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
171 lastposition = [-1]*(strain_nr-1)
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
172 new_codons = ['']*(strain_nr-1)
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
173 if codon[-1] == -1:# Change codon position order for -1 features;
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
174 temp = codon [1:-1]
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
175 temp.reverse()
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
176 codon[1:-1] = temp
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
177 for i,line in enumerate(codon[1:-1],1):
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
178 if line == '': continue
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
179 for j,strain in enumerate(line[2:]):
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
180 if strain[1] in ['a','g','c','t']:
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
181 lastposition[j] = i
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
182 new_codons[j] = codon[i][1][8]
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
183
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
184 for i,line in enumerate(codon[1:-1],1):
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
185 if codon[-1] == -1: pos_in_cod = 4-i
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
186 else: pos_in_cod = i
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
187
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
188 if line == '': continue
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
189 for j,strain in enumerate(line[2:]):
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
190 if i == lastposition[j]: # Check for synonymous etc.;
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
191 new_codons[j] = new_codon_calc(new_codons[j],strain[1],pos_in_cod)
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
192 codon[i][j+2] = mut_type_check(line[1][9],line[1][8],codon[0],strain[1],new_codons[j])
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
193 straininfo[j][codon[i][j+2][0]] += 1# Counting;
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
194 elif strain[1] in ['a','g','c','t']:
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
195 codon[i][j+2] = ['MNP',strain[1],'','']
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
196 straininfo[j]['mnps'] += 1
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
197 new_codons[j] = new_codon_calc(new_codons[j],strain[1],pos_in_cod)
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
198 elif strain[0] == 'Allele missing': codon[i][j+2] = strain
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
199 else: codon[i][j+2] = ['']*4
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
200
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
201 for line in codon[1:-1]:
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
202 if line != '': write_output(line)
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
203
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
204 def feature_props(feature):
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
205 properties = {'type':feature.type,'strand':feature.location._strand,
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
206 'sequence':feature.extract(seq_record.seq),'pseudo': False,
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
207 'locus_tag':'','gene_name':'','product':'',
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
208 'start':int(feature.location._start.position),
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
209 'end':int(feature.location._end.position)}
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
210 if 'pseudo' in feature.qualifiers:
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
211 properties['pseudo'] = True
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
212 properties['type'] = 'pseudogene'
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
213 properties['pure_seq'] = properties['sequence']
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
214 if properties['strand'] == -1:
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
215 properties['sequence'] = seq_record.seq[feature.location._start.position:feature.location._end.position].reverse_complement()
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
216 else:
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
217 properties['sequence'] = seq_record.seq[feature.location._start.position:feature.location._end.position]
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
218 if feature.sub_features: properties['subfeats'] = feature.sub_features
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
219 if 'locus_tag' in feature.qualifiers: properties['locus_tag'] = feature.qualifiers['locus_tag'][0]
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
220 if 'gene' in feature.qualifiers: properties['gene_name']= feature.qualifiers['gene'][0]
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
221 if feature.type in ['tRNA','rRNA','CDS']: properties['product'] = feature.qualifiers['product'][0]
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
222
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
223 return properties
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
224
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
225 # Read embl/genbank file for information on sequence features;
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
226 try:
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
227 seq_record = SeqIO.parse(file_ref, filetype_reference).next()
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
228 except:
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
229 file_ref.close()
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
230 quit("Error reading "+sys.argv[2]+", please check file for errors.")
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
231 file_ref.close()
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
232
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
233 # Loop through genome features and save relevant properties;
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
234 feats = []# Dictionary of properties;
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
235
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
236 feature_types = {'intergenic':0,'gene':0,'pseudogene':0}
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
237 feat_temp_store = ''
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
238 for feature in seq_record.features:
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
239 # Check if gene is defined as other feature (e.g. CDS). Else, save info from 'gene';
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
240 if feat_temp_store != '':
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
241 if (feature.location._start.position == feat_temp_store.location._start.position and
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
242 feature.location._end.position == feat_temp_store.location._end.position):# Gene also defined as other feature;
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
243 feat_temp_store = ''
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
244 else:# Gene not also defined as CDS;
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
245 feats.append(feature_props(feat_temp_store))
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
246 feat_temp_store = ''
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
247 elif feature.type == 'gene':
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
248 feat_temp_store = feature
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
249
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
250 if not feature.type in ['source','gene','misc_feature']:
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
251 if not feature.type in feature_types and feature.type != 'CDS': feature_types[feature.type] = 0
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
252 feats.append(feature_props(feature))
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
253
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
254
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
255 feat_props = sorted(feats, key=lambda cells:int(cells['start']))
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
256 feat_boundaries = [{'start':item['start'],'end':item['end']} for item in feat_props]
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
257 regions = region_calc(feat_boundaries,len(seq_record.seq))
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
258 feat_overlap = overlap_calc(regions)
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
259
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
260 reference_loaded = time.clock()
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
261
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
262 # Create array of SNPs from input file for processing;
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
263 lines = [line.split('\t') for line in file_snps if line.strip()]
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
264 file_snps.close()
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
265 # First line contains headers, extract number of strains etc;
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
266 headers = lines[0]
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
267 snp_table = sorted(lines[1:], key=lambda cells:int(cells[0]))
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
268
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
269 snps_loaded = time.clock()
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
270
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
271 # Print output file headers;
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
272 headers[-1] = headers[-1].rstrip()# Remove newline character;
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
273 strain_nr = len(headers)-1
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
274 strains_found = 'Found '+str(strain_nr)+' strains: '+headers[1]+' (reference)'
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
275 first_line = '\t'+headers[1]+'\t'*9
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
276 second_line = 'Position\tFeature\tLocus tag\tGene\tProduct\tStart\tEnd\tStrand\tRef. base\tRef. codon\tRef. res.'
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
277 straininfo = [0]*(len(headers[2:]))
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
278 for i,strain in enumerate(headers[2:]):
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
279 straininfo[i] = {'snps':0,'mnps':0,'synonymous':0,'nonsynonymous':0,'nonstart':0,'nonstop':0,'nonsense':0}
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
280 straininfo[i].update(feature_types)
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
281 strains_found += ', '+strain
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
282 first_line += '\t\t'+strain+'\t'*3
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
283 second_line += '\t\tSNP type\tNew base\tNew codon\tNew res.'
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
284
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
285 file_out.write(first_line+'\n'+second_line)
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
286 file_out.flush()
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
287
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
288 # Loop through SNPs from array and process them;
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
289 props = {}# Properties of a feature;
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
290 prev_snp = ''# Position of previous SNP;
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
291 to_write = []# Information of current SNP;
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
292 compl_bases = {'a':'t','t':'a','g':'c','c':'g'}
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
293 firstsnp = True# First snp of region, or of codon in cases of 3 positions in codon mutated;
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
294 prev_start=j=k=0
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
295 overlap_snps = []
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
296 codon = ['']*5# Array of codon positions. First item is position of first base of codon in the gene;
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
297
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
298 for region in regions:
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
299 firstsnp = True
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
300 i = prev_start
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
301 while i < len(snp_table):# Loop through SNPs
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
302 snp_entry = snp_table[i]
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
303 if not str(snp_entry[0]).isdigit():# Not a valid line, skip;
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
304 i += 1
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
305 continue
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
306
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
307 pos = int(snp_entry[0])-1
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
308 if pos < region[0]:# Not inside region yet;
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
309 i += 1
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
310 continue
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
311 elif firstsnp and pos < region[1]:
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
312 prev_start = i
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
313 elif pos >= region[1]:# End of region, process and next;
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
314 if not firstsnp and codon != ['','','','','']:
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
315 codon_process(codon)
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
316 break
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
317
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
318 # Documentation of SNPs in feature overlaps;
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
319 while j < len(feat_overlap)-1 and pos > feat_overlap[j][1]: j += 1
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
320 k = j
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
321 while k < len(feat_overlap) and pos >= feat_overlap[k][0]:
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
322 if pos < feat_overlap[k][1]:
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
323 if feat_overlap[k][4][0] == 0:
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
324 feat_overlap[k][4][0] = pos
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
325 feat_overlap[k][4][1] = pos
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
326 k += 1
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
327
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
328
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
329 snp_entry[-1] = snp_entry[-1].rstrip()# Remove newline character at end of line;
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
330
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
331 # Prevent crash upon encounter nonstandard base character;
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
332 if snp_entry[1] not in ['a','g','c','t']:# Ref base
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
333 i += 1
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
334 continue
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
335 for m,base in enumerate(snp_entry[2:],2):
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
336 if base.lower() not in ['a','g','c','t']:
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
337 snp_entry[m] = ''
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
338 # Crash prevented
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
339
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
340 mnp=in_feat=False
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
341 snp_feat = region[2]
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
342 ref_base = snp_entry[1]
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
343
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
344 to_write = [[pos+1]]
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
345
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
346 # Output feature properties and reference situation;
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
347 if snp_feat == -1:
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
348 codon = ['']*5
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
349 to_write.append(['intergenic','','','','','','',ref_base.upper(),'',''])
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
350 elif feat_props[snp_feat]['type'] not in ['CDS','gene','pseudogene']:# In feature, but non-coding;
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
351 codon = ['']*5
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
352 props = feat_props[snp_feat]
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
353 if props['strand'] == -1: ref_base = (compl_bases[snp_entry[1].lower()])
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
354 else: ref_base = snp_entry[1]
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
355 to_write.append([props['type'],props['locus_tag'],props['gene_name'],
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
356 props['product'],props['start']+1,props['end'],
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
357 '',ref_base.upper(),'',''])
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
358 else:# in CDS/gene feature, check codon etc;
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
359 props = feat_props[snp_feat]
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
360 sequence = props['sequence']
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
361 if props['strand'] == -1:
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
362 pos_in_gene = props['end'] - pos - 1# Python counting
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
363 ref_base = (compl_bases[snp_entry[1].lower()])
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
364 else:
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
365 pos_in_gene = pos - props['start']# Python counting
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
366 ref_base = snp_entry[1]
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
367
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
368 in_feat = True
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
369 if props['pseudo'] and 'subfeats' in props:# Pseudogene that needs special attention;
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
370 in_feat = False
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
371 subfeat_boundaries = [{'start':item.location._start.position,'end':item.location._end.position}
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
372 for item in props['subfeats']]
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
373 snp_subfeat = match_feature(subfeat_boundaries,pos)
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
374 if snp_subfeat != -1:
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
375 in_feat = True
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
376 pos_in_gene -= non_coding_calc({'start':props['start'],'subfeats':props['subfeats'],
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
377 'pseudo':True,'strand':props['strand']},pos)
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
378 sequence = props['pure_seq']
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
379
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
380 if not in_feat:# In pseudogene non-coding region;
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
381 codon = ['']*5
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
382 to_write.append(['non coding',props['locus_tag'],props['gene_name'],props['product'],
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
383 props['start']+1,props['end'],props['strand'],ref_base.upper(),
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
384 '',''])
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
385 else:# In coding region;
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
386 pos_in_cod = (pos_in_gene+1)%3
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
387 if pos_in_cod == 0: pos_in_cod = 3# Remainder of division 0 means 3rd place in codon;
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
388
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
389 old_codon = sequence[pos_in_gene-pos_in_cod+1:pos_in_gene-pos_in_cod+4].upper()
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
390 old_residue = old_codon.translate(table=transl_table)
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
391 to_write.append([props['type'],props['locus_tag'],props['gene_name'],props['product'],
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
392 props['start']+1,props['end'],props['strand'],ref_base.upper(),
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
393 old_codon,old_residue])
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
394
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
395 if in_feat and not firstsnp and (pos >= prev_snp):# Check if snp is in same codon as previous snp. Position check for overlapping features;
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
396 if props['strand'] == 1 and (pos - prev_snp + 1) < pos_in_cod:# Same codon (Positive strand);
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
397 mnp = True
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
398 elif props['strand'] == -1 and (pos - prev_snp + 1) <= (3 - pos_in_cod):# Same codon (negative strand);
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
399 mnp = True
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
400
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
401 # Process previous codon if not MNP;
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
402 if in_feat and not mnp:
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
403 if not firstsnp:
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
404 codon_process(codon)
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
405 codon = [pos_in_gene-pos_in_cod+1,'','','',props['strand']]
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
406
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
407
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
408 for l, snp in enumerate(snp_entry[2:]):# Loop through SNPs/strains;
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
409
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
410 snp = snp.lower()
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
411 if snp == '':# Empty cell;
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
412 to_write.append(['','','',''])
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
413 continue
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
414
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
415 if snp == '-': # Feature not present in this strain;
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
416 to_write.append(['Allele missing','','',''])
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
417 continue
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
418
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
419 if snp_feat == -1:# Intergenic;
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
420 if snp == ref_base.lower():
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
421 to_write.append(['']*4)
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
422 else:
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
423 to_write.append(['',snp,'',''])
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
424 straininfo[l]['intergenic'] += 1
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
425 straininfo[l]['snps'] += 1
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
426 continue
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
427
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
428 if props['strand'] == -1:
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
429 snp = compl_bases[snp]
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
430
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
431 if snp == ref_base.lower():
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
432 to_write.append(['']*4)
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
433 else:
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
434 to_write.append(['',snp,'',''])
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
435 straininfo[l]['snps'] += 1
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
436 if props['type'] != 'CDS':
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
437 straininfo[l][props['type']] += 1
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
438
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
439
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
440
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
441 if props['type'] in ['CDS','gene','pseudogene'] and in_feat:
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
442 codon[pos_in_cod] = to_write
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
443 else:
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
444 write_output(to_write)
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
445
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
446 if firstsnp: firstsnp = False
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
447 prev_snp = pos+1
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
448 i += 1
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
449
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
450
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
451 if codon != ['','','','','']: codon_process(codon)
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
452
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
453 file_out.close()
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
454
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
455 end = time.clock()
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
456
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
457 file_summary.write("\n")
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
458 file_summary.write(intro_message)
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
459 file_summary.write('\n'+strains_found+'.\n')
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
460
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
461 file_summary.write("\nFinished annotation. Total time: %s s\n\n" % round(end-start,1))
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
462
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
463
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
464 file_overlap.write('SNP start\tSNP end\tFeature 1\tLocus tag\tProduct\t\tFeature 2\tLocus tag\tProduct')
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
465 for overlap in feat_overlap:
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
466 if overlap[4] != [0,0]:
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
467 overlap[4][0]+=1
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
468 overlap[4][1]+=1
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
469 if overlap[4][0] == overlap[4][1]: overlap[4][1] = ''
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
470 write_output([[str(overlap[4][0])],[str(overlap[4][1]),feat_props[overlap[2]]['type'],feat_props[overlap[2]]['locus_tag'],feat_props[overlap[2]]['product']],
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
471 [feat_props[overlap[3]]['type'],feat_props[overlap[3]]['locus_tag'],feat_props[overlap[3]]['product']]],
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
472 file_overlap)
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
473
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
474
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
475 for i,strain in enumerate(headers[2:]):
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
476 file_summary.write("\n")
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
477 info = straininfo[i]
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
478 file_summary.write("+ Strain %s:\n" % strain)
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
479 file_summary.write(" %s SNPs found\n" % info['snps'])
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
480 file_summary.write(" Number of SNPs found CDS features: %s\n" % (info['mnps']+info['nonstart']+info['nonstop']+info['nonsense']+
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
481 info['synonymous']+info['nonsynonymous']))
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
482 file_summary.write(" (of which in pseudogenes: %s)\n" % info['pseudogene'])
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
483 file_summary.write(" - MNPs: %s\n" % info['mnps'])
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
484 file_summary.write(" - Synonymous: %s\n" % info['synonymous'])
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
485 file_summary.write(" - Nonsynonymous: %s\n" % info['nonsynonymous'])
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
486 file_summary.write(" - Nonsense: %s\n" % info['nonsense'])
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
487 file_summary.write(" - Nonstart: %s\n" % info['nonstart'])
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
488 file_summary.write(" - Nonstop: %s\n" % info['nonstop'])
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
489 file_summary.write(" Intergenic: %s\n" % info['intergenic'])
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
490
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
491 for typ in feature_types:
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
492 if typ not in ['intergenic','pseudogene'] and info[typ] != 0:
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
493 file_summary.write(" %s: %s\n" % (typ,info[typ]))
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
494 file_summary.flush()
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
495
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
496 file_overlap.close()
bd5692103d5b Uploaded
rreumerman
parents:
diff changeset
497 file_summary.close()