-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmop-up.py
383 lines (334 loc) · 12.2 KB
/
mop-up.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
from __future__ import print_function, unicode_literals
import os
import pandas
import subprocess
import argparse
import multiprocessing
from utils import (
CutToGenome,
RemoveSingletons,
CodeGenomes,
ExtractTitulars,
ExtractFamilies,
ExtractSubgroupMembers,
)
from pyfiglet import Figlet
from time import process_time
from pyfaidx import Fasta
if __name__ == "__main__":
f = Figlet(font="speed")
ff = Figlet(font="doh")
print(f.renderText("MOP-UP"))
parser = argparse.ArgumentParser()
parser.add_argument("nameofrun", help="Name of the run", action="store")
parser.add_argument("fasta", help="Enter full path to fasta file", action="store")
parser.add_argument(
"delimiter", help="Delimiter between genome and gene in fasta", action="store"
)
parser.add_argument(
"--miniden",
help="Minimum percent identity to accept blast hits for building families. Default is 0.30",
action="store",
default=0.30,
dest="miniden",
)
parser.add_argument(
"--minover",
help="Minimum percent overlap to accept blast hits for building families. Default is 0.80",
action="store",
default=0.8,
dest="minover",
)
parser.add_argument(
"--singleton", help="Add this flag to remove singletons", action="store_true"
)
parser.add_argument(
"--cpu",
help="Enter how many threads for diamond to use",
action="store",
default=1,
dest="diamondthreads",
)
parser.add_argument(
"--iter",
help="Enter how many infomap iterations to run",
action="store",
default=1000,
dest="iters",
)
parser.add_argument(
"outputfolder", help="Enter full path to output directory", action="store"
)
parser.add_argument(
"--noMicro",
help="Add flag to not use included Microviridae database",
action="store_true",
dest="dbcheck",
)
parser.add_argument(
"--block-size",
help="Change Diamond Search block size to increase performance. Default is 2",
action="store",
default=2,
dest="block",
)
parser.add_argument(
"--sensitivity",
help="Tune alignment sensitivity for Diamond. Choose between mid-sensitive, sensitive, more-sensitive, very-sensitive, and ultra-sensitive.",
action="store",
default=False,
dest="DiamondSens",
)
args = parser.parse_args()
fasta = args.fasta
delim = args.delimiter
minimumidentity = str(args.miniden)
minimumoverlap = str(args.minover)
sinless = args.singleton
runname = args.nameofrun
threads = str(args.diamondthreads)
outputpath = args.outputfolder
db = args.dbcheck
infoiters = str(args.iters)
blocksize = str(args.block)
if args.DiamondSens != False:
dmndsens = "--" + str(args.DiamondSens)
else:
dmndsens = args.DiamondSens
blockarg = "-b" + blocksize
if not os.path.exists(os.path.abspath(outputpath)):
os.mkdir(outputpath)
outpath = os.path.abspath(outputpath)
workingDirectory = os.getcwd()
if db == False:
with open("final.fasta", "w") as file:
subprocess.run(["cat", "Micro2022_02_24_input.fasta", fasta], stdout=file)
file.close()
final = str(os.getcwd()) + "/final.fasta"
else:
with open("final.fasta", "w") as finalfile:
subprocess.run(["cat", fasta], stdout=finalfile)
finalfile.close()
final = str(os.getcwd()) + "/final.fasta"
diamondcmd = str(os.getcwd()) + "/./diamond"
silixcmd = str(os.getcwd()) + "/silix"
genomefai = Fasta("microgenomes_20220204.fasta")
subprocess.run([diamondcmd, "makedb", "--in", final, "-d", "db"], check=True)
if dmndsens == False:
subprocess.run(
[
diamondcmd,
"blastp",
"-d",
"db",
"-q",
final,
"-o",
"allvall.csv",
"-p",
"threads",
blockarg,
],
check=True,
)
else:
subprocess.run(
[
diamondcmd,
"blastp",
"-d",
"db",
"-q",
final,
"-o",
"allvall.csv",
"-p",
"threads",
blockarg,
dmndsens,
],
check=True,
)
outputpath = os.path.join(outputpath, "mop-up_output")
subprocess.run(["mkdir", outputpath])
outpath = os.path.abspath(outputpath)
with open("clusteroutput.txt", "w") as file:
# subprocess.run(["mkdir", outputpath])
subprocess.run(
[
silixcmd,
"-i",
minimumidentity,
"-r",
minimumoverlap,
final,
"allvall.csv",
],
stdout=file,
)
outputog = outputpath
outputpath = outputpath + "/"
CutToGenome("clusteroutput.txt", delim)
if args.singleton == True:
RemoveSingletons("CutFile.txt")
CodeGenomes("CutFileSinless.txt")
else:
CodeGenomes("CutFile.txt")
subprocess.run(
[
"infomap",
"-i",
"bipartite",
"--clu",
"-2",
"-N",
infoiters,
"-s",
"1",
"Coded.txt",
"./",
],
check=True,
)
# Import Silix output
clustdf = pandas.read_csv(
"clusteroutput.txt", delimiter=" ", names=["ProteinCluster", "Gene"]
)
# Import raw InfoMap output
df2 = pandas.read_csv(
"Coded.clu", delimiter=" ", names=["A", "B", "C"], comment="#"
)
# Import infomap input
df1 = pandas.read_csv("Coded.txt", delimiter=" ", names=["A", "B"])
# Import Silix data that has genomes to protein clusters
if args.singleton == True:
decodedf = pandas.read_csv(
"CutFileSinless.txt", delimiter=" ", names=["Genome", "Cluster"]
)
else:
decodedf = pandas.read_csv(
"CutFile.txt", delimiter=" ", names=["Genome", "Cluster"]
)
decodedf = pandas.read_csv(
"CutFile.txt", delimiter=" ", names=["Genome", "Cluster"]
)
# Merge infomap input with genomes to protein clusters to get a table with network ID and
# protein clusters from infomap input, and also genomes and protein clusters from the modified
# silix output
fulldf = df1.merge(decodedf, how="outer", left_index=True, right_index=True)
# Merges the infomap output to the df with genomes, PCs and network IDs on network IDs,
# which both dfs share. The infomap output also adds the subgroups that each genome
# belongs to
df3 = fulldf.merge(df2, how="left", left_on="A", right_on="A")
# Drops all duplicates of protein clusters, to get one genome for each subgroup,
# which will be used for naming the subgroups
firstuniqs = df3.drop_duplicates(subset="B_y", keep="first")
firstuniqs = firstuniqs.drop(columns=["B_x", "Cluster", "C"])
firstuniqs.to_csv('firstuniqs.csv')
# This merges the new subgroup name (first genome alphabetically in the subgroup),
# to df3, which gives each genome a subgroup name
newdf = df3.merge(firstuniqs, how="left", left_on="B_y", right_on="B_y")
df5 = newdf.drop(columns=["A_x", "B_y", "C", "Genome_x", "B_x"])
df5.to_csv('df5.csv')
# for the master spreadsheet to link network id to genome to subgroup
# This drops all duplicate entries that arise from merging
spreadf = newdf.drop_duplicates(keep="first", inplace=False)
# Drops uneccessary columns
# spreadf = spreadf.drop(columns=["B_y", "B_x", "C", "A_y", "C"])
spreadf = spreadf.drop(columns=["Cluster", "B_y", "B_x", "C", "A_y", "C"])
# Drops duplicates
spreadf = spreadf.drop_duplicates(keep="first", inplace=False)
# Imports metadata
metadata = pandas.read_csv("Metadata.csv", comment="#", header = 1)
metadata = metadata[["Genome designation", "Reported Source", "Source Attribution for Analyses", "GC content", "Sequence Length (nt)", "Phylum", "Class", "Order", "Family", "Genus", "Genus CRISPR prediction", "CRISPR Prediction Correct?"]]
spreadf = spreadf.merge(metadata, how="left", left_on="Genome_x", right_on="Genome designation")
spreadf = spreadf.drop(columns=["Genome designation"])
spreadf.to_csv('spreadf2.csv')
spreadf.to_csv(
outputpath + runname + "Master.csv",
mode="w",
header=["NetworkID", "Genome designation", "Subgroup", "Reported Source", "Source Attribution for Analyses", "GC content", "Sequence Length (nt)", "Phylum", "Class", "Order", "Family", "Genus", "Genus CRISPR prediction", "CRISPR Prediction Correct?"],
index=None,
sep=",",
)
# For normal cytoscape visualization
df5 = df5.drop_duplicates(keep="first", inplace=False)
masterdf2 = pandas.read_csv(outputpath + runname + "Master.csv", delimiter=",")
newcol2 = masterdf2["Subgroup"].value_counts().rename_axis("Unique").reset_index()
df5 = df5.merge(newcol2, how="left", left_on="Genome_y", right_on="Unique")
df5 = df5.drop(columns=["A_y", "Unique"])
clustdf = clustdf.drop_duplicates(
subset="ProteinCluster", keep="first", inplace=False
)
df6 = df5.merge(clustdf, how="left", left_on="Cluster", right_on="ProteinCluster")
df6 = df6.drop(columns=["ProteinCluster", "Cluster"])
clustdf["Annot"] = "ProtCluster"
clustdf = clustdf.drop(columns=["ProteinCluster"])
clustdf.to_csv(
outputpath + runname + "CytoscapeHelper.csv",
index=None,
sep=",",
mode="w",
header=["Protein", "Annotation"],
)
df6.to_csv(
outputpath + runname + "ForCytoscape.csv",
index=None,
sep=",",
mode="w",
header=["Subgroup", "SubgroupCount", "ProteinCluster"],
)
subprocess.run(
["rm", "Coded.clu", "Coded.txt", "CutFile.txt", "allvall.csv", "db.dmnd"]
)
if args.singleton == True:
subprocess.run(["rm", "CutFileSinless.txt"])
path = os.path.abspath(os.getcwd())
clustpath = str(path) + "/clusteroutput.txt"
os.rename(clustpath, outputpath + "clusteroutput.txt")
# os.chdir(outputpath)
SubList = runname + "SubgroupMemberLists"
ProtFam = runname + "ProteinFamilies"
# subprocess.run(["mkdir", SubList])
# outdir = outputpath + SubList + "/"
outdir = os.path.join(outpath, SubList)
os.mkdir(outdir)
# subprocess.run(["mkdir", ProtFam])
# outfolder = outputpath + ProtFam + "/"
outfolder = os.path.join(outputpath, ProtFam)
os.mkdir(outfolder)
fff = Figlet(font="poison")
fastafai = Fasta(final)
p1 = multiprocessing.Process(
target=ExtractSubgroupMembers,
args=(os.path.join(outpath, runname + "Master.csv"), outdir, genomefai, db),
)
p2 = multiprocessing.Process(
target=ExtractTitulars,
args=(os.path.join(outpath, runname + "ForCytoscape.csv"), final, fastafai, runname),
)
p3 = multiprocessing.Process(
target=ExtractFamilies,
args=(
os.path.join(outpath, "clusteroutput.txt"),
os.path.join(outpath, runname + "ForCytoscape.csv"),
final,
outfolder,
fastafai,
),
)
p1.start()
p2.start()
p3.start()
p1.join()
p2.join()
p3.join()
subprocess.run(["rm", os.path.join(outpath, "clusteroutput.txt")])
if os.stat("Errors.txt").st_size == 0:
print(ff.renderText("Done"))
else:
print("Pipeline Failed")
os.chdir(workingDirectory)
subprocess.run(
["rm", "final.fasta", "final.fasta.fai", "microgenomes_20220204.fasta.fai"]
)