annotate extract_primers.py @ 17:a3af29edcce2

Uploaded Miller Lab Devshed version a51c894f5bed
author miller-lab
date Fri, 28 Sep 2012 11:57:18 -0400
parents 2c498d40ecde
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('--species',
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
45 type="string", dest='species',
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
46 help='species')
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
47 return parser.parse_args( arguments[1:] )
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
48
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
49
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
50 @main_function( parse_arguments )
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
51 def main( options, arguments ):
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
52 if not options.input:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
53 raise RuntimeError( 'missing --input option' )
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
54 if not options.output:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
55 raise RuntimeError( 'missing --output option' )
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
56 if not options.primers_loc:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
57 raise RuntimeError( 'missing --primers_loc option' )
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
58 if not options.scaffold_col:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
59 raise RuntimeError( 'missing --scaffold_col option' )
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
60 if not options.pos_col:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
61 raise RuntimeError( 'missing --pos_col option' )
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
62 if not options.species:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
63 raise RuntimeError( 'missing --species option' )
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
64
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
65 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
66
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
67 out_fh = gd._openfile( options.output, 'w' )
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
68
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
69 primer_data_file = gd.get_filename_from_loc( options.species, options.primers_loc )
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
70
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
71 file_root, file_ext = os.path.splitext( primer_data_file )
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
72 primer_index_file = file_root + ".cdb"
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
73 primers = gd.PrimersFile( data_file=primer_data_file, index_file=primer_index_file )
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
74
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
75 while snps.next():
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
76 seq, pos = snps.get_seq_pos()
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
77 primer = primers.get_entry( seq, pos )
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
78 if primer:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
79 out_fh.write( primer )
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
80
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
81 out_fh.close()
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
82
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
83 if __name__ == "__main__":
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
84 main()
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
85