-
Notifications
You must be signed in to change notification settings - Fork 0
/
runProdigal.py
127 lines (101 loc) · 2.72 KB
/
runProdigal.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
__author__ = 'Roshan'
__version__ = '0.1'
import shlex
import subprocess
import argparse
import os
class Run_Prodigal(object):
""" Methods for running Prodigal and gives an edited version of fasta files """
def __init__(self, genomefilename):
super(Run_Prodigal, self).__init__()
self.genome = genomefilename # single genome file
self.gcode = '11'
base= self.genome.split('/')[-1].split('.')[0]
self.nuc = base + ".nuc"
self.pep = base + ".pep"
self.sco = base + ".sco"
def P_cmdline(self, gcode=None):
'''
Returns str of prodigal commandline
'''
if gcode is None:
self.gcode == str(gcode)
else :
self.gcode == str(gcode)
cmd = "prodigal -i " + self.genome + " -c -m -q -g " + self.gcode + " -o sco -a " + self.pep + " -d " + self.nuc
c = shlex.split(cmd)
return c
def P_run(self,args):
'''
Returns True if prodigal runs
>>>Run_Prodigal.P_run()
True
>>>Run_Prodigal.P_run()
False
'''
self.args = args
try :
s = subprocess.call(args, shell=False)
except :
raise
return s
def shorten_fasta_header(self,fn):
'''
creates another edited version of input fasta file
'''
with open(fn,'r') as fh:
fr = fh.readlines()
nfn = fn + ".modified"
fw = open(nfn,'w+')
for i in fr:
if i.startswith(">"):
fw.write(i.split(" ")[0]+"\n")
else:
fw.write(i)
fw.close()
return nfn
def remove_files(self):
'''
just remove the files from the disk
'''
os.remove(self.nuc)
os.remove(self.pep)
#os.remove(self.sco)
def rename_files(self):
'''
just remove the files from the disk
'''
nucfile = self.nuc + ".modified"
pepfile = self.pep + ".modified"
os.rename(nucfile,self.nuc)
os.rename(pepfile,self.pep)
if __name__ == '__main__':
###################### Command Line Argument Parsing ##########################
des="""
\n
This is a suplimentary script which runs prodigal and parse the file and give correct filenames\n
Requirements: prodigal\n
"""
parser = argparse.ArgumentParser(description=des,formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument('-g', help='path to the genome files',nargs='+', action='store',dest='genome_list',required=True)
args = parser.parse_args()
x = args.genome_list
for i in x:
#checkfile(i)
n = Run_Prodigal(i)
cln = n.P_cmdline()
print("running prodigal for {}".format(n.genome))
n.P_run(cln)
n.shorten_fasta_header(n.nuc)
n.shorten_fasta_header(n.pep)
n.remove_files()
n.rename_files()
print("check result files : {} {} ".format(n.nuc, n.pep))
'''
TODO : Checking Files Before Running Should Be Included
def checkfile(fname):
if os.path.isfile(fname) is False:
print ("\nError : File don't exists\n")
else :
return os.path.isfile(fname)
'''