annotate specify_restriction_enzymes.py @ 0:2c498d40ecde

Uploaded
author miller-lab
date Mon, 09 Apr 2012 12:03:06 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
1 #!/usr/bin/env python
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
2
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
3 import os
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
4 import sys
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
5 from optparse import OptionParser
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
6 import genome_diversity as gd
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
7
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
8 def main_function( parse_arguments=None ):
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
9 if parse_arguments is None:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
10 parse_arguments = lambda arguments: ( None, arguments )
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
11 def main_decorator( to_decorate ):
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
12 def decorated_main( arguments=None ):
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
13 if arguments is None:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
14 arguments = sys.argv
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
15 options, arguments = parse_arguments( arguments )
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
16 rc = 1
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
17 try:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
18 rc = to_decorate( options, arguments )
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
19 except Exception, err:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
20 sys.stderr.write( 'ERROR: %s\n' % str( err ) )
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
21 traceback.print_exc()
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
22 finally:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
23 sys.exit( rc )
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
24 return decorated_main
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
25 return main_decorator
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
26
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
27 def parse_arguments( arguments ):
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
28 parser = OptionParser()
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
29 parser.add_option('--input',
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
30 type='string', dest='input',
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
31 help='file of selected SNPs')
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
32 parser.add_option('--output',
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
33 type='string', dest='output',
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
34 help='output file')
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
35 parser.add_option('--primers_loc',
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
36 type='string', dest='primers_loc',
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
37 help='primers .loc file')
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
38 parser.add_option('--scaffold_col',
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
39 type="int", dest='scaffold_col',
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
40 help='scaffold column in the input file')
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
41 parser.add_option('--pos_col',
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
42 type="int", dest='pos_col',
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
43 help='position column in the input file')
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
44 parser.add_option('--enzyme_list',
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
45 type="string", dest='enzyme_list_string',
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
46 help='comma separated list of enzymes')
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
47 parser.add_option('--species',
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
48 type="string", dest='species',
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
49 help='species')
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
50 return parser.parse_args( arguments[1:] )
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
51
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
52
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
53 @main_function( parse_arguments )
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
54 def main( options, arguments ):
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
55 if not options.input:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
56 raise RuntimeError( 'missing --input option' )
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
57 if not options.output:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
58 raise RuntimeError( 'missing --output option' )
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
59 if not options.primers_loc:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
60 raise RuntimeError( 'missing --primers_loc option' )
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
61 if not options.scaffold_col:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
62 raise RuntimeError( 'missing --scaffold_col option' )
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
63 if not options.pos_col:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
64 raise RuntimeError( 'missing --pos_col option' )
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
65 if not options.enzyme_list_string:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
66 raise RuntimeError( 'missing --enzyme_list option' )
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
67 if not options.species:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
68 raise RuntimeError( 'missing --species option' )
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
69
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
70 snps = gd.SnpFile( filename=options.input, seq_col=int( options.scaffold_col ), pos_col=int( options.pos_col ) )
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
71
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
72 out_fh = gd._openfile( options.output, 'w' )
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
73
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
74 enzyme_dict = {}
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
75 for enzyme in options.enzyme_list_string.split( ',' ):
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
76 enzyme = enzyme.strip()
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
77 if enzyme:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
78 enzyme_dict[enzyme] = 1
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
79
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
80 primer_data_file = gd.get_filename_from_loc( options.species, options.primers_loc )
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
81 file_root, file_ext = os.path.splitext( primer_data_file )
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
82 primer_index_file = file_root + ".cdb"
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
83 primers = gd.PrimersFile( data_file=primer_data_file, index_file=primer_index_file )
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
84
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
85 comments_printed = False
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
86
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
87 while snps.next():
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
88 seq, pos = snps.get_seq_pos()
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
89 enzyme_list = primers.get_enzymes( seq, pos )
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
90 for enzyme in enzyme_list:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
91 if enzyme in enzyme_dict:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
92 if not comments_printed:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
93 for comment in snps.comments:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
94 out_fh.write( "%s\n" % comment )
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
95 comments_printed = True
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
96 out_fh.write( "%s\n" % snps.line )
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
97 break
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
98
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
99 out_fh.close()
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
100
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
101 if __name__ == "__main__":
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
102 main()
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
103