annotate mutate_snp_codon.py @ 0:8f0af7251167 draft default tip

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