comparison tools/genome_diversity/extract_primers.py @ 0:9071e359b9a3

Uploaded
author xuebing
date Fri, 09 Mar 2012 19:37:19 -0500
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:9071e359b9a3
1 #!/usr/bin/env python2.5
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 file_root, file_ext = os.path.splitext( primer_data_file )
71 primer_index_file = file_root + ".cdb"
72 primers = gd.PrimersFile( data_file=primer_data_file, index_file=primer_index_file )
73
74 while snps.next():
75 seq, pos = snps.get_seq_pos()
76 primer = primers.get_entry( seq, pos )
77 if primer:
78 out_fh.write( primer )
79
80 out_fh.close()
81
82 if __name__ == "__main__":
83 main()
84