-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcinthia.py
executable file
·215 lines (205 loc) · 11 KB
/
cinthia.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
#!/usr/bin/env python
import sys
import os
import logging
import json
if 'CINTHIA_ROOT' in os.environ:
sys.path.append(os.environ['CINTHIA_ROOT'])
else:
logging.error("CINTHIA_ROOT environment varible is not set")
logging.error("Please, set and export CINTHIA_ROOT to point to cinthia root folder")
sys.exit(1)
import argparse
from cinthialib import cinthiaalgo as cinthia
from cinthialib import config as cfg
from cinthialib import cpparser as bcp
from cinthialib import workenv
from cinthialib import utils
from cinthialib import blast
import re
import numpy
from Bio import SeqIO
def run_multifasta(ns):
data_cache = utils.get_data_cache(ns.cache_dir)
k = 0
ofsout = open(ns.outf, 'w')
protein_jsons = []
profiles = []
for record in SeqIO.parse(ns.fasta, 'fasta'):
we = workenv.TemporaryEnv()
acc = record.id
sequence = str(record.seq)
seq_t=sequence.replace("U","C")
seq_t=seq_t.replace("Z","A")
seq_t=seq_t.replace("B","A")
seq_t=seq_t.replace("X","A")
prefix = "seq%d" % k
fastaSeq = we.createFile(prefix+".", ".fasta")
#SeqIO.write([record], fastaSeq, 'fasta')
fsfp=open(fastaSeq, 'w')
print(">%s" % acc, file=fsfp)
print(seq_t, file=fsfp)
fsfp.close()
pssm = blast.runPsiBlast(prefix, ns.dbfile, fastaSeq, we, data_cache=data_cache,
num_alignments=ns.pbnalign, num_iterations=ns.pbniter, evalue=ns.pbeval,
threads=ns.threads)
try:
profile = bcp.BlastCheckPointProfile(pssm)
profile = utils.rearrange_profile(profile, cfg.BLASTALPH, cfg.HSSPALPH)
profile = utils.clip_profile(seq_t, profile)
except:
profile = utils.one_hot_encoding(seq_t)
profiles.append(profile)
k = k + 1
we.destroy()
we = workenv.TemporaryEnv()
crf_model = cfg.CRFMODEL
if ns.forcetopo:
crf_model = cfg.CRFFORCEDMODEL
CRFprediction, CRFprobs = cinthia.runCRF_multi(crf_model, profiles, we, num_threads = ns.threads)
HMMUprediction = cinthia.runHMM_multi(cfg.HMMUMODEL, profiles, we, num_threads = ns.threads)
HMMWprediction = cinthia.runHMM_multi(cfg.HMMWMODEL, profiles, we, num_threads = ns.threads)
we.destroy()
seq_idx = 0
for record in SeqIO.parse(ns.fasta, 'fasta'):
we = workenv.TemporaryEnv()
acc = record.id
sequence = str(record.seq)
topology = ""
if ns.forcetopo:
cinthia_input_tmp_file = we.createFile("cinthia.", ".input.dat")
cinthia_output_tmp_file = we.createFile("cinthia.", ".output.dat")
ofs = open(cinthia_input_tmp_file, 'w')
ofs.write("# M1 M2 M3 M4\n")
for i in range(len(CRFprediction)):
ofs.write("\t".join([CRFprediction[seq_idx][i], CRFprediction[seq_idx][i],
CRFprediction[seq_idx][i], CRFprediction[seq_idx][i]]) + '\n')
ofs.close()
DP,names=cinthia.readPreds(cinthia_input_tmp_file)
tsymb=cinthia.tmsymbols()
tmseg,topSeg,topSum,mVote=cinthia.topology0(DP,names,12,35,0.0,tsymb)
pLen=len(DP[names[0]])
cinthia.writeConsensus(tmseg,pLen,topSeg,topSum,mVote,tsymb,cinthia_output_tmp_file)
topology = "".join([x.strip() for x in open(cinthia_output_tmp_file).readlines()]).replace("l", "i").replace("L", "o")
else:
if 'T' in CRFprediction[seq_idx] and 'T' in HMMUprediction[seq_idx] and 'T' in HMMWprediction[seq_idx]:
cinthia_input_tmp_file = we.createFile("cinthia.", ".input.dat")
cinthia_output_tmp_file = we.createFile("cinthia.", ".output.dat")
ofs = open(cinthia_input_tmp_file, 'w')
ofs.write("# M1 M2 M3 M4\n")
for i in range(len(CRFprediction[seq_idx])):
ofs.write("\t".join([HMMUprediction[seq_idx][i], HMMWprediction[seq_idx][i],
CRFprediction[seq_idx][i], CRFprediction[seq_idx][i]]) + '\n')
ofs.close()
DP,names=cinthia.readPreds(cinthia_input_tmp_file)
tsymb=cinthia.tmsymbols()
tmseg,topSeg,topSum,mVote=cinthia.topology0(DP,names,12,35,0.0,tsymb)
pLen=len(DP[names[0]])
cinthia.writeConsensus(tmseg,pLen,topSeg,topSum,mVote,tsymb,cinthia_output_tmp_file)
topology = "".join([x.strip() for x in open(cinthia_output_tmp_file).readlines()])
if not re.match("-GLOBULAR", topology):
topology = "".join([x.strip() for x in open(cinthia_output_tmp_file).readlines()]).replace("l", "i").replace("L", "o")
else:
topology = ""
#topology = "".join(CRFprediction[seq_idx])
else:
topology = ""
we.destroy()
if ns.outfmt == "json":
acc_json = utils.get_json_output(acc, sequence, topology, CRFprobs[seq_idx])
#json.dump([acc_json], ofsout, indent=5)
protein_jsons.append(acc_json)
else:
utils.write_gff_output(acc, sequence, ofsout, topology, CRFprobs[seq_idx])
seq_idx = seq_idx + 1
if ns.outfmt == "json":
json.dump(protein_jsons, ofsout, indent=5)
ofsout.close()
sys.exit(0)
def run_pssm(ns):
we = workenv.TemporaryEnv()
record = SeqIO.read(ns.fasta, "fasta")
acc = record.id
sequence = str(record.seq)
profile = bcp.BlastCheckPointProfile(ns.pssm)
profile = utils.rearrange_profile(profile, cfg.BLASTALPH, cfg.HSSPALPH)
topology = ""
ofsout = open(ns.outf, 'w')
if ns.forcetopo:
CRFprediction, CRFprobs = cinthia.runCRF(cfg.CRFFORCEDMODEL, profile, we)
cinthia_input_tmp_file = we.createFile("cinthia.", ".input.dat")
cinthia_output_tmp_file = we.createFile("cinthia.", ".output.dat")
ofs = open(cinthia_input_tmp_file, 'w')
ofs.write("# M1 M2 M3 M4\n")
for i in range(len(CRFprediction)):
ofs.write("\t".join([CRFprediction[i], CRFprediction[i], CRFprediction[i], CRFprediction[i]]) + '\n')
ofs.close()
DP,names=cinthia.readPreds(cinthia_input_tmp_file)
tsymb=cinthia.tmsymbols()
tmseg,topSeg,topSum,mVote=cinthia.topology0(DP,names,12,35,0.0,tsymb)
pLen=len(DP[names[0]])
cinthia.writeConsensus(tmseg,pLen,topSeg,topSum,mVote,tsymb,cinthia_output_tmp_file)
topology = "".join([x.strip() for x in open(cinthia_output_tmp_file).readlines()]).replace("l", "i").replace("L", "o")
else:
CRFprediction, CRFprobs = cinthia.runCRF(cfg.CRFMODEL, profile, we)
#HMMUprediction = cinthia.runHMM(cfg.HMMUMODEL, profile, we)
#HMMWprediction = cinthia.runHMM(cfg.HMMWMODEL, profile, we)
if 'T' in CRFprediction:
cinthia_input_tmp_file = we.createFile("cinthia.", ".input.dat")
cinthia_output_tmp_file = we.createFile("cinthia.", ".output.dat")
ofs = open(cinthia_input_tmp_file, 'w')
ofs.write("# M1 M2 M3 M4\n")
for i in range(len(CRFprediction)):
ofs.write("\t".join([CRFprediction[i], CRFprediction[i], CRFprediction[i], CRFprediction[i]]) + '\n')
ofs.close()
DP,names=cinthia.readPreds(cinthia_input_tmp_file)
tsymb=cinthia.tmsymbols()
tmseg,topSeg,topSum,mVote=cinthia.topology0(DP,names,12,35,0.0,tsymb)
pLen=len(DP[names[0]])
cinthia.writeConsensus(tmseg,pLen,topSeg,topSum,mVote,tsymb,cinthia_output_tmp_file)
topology = "".join([x.strip() for x in open(cinthia_output_tmp_file).readlines()])
if not re.match("-GLOBULAR", topology):
topology = "".join([x.strip() for x in open(cinthia_output_tmp_file).readlines()]).replace("l", "i").replace("L", "o")
else:
topology = ""
else:
topology = ""
if ns.outfmt == "json":
acc_json = utils.get_json_output(acc, sequence, topology, CRFprobs)
json.dump([acc_json], ofsout, indent=5)
else:
utils.write_gff_output(acc, sequence, ofsout, topology, CRFprobs)
ofsout.close()
we.destroy()
sys.exit(0)
def main():
## Parsing input arguments
DESC="Cinthia: Predictor of helical transmembrane topology"
parser = argparse.ArgumentParser(description=DESC)
subparsers = parser.add_subparsers(title = "subcommands", description = "valid subcommands")
multifasta = subparsers.add_parser("multi-fasta", help = "Multi-FASTA input module", description = "Cinthia: Multi-FASTA input module.")
pssm = subparsers.add_parser("pssm", help = "PSSM input module (one sequence at a time)", description = "Cinthia: PSSM input module.")
multifasta.add_argument("-f", "--fasta", help = "The input multi-FASTA file name", dest = "fasta", required = True)
multifasta.add_argument("-d", "--dbfile", help = "The PSIBLAST DB file", dest = "dbfile", required= True)
multifasta.add_argument("-o", "--outf", help = "The output file", dest = "outf", required = True)
multifasta.add_argument("-m", "--outfmt", help = "The output format: json or gff3 (default)", choices=['json', 'gff3'], required = False, default = "gff3")
multifasta.add_argument("-t", "--forcetopo", help = "Force topology to contain at least one TM segment", dest = "forcetopo", action="store_true")
multifasta.add_argument("-c", "--cache-dir", help="Cache dir for alignemnts", dest="cache_dir", required=False, default=None)
multifasta.add_argument("-a", "--threads", help="Number of threads (default 1)", dest="threads", required=False, default=1, type=int)
multifasta.add_argument("-j", "--psiblast-iter", help="Number of PSIBLAST iterations (default 3)", dest="pbniter", required=False, default=3, type=int)
multifasta.add_argument("-n", "--psiblast-nalign", help="PSIBLAST num_alignments parameter (default 5000)", dest="pbnalign", required=False, default=5000, type=int)
multifasta.add_argument("-e", "--psiblast-evalue", help="PSIBLAST evalue parameter (default 0.001)", dest="pbeval", required=False, default=0.001, type=float)
multifasta.set_defaults(func=run_multifasta)
pssm.add_argument("-f", "--fasta", help = "The input FASTA file name (one sequence)", dest = "fasta", required = True)
pssm.add_argument("-p", "--pssm", help = "The PSIBLAST PSSM file", dest = "pssm", required= True)
pssm.add_argument("-o", "--outf", help = "The output file", dest = "outf", required = True)
pssm.add_argument("-m", "--outfmt", help = "The output format: json or gff3 (default)", choices=['json', 'gff3'], required = False, default = "gff3")
pssm.add_argument("-t", "--forcetopo", help = "Force topology to contain at least one TM segment", dest = "forcetopo", action="store_true")
pssm.set_defaults(func=run_pssm)
if len(sys.argv) == 1:
parser.print_help()
else:
ns = parser.parse_args()
ns.func(ns)
if __name__ == "__main__":
main()