-
Notifications
You must be signed in to change notification settings - Fork 2
/
makePaddedMG.py
executable file
·446 lines (334 loc) · 18.6 KB
/
makePaddedMG.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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
import os
import sys
import subprocess
import glob
import time
import argparse
from collections import defaultdict
import shlex
bindir = os.path.abspath(os.path.dirname(__file__))
#input: contigs fastas
#gene prediction (wuerde das mit prodigal machen, output ist ein gbk oder gff und die sequences)
#fetchMG
#dann fuer die fetchMG genes die +-100 bp extracten (shini kannst du jaime fragen ob wir sein script da benutzten koennen, haben ja das gbk)
#db file generieren (die daten sind ja alle da, ausser der motu zugehoerigkeit, aber die muss spaeter gemacht werden)
def readSingleColumnFileToList(infileName):
infile = open(infileName, "r")
listInfileData = []
for strLine in infile:
strLine = strLine.rstrip('\n')
if (" " in strLine):
arrLine = strLine.split(' ')
strLine = arrLine[0]
if ("\t" in strLine):
arrLine = strLine.split('\t')
strLine = arrLine[0]
listInfileData.append(strLine)
return(listInfileData)
def parse2columnFile(infileName):
dictIn = defaultdict(list)
dictInvOut = defaultdict(list)
infile = open(infileName, "r")
for strLine in infile:
strLine = strLine.rstrip('\n')
arrLine = strLine.split('\t')
strColumn_1_entry = arrLine[0]
strColumn_2_entry = arrLine[1]
dictIn[strColumn_1_entry] = strColumn_2_entry
dictInvOut[strColumn_2_entry].append(strColumn_1_entry)
return(dictIn, dictInvOut)
def predictGenes(contigsFasta, metagenomicMode, transTableNumber, protOut, nuclOut, gffOut):
if (metagenomicMode):
mode = " -p meta"
else:
mode = " -p single"
transTableParameter = ""
if (transTableNumber != "11"):
transTableParameter = " -g " + str(transTableNumber)
#prodigal_cmd = subprocess.Popen(['prodigal', '-f gff', '-q', '-c', '-a ' + str(protOut), '-d ' + str(nuclOut), '-o ' + str(gffOut), transTableParameter, '-m', mode, '-i ' + contigsFasta])
#else:
#prodigal_cmd = subprocess.Popen(['prodigal', '-f gff', '-q', '-c', '-a ' + str(protOut), '-d ' + str(nuclOut), '-o ' + str(gffOut), '-m' , mode, '-i ' + contigsFasta])
cmd = "prodigal " + "-a " + str(protOut) + " -d " + str(nuclOut) + " -f gff -o " + str(gffOut) + " -c" + str(transTableParameter) + " -q -m " + mode + " -i " + contigsFasta
print (cmd)
popenCMD = shlex.split(cmd)
prodigal_cmd = subprocess.Popen(popenCMD)
prodigal_cmd.wait()
#cmd = "prodigal " + "-a " + protOut + " -d " + nuclOut + " -f -o " + gffOut + " -c " + transTableParameter + " -q -m " + mode + " " + contigsFasta
#os.system(cmd)
def runFetchMGs(protFasta, genFasta, numThreads, outMGfolder):
#if i want to use this I need to reformat the genomes
#if (metagenomicMode):
# besthit = ""
#else:
# mode = " -v"
#cmd = bin + "MOCATFetchMGs05.pl " + "-t " + str(numThreads) " -d " + nuclFasta + " -o " + outMGfolder + " " + protFasta
#os.system(cmd)
cmd = "perl " + str(bindir) + "/fetchMG.pl -m extraction -t " + str(numThreads) + " -o " + str(outMGfolder) + " -d " + str(genFasta) + " " + str(protFasta)
print (cmd)
popenCMD = shlex.split(cmd)
fetchMGs_cmd = subprocess.Popen(popenCMD)
fetchMGs_cmd.wait()
def parseFetchMGs(outMGfolder, OGType):
setMGs = set()
glob.glob('*.gif')
OGs2use = ["all"]
if (OGType == "mOTU"):
OGs2use = ["COG0012", "COG0016", "COG0018", "COG0172", "COG0215", "COG0495", "COG0525", "COG0533", "COG0541", "COG0552"]
currentPath= os.path.sep.join([outMGfolder, "*.all.marker_genes_scores.table"])
for currentFileName in glob.glob(currentPath):
with open(currentFileName,'r') as currentFile:
for strLine in currentFile:
strLine = strLine.rstrip('\n')
arrLine = strLine.split('\t')
mgID = arrLine[0]
currOG = arrLine[2]
if ( currOG in OGs2use or "all" in OGs2use):
setMGs.add(mgID)
return(setMGs)
def MakeContigLengthFile(contigsFasta, contigLengthsFileName):
# cmd = 'seqtk comp ' + contigsFasta + ' | cut -f1,2 > ' + contigLengthsFileName
# os.system(cmd)
# recoded above in pure python
contigLengthsFile = open(contigLengthsFileName,"w")
seqtk_comp = subprocess.Popen(['seqtk', 'comp', contigsFasta], stdout=subprocess.PIPE)
cut = subprocess.Popen(['cut', '-f1,2'], stdin=seqtk_comp.stdout, stdout=contigLengthsFile)
cut.wait()
#genomes/SAGs/assemblies3/SAR324_J029/SAR324_J029.contigs.fasta
#MakeContigLengthFile("genomes/SAGs/assemblies3/SAR324_J029/SAR324_J029.contigs.fasta", "temp.txt")
#cmd = seqtk comp genomes/SAGs/assemblies3/SAR324_J029/SAR324_J029.contigs.fasta | cut -f1,3 > temp2.txt
def parseGeneLocations_contigCoords(setMGs, contigCoordsFileName, dictContig2Length, padLength, paddedLocationsFileName, extractLocationsFileName, geneLengthFileName, paddedLengthFileName):
contigCoordsFile = open(contigCoordsFileName, "r")
paddedLocationsFile = open(paddedLocationsFileName, "w")
geneLengthFile = open(geneLengthFileName, "w")
extractLocationsFile = open(extractLocationsFileName, "w")
paddedLengthFile = open(paddedLengthFileName, "w")
paddedLocationsFileSpace = open(paddedLocationsFileName + ".ssv" , "w")
dictExtractName2writeName = defaultdict(list)
padLength = int(padLength)
for strLine in contigCoordsFile:
strLine = strLine.rstrip('\n')
arrLine = strLine.split('\t')
geneName = arrLine[0]
contigID = arrLine[1]
start = int(arrLine[2])
start = max(start, 1)
end = int(arrLine[3])
length = end - start + 1
if (geneName in setMGs or "all" in setMGs):
if (contigID in dictContig2Length):
contigLength = int(dictContig2Length[contigID])
paddedStart = max(start - padLength, 1)
paddedEnd = min(end + padLength, contigLength)
newGeneStart = start - paddedStart + 1
newGeneEnd = newGeneStart + length - 1
#geneName contigID ID in padded file start of gene on padded seq
writeline = geneName + "\t" + contigID + "\t" + str(newGeneStart) + "\t" + str(newGeneEnd) + "\n"
paddedLocationsFile.write(writeline)
writeline = geneName + " " + contigID + " " + str(newGeneStart) + " " + str(newGeneEnd) + "\n"
paddedLocationsFileSpace.write(writeline)
#lyrata:1-108
extractLine = contigID + ":" + str(paddedStart) + "-" + str(paddedEnd)
extractLocationsFile.write(extractLine + "\n")
paddedGeneName = geneName + " " + str(newGeneStart) + " " + str(newGeneEnd)
dictExtractName2writeName[extractLine].append(paddedGeneName)
lengthLine = geneName + "\t" + str(length)
geneLengthFile.write(lengthLine + "\n")
lengthLine2 = geneName + "\t" + str(paddedEnd - paddedStart + 1)
paddedLengthFile.write(lengthLine2 + "\n")
else:
print("Could not find length for " + str(contigID) + " . Please check fasta or length file.")
return(dictExtractName2writeName)
def parseGeneLocations_gff(setMGs, gffFileName, padLength, paddedLocationsFileName, extractLocationsFileName, geneLengthFileName, paddedLengthFileName):
gffFile = open(gffFileName, "r")
paddedLocationsFile = open(paddedLocationsFileName, "w")
extractLocationsFile = open(extractLocationsFileName, "w")
geneLengthFile = open(geneLengthFileName, "w")
paddedLengthFile = open(paddedLengthFileName, "w")
paddedLocationsFileSpace = open(paddedLocationsFileName + ".ssv" , "w")
dictExtractName2writeName = defaultdict(list)
padLength = int(padLength)
#SAR324_J029_c172 Prodigal_v2.6.1 CDS 33 836 91.7 + 0 ID=170_1;partial=00;start_type=ATG;rbs_motif=GGAGG;rbs_spacer=5-10bp;gc_cont=0.417;conf=100.00;score=91.69;cscore=79.91;sscore=11.78;rscore=8.57;uscore=-0.99;tscore=4.21;
for strLine in gffFile:
strLine = strLine.strip()
dictContigInfo = {}
# Sequence Data: seqnum=1;seqlen=4639675;seqhdr="NC_000913 # Escherichia coli str. K-12 substr. MG1655, complete genome."
if(strLine.startswith("# Sequence Data:")):
strLine = strLine.replace("# Sequence Data:", "")
strLine = strLine.split("#")[0]
strLine = strLine.strip()
infoList = strLine.split(';')
for infoPart in infoList:
infoSplit = infoPart.split('=')
dictContigInfo[infoSplit[0]] = infoSplit[1]
contigLength = int(dictContigInfo["seqlen"])
elif(not strLine.startswith("#")):
arrLine = strLine.split('\t')
contigID = arrLine[0]
strColumn_2_entry = arrLine[1]
start = int(arrLine[3])
start = max(start, 1)
end = int(arrLine[4])
#strand = arrLine[6]
length = end - start + 1
dictInfo = {}
info = arrLine[8]
infoList = info.split(';')
for infoPart in infoList:
if ('=' in infoPart):
infoSplit = infoPart.split('=')
dictInfo[infoSplit[0]] = infoSplit[1]
paddedStart = max(start - padLength, 1)
paddedEnd = min(end + padLength, contigLength)
newGeneStart = start - paddedStart + 1
newGeneEnd = newGeneStart + length - 1
if "name" in dictInfo:
geneName = dictInfo[name]
elif "ID" in dictInfo:
geneNumber = dictInfo["ID"].split("_")[1]
geneName = contigID + "_" + geneNumber
#print geneName
else:
print("Cannot find ID in GFF, please provide gff in prodigal format")
if (geneName in setMGs or "all" in setMGs):
#geneName contigID ID in padded file start of gene on padded seq
#print geneName
writeline = geneName + "\t" + contigID + "\t" + str(newGeneStart) + "\t" + str(newGeneEnd) + "\n"
paddedLocationsFile.write(writeline)
writeline = geneName + " " + contigID + " " + str(newGeneStart) + " " + str(newGeneEnd) + "\n"
paddedLocationsFileSpace.write(writeline)
#lyrata:1-108
extractLine = contigID + ":" + str(paddedStart) + "-" + str(paddedEnd)
extractLocationsFile.write(extractLine + "\n")
lengthLine = geneName + "\t" + str(length)
geneLengthFile.write(lengthLine + "\n")
lengthLine2 = geneName + "\t" + str(paddedEnd - paddedStart + 1)
paddedLengthFile.write(lengthLine2 + "\n")
paddedGeneName = geneName + " " + str(newGeneStart) + " " + str(newGeneEnd)
dictExtractName2writeName[extractLine].append(paddedGeneName)
return(dictExtractName2writeName)
#==> data/mOTU.v1.padded <==
#>1048834.TC41_3292 101 1561
#==> data/mOTU.v1.padded.coord <==
#1048834.TC41_3292 101 1561
#==> data/mOTU.v1.padded.len <==
#1048834.TC41_3292 1661
# ==> change to length of gene?
def getPaddedSequences(extractLocationsFileName, contigsFasta, paddedFastaFileName, dictExtractName2writeName):
paddedFastaFile = open(paddedFastaFileName, "w")
cat = subprocess.Popen(['cat', extractLocationsFileName],
stdout=subprocess.PIPE,
)
samtools = subprocess.Popen(['xargs', 'samtools', 'faidx', contigsFasta],
stdin=cat.stdout,
stdout=subprocess.PIPE,
)
output = samtools.stdout
for line in output:
if (line.startswith('>')):
line = line.strip()
recordName = line.replace('>', '')
newRecordName = dictExtractName2writeName[recordName].pop()
newRecordName = newRecordName.strip()
paddedFastaFile.write(">" + newRecordName + "\n")
else:
paddedFastaFile.write(line)
def runMakePaddedMG_DB(contigsFasta, outfolder, runPrefix, gffFileName, contigCoordsFileName, contigLengthsFileName, padLength, geneNamesFileName, proteinFasta, transTableNumber, metagenomicMode, numThreads, OGType):
#actual input
#contigsFasta
#outfolder
#gffFileName (optional)
#contigCoordsFileName (optional)
#runPrefix
#padLength (optional)
#geneNamesFileName (optional)
#genesFasta (optional)
#proteinFasta (optional)
#transTableNumber (optional; only genepred)
#metagenomicMode (optional; only genepred)
#numThreads (optional; only fetchmgs)
if not os.path.exists(outfolder):
os.makedirs(outfolder)
else:
print("Outfolder exists, hopefully nothing important will be overwritten...waiting 5 seconds so you can emergency cancel")
time.sleep(5)
#internal (files are produced by script)
outMGfolder = os.path.sep.join([outfolder, runPrefix + "_markerGenes"])
extractLocationsFileName = os.path.sep.join([outfolder, runPrefix + ".extract.coords"])
paddedFastaFileName = os.path.sep.join([outfolder, runPrefix + ".padded.fasta"])
paddedLengthFileName = os.path.sep.join([outfolder, runPrefix + ".padded.len"])
paddedLocationsFileName = os.path.sep.join([outfolder, runPrefix + ".padded.coords"])
geneLengthFileName = os.path.sep.join([outfolder, runPrefix + ".genes.len"])
contigLengthGiven = False
if (contigLengthsFileName):
contigLengthGiven = True
else:
contigLengthsFileName = os.path.sep.join([outfolder, runPrefix + ".contigs.lengths"])
#this section is about the genes for which to extract the padded sequence. either they are found using fetchMGs or they are preselected by the user using geneNamesFileName ('all' can be specified to get padded sequence for all genes)
setMGs = set()
#first step: predict genes or not?
if ((not gffFileName) and (not contigCoordsFileName)):
#print("Running prodigal...")
gffFileName = os.path.sep.join([outfolder, runPrefix + ".all.genes.gff"])
genesFasta = os.path.sep.join([outfolder, runPrefix + ".all.genes.fasta"])
proteinFasta = os.path.sep.join([outfolder, runPrefix + ".all.proteins.fasta"])
predictGenes(contigsFasta, metagenomicMode, transTableNumber, proteinFasta, genesFasta, gffFileName)
#print("...done")
#second step: search for MGs or not?
if (proteinFasta and not geneNamesFileName):
#print("Running fetchMGs...")
#runFetchMGs(proteinFasta, genesFasta, numThreads, outMGfolder)
runFetchMGs(proteinFasta, genesFasta, numThreads, outMGfolder)
#setMGs = parseFetchMGs(outMGfolder, OGType)
#print("...done")
sys.exit(0) # AFTER RUNNING FETCH_MG WE EXIT -------------- MODIFIED FOR PROGENOMES
if(geneNamesFileName == "all"):
setMGs.add("all")
elif(geneNamesFileName):
listMGs = readSingleColumnFileToList(geneNamesFileName)
setMGs = set(listMGs)
#print(setMGs)
if (len(setMGs) == 0):
print("No genes provided for extraction, provide parameter 'XXX=all' to extract all padded sequences for all genes.")
sys.exit(1)
#this part is about getting the locations of the genes from either provided files or from gff files from geneprediction above
if (gffFileName):
dictExtractName2writeName = parseGeneLocations_gff(setMGs, gffFileName, padLength, paddedLocationsFileName, extractLocationsFileName, geneLengthFileName, paddedLengthFileName)
#parseGeneLocations_gff(setMGs, gffFileName, padLength, paddedLocationsFileName, extractLocationsFileName, geneLengthFileName, paddedLengthFileName)
elif(contigCoordsFileName and contigLengthsFileName):
if (not contigLengthGiven):
MakeContigLengthFile(contigsFasta, contigLengthsFileName)
dictContig2Length, dictLength2Contig = parse2columnFile(contigLengthsFileName)
dictExtractName2writeName = parseGeneLocations_contigCoords(setMGs, contigCoordsFileName, dictContig2Length, padLength, paddedLocationsFileName, extractLocationsFileName, geneLengthFileName, paddedLengthFileName)
else:
print("No gff or gene coordinate file provided; also gene calling disabled. Cannot parse gene locations on contigs. Exiting.")
sys.exit(1)
#And now the padded sequences are actually extracted
if(len(dictExtractName2writeName.keys()) > 0):
getPaddedSequences(extractLocationsFileName, contigsFasta, paddedFastaFileName, dictExtractName2writeName)
#needed files
# contigs fasta
# dependencies
def main(argv=None):
parser = argparse.ArgumentParser(description='This script is used to extract padded sequences from contigs. Can predict genes and extract marker genes in the process. Part of the mOTUs v2 pipeline', add_help = True)
parser.add_argument('contigsFasta', action="store", help='Fasta file with contig sequences')
parser.add_argument('outfolder', action="store", help='Folder for output')
parser.add_argument('runPrefix', action="store", help='Name for this run')
parser.add_argument('--gffFile', '-g', action="store", dest='gffFileName', help='gff formatted file that provides the location of genes. Gene names should be encoded in the attribute section as either ID (ID=*_XX where contigname_XX is the gene name; this is prodigal style) or name (name=geneName). If neither --gffFile nor -contigCoordsFile provided, de novo gene prediction will be performed.')
parser.add_argument('--contigCoordsFile', '-c',action="store", dest='contigCoordsFileName', help='Alternative to --gffFile. Tab delimited file with geneName\\tcontigName\\tgeneStart\\geneEnd. If neither --gffFile nor -contigCoordsFile provided, de novo gene prediction will be performed.')
parser.add_argument('--contigLengthsFile', '-s',action="store", dest='contigLengthsFileName', help='Can be provided with --contigCoordsFile. Tab delimited file with contigName\\tcontigLength. If not given contig length will be calculated from fasta file.')
parser.add_argument('--padLength', '-l', action='store', dest='padLength', type=int, default=100, help="Lengths of the padding at each side of the gene, default is 100bp.")
parser.add_argument('--geneNamesFile', '-n', action='store', dest='geneNamesFileName', help="File with names of genes to be padded. If not set and --proteinFasta provided (or when gene prediction is run), fetchMGs will be run to extract the 40 single copy marker genes. If set to 'all', all gene will be padded.")
parser.add_argument('--proteinFasta', '-p', action='store', dest='proteinFasta', help="If gene prediction is not run, but marker genes should be extracted using fetchMGs and padded, please provide the protein sequences using this parameter and do not set --geneNamesFile")
parser.add_argument('--transTableNumber', '-t',action="store", dest='transTableNumber', type=int, default=11,help='Select a translation table for prodigal gene prediction, default is 11.')
parser.add_argument('--metagenomicMode', '-m',action="store", dest='metagenomicMode', default="meta",help="Select a procedure for prodigal gene prediction, default is meta; alternative is 'single'.")
parser.add_argument('--OGType', '-o',action="store", dest='OGType', default="all",help="If running fetchMGs, set to 'mOTU' to constrain to 10 mOTU MGs.")
parser.add_argument('--numThreads', '-a',action="store", dest='numThreads', type=int, default=1, help="Number of threads for fetchMGs.")
parser.add_argument('--version', action='version', version='%(prog)s 1.0')
args = parser.parse_args()
numThreads = str(args.numThreads)
transTableNumber = str(args.transTableNumber)
runMakePaddedMG_DB(args.contigsFasta, args.outfolder, args.runPrefix, args.gffFileName, args.contigCoordsFileName, args.contigLengthsFileName,args.padLength, args.geneNamesFileName, args.proteinFasta, transTableNumber, args.metagenomicMode, numThreads, args.OGType)
return 0 # success
if __name__ == '__main__':
status = main()
sys.exit(status)