-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathncbiFileSubs.py
executable file
·350 lines (279 loc) · 12.6 KB
/
ncbiFileSubs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#!/bin/env python2.7
import sys
import os
import re
import argparse
import subprocess
import re
import copy
import collections
def main():
args=processArgs()
checkErrors(args)
#nameConvertFile = "/raid1/home/bpp/jogdeos/cronn/geoSubs/nameConverts.tab"
outDir = args.o or "./"
sourceList = args.f
if (args.f == None) :
sourceList = (args.s,args.i)
foundFiles={}
noneFound=[]
lineMismatches={}
sourcefiles=[]
#find the original source files (there may be more than one per line in the -f) and add them to an array
for source in sourceList :
sourcedir, filters, dest = source
inputDir = sourcedir
fileFilters = filters.split(",")
filtercount = len(fileFilters)
#get name conversions
#with open(nameConvertFile) as fd:
# nameConv = dict(line.strip().split(None, 1) for line in fd)
fileList=[]
for dirname,dirs,files in os.walk(inputDir):
for f in files :
passcount=0
for filt in fileFilters :
#print "filt=%s, filename=%s" % (filt,f)
match=re.search(filt,f)
#print "matchval=",match
if match != None : passcount+=1
#print "passcount=",passcount
#print "file = ",f
#print "ar",args.a
if ((passcount==filtercount and args.a == False) or
(passcount > 0 and args.a == True)) :
#print "yes!"
#print (dirname,f)
fileList.append(os.path.join(dirname,f))
#print "filelist",fileList
if (len(fileList) > 0) :
if dest in foundFiles :
foundFiles[dest]+=fileList
else :
foundFiles[dest]=fileList
else :
noneFound.append(source)
processThis = collections.defaultdict(list)
for outpath in foundFiles :
fileTypeList = []
foundFiles[outpath].sort
peflag = 0
for filename in foundFiles[outpath] :
if (re.search("R2_\d+", filename) != None) :
peflag=1
break
if peflag == 1 :
#print foundFiles[outpath]
#print "outpath="+outpath
#print "name = "+foundFiles[outpath][0]
outbase ="outpath"
outsuffix = ""
match = re.search(r"(.*)(\.f[ast]*q.*)",outpath)
if (match != None) :
outbase = match.group(1)
outsuffix = match.group(2)
#print "outbase,suffix=%s %s" % (outbase,outsuffix)
#sys.exit()
for filename in foundFiles[outpath] :
if (re.search("R2_\d",filename) == None) :
newoutpath = "%s_R1%s" % (outbase,outsuffix)
processThis[newoutpath].append(filename)
else :
newoutpath = "%s_R2%s" % (outbase,outsuffix)
processThis[newoutpath].append(filename)
else :
#print outpath
processThis[outpath]=copy.deepcopy(foundFiles[outpath])
#print "outpath:"+outpath
#print "\n".join(foundFiles[outpath])
#print ""
for key in processThis :
#print "\n".join(processThis[key])
processThis[key]=sorted(processThis[key])
sFH = ""
if args.l == False :
sfilepath = outDir+"sourcefiles.txt"
if os.path.isfile(sfilepath) == True :
print "%s already exists. Can't overwrite. Exiting." % (sfilepath)
sys.exit()
sFH = open (sfilepath,'w')
outfiles=[]
messages=[]
for dest in sorted(processThis.keys()) :
#check to see if zipped and unzipped files are being mixed
#check to see if the source and destination files match in terms of their zipped status
outfile = checkZip(processThis[dest],dest)
#check if the outfile already exists and if so quit
if (os.path.isfile(outfile)==True and args.l == False) :
print "Please delete %s before proceeding" % (outfile)
sys.exit()
#now to the moving and counting
sourceLines = 0
for sfile in processThis[dest] :
#root,file=fileTup
#infile=os.path.join(root,file)
infile=sfile
if args.l == False :
sFH.write(infile+"\n")
cmd="cat %s >> %s" % (infile,outfile)
print "cmd="+cmd
if (args.l == False) :
subprocess.call(cmd, shell=True)
tempLines = getLines(infile)
try :
sourceLines+=tempLines
except :
print "Couldn't count the number of lines in source"
print ""
if args.l != True :
print "Total lines in source: %s\n" % (sourceLines)
destLines = getLines(outfile)
if destLines == 'ERROR' :
print "Couldn't count lines if the destination file"
else :
print "Total lines in destination: %s" % (destLines)
outfiles.append(outfile)
if (destLines != sourceLines) :
addMismatch(lineMismatches,dest,source,sourceLines,destLines)
if (args.l == False) :
sFH.close()
if (args.l != True) :
md5path = os.path.join(outDir,"md5sums")
md5FH = open (md5path,'w')
for file in outfiles :
cmd = "md5sum %s" % (file)
md5sum = subprocess.check_output(cmd,shell=True)
md5FH.write(md5sum)
md5FH.close()
if (len(lineMismatches) > 0) :
print "\nThere were line count mismatches for the follwing sources:"
for key in lineMismatches :
print lineMismatches[key]['source']
print lineMismatches[key]['counts']
else:
print "\nAll files had matching line counts"
print ""
if (len(noneFound)==0) :
print "At least one file was found for each directory input (which is good)"
else :
print "The following criteria sets had no matching files:"
for dir in noneFound :
print dir
print ""
for m in messages :
print m+"\n"
def checkZip (filelist,dest) :
outFilename = dest
messages=[]
zipcount = {'zipped':0, 'notzipped':0}
for filename in filelist :
#root,filename=fileTup
sourcematch=re.search(".gz$",filename)
if sourcematch==None :
zipcount['notzipped']+=1
else :
zipcount['zipped']+=1
#quit if the sources are mixed zipped and unzipped
if (zipcount['notzipped'] != 0 and zipcount['zipped'] != 0) :
print "The files that are supposed to go into %s are a mix of zipped and non-zipped files. Check your inputs and try again." % (dest)
print "\n".join(filelist)+"\n"
sys.exit()
#add a .gz to the end of the destination filename if the sources are all zipped
if (re.search(".gz",outFilename)==None and zipcount['zipped'] > 0) :
outFilename+=".gz"
messages.append("A '.gz' was added to the end of %s, since the source appears to be zipped." % (outFilename))
#remove the .gz if source is not zipped
match = re.search("(.*).gz$",outFilename)
if (match != None and zipcount['notzipped'] > 0) :
outFilename = match.group(1)
messages.append("The '.gz' was removed from the end of %s, since the source appears to be unzipped." % (outFilename))
print "\n".join(messages)
return outFilename
def addMismatch (container,key,source,scount,dcount):
sourcetext="\t".join(source)
container[key]={}
container[key]['source']=sourcetext
container[key]['counts']="In=%s , Out=%s\n" % (scount,dcount)
def getLines(file):
if (re.search(".gz",file)==None) :
#cmd = "cat %s | grep -c \"\" " % (file)
cmd = "cat %s | wc -l " % (file)
else :
#cmd = "zcat %s | grep -c \"\" " % (file)
cmd = "zcat %s | wc -l " % (file)
print "linecount cmd = "+cmd
try :
linecount = subprocess.check_output(cmd,shell=True)
print linecount
return int(linecount)
except :
return "ERROR"
#return 0
def checkErrors(args):
#if (args.i!=None and os.path.isfile(args.i) == False) :
# sys.exit("A file must be provided with the -i flag.")
#if (args.s != None and os.path.isdir(args.s) == False):
# sys.exit("The argument you entered for -s is not a directory.")
if (os.path.isdir(args.o)==False) :
print "The value you provided with the -o paramaater is not a directory."
sys.exit()
def processArgs():
class MyParser(argparse.ArgumentParser):
def error(self, message):
sys.stderr.write('\nerror: %s\n\n' % message)
self.print_help()
sys.exit(2)
class Checkerrors(argparse.Action) :
def __call__(self,parser,namespace,value,option_string) :
if (option_string=="-f") :
if (os.path.isfile(value)==False) :
parser.error("The -f flag needs a valid filename")
else :
filtlist = []
errors=[""]
filterLenError = 0
with open(value,'r') as fh :
for line in fh :
if (re.search('^[\s\n]+$',line)!=None) :
continue
if (re.search('^#',line)!=None) :
continue
line = line.rstrip("\n")
components = tuple(line.split("\t"))
if len(components) != 3 :
filterLenError = 1
if (os.path.isdir(components[0])==True) :
filtlist.append(components)
else :
errors.append("%s is an invalid dirname in %s" % (components[0],value))
if filterLenError == 1 :
errors.append("You need 3 columns in your filter file")
if len(errors) > 1 :
parser.error("\n".join(errors))
setattr(namespace,self.dest,filtlist)
if (option_string=="-s") :
if (os.path.isdir(value)==False) :
parser.error("The value you proivded for the -s flag is not a valid directory")
else :
setattr(namespace,self.dest,value)
#argParser = MyParser(usage=("%s (sourceDir & filter) | filterFile" % (os.path.basename(sys.argv[0]))))
argParser = MyParser(description="""Prepares a set of files for submission to NCBI (e.g. GEO or SRA databases).
First finds all files in the directory you have specified. Checks the file name against a list
of filters that you provide (must pass all filters).
""")
group=argParser.add_mutually_exclusive_group(required=True)
group.add_argument('-s', metavar="sourceDir", action=Checkerrors, help="The directory in which the sequence files can be found.")
group.add_argument('-f', metavar="filterFile", action=Checkerrors, help="""A tab seperated file with source directory names in the first column, csv filter text (to filter in file names) in the second column,
and destination file in a third column.""")
argParser.add_argument('-i', metavar="filter", help="Only use with the -s option. Either a csv text value or a file with one csv text item per line. Default=None.")
argParser.add_argument('-a', action='store_true', help="Use an OR operator with the filter list. Default is to use an AND operator (i.e. the file must pass all filters).")
argParser.add_argument('-l', action='store_true', help="Just list the files that will be copied if the script is run without the -l flag.")
argParser.add_argument('-o', metavar="outputdir", default="./", help="Output directory. Default=./")
#if len(sys.argv)==1:
# argParser.print_help()
# sys.exit(1)
args = argParser.parse_args()
return args
#This is required because by default this is a module. Running this makes it execute main as if it is a script
if __name__ == '__main__':
main()