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)')
|
1
|
43 parser.add_option( '-I', '--case_insensitive', dest='ignore_case', action="store_true", default=False, help='case insensitive' )
|
0
|
44 parser.add_option( '-k', '--keep', dest='keep', action="store_true", default=False, help='' )
|
|
45 parser.add_option( '-d', '--debug', dest='debug', action='store_true', default=False, help='Turn on wrapper debugging to stdout' )
|
|
46 (options, args) = parser.parse_args()
|
|
47 # Input files
|
|
48 if options.input != None:
|
|
49 try:
|
|
50 inputPath = os.path.abspath(options.input)
|
|
51 inputFile = open(inputPath, 'r')
|
|
52 except Exception, e:
|
|
53 print >> sys.stderr, "failed: %s" % e
|
|
54 exit(2)
|
|
55 else:
|
|
56 inputFile = sys.stdin
|
|
57 # Reference
|
|
58 if options.reference == None:
|
|
59 print >> sys.stderr, "failed: reference file is required"
|
|
60 exit(2)
|
|
61 # Output files
|
|
62 outFile = None
|
|
63 filteredFile = None
|
|
64 if options.filtered == None and options.output == None:
|
|
65 #write to stdout
|
|
66 outFile = sys.stdout
|
|
67 else:
|
|
68 if options.output != None:
|
|
69 try:
|
|
70 outPath = os.path.abspath(options.output)
|
|
71 outFile = open(outPath, 'w')
|
|
72 except Exception, e:
|
|
73 print >> sys.stderr, "failed: %s" % e
|
|
74 exit(3)
|
|
75 if options.filtered != None:
|
|
76 try:
|
|
77 filteredPath = os.path.abspath(options.filtered)
|
|
78 filteredFile = open(filteredPath, 'w')
|
|
79 except Exception, e:
|
|
80 print >> sys.stderr, "failed: %s" % e
|
|
81 exit(3)
|
|
82 incol = -1
|
|
83 if options.input_column and options.input_column > 0:
|
|
84 incol = int(options.input_column)-1
|
|
85 refcol = -1
|
|
86 if options.reference_column and options.reference_column > 0:
|
|
87 refcol = int(options.reference_column)-1
|
|
88 refFile = None
|
|
89 for ln,line in enumerate(inputFile):
|
|
90 try:
|
|
91 found = False
|
|
92 search_string = line.split('\t')[incol].rstrip('\r\n')
|
1
|
93 if options.ignore_case:
|
|
94 search_string = search_string.upper()
|
0
|
95 if options.debug:
|
|
96 print >> sys.stderr, "search: %s" % (search_string)
|
|
97 refFile = open(options.reference,'r')
|
|
98 for tn,fline in enumerate(refFile):
|
|
99 target_string = fline.split('\t')[refcol]
|
1
|
100 if options.ignore_case:
|
|
101 target_string = target_string.upper()
|
0
|
102 if options.debug:
|
|
103 print >> sys.stderr, "in: %s %s %s" % (search_string,search_string in target_string,target_string)
|
|
104 if search_string in target_string:
|
|
105 found = True
|
|
106 break
|
|
107 if found:
|
|
108 if options.keep == True:
|
|
109 if outFile:
|
|
110 outFile.write(line)
|
|
111 else:
|
|
112 if filteredFile:
|
|
113 filteredFile.write(line)
|
|
114 else:
|
|
115 if options.keep == True:
|
|
116 if filteredFile:
|
|
117 filteredFile.write(line)
|
|
118 else:
|
|
119 if outFile:
|
|
120 outFile.write(line)
|
|
121 except Exception, e:
|
|
122 print >> sys.stderr, "failed: Error reading %s - %s" % (options.reference,e)
|
|
123 finally:
|
|
124 if refFile:
|
|
125 refFile.close()
|
|
126
|
|
127 if __name__ == "__main__" : __main__()
|
|
128
|