annotate tools/genome_diversity/extract_flanking_dna.py @ 1:cdcb0ce84a1b

Uploaded
author xuebing
date Fri, 09 Mar 2012 19:45:15 -0500
parents 9071e359b9a3
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
1 #!/usr/bin/env python2.5
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
2
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
3 import os
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
4 import sys
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
5 from optparse import OptionParser
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
6 import genome_diversity as gd
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
7
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
8 def main_function( parse_arguments=None ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
9 if parse_arguments is None:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
10 parse_arguments = lambda arguments: ( None, arguments )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
11 def main_decorator( to_decorate ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
12 def decorated_main( arguments=None ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
13 if arguments is None:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
14 arguments = sys.argv
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
15 options, arguments = parse_arguments( arguments )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
16 rc = 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
17 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
18 rc = to_decorate( options, arguments )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
19 except Exception, err:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
20 sys.stderr.write( 'ERROR: %s\n' % str( err ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
21 traceback.print_exc()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
22 finally:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
23 sys.exit( rc )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
24 return decorated_main
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
25 return main_decorator
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
26
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
27 def parse_arguments( arguments ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
28 parser = OptionParser()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
29 parser.add_option('--input',
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
30 type='string', dest='input',
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
31 help='file of selected SNPs')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
32 parser.add_option('--output',
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
33 type='string', dest='output',
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
34 help='output file')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
35 parser.add_option('--snps_loc',
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
36 type='string', dest='snps_loc',
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
37 help='snps .loc file')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
38 parser.add_option('--scaffold_col',
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
39 type="int", dest='scaffold_col',
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
40 help='scaffold column in the input file')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
41 parser.add_option('--pos_col',
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
42 type="int", dest='pos_col',
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
43 help='position column in the input file')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
44 parser.add_option('--output_format',
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
45 type="string", dest='output_format',
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
46 help='output format, fasta or primer3')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
47 parser.add_option('--species',
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
48 type="string", dest='species',
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
49 help='species')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
50 return parser.parse_args( arguments[1:] )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
51
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
52
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
53 @main_function( parse_arguments )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
54 def main( options, arguments ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
55 if not options.input:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
56 raise RuntimeError( 'missing --input option' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
57 if not options.output:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
58 raise RuntimeError( 'missing --output option' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
59 if not options.snps_loc:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
60 raise RuntimeError( 'missing --snps_loc option' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
61 if not options.scaffold_col:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
62 raise RuntimeError( 'missing --scaffold_col option' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
63 if not options.pos_col:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
64 raise RuntimeError( 'missing --pos_col option' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
65 if not options.output_format:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
66 raise RuntimeError( 'missing --output_format option' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
67 if not options.species:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
68 raise RuntimeError( 'missing --species option' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
69
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
70 snps = gd.SnpFile( filename=options.input, seq_col=int( options.scaffold_col ), pos_col=int( options.pos_col ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
71
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
72 out_fh = gd._openfile( options.output, 'w' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
73
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
74 snpcalls_file = gd.get_filename_from_loc( options.species, options.snps_loc )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
75 file_root, file_ext = os.path.splitext( snpcalls_file )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
76 snpcalls_index_file = file_root + ".cdb"
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
77 snpcalls = gd.SnpcallsFile( data_file=snpcalls_file, index_file=snpcalls_index_file )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
78
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
79 while snps.next():
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
80 seq, pos = snps.get_seq_pos()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
81 flanking_dna = snpcalls.get_flanking_dna( sequence=seq, position=pos, format=options.output_format )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
82 if flanking_dna:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
83 out_fh.write( flanking_dna )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
84
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
85 out_fh.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
86
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
87 if __name__ == "__main__":
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
88 main()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
89