annotate extract_flanking_dna.py @ 32:03c22b722882

remove BeautifulSoup dependency
author Richard Burhans <burhans@bx.psu.edu>
date Fri, 20 Sep 2013 13:54:23 -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('--snps_loc',
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
36 type='string', dest='snps_loc',
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
37 help='snps .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('--output_format',
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
45 type="string", dest='output_format',
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
46 help='output format, fasta or primer3')
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.snps_loc:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
60 raise RuntimeError( 'missing --snps_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.output_format:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
66 raise RuntimeError( 'missing --output_format 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 snpcalls_file = gd.get_filename_from_loc( options.species, options.snps_loc )
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
75 file_root, file_ext = os.path.splitext( snpcalls_file )
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
76 snpcalls_index_file = file_root + ".cdb"
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
77 snpcalls = gd.SnpcallsFile( data_file=snpcalls_file, index_file=snpcalls_index_file )
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
78
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
79 while snps.next():
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
80 seq, pos = snps.get_seq_pos()
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
81 flanking_dna = snpcalls.get_flanking_dna( sequence=seq, position=pos, format=options.output_format )
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
82 if flanking_dna:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
83 out_fh.write( flanking_dna )
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
84
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
85 out_fh.close()
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
86
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
87 if __name__ == "__main__":
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
88 main()
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
89