tabtk 1.0.7__tar.gz → 1.0.9__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: tabtk
3
- Version: 1.0.7
3
+ Version: 1.0.9
4
4
  Summary: tabtk: A toolkit for sequence similarity analysis based on tabular data
5
5
  Author-email: Chrispin Chaguza <chrispin.chaguza@gmail.com>
6
6
  Maintainer-email: Chrispin Chaguza <chrispin.chaguza@gmail.com>
@@ -1,4 +1,4 @@
1
1
  """tabkit: A toolkit for sequence similarity analysis based on tabular data"""
2
2
 
3
- __version__ = "1.0.7"
3
+ __version__ = "1.0.9"
4
4
 
@@ -129,6 +129,109 @@ def mashSimilarity(cmdValues):
129
129
 
130
130
  return(mashResults)
131
131
 
132
+
133
+ def blastANISimilarity(cmdValues):
134
+ if not shutil.which("blastn"):
135
+ print("Install BLAST and try again... exiting...")
136
+ sys.exit()
137
+
138
+ else:
139
+ pass
140
+
141
+ tmpFileNames = []
142
+ fileNames = []
143
+
144
+ tmpCmd="wc -l "+str(cmdValues["data"])+" | awk '{print $1}'"
145
+ dataFileLines=subprocess.Popen(tmpCmd,shell=True, stderr=subprocess.STDOUT, stdout=subprocess.PIPE)
146
+ dataLines = int(dataFileLines.communicate()[0])
147
+ dataFileLines.kill()
148
+
149
+ bar = ChargingBar("Loading data", max=int(dataLines))
150
+ for fileName in open(cmdValues["data"],"r"):
151
+ fileNames.append(str(fileName).strip())
152
+
153
+ bar.next()
154
+ bar.finish()
155
+
156
+ for i in fileNames:
157
+ tmpFileNames.append(f"{Path(i).stem}.tmpfna")
158
+
159
+ with open(f"{Path(i).stem}.tmpfna","w") as fh:
160
+ for j in SeqIO.parse(i,"fasta"):
161
+ fh.write(f">{Path(os.path.basename(i)).stem}\n{j.seq}\n")
162
+
163
+ tmpOut = f"blast.{str(datetime.datetime.now()).replace(" ","").replace("-",".").replace(":",".")}"
164
+
165
+ bar = ChargingBar("Creating database", max=len(tmpFileNames))
166
+ with open(tmpOut,"w") as tmpOutFile:
167
+ for fileName in tmpFileNames:
168
+ for seq in SeqIO.parse(fileName,"fasta"):
169
+ tmpOutFile.write(f">{seq.id}\n{seq.seq}\n")
170
+ bar.next()
171
+ bar.finish()
172
+
173
+ Cmd2 = f"makeblastdb -in {tmpOut} -dbtype nucl"
174
+
175
+ subprocess.call(Cmd2,shell=True,stdout=subprocess.DEVNULL,stderr=subprocess.STDOUT)
176
+
177
+ print("Calculating percent similarity... may take longer... be patient...")
178
+
179
+ tmpOut1 = f"blast.{str(datetime.datetime.now()).replace(" ","").replace("-",".").replace(":",".")}"
180
+ tmpOut2 = f"blast.{str(datetime.datetime.now()).replace(" ","").replace("-",".").replace(":",".")}"
181
+
182
+ blastCmd1 = f"echo \'qseqid sseqid length pident qcovs\' | tr \' \' \'\\t\' > {tmpOut2}"
183
+ blastCmd2 = f"blastn -db {tmpOut} -query {tmpOut} -word_size {cmdValues["length"]} -num_threads {cmdValues["threads"]} -outfmt \'6 qseqid sseqid length pident qcovs\' -evalue {cmdValues["evalue"]} >> {tmpOut2}"
184
+
185
+ subprocess.call(blastCmd1,shell=True,stdout=subprocess.DEVNULL,stderr=subprocess.STDOUT)
186
+ subprocess.call(blastCmd2,shell=True,stdout=subprocess.DEVNULL,stderr=subprocess.STDOUT)
187
+
188
+ blastData = pd.read_csv(tmpOut2,delimiter="\t",dtype={'pident': float,'qcovs': float, 'length': int})
189
+ blastData = blastData[blastData["qcovs"] >= (cmdValues["fraction"]*100)]
190
+
191
+ blastData["tmp"] = blastData.apply(lambda x: (x["length"] * x["pident"]),axis=1)
192
+
193
+ blastData = blastData[["qseqid","sseqid","tmp","length","qcovs"]].groupby(['qseqid','sseqid']).agg({'tmp':'sum','length':'sum','qcovs':'mean'}).reset_index()
194
+ blastData["tmp1"] = blastData.apply(lambda x: ((x["tmp"] / x["length"]) if x["length"]>0 else 0 ),axis=1)
195
+
196
+ blastData["qseqidR"] = blastData.apply(lambda x: x["qseqid"] if x["qseqid"] > x["sseqid"] else x["sseqid"],axis=1)
197
+ blastData["sseqidR"] = blastData.apply(lambda x: x["sseqid"] if x["qseqid"] > x["sseqid"] else x["qseqid"],axis=1)
198
+
199
+ blastData["qseqid"] = blastData["qseqidR"]
200
+ blastData["sseqid"] = blastData["sseqidR"]
201
+
202
+ blastDataR = blastData[["qseqid","sseqid","tmp1"]].groupby(['qseqid','sseqid']).agg({'tmp1':'min'}).reset_index()
203
+
204
+ blastDataFinal = blastDataR[["sseqid","qseqid","tmp1"]].rename(columns={"sseqid":"seq1","qseqid":"seq2","tmp1":"pid"}).values.tolist()
205
+
206
+ bar = ChargingBar("Cleaning temporary files", max=len(fileNames))
207
+ for fileName in tmpFileNames:
208
+ if os.path.exists(fileName):
209
+ os.remove(fileName)
210
+ else:
211
+ pass
212
+
213
+ bar.next()
214
+ bar.finish()
215
+
216
+ if os.path.exists(tmpOut1):
217
+ os.remove(tmpOut1)
218
+ else:
219
+ pass
220
+
221
+ if os.path.exists(tmpOut2):
222
+ os.remove(tmpOut2)
223
+ else:
224
+ pass
225
+
226
+ for file in glob.glob(f"{tmpOut}*"):
227
+ if os.path.exists(file):
228
+ os.remove(file)
229
+ else:
230
+ pass
231
+
232
+ return(blastDataFinal)
233
+
234
+
132
235
  def blastSimilarity(cmdValues):
133
236
  if not shutil.which("blastn"):
134
237
  print("Install BLAST and try again... exiting...")
@@ -189,7 +292,7 @@ def blastSimilarity(cmdValues):
189
292
 
190
293
  blastData = blastData[["qseqid","sseqid","tmp","length","qcovs"]].groupby(['qseqid','sseqid']).agg({'tmp':'sum','length':'sum','qcovs':'mean'}).reset_index()
191
294
 
192
- blastData["tmp1"] = blastData.apply(lambda x: (((x["tmp"] / x["length"]) * x["qcovs"]) / 100),axis=1)
295
+ blastData["tmp1"] = blastData.apply(lambda x: ((x["tmp"] / x["length"]) * (x["qcovs"] / 100)),axis=1)
193
296
 
194
297
  blastData["qseqidR"] = blastData.apply(lambda x: x["qseqid"] if x["qseqid"] > x["sseqid"] else x["sseqid"],axis=1)
195
298
  blastData["sseqidR"] = blastData.apply(lambda x: x["sseqid"] if x["qseqid"] > x["sseqid"] else x["qseqid"],axis=1)
@@ -283,13 +386,13 @@ def fastaniSimilarity(cmdValues):
283
386
 
284
387
  def similarity(cmdValues):
285
388
 
286
- if sum([1 if cmdValues["aln"] else 0, 1 if cmdValues["mash"] else 0, 1 if cmdValues["fastani"] else 0]) > 1:
389
+ if sum([1 if cmdValues["blastani"] else 0, 1 if cmdValues["blast"] else 0, 1 if cmdValues["aln"] else 0, 1 if cmdValues["mash"] else 0, 1 if cmdValues["fastani"] else 0]) > 1:
287
390
  print("Specify none or only one of these options --aln/-a, --mash/-m, and --fastani/-n... exiting...")
288
391
  sys.exit()
289
392
  else:
290
393
  pass
291
394
 
292
- if (not cmdValues["blast"]) and (not cmdValues["aln"]) and (not cmdValues["mash"]) and (not cmdValues["fastani"]):
395
+ if (not cmdValues["blastani"]) and (not cmdValues["blast"]) and (not cmdValues["aln"]) and (not cmdValues["mash"]) and (not cmdValues["fastani"]):
293
396
  dataMatrixDF = loadDataTable(cmdValues)
294
397
 
295
398
  itemPairs = itertools.permutations([item for pos,item in enumerate(dataMatrixDF.keys()) if pos>0], r=2)
@@ -311,7 +414,7 @@ def similarity(cmdValues):
311
414
  bar.next()
312
415
  bar.finish()
313
416
 
314
- elif (not cmdValues["blast"]) and (not cmdValues["aln"]) and (cmdValues["mash"]) and (not cmdValues["fastani"]):
417
+ elif (not cmdValues["blastani"]) and (not cmdValues["blast"]) and (not cmdValues["aln"]) and (cmdValues["mash"]) and (not cmdValues["fastani"]):
315
418
  similarityResults = mashSimilarity(cmdValues)
316
419
 
317
420
  with open(cmdValues["output"],"w") as simData:
@@ -324,7 +427,7 @@ def similarity(cmdValues):
324
427
  bar.next()
325
428
  bar.finish()
326
429
 
327
- elif (not cmdValues["blast"]) and (not cmdValues["aln"]) and (not cmdValues["mash"]) and (cmdValues["fastani"]):
430
+ elif (not cmdValues["blastani"]) and (not cmdValues["blast"]) and (not cmdValues["aln"]) and (not cmdValues["mash"]) and (cmdValues["fastani"]):
328
431
  similarityResults = fastaniSimilarity(cmdValues)
329
432
 
330
433
  with open(cmdValues["output"],"w") as simData:
@@ -337,7 +440,7 @@ def similarity(cmdValues):
337
440
  bar.next()
338
441
  bar.finish()
339
442
 
340
- elif (not cmdValues["blast"]) and (cmdValues["aln"]) and (not cmdValues["mash"]) and (not cmdValues["fastani"]):
443
+ elif (not cmdValues["blastani"]) and (not cmdValues["blast"]) and (cmdValues["aln"]) and (not cmdValues["mash"]) and (not cmdValues["fastani"]):
341
444
  alignment={}
342
445
 
343
446
  for i in SeqIO.parse(cmdValues['data'],"fasta"):
@@ -352,7 +455,7 @@ def similarity(cmdValues):
352
455
  args=[(alignment[m],alignment[n]) for m,n in tmpList]
353
456
  results=pool.starmap(alignmentSimilarity, args)
354
457
 
355
- elif (cmdValues["blast"]) and (not cmdValues["aln"]) and (not cmdValues["mash"]) and (not cmdValues["fastani"]):
458
+ elif (not cmdValues["blastani"]) and (cmdValues["blast"]) and (not cmdValues["aln"]) and (not cmdValues["mash"]) and (not cmdValues["fastani"]):
356
459
  similarityResults = blastSimilarity(cmdValues)
357
460
 
358
461
  with open(cmdValues["output"],"w") as simData:
@@ -365,6 +468,19 @@ def similarity(cmdValues):
365
468
  bar.next()
366
469
  bar.finish()
367
470
 
471
+ elif (cmdValues["blastani"]) and (not cmdValues["blast"]) and (not cmdValues["aln"]) and (not cmdValues["mash"]) and (not cmdValues["fastani"]):
472
+ similarityResults = blastANISimilarity(cmdValues)
473
+
474
+ with open(cmdValues["output"],"w") as simData:
475
+ simData.write("seq1\tseq2\tpid\n")
476
+
477
+ bar = ChargingBar('Writing similarity values', max=len(similarityResults))
478
+ for itemPair in similarityResults:
479
+ simData.write(f"{"\t".join([str(r) for r in itemPair])}\n")
480
+
481
+ bar.next()
482
+ bar.finish()
483
+
368
484
  else:
369
485
  pass
370
486
 
@@ -1355,11 +1471,13 @@ def tabtkMain():
1355
1471
  dest='fastani',help='Calculate similarity using fastANI')
1356
1472
  similarityOptions.add_argument('--blast','-b',action='store_true',default=False,
1357
1473
  dest='blast',help='Calculate similarity using BLAST')
1474
+ similarityOptions.add_argument('--blastani','-x',action='store_true',default=False,
1475
+ dest='blastani',help='Calculate blast ANI similarity')
1358
1476
  similarityOptions.add_argument('--aln','-a',action='store_true',default=False,
1359
1477
  dest='aln',help='Calculate similarity based on a given sequence alignment')
1360
1478
  similarityOptions.add_argument('--fraction','-c',action='store',required=False,nargs=1,
1361
1479
  metavar='fraction',dest='fraction',default=0.70,type=float,
1362
- help='Minimum overlap fraction with shorter sequence for ANI calculation; use with --fastani (default: 0.70)')
1480
+ help='Minimum overlap fraction with shorter sequence for ANI calculation; use with --fastani or --blastani (default: 0.70)')
1363
1481
  similarityOptions.add_argument('--fragment','-r',action='store',required=False,nargs=1,
1364
1482
  metavar='fragment',dest='fragment',default=500,type=int,
1365
1483
  help='Fragment length for calculating ANI; use with --fastani (default: 500)')
@@ -1590,6 +1708,7 @@ def tabtkMain():
1590
1708
  'mash': options.mash,
1591
1709
  'fastani': options.fastani,
1592
1710
  'blast': options.blast,
1711
+ 'blastani': options.blastani,
1593
1712
  'aln': options.aln,
1594
1713
  'evalue': float(options.evalue[0:][0]) if isinstance(options.evalue,list) else float(options.evalue),
1595
1714
  'threads': int(options.threads[0:][0]) if isinstance(options.threads,list) else int(options.threads)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: tabtk
3
- Version: 1.0.7
3
+ Version: 1.0.9
4
4
  Summary: tabtk: A toolkit for sequence similarity analysis based on tabular data
5
5
  Author-email: Chrispin Chaguza <chrispin.chaguza@gmail.com>
6
6
  Maintainer-email: Chrispin Chaguza <chrispin.chaguza@gmail.com>
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes