Mercurial > repos > jjohnson > find_in_reference
annotate find_in_reference.py @ 1:e83e0ce8fb68
Add option to reverse the search, find reference field in input field
author | Jim Johnson <jj@umn.edu> |
---|---|
date | Wed, 13 Aug 2014 15:01:33 -0500 |
parents | e7e56b51d156 |
children | c4fd2ea4f988 |
rev | line source |
---|---|
0 | 1 #!/usr/bin/env python |
2 """ | |
3 # | |
4 #------------------------------------------------------------------------------ | |
5 # University of Minnesota | |
6 # Copyright 2013, Regents of the University of Minnesota | |
7 #------------------------------------------------------------------------------ | |
8 # Author: | |
9 # | |
10 # James E Johnson | |
11 # | |
12 #------------------------------------------------------------------------------ | |
13 """ | |
14 | |
15 """ | |
16 Takes 2 tabular files as input: | |
17 1. The file to be filtered | |
18 2. The reference file | |
19 | |
20 The string value of selected column of the input file is searched for | |
21 in the string values of the selected column of the reference file. | |
22 | |
23 The intended purpose is to filter a peptide fasta file in tabular format | |
24 by whether those peptide sequences are found in a reference fasta file. | |
25 | |
26 """ | |
27 import sys,re,os.path | |
28 import tempfile | |
29 import optparse | |
30 from optparse import OptionParser | |
31 import logging | |
32 | |
33 | |
34 def __main__(): | |
35 #Parse Command Line | |
36 parser = optparse.OptionParser() | |
37 parser.add_option( '-i', '--input', dest='input', help='The input file to filter. (Otherwise read from stdin)' ) | |
38 parser.add_option( '-r', '--reference', dest='reference', help='The reference file to filter against' ) | |
39 parser.add_option( '-o', '--output', dest='output', help='The output file for input lines filtered by reference') | |
40 parser.add_option( '-f', '--filtered', dest='filtered', help='The output file for input lines not in the output') | |
41 parser.add_option('-c','--input_column', dest='input_column', default=None, help='The column for the value in the input file. (first column = 1, default to last column)') | |
42 parser.add_option('-C','--reference_column', dest='reference_column', default=None, help='The column for the value in the reference file. (first column = 1, default to last column)') | |
43 parser.add_option( '-I', '--case_insensitive', dest='ignore_case', action="store_true", default=False, help='case insensitive' ) | |
1
e83e0ce8fb68
Add option to reverse the search, find reference field in input field
Jim Johnson <jj@umn.edu>
parents:
0
diff
changeset
|
44 parser.add_option( '-R', '--reverse_find', dest='reverse_find', action="store_true", default=False, help='find the reference string in the input string' ) |
0 | 45 parser.add_option( '-k', '--keep', dest='keep', action="store_true", default=False, help='' ) |
46 parser.add_option( '-a', '--annotation_columns', dest='annotation_columns', default=None, help='If string is found, add these columns from reference' ) | |
47 parser.add_option( '-s', '--annotation_separator', dest='annotation_separator', default=';', help='separator character between annotations from different lines' ) | |
48 parser.add_option( '-S', '--annotation_col_sep', dest='annotation_col_sep', default=',', help='separator character between annotation column from the same line' ) | |
49 parser.add_option( '-d', '--debug', dest='debug', action='store_true', default=False, help='Turn on wrapper debugging to stdout' ) | |
50 (options, args) = parser.parse_args() | |
51 # Input files | |
52 if options.input != None: | |
53 try: | |
54 inputPath = os.path.abspath(options.input) | |
55 inputFile = open(inputPath, 'r') | |
56 except Exception, e: | |
57 print >> sys.stderr, "failed: %s" % e | |
58 exit(2) | |
59 else: | |
60 inputFile = sys.stdin | |
61 # Reference | |
62 if options.reference == None: | |
63 print >> sys.stderr, "failed: reference file is required" | |
64 exit(2) | |
65 # Output files | |
66 outFile = None | |
67 filteredFile = None | |
68 if options.filtered == None and options.output == None: | |
69 #write to stdout | |
70 outFile = sys.stdout | |
71 else: | |
72 if options.output != None: | |
73 try: | |
74 outPath = os.path.abspath(options.output) | |
75 outFile = open(outPath, 'w') | |
76 except Exception, e: | |
77 print >> sys.stderr, "failed: %s" % e | |
78 exit(3) | |
79 if options.filtered != None: | |
80 try: | |
81 filteredPath = os.path.abspath(options.filtered) | |
82 filteredFile = open(filteredPath, 'w') | |
83 except Exception, e: | |
84 print >> sys.stderr, "failed: %s" % e | |
85 exit(3) | |
86 incol = -1 | |
87 if options.input_column and options.input_column > 0: | |
88 incol = int(options.input_column)-1 | |
89 refcol = -1 | |
90 if options.reference_column and options.reference_column > 0: | |
91 refcol = int(options.reference_column)-1 | |
92 if options.annotation_columns: | |
93 annotate = True | |
94 annotation_columns = [int(x) - 1 for x in options.annotation_columns.split(',')] | |
95 else: | |
96 annotate = False | |
97 refFile = None | |
98 num_found = 0 | |
99 num_novel = 0 | |
100 for ln,line in enumerate(inputFile): | |
101 annotations = [] | |
102 try: | |
103 found = False | |
104 search_string = line.split('\t')[incol].rstrip('\r\n') | |
105 if options.ignore_case: | |
106 search_string = search_string.upper() | |
107 if options.debug: | |
108 print >> sys.stderr, "search: %s" % (search_string) | |
109 refFile = open(options.reference,'r') | |
110 for tn,fline in enumerate(refFile): | |
111 fields = fline.split('\t') | |
1
e83e0ce8fb68
Add option to reverse the search, find reference field in input field
Jim Johnson <jj@umn.edu>
parents:
0
diff
changeset
|
112 target_string = fields[refcol].rstrip('\r\n') |
0 | 113 if options.ignore_case: |
114 target_string = target_string.upper() | |
1
e83e0ce8fb68
Add option to reverse the search, find reference field in input field
Jim Johnson <jj@umn.edu>
parents:
0
diff
changeset
|
115 search = search_string if not options.reverse_find else target_string |
e83e0ce8fb68
Add option to reverse the search, find reference field in input field
Jim Johnson <jj@umn.edu>
parents:
0
diff
changeset
|
116 target = target_string if not options.reverse_find else search_string |
0 | 117 if options.debug: |
1
e83e0ce8fb68
Add option to reverse the search, find reference field in input field
Jim Johnson <jj@umn.edu>
parents:
0
diff
changeset
|
118 print >> sys.stderr, "in: %s %s %s" % (search,search in target,target) |
e83e0ce8fb68
Add option to reverse the search, find reference field in input field
Jim Johnson <jj@umn.edu>
parents:
0
diff
changeset
|
119 if search in target: |
0 | 120 found = True |
121 if annotate: | |
122 annotation = options.annotation_col_sep.join([fields[i] for i in annotation_columns]) | |
123 annotations.append(annotation) | |
124 else: | |
125 break | |
126 if found: | |
127 num_found += 1 | |
128 if annotate: | |
129 line = '%s\t%s\n' % (line.rstrip('\r\n'),options.annotation_separator.join(annotations)) | |
130 if options.keep == True: | |
131 if outFile: | |
132 outFile.write(line) | |
133 else: | |
134 if filteredFile: | |
135 filteredFile.write(line) | |
136 else: | |
137 num_novel += 1 | |
138 if options.keep == True: | |
139 if filteredFile: | |
140 filteredFile.write(line) | |
141 else: | |
142 if outFile: | |
143 outFile.write(line) | |
144 except Exception, e: | |
145 print >> sys.stderr, "failed: Error reading %s - %s" % (options.reference,e) | |
146 finally: | |
147 if refFile: | |
148 refFile.close() | |
149 print >> sys.stdout, "found: %d novel: %d" % (num_found,num_novel) | |
150 | |
151 if __name__ == "__main__" : __main__() | |
152 |