1 | #!/usr/bin/env python |
---|
2 | #Guruprasad Ananda |
---|
3 | """ |
---|
4 | Extract features from GFF file. |
---|
5 | |
---|
6 | usage: %prog input1 out_file1 column features |
---|
7 | """ |
---|
8 | |
---|
9 | import sys, os |
---|
10 | |
---|
11 | from galaxy import eggs |
---|
12 | import pkg_resources; pkg_resources.require( "bx-python" ) |
---|
13 | from bx.cookbook import doc_optparse |
---|
14 | |
---|
15 | assert sys.version_info[:2] >= ( 2, 4 ) |
---|
16 | |
---|
17 | def stop_err( msg ): |
---|
18 | sys.stderr.write( msg ) |
---|
19 | sys.exit() |
---|
20 | |
---|
21 | def main(): |
---|
22 | # Parsing Command Line here |
---|
23 | options, args = doc_optparse.parse( __doc__ ) |
---|
24 | |
---|
25 | try: |
---|
26 | inp_file, out_file, column, features = args |
---|
27 | except: |
---|
28 | stop_err( "One or more arguments is missing or invalid.\nUsage: prog input output column features" ) |
---|
29 | try: |
---|
30 | column = int( column ) |
---|
31 | except: |
---|
32 | stop_err( "Column %s is an invalid column." % column ) |
---|
33 | |
---|
34 | if features == None: |
---|
35 | stop_err( "Column %d has no features to display, select another column." %( column + 1 ) ) |
---|
36 | |
---|
37 | fo=open( out_file, 'w' ) |
---|
38 | for i, line in enumerate( file( inp_file ) ): |
---|
39 | line = line.rstrip( '\r\n' ) |
---|
40 | if line and line.startswith( '#' ): |
---|
41 | # Keep valid comment lines in the output |
---|
42 | fo.write( "%s\n" % line ) |
---|
43 | else: |
---|
44 | try: |
---|
45 | if line.split( '\t' )[column] in features.split( ',' ): |
---|
46 | fo.write( "%s\n" % line ) |
---|
47 | except: |
---|
48 | pass |
---|
49 | fo.close() |
---|
50 | |
---|
51 | print 'Column %d features: %s' %( column + 1, features ) |
---|
52 | |
---|
53 | if __name__ == "__main__": |
---|
54 | main() |
---|