annotate tools/evolution/mutate_snp_codon.py @ 1:cdcb0ce84a1b

Uploaded
author xuebing
date Fri, 09 Mar 2012 19:45:15 -0500
parents 9071e359b9a3
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
1 #!/usr/bin/env python
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
2 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
3 Script to mutate SNP codons.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
4 Dan Blankenberg
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
5 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
6
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
7 import sys, string
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
8
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
9 def strandify( fields, column ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
10 strand = '+'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
11 if column >= 0 and column < len( fields ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
12 strand = fields[ column ]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
13 if strand not in [ '+', '-' ]:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
14 strand = '+'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
15 return strand
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
16
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
17 def main():
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
18 # parse command line
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
19 input_file = sys.argv[1]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
20 out = open( sys.argv[2], 'wb+' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
21 codon_chrom_col = int( sys.argv[3] ) - 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
22 codon_start_col = int( sys.argv[4] ) - 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
23 codon_end_col = int( sys.argv[5] ) - 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
24 codon_strand_col = int( sys.argv[6] ) - 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
25 codon_seq_col = int( sys.argv[7] ) - 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
26
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
27 snp_chrom_col = int( sys.argv[8] ) - 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
28 snp_start_col = int( sys.argv[9] ) - 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
29 snp_end_col = int( sys.argv[10] ) - 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
30 snp_strand_col = int( sys.argv[11] ) - 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
31 snp_observed_col = int( sys.argv[12] ) - 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
32
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
33 max_field_index = max( codon_chrom_col, codon_start_col, codon_end_col, codon_strand_col, codon_seq_col, snp_chrom_col, snp_start_col, snp_end_col, snp_strand_col, snp_observed_col )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
34
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
35 DNA_COMP = string.maketrans( "ACGTacgt", "TGCAtgca" )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
36 skipped_lines = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
37 errors = {}
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
38 for name, message in [ ('max_field_index','not enough fields'), ( 'codon_len', 'codon length must be 3' ), ( 'codon_seq', 'codon sequence must have length 3' ), ( 'snp_len', 'SNP length must be 3' ), ( 'snp_observed', 'SNP observed values must have length 3' ), ( 'empty_comment', 'empty or comment'), ( 'no_overlap', 'codon and SNP do not overlap' ) ]:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
39 errors[ name ] = { 'count':0, 'message':message }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
40 line_count = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
41 for line_count, line in enumerate( open( input_file ) ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
42 line = line.rstrip( '\n\r' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
43 if line and not line.startswith( '#' ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
44 fields = line.split( '\t' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
45 if max_field_index >= len( fields ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
46 skipped_lines += 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
47 errors[ 'max_field_index' ]['count'] += 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
48 continue
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
49
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
50 #read codon info
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
51 codon_chrom = fields[codon_chrom_col]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
52 codon_start = int( fields[codon_start_col] )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
53 codon_end = int( fields[codon_end_col] )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
54 if codon_end - codon_start != 3:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
55 #codons must be length 3
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
56 skipped_lines += 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
57 errors[ 'codon_len' ]['count'] += 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
58 continue
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
59 codon_strand = strandify( fields, codon_strand_col )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
60 codon_seq = fields[codon_seq_col].upper()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
61 if len( codon_seq ) != 3:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
62 #codon sequence must have length 3
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
63 skipped_lines += 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
64 errors[ 'codon_seq' ]['count'] += 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
65 continue
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
66
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
67 #read snp info
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
68 snp_chrom = fields[snp_chrom_col]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
69 snp_start = int( fields[snp_start_col] )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
70 snp_end = int( fields[snp_end_col] )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
71 if snp_end - snp_start != 1:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
72 #snps must be length 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
73 skipped_lines += 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
74 errors[ 'snp_len' ]['count'] += 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
75 continue
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
76 snp_strand = strandify( fields, snp_strand_col )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
77 snp_observed = fields[snp_observed_col].split( '/' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
78 snp_observed = [ observed for observed in snp_observed if len( observed ) == 1 ]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
79 if not snp_observed:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
80 #sequence replacements must be length 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
81 skipped_lines += 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
82 errors[ 'snp_observed' ]['count'] += 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
83 continue
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
84
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
85 #Determine index of replacement for observed values into codon
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
86 offset = snp_start - codon_start
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
87 #Extract DNA on neg strand codons will have positions reversed relative to interval positions; i.e. position 0 == position 2
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
88 if codon_strand == '-':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
89 offset = 2 - offset
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
90 if offset < 0 or offset > 2: #assert offset >= 0 and offset <= 2, ValueError( 'Impossible offset determined: %s' % offset )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
91 #codon and snp do not overlap
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
92 skipped_lines += 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
93 errors[ 'no_overlap' ]['count'] += 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
94 continue
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
95
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
96 for observed in snp_observed:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
97 if codon_strand != snp_strand:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
98 #if our SNP is on a different strand than our codon, take complement of provided observed SNP base
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
99 observed = observed.translate( DNA_COMP )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
100 snp_codon = [ char for char in codon_seq ]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
101 snp_codon[offset] = observed.upper()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
102 snp_codon = ''.join( snp_codon )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
103
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
104 if codon_seq != snp_codon: #only output when we actually have a different codon
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
105 out.write( "%s\t%s\n" % ( line, snp_codon ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
106 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
107 skipped_lines += 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
108 errors[ 'empty_comment' ]['count'] += 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
109 if skipped_lines:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
110 print "Skipped %i (%4.2f%%) of %i lines; reasons: %s" % ( skipped_lines, ( float( skipped_lines )/float( line_count ) ) * 100, line_count, ', '.join( [ "%s (%i)" % ( error['message'], error['count'] ) for error in errors.itervalues() if error['count'] ] ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
111
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
112 if __name__ == "__main__": main()