Mercurial > repos > miller-lab > genome_diversity
comparison extract_primers.py @ 12:4b6590dd7250
Uploaded
author | miller-lab |
---|---|
date | Wed, 12 Sep 2012 17:10:26 -0400 |
parents | 2c498d40ecde |
children |
comparison
equal
deleted
inserted
replaced
11:d4ec09e8079f | 12:4b6590dd7250 |
---|---|
1 #!/usr/bin/env python | |
2 | |
3 import os | |
4 import sys | |
5 from optparse import OptionParser | |
6 import genome_diversity as gd | |
7 | |
8 def main_function( parse_arguments=None ): | |
9 if parse_arguments is None: | |
10 parse_arguments = lambda arguments: ( None, arguments ) | |
11 def main_decorator( to_decorate ): | |
12 def decorated_main( arguments=None ): | |
13 if arguments is None: | |
14 arguments = sys.argv | |
15 options, arguments = parse_arguments( arguments ) | |
16 rc = 1 | |
17 try: | |
18 rc = to_decorate( options, arguments ) | |
19 except Exception, err: | |
20 sys.stderr.write( 'ERROR: %s\n' % str( err ) ) | |
21 traceback.print_exc() | |
22 finally: | |
23 sys.exit( rc ) | |
24 return decorated_main | |
25 return main_decorator | |
26 | |
27 def parse_arguments( arguments ): | |
28 parser = OptionParser() | |
29 parser.add_option('--input', | |
30 type='string', dest='input', | |
31 help='file of selected SNPs') | |
32 parser.add_option('--output', | |
33 type='string', dest='output', | |
34 help='output file') | |
35 parser.add_option('--primers_loc', | |
36 type='string', dest='primers_loc', | |
37 help='primers .loc file') | |
38 parser.add_option('--scaffold_col', | |
39 type="int", dest='scaffold_col', | |
40 help='scaffold column in the input file') | |
41 parser.add_option('--pos_col', | |
42 type="int", dest='pos_col', | |
43 help='position column in the input file') | |
44 parser.add_option('--species', | |
45 type="string", dest='species', | |
46 help='species') | |
47 return parser.parse_args( arguments[1:] ) | |
48 | |
49 | |
50 @main_function( parse_arguments ) | |
51 def main( options, arguments ): | |
52 if not options.input: | |
53 raise RuntimeError( 'missing --input option' ) | |
54 if not options.output: | |
55 raise RuntimeError( 'missing --output option' ) | |
56 if not options.primers_loc: | |
57 raise RuntimeError( 'missing --primers_loc option' ) | |
58 if not options.scaffold_col: | |
59 raise RuntimeError( 'missing --scaffold_col option' ) | |
60 if not options.pos_col: | |
61 raise RuntimeError( 'missing --pos_col option' ) | |
62 if not options.species: | |
63 raise RuntimeError( 'missing --species option' ) | |
64 | |
65 snps = gd.SnpFile( filename=options.input, seq_col=int( options.scaffold_col ), pos_col=int( options.pos_col ) ) | |
66 | |
67 out_fh = gd._openfile( options.output, 'w' ) | |
68 | |
69 primer_data_file = gd.get_filename_from_loc( options.species, options.primers_loc ) | |
70 | |
71 file_root, file_ext = os.path.splitext( primer_data_file ) | |
72 primer_index_file = file_root + ".cdb" | |
73 primers = gd.PrimersFile( data_file=primer_data_file, index_file=primer_index_file ) | |
74 | |
75 while snps.next(): | |
76 seq, pos = snps.get_seq_pos() | |
77 primer = primers.get_entry( seq, pos ) | |
78 if primer: | |
79 out_fh.write( primer ) | |
80 | |
81 out_fh.close() | |
82 | |
83 if __name__ == "__main__": | |
84 main() | |
85 |