-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.py
executable file
·117 lines (89 loc) · 4.05 KB
/
search.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
#!/usr/bin/env python
import argparse
import os
from os import path
import shutil
import subprocess
import sweetconfig
import tempfile
import genblastA_to_gff3
from sweet_utils import dbpath2seq
class Blast:
def __init__(self):
self.toolname = 'blast'
@staticmethod
def writeraw(dbpath, proteinspath, outpath):
subprocess.check_call(['tblastn', '-db', path.join(dbpath, path.basename(dbpath)), '-query', proteinspath, '-out', outpath])
@staticmethod
def writeparseable(dbpath, proteinspath, outpath):
subprocess.check_call(['tblastn', '-db', path.join(dbpath, path.basename(dbpath)), '-query', proteinspath, '-outfmt', '5', '-out', outpath])
class Exonerate:
def __init__(self):
self.toolname = 'exonerate'
@staticmethod
def writeraw(dbpath, proteinspath, outpath):
with open(outpath, 'w') as outfile:
subprocess.check_call(
['exonerate', '--model', 'protein2genome', '--query',
proteinspath, '--softmasktarget', 'yes',
'--target', dbpath2seq(dbpath),
'--ryo', '<< %pi >>\n'], stdout=outfile)
@staticmethod
def writeparseable(dbpath, proteinspath, outpath):
Exonerate.writeraw(dbpath, proteinspath, outpath)
class Genewise:
def __init__(self):
self.toolname = 'genewise'
@staticmethod
def writeraw(dbpath, proteinspath, outpath):
pass
class Genblasta:
def __init__(self):
self.toolname = 'genblasta'
@staticmethod
def _prepareenv():
newenv = os.environ.copy()
newenv['PATH'] = sweetconfig.genblasta_cfg['oldblastbin'] + ':' + newenv['PATH']
return newenv
@staticmethod
def _prepare_aux_dir(proteinspath, outpath):
outpath_abs = path.abspath(outpath)
auxdirpath = path.join(path.dirname(outpath_abs), '.aux.' + path.basename(outpath_abs))
if not path.exists(auxdirpath):
os.mkdir(auxdirpath)
proteinspath_aux = path.abspath(path.join(auxdirpath, 'query.fasta'))
shutil.copyfile(proteinspath, proteinspath_aux)
return auxdirpath, proteinspath_aux
@staticmethod
def writeraw(dbpath, proteinspath, outpath):
auxdir, proeins_aux = Genblasta._prepare_aux_dir(proteinspath, outpath)
subprocess.check_call(['genblasta', '-q', os.path.abspath(proeins_aux), '-t', os.path.abspath(dbpath2seq(dbpath))
, '-o', os.path.abspath(outpath)], cwd=auxdir, env=Genblasta._prepareenv())
@staticmethod
def writeparseable(dbpath, proteinspath, outpath):
Genblasta.writeraw(dbpath, proteinspath, outpath)
def runraw(args):
filename = ('' if args.runname is None else os.path.basename(args.runname) + '.') + args.toolname + '.out'
print('running ' + args.toolname + ' (raw output)...')
tools[args.toolname].writeraw(args.dbname, args.proteins, filename)
print(filename + ' was written')
def runparseable(args):
filename = ('' if args.runname is None else os.path.basename(args.runname) + '.') + args.toolname + '.parseable.out'
print('running ' + args.toolname + ' (parseable output)...')
tools[args.toolname].writeparseable(args.dbname, args.proteins, filename)
print(filename + ' was written')
if __name__ == '__main__':
tools = dict()
for x in [Blast(), Genewise(), Genblasta(), Exonerate()]:
tools[x.toolname] = x
parser = argparse.ArgumentParser(description="Sweet bioinformatics tool.")
parser.add_argument('operation', choices=['raw', 'parseable'], help='Run mode')
parser.add_argument('toolname', choices=tools.keys(), help='Name of a tool to be used')
parser.add_argument('proteins', help='Input proteins file name')
parser.add_argument('dbname', help='Sequence database name (created with initialize.py)')
parser.add_argument('--runname', dest='runname', help='Run name (saving output to runname.%someextension%)')
args = parser.parse_args()
if args.operation == 'raw':
runraw(args)
elif args.operation == 'parseable':
runparseable(args)