forked from ziyewang/MetaBinner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMetabinner.py
1385 lines (1194 loc) · 65.2 KB
/
Metabinner.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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
# @Author : ZWang
# @FileName: Metabinner.py
# scikit-learn == 0.20.4
import argparse
import csv
import logging
import math
import mimetypes
import os
import re
import shutil
import subprocess
import sys
import time
import gzip
import functools
from argparse import RawTextHelpFormatter
import numpy as np
import pandas as pd
import util
from Bio import SeqIO
# from sklearn.cluster import KMeans
from sklearn.cluster import DBSCAN
import sklearn.cluster.k_means_ as kmeans
import scipy.sparse as sp
from sklearn.cluster.k_means_ import euclidean_distances, stable_cumsum, KMeans, check_random_state, row_norms
from sklearn.metrics import pairwise_distances
from multiprocessing import Pool
# from concurrent.futures import ProcessPoolExecutor
# pool = Pool()
from functools import partial
logger = logging.getLogger('Metabinner')
logger.setLevel(logging.INFO)
# logging
formatter = logging.Formatter('%(asctime)s - %(message)s')
console_hdr = logging.StreamHandler()
console_hdr.setFormatter(formatter)
logger.addHandler(console_hdr)
def arguments():
parser = argparse.ArgumentParser()
parser.add_argument('--contig_file', type=str, help=("The contigs file."))
parser.add_argument('--coverage_profiles', type=str, help=(
"The coverage profiles, containing a table where each row correspond to a contig, and each column correspond to a sample. All values are separated with tabs."))
parser.add_argument('--composition_profiles', type=str, help=(
"The composition profiles, containing a table where each row correspond to a contig, and each column correspond to the kmer composition of particular kmer. All values are separated with comma."))
parser.add_argument('--output', type=str, help="The output file, storing the binning result.")
parser.add_argument('--log', type=str, help="Specify where to store log file")
parser.add_argument('--clusters', default=0, type=int,
help="Specify the number of clusters. If not specified, the cluster number is estimated by single-copy genes.")
"""parser.add_argument('--weight_length', action="store_true",
help="use contig length as the weight of the contigs") ##?
parser.add_argument('--seed', action='store_true',
help="use part of the contigs with single-marker gene as the initial of the kmeans")"""
parser.add_argument('--use_hmm', action="store_true", help="use hmm profile as another feature")
parser.add_argument('--hmm_icm_path', type=str,
help="The path that contains trained imm model (please end with '/').")
parser.add_argument('--hmm_file', type=str, help=("The hmm profiles."))
parser.add_argument('--pacbio_read_profiles', type=str, help=(
"The pacbio_read coverage profiles, containing a table where each row correspond to a contig, and each column correspond to a sample. All values are separated with tabs."))
parser.add_argument('--binscore', default=0.3, type=float,
help="Specify the score threshold for das_tool.")
args = parser.parse_args()
if not (args.contig_file and args.coverage_profiles and args.composition_profiles and args.output):
parser.error(
"Data is missing, add file(s) using --contig_file <contig_file> and/or --coverage_profiles <abund_profiles> and/or --composition_profiles <comp_profiles> and/or --output <out_file>")
sys.exit(0)
return args
def gen_X(com_file, cov_file):
covHeader = pd.read_csv(cov_file, sep='\t', nrows=1)
covMat = pd.read_csv(cov_file, sep='\t', usecols=range(1, covHeader.shape[1])).values
namelist = pd.read_csv(cov_file, sep='\t', usecols=range(1)).values[:, 0]
mapObj = dict(zip(namelist, range(len(namelist))))
compositHeader = pd.read_csv(com_file, sep=',', nrows=1)
shuffled_compositMat = pd.read_csv(com_file, sep=',', usecols=range(1, compositHeader.shape[1])).values
shuffled_namelist = pd.read_csv(com_file, sep=',', usecols=range(1)).values[:, 0]
covIdxArr = np.empty(len(mapObj), dtype=np.int)
for contigIdx in range(len(shuffled_namelist)):
if shuffled_namelist[contigIdx] in mapObj:
covIdxArr[mapObj[shuffled_namelist[contigIdx]]] = contigIdx
compositMat = shuffled_compositMat[covIdxArr]
covMat = covMat + 1e-2
covMat = covMat / covMat.sum(axis=0)[None, :]
if covMat.shape[1] >= 10:
covMat = covMat / covMat.sum(axis=1)[:, None]
compositMat = compositMat + 1
compositMat = compositMat / compositMat.sum(axis=1)[:, None]
X_t = np.hstack((covMat, compositMat)) * 1e1
return X_t, namelist, mapObj, covMat, compositMat
def gen_X_cov_pb(cov_pb_file, mapObj):
cov_pb_Header = pd.read_csv(cov_pb_file, sep='\t', nrows=1)
shuffled_cov_pb_Mat = pd.read_csv(cov_pb_file, sep='\t', usecols=range(1, cov_pb_Header.shape[1])).values
shuffled_namelist = pd.read_csv(cov_pb_file, sep='\t', usecols=range(1)).values[:, 0]
covIdxArr = []
for contigIdx in range(len(shuffled_namelist)):
assert (shuffled_namelist[contigIdx] in mapObj)
covIdxArr.append(mapObj[shuffled_namelist[contigIdx]])
cov_pb_Mat = shuffled_cov_pb_Mat.copy()
cov_pb_Mat[covIdxArr, :] = shuffled_cov_pb_Mat
cov_pb_Mat = cov_pb_Mat + 1e-2
cov_pb_Mat = cov_pb_Mat / cov_pb_Mat.sum(axis=0)[None, :]
if cov_pb_Mat.shape[1] >= 10:
cov_pb_Mat = cov_pb_Mat / cov_pb_Mat.sum(axis=1)[:, None]
return cov_pb_Mat
def gen_X_hmm(hmm_file, cov_file):
print("hmm_profiles:\t" + hmm_file)
print("abundance_profiles:\t" + cov_file)
namelist = pd.read_csv(cov_file, sep='\t', usecols=range(1)).values[:, 0]
mapObj = dict(zip(namelist, range(len(namelist))))
hmmHeader = pd.read_csv(hmm_file, sep='\t', nrows=1)
shuffled_hmmMat = pd.read_csv(hmm_file, sep='\t', usecols=range(1, hmmHeader.shape[1])).values
shuffled_namelist = pd.read_csv(hmm_file, sep='\t', usecols=range(1)).values[:, 0]
covIdxArr = np.empty(len(mapObj), dtype=np.int)
for contigIdx in range(len(shuffled_namelist)): # 就这一处用到shuffled_namelist
if shuffled_namelist[contigIdx] in mapObj:
covIdxArr[mapObj[shuffled_namelist[contigIdx]]] = contigIdx
hmmMat = shuffled_hmmMat[covIdxArr]
return hmmMat
# changed from scimm
def score_reads(path, files, readsf, par, output): # 需要改路径
cmds = []
hmm_score_dir = os.path.dirname(output) + '/hmm_score'
simple_scoreURL = os.path.join(os.getcwd(), 'simple-score')
os.system("chmod 777 " + simple_scoreURL)
os.mkdir(hmm_score_dir)
for file in files:
"""if not os.path.isdir(path + file):
cmds.append(
'/home/wzy/binning/scimm/bin/simple-score -N %shmm/%s.icm < %s > %sscore/%s.scores.tmp ' % (
path, file, readsf, path, file))"""
if not os.path.isdir(path + file):
cmds.append(
'%s -N %s%s < %s > %s/%s.scores.tmp ' % (simple_scoreURL,
path, file, readsf, hmm_score_dir, file))
util.exec_par(cmds, par)
def get_read_probs(path, files, output):
hmm_score_dir = os.path.dirname(output) + '/hmm_score/'
k = len(os.listdir(hmm_score_dir))
read_likes = {}
c = 0
for file in files:
if not os.path.isdir(path + file):
for line in open('%s%s.scores.tmp' % (hmm_score_dir, file)):
(r, s) = line.split('\t')
r = r.strip()
# 增加能去掉字符串k141_12130 flag=1 multi=29.9095 len=7194后面一�?
r = r.split()[0]
if not r in read_likes:
read_likes[r] = [0] * k
read_likes[r][c] = float(s)
c = c + 1
read_probs = {}
likelihood = 0.0
for r in read_likes:
# combine mate likelihoods and priors
r1 = read_likes[r]
# read_scores = [r1[x] + math.log(priors[x]) for x in range(k)]
read_scores = [r1[x] for x in range(k)]
# determine probabilities of assignments
sum_score = read_scores[0]
for i in range(1, k):
sum_score = log_add(sum_score, read_scores[i])
read_probs[r] = []
for i in range(k):
sc = math.exp(read_scores[i] - sum_score)
if sc > 1e-8:
read_probs[r].append(sc)
else:
read_probs[r].append(0)
# update likelihood, accounting for mates being assigned twice
likelihood += max(read_scores)
return likelihood, read_probs
def log_add(l_i, l_j):
if l_i > l_j:
return l_i + math.log(1 + math.exp((l_j - l_i)))
else:
return l_j + math.log(1 + math.exp((l_i - l_j)))
def gen_bestk(contig_file, X_mat, bestK=0): # 改成无论是否固定k都要跑生成seed,去掉没有生成的话从5开始随机的情况
fragScanURL = os.path.join(os.getcwd(), 'auxiliary', 'FragGeneScan1.19', 'run_FragGeneScan.pl')
os.system("chmod +777 " + fragScanURL)
hmmExeURL = os.path.join(os.getcwd(), 'auxiliary', 'hmmer-3.1b1', 'bin', 'hmmsearch')
os.system("chmod 777 " + hmmExeURL)
markerExeURL = os.path.join(os.getcwd(), 'auxiliary', 'test_getmarker.pl')
os.system("chmod 777 " + markerExeURL)
markerURL = os.path.join(os.getcwd(), 'auxiliary', 'marker.hmm')
seedURL = contig_file + ".seed"
fragResultURL = contig_file + ".frag.faa"
hmmResultURL = contig_file + ".hmmout"
if not (os.path.exists(fragResultURL)):
fragCmd = fragScanURL + " -genome=" + contig_file + " -out=" + contig_file + ".frag -complete=0 -train=complete -thread=48 1>" + contig_file + ".frag.out 2>" + contig_file + ".frag.err"
logger.info("exec cmd: " + fragCmd)
os.system(fragCmd)
if os.path.exists(fragResultURL):
if not (os.path.exists(hmmResultURL)):
hmmCmd = hmmExeURL + " --domtblout " + hmmResultURL + " --cut_tc --cpu 48 " + markerURL + " " + fragResultURL + " 1>" + hmmResultURL + ".out 2>" + hmmResultURL + ".err"
logger.info("exec cmd: " + hmmCmd)
os.system(hmmCmd)
if os.path.exists(hmmResultURL):
if not (os.path.exists(seedURL)):
markerCmd = markerExeURL + " " + hmmResultURL + " " + contig_file + " 1000 " + seedURL
logger.info("exec cmd: " + markerCmd)
os.system(markerCmd)
if os.path.exists(seedURL):
candK = file_len(seedURL)
maxK = 2 * candK
stepK = 2
else:
logger.info("seed not exist, k start from 2 ")
# sys.exit()
candK = 2
maxK = 20
stepK = 2
else:
logger.info("Hmmsearch failed! Not exist: " + hmmResultURL)
sys.exit()
else:
logger.info("FragGeneScan failed! Not exist: " + fragResultURL)
sys.exit()
if bestK == 0:
bestK = candK
bestSilVal = 0
t = time.time()
for k in range(candK, maxK, stepK):
kmeans = KMeans(n_clusters=k, init='k-means++', random_state=9, n_jobs=-1)
kmeans.fit(X_mat)
silVal = silhouette(X_mat, kmeans.cluster_centers_, kmeans.labels_)
logger.info("k:" + str(k) + "\tsilhouette:" + str(silVal) + "\telapsed time:" + str(time.time() - t))
t = time.time()
if silVal > bestSilVal:
bestSilVal = silVal
bestK = k
else:
break
candK = bestK + 4
bestSilVal_2nd = 0
for k in range(candK, maxK, stepK):
kmeans = KMeans(n_clusters=k, init='k-means++', random_state=9, n_jobs=-1)
kmeans.fit(X_mat)
silVal_2nd = silhouette(X_mat, kmeans.cluster_centers_, kmeans.labels_)
logger.info("k:" + str(k) + "\tsilhouette:" + str(silVal_2nd) + "\telapsed time:" + str(time.time() - t))
t = time.time()
if silVal_2nd > bestSilVal_2nd:
bestSilVal_2nd = silVal_2nd
bestK = k
else:
break
if bestSilVal_2nd > bestSilVal:
bestSilVal = bestSilVal_2nd
else:
bestK = candK - 4
logger.info("bestk:" + str(bestK) + "\tsilVal:" + str(bestSilVal))
else:
logger.info("Use the pre-specified cluster number! k=" + str(bestK))
return bestK
def get_length(fastx_file):
file_type = mimetypes.guess_type(fastx_file)[1]
if file_type == 'gzip':
f = gzip.open(fastx_file, "rt")
elif not file_type:
f = open(fastx_file, "rt")
else:
raise RuntimeError("Unknown type of file: '{}".format(fastx_file))
length = {}
if os.path.getsize(fastx_file) == 0:
return length
file_format = None
line = f.readline()
if line.startswith('@'):
file_format = "fastq"
elif line.startswith(">"):
file_format = "fasta"
f.seek(0)
if not file_format:
raise RuntimeError("Invalid sequence file: '{}".format(fastx_file))
for seq_record in SeqIO.parse(f, file_format):
length[seq_record.id] = len(seq_record.seq)
f.close()
return length
def file_len(fname):
with open(fname) as f:
for i, l in enumerate(f):
pass
return i + 1
def silhouette(X, W, label):
X_colsum = np.sum(X ** 2, axis=1)
X_colsum = X_colsum.reshape(len(X_colsum), 1)
W_colsum = np.sum(W ** 2, axis=1)
W_colsum = W_colsum.reshape(len(W_colsum), 1)
Dsquare = np.tile(X_colsum, (1, W.shape[0])) + np.tile(W_colsum.T, (X.shape[0], 1)) - 2 * X.dot(W.T)
# avoid error caused by accuracy
Dsquare[Dsquare < 0] = 0
D = np.sqrt(Dsquare)
aArr = D[np.arange(D.shape[0]), label]
D[np.arange(D.shape[0]), label] = np.inf
bArr = np.min(D, axis=1)
tmp = (bArr - aArr) / np.maximum(aArr, bArr)
return np.mean(tmp)
def gen_bins(fastafile, resultfile, outputdir, prefix_str):
# read fasta file
logger.info("Processing file:\t{}".format(fastafile))
sequences = {}
if fastafile.endswith("gz"):
with gzip.open(fastafile, 'r') as f:
for line in f:
line = str(line, encoding="utf-8")
if line.startswith(">"):
if " " in line:
seq, others = line.split(' ', 1)
sequences[seq] = ""
else:
seq = line.rstrip("\n")
sequences[seq] = ""
else:
sequences[seq] += line.rstrip("\n")
else:
with open(fastafile, 'r') as f:
for line in f:
if line.startswith(">"):
if " " in line:
seq, others = line.split(' ', 1)
sequences[seq] = ""
else:
seq = line.rstrip("\n")
sequences[seq] = ""
else:
sequences[seq] += line.rstrip("\n")
logger.info("Reading Map:\t{}".format(resultfile))
dic = {}
with open(resultfile, "r") as f:
for line in f:
contig_name, cluster_name = line.strip().split('\t') # change from split(',')
try:
dic[cluster_name].append(contig_name)
except:
dic[cluster_name] = []
dic[cluster_name].append(contig_name)
logger.info("Writing bins:\t{}".format(outputdir))
if not os.path.exists(outputdir):
os.makedirs(outputdir)
bin_name = 0
for _, cluster in dic.items():
binfile = os.path.join(outputdir, "{}_{}.bin".format(prefix_str, bin_name))
with open(binfile, "w") as f:
for contig_name in cluster:
contig_name = ">" + contig_name
try:
sequence = sequences[contig_name]
except:
bin_name += 1
continue
f.write(contig_name + "\n")
f.write(sequence + "\n")
bin_name += 1
def save_result(result, filepath, namelist):
filedir, filename = os.path.split(filepath)
if not filename:
filename = "result.tsv"
if not os.path.exists(filedir):
os.makedirs(filedir)
f = open(filepath, 'w')
for contigIdx in range(len(result)):
f.write(namelist[contigIdx] + "\t" + str(result[contigIdx].item(0)) + "\n")
f.close()
def save_result_refine(result, filepath, namelist, unclassified_contigs_id_number):
filedir, filename = os.path.split(filepath)
if not filename:
filename = "result.tsv"
if not os.path.exists(filedir):
os.makedirs(filedir)
f = open(filepath, 'w')
for Idx in range(len(result)):
f.write(namelist[unclassified_contigs_id_number[Idx]] + "\t" + str(result[Idx].item(0)) + "\n")
f.close()
# change from binsanity
def checkm_analysis(file_, suffix_str, output):
file_object = open(file_, 'r')
lines = file_object.readlines()
file_deal = open(file_ + '_deal.txt', 'w')
try:
for line in lines:
if line.startswith(' '):
print(line)
file_deal.writelines(line)
finally:
file_object.close()
file_deal.close() # 要不会影响读�?
checkm = list(csv.reader(open(file_ + '_deal.txt', 'r')))
new = []
for list_ in checkm:
x = re.sub(' +', ' ', str(re.split(r'\t+', list_[0].rstrip('\t'))))
new.append(x)
del new[0]
checkm_info_list = [list_.strip("['']") for list_ in new]
checkm_info_list = [x.split() for x in checkm_info_list]
good_bins = []
High_completion_high_contamination = []
# low_completion=[]
others = []
for list_ in checkm_info_list:
if ((float(list_[12]) > 70 and (float(list_[13]) < 15)) or (float(list_[12]) - 5 * float(list_[13])) > 50):
good_bins.append(list_[0])
elif (float(list_[12]) > 70 and (float(list_[13]) > 50)):
High_completion_high_contamination.append(list_[0])
else:
others.append(list_[0])
if os.path.isdir(os.path.dirname(output) + "/good_bins") is False:
os.makedirs(os.path.dirname(output) + "/good_bins")
if os.path.isdir(os.path.dirname(output) + "/High_completion_high_contamination") is False:
os.makedirs(os.path.dirname(output) + "/High_completion_high_contamination")
if os.path.isdir(os.path.dirname(output) + "/others") is False:
os.makedirs(os.path.dirname(output) + "/others")
for name in good_bins:
shutil.move((os.path.dirname(output) + '/' + str(name) + suffix_str),
os.path.dirname(output) + "/good_bins")
for name in High_completion_high_contamination:
shutil.move((os.path.dirname(output) + '/' + str(name) + suffix_str),
os.path.dirname(output) + "/High_completion_high_contamination")
for name in others:
shutil.move((os.path.dirname(output) + '/' + str(name) + suffix_str), os.path.dirname(output) + "/others")
# change from sklearn.cluster.kmeans
def partial_seed_init(X, n_clusters, random_state, seed_idx, n_local_trials=None):
logger.info('Partial Seed Initialization')
random_state = check_random_state(random_state)
x_squared_norms = row_norms(X, squared=True)
n_samples, n_features = X.shape
centers = np.empty((n_clusters, n_features), dtype=X.dtype)
assert x_squared_norms is not None, 'x_squared_norms None in _k_init'
# Set the number of local seeding trials if none is given
if n_local_trials is None:
# This is what Arthur/Vassilvitskii tried, but did not report
# specific results for other than mentioning in the conclusion
# that it helped.
n_local_trials = 2 + int(np.log(n_clusters))
# global seed_idx
# Pick first center randomly ###修改�?
if len(seed_idx) != n_samples:
while True:
center_id = random_state.randint(n_samples)
if center_id not in seed_idx:
break
if sp.issparse(X):
centers[0] = X[center_id].toarray()
else:
centers[0] = X[center_id]
# Initialize list of closest distances and calculate current potential
closest_dist_sq = euclidean_distances(
centers[0, np.newaxis], X, Y_norm_squared=x_squared_norms,
squared=True)
current_pot = closest_dist_sq.sum()
# Pick the remaining n_clusters-1 points
for c in range(1, n_clusters - len(seed_idx)): # changed
# Choose center candidates by sampling with probability proportional
# to the squared distance to the closest existing center
rand_vals = random_state.random_sample(n_local_trials) * current_pot
candidate_ids = np.searchsorted(stable_cumsum(closest_dist_sq),
rand_vals)
# Compute distances to center candidates
distance_to_candidates = euclidean_distances(
X[candidate_ids], X, Y_norm_squared=x_squared_norms, squared=True)
# Decide which candidate is the best
best_candidate = None
best_pot = None
best_dist_sq = None
for trial in range(n_local_trials):
# Compute potential when including center candidate
new_dist_sq = np.minimum(closest_dist_sq,
distance_to_candidates[trial])
new_pot = new_dist_sq.sum()
# Store result if it is the best local trial so far
if ((best_candidate is None) or (new_pot < best_pot)) and (
(candidate_ids[trial]) not in seed_idx): # changed
best_candidate = candidate_ids[trial]
best_pot = new_pot
best_dist_sq = new_dist_sq
# Permanently add best center candidate found in local tries
if sp.issparse(X):
centers[c] = X[best_candidate].toarray()
else:
centers[c] = X[best_candidate]
current_pot = best_pot
closest_dist_sq = best_dist_sq
if sp.issparse(X):
centers[(n_clusters - len(seed_idx)):n_clusters] = X[seed_idx].toarray()
else:
centers[(n_clusters - len(seed_idx)):n_clusters] = X[seed_idx]
# print(seed_idx)
return centers
def recluster_other_contigs(not_clustered_path, X_t, namelist, mapObj, length_weight):
files = os.listdir(not_clustered_path)
other_contig_file = not_clustered_path + '/init_unclustered_contigs.fa'
ofile = open(other_contig_file, 'w')
# 遍历读取所有文件,并写入到输出文件
for fr in files:
if fr != 'init_unclustered_contigs.fa':
for txt in open(not_clustered_path + '/' + fr, 'r'):
ofile.write(txt)
ofile.close()
unclassified_contigs_id = []
for seq_record in SeqIO.parse(other_contig_file, "fasta"):
unclassified_contigs_id.append(seq_record.id)
unclassified_contigs_id_number = [mapObj[x] for x in unclassified_contigs_id]
X_t_unclustered = X_t[unclassified_contigs_id_number]
bin_number = gen_bestk(other_contig_file, X_t_unclustered, 0)
logger.info("bin_number for other contigs: %d", bin_number)
unclassified_contigs_weight = []
for i in range(len(unclassified_contigs_id_number)):
unclassified_contigs_weight.append(length_weight[unclassified_contigs_id_number[i]])
seedURL = other_contig_file + ".seed"
# global seed_idx
if os.path.exists(seedURL):
seed_list = []
with open(seedURL) as f:
for line in f:
seed_list.append(line.rstrip('\n'))
name_map = dict(zip(unclassified_contigs_id, range(len(unclassified_contigs_id))))
seed_idx = [name_map[seed_name] for seed_name in seed_list]
km = KMeans(n_clusters=bin_number, n_jobs=-1, n_init=30, random_state=7,
init=functools.partial(partial_seed_init, seed_idx=seed_idx))
else:
km = KMeans(n_clusters=bin_number, n_jobs=-1, n_init=30, random_state=7)
logger.info("Start bin the other bins.")
# 之后可以加GC,像binsanity那样
km.fit(X_t_unclustered, sample_weight=unclassified_contigs_weight)
idx = km.labels_
not_clustered_path_output = not_clustered_path + 'reclustered_result.tsv'
save_result_refine(idx, not_clustered_path_output, namelist, unclassified_contigs_id_number)
gen_bins(other_contig_file, not_clustered_path_output, os.path.dirname(not_clustered_path) + '/good_bins',
"reclustered")
def gen_seed_number(contig_file):
fragScanURL = os.path.join(os.getcwd(), 'auxiliary', 'FragGeneScan1.19', 'run_FragGeneScan.pl')
os.system("chmod 777 " + fragScanURL)
hmmExeURL = os.path.join(os.getcwd(), 'auxiliary', 'hmmer-3.1b1', 'bin', 'hmmsearch')
os.system("chmod 777 " + hmmExeURL)
markerExeURL = os.path.join(os.getcwd(), 'auxiliary', 'test_getmarker.pl')
os.system("chmod 777 " + markerExeURL)
markerURL = os.path.join(os.getcwd(), 'auxiliary', 'marker.hmm')
seedURL = contig_file + ".seed"
fragResultURL = contig_file + ".frag.faa"
hmmResultURL = contig_file + ".hmmout"
if not (os.path.exists(fragResultURL)):
fragCmd = fragScanURL + " -genome=" + contig_file + " -out=" + contig_file + ".frag -complete=0 -train=complete -thread=10 1>" + contig_file + ".frag.out 2>" + contig_file + ".frag.err"
logger.info("exec cmd: " + fragCmd)
os.system(fragCmd)
if os.path.exists(fragResultURL):
if not (os.path.exists(hmmResultURL)):
hmmCmd = hmmExeURL + " --domtblout " + hmmResultURL + " --cut_tc --cpu 10 " + markerURL + " " + fragResultURL + " 1>" + hmmResultURL + ".out 2>" + hmmResultURL + ".err"
logger.info("exec cmd: " + hmmCmd)
os.system(hmmCmd)
if os.path.exists(hmmResultURL):
if not (os.path.exists(seedURL)):
markerCmd = markerExeURL + " " + hmmResultURL + " " + contig_file + " 1000 " + seedURL
logger.info("exec cmd: " + markerCmd)
os.system(markerCmd)
if os.path.exists(seedURL):
candK = file_len(seedURL)
else:
logger.info("markerCmd failed! Not exist: " + markerCmd)
candK = 2
else:
logger.info("Hmmsearch failed! Not exist: " + hmmResultURL)
sys.exit()
else:
logger.info("FragGeneScan failed! Not exist: " + fragResultURL)
sys.exit()
return candK
def gen_seed_number_bacar_marker(contig_file):
fragScanURL = os.path.join(os.getcwd(), 'auxiliary', 'FragGeneScan1.19', 'run_FragGeneScan.pl')
os.system("chmod 777 " + fragScanURL)
hmmExeURL = os.path.join(os.getcwd(), 'auxiliary', 'hmmer-3.1b1', 'bin', 'hmmsearch')
os.system("chmod 777 " + hmmExeURL)
markerExeURL = os.path.join(os.getcwd(), 'auxiliary', 'test_getmarker.pl')
os.system("chmod 777 " + markerExeURL)
markerURL = os.path.join(os.getcwd(), 'auxiliary', 'bacar_marker.hmm')
seedURL = contig_file + ".bacar_marker.seed"
fragResultURL = contig_file + ".frag.faa"
hmmResultURL = contig_file + ".bacar_marker.hmmout"
if not (os.path.exists(fragResultURL)):
fragCmd = fragScanURL + " -genome=" + contig_file + " -out=" + contig_file + ".frag -complete=0 -train=complete -thread=20 1>" + contig_file + ".frag.out 2>" + contig_file + ".frag.err"
logger.info("exec cmd: " + fragCmd)
os.system(fragCmd)
if os.path.exists(fragResultURL):
if not (os.path.exists(hmmResultURL)):
hmmCmd = hmmExeURL + " --domtblout " + hmmResultURL + " --cut_tc --cpu 20 " + markerURL + " " + fragResultURL + " 1>" + hmmResultURL + ".out 2>" + hmmResultURL + ".err"
logger.info("exec cmd: " + hmmCmd)
os.system(hmmCmd)
if os.path.exists(hmmResultURL):
if not (os.path.exists(seedURL)):
markerCmd = markerExeURL + " " + hmmResultURL + " " + contig_file + " 1000 " + seedURL
logger.info("exec cmd: " + markerCmd)
os.system(markerCmd)
if os.path.exists(seedURL):
candK = file_len(seedURL)
else:
logger.info("markerCmd failed! Not exist: " + markerCmd)
candK = 0
else:
logger.info("Hmmsearch failed! Not exist: " + hmmResultURL)
sys.exit()
else:
logger.info("FragGeneScan failed! Not exist: " + fragResultURL)
sys.exit()
if candK == 0:
bestK = 0
else:
bestK = candK
maxK = 2 * candK
stepK = 2
bestSilVal = 0
t = time.time()
for k in range(candK, maxK, stepK):
kmeans = KMeans(n_clusters=k, init='k-means++', random_state=9, n_jobs=-1)
kmeans.fit(X_t)
silVal = silhouette(X_t, kmeans.cluster_centers_, kmeans.labels_)
logger.info("k:" + str(k) + "\tsilhouette:" + str(silVal) + "\telapsed time:" + str(time.time() - t))
t = time.time()
if silVal > bestSilVal:
bestSilVal = silVal
bestK = k
else:
break
candK = bestK + 4
bestSilVal_2nd = 0
for k in range(candK, maxK, stepK):
kmeans = KMeans(n_clusters=k, init='k-means++', random_state=9, n_jobs=-1)
kmeans.fit(X_t)
silVal_2nd = silhouette(X_t, kmeans.cluster_centers_, kmeans.labels_)
logger.info("k:" + str(k) + "\tsilhouette:" + str(silVal_2nd) + "\telapsed time:" + str(time.time() - t))
t = time.time()
if silVal_2nd > bestSilVal_2nd:
bestSilVal_2nd = silVal_2nd
bestK = k
else:
break
if bestSilVal_2nd > bestSilVal:
bestSilVal = bestSilVal_2nd
else:
bestK = candK - 4
logger.info("bestk:" + str(bestK) + "\tsilVal:" + str(bestSilVal))
return bestK
def recluster_hh_bins(high_com_p_high_cont_path, mapObj, X_t, length_weight, namelist):
hh_files = os.listdir(high_com_p_high_cont_path)
for file_ in hh_files:
if file_.endswith('.bin'):
hh_contigs_id = []
hh_contig_file = high_com_p_high_cont_path + '/' + file_
for seq_record in SeqIO.parse(hh_contig_file, "fasta"):
hh_contigs_id.append(seq_record.id)
hh_contigs_id_number = [mapObj[x] for x in hh_contigs_id]
X_t_hh_unclustered = X_t[hh_contigs_id_number]
bin_number = gen_bestk(hh_contig_file, X_t_hh_unclustered, 0)
hh_weight = []
for i in range(len(hh_contigs_id_number)):
hh_weight.append(length_weight[hh_contigs_id_number[i]])
# seedurl不一定存�?
seedURL = hh_contig_file + ".seed"
# global seed_idx
if os.path.exists(seedURL):
seed_list = []
with open(seedURL) as f:
for line in f:
seed_list.append(line.rstrip('\n'))
name_map = dict(zip(hh_contigs_id, range(len(hh_contigs_id))))
seed_idx = [name_map[seed_name] for seed_name in seed_list]
km = KMeans(n_clusters=bin_number, n_jobs=-1, n_init=30, random_state=7,
init=functools.partial(partial_seed_init, seed_idx=seed_idx))
else:
km = KMeans(n_clusters=bin_number, n_jobs=-1, n_init=30, random_state=7)
km.fit(X_t_hh_unclustered, sample_weight=hh_weight)
idx = km.labels_
save_result_refine(idx, hh_contig_file + ".reclustered.tsv",
namelist, hh_contigs_id_number)
gen_bins(hh_contig_file, hh_contig_file + ".reclustered.tsv",
os.path.dirname(high_com_p_high_cont_path) + '/good_bins', file_ + "_reclustered")
def read_fasta_file(fasta_file):
with open(fasta_file, 'r') as read_handler:
for line in read_handler:
line = line.strip()
if not line:
continue
if line.startswith(">"):
yield line[1:]
def convert(paths, output_file):
files = os.listdir(paths)
fasta_files = []
for file in files:
if file.endswith(('.fasta', '.fa', '.fna', '.bin')):
fasta_files.append(file)
with open(output_file, 'w') as write_handler:
for bin_id, fasta_file in enumerate(fasta_files):
for sequence_id in read_fasta_file(paths + '/' + fasta_file):
write_handler.write("%s\t%s\n" % (sequence_id, bin_id)) # change from ","
def calculate_eps_for_l1(goodbin_path, mapObj, X_t, length_weight, namelist):
goodbin_files = os.listdir(goodbin_path)
score = []
for file_ in goodbin_files:
if file_.endswith('.bin'):
goodbin_contigs_id = []
goodbin_contig_file = goodbin_path + '/' + file_
for seq_record in SeqIO.parse(goodbin_contig_file, "fasta"):
goodbin_contigs_id.append(seq_record.id)
goodbin_contigs_id_number = [mapObj[x] for x in goodbin_contigs_id]
X_t_goodbin = X_t[goodbin_contigs_id_number]
l1_distance_score = pairwise_distances(X_t_goodbin, metric='l1', n_jobs=-1)
print(len(X_t_goodbin))
if len(X_t_goodbin) >= 20: # 这个20的数是根据minsamples的信息估算的
l1_distance_score = np.sort(l1_distance_score, axis=-1)[:, 19]
l1_distance_score = np.mean(l1_distance_score)
# l1_distance_score=np.max(l1_distance_score)#这个想法不对,eps是邻�?
score.append(l1_distance_score)
return score
def gen_remained_fasta_file(fastafile, remained_contig_id, outputdir, prefix_str):
# read fasta file
logger.info("Processing file:\t{}".format(fastafile))
sequences = {}
if fastafile.endswith("gz"):
with gzip.open(fastafile, 'r') as f:
for line in f:
line = str(line, encoding="utf-8")
if line.startswith(">"):
if " " in line:
seq, others = line.split(' ', 1)
sequences[seq] = ""
else:
seq = line.rstrip("\n")
sequences[seq] = ""
else:
sequences[seq] += line.rstrip("\n")
else:
with open(fastafile, 'r') as f:
for line in f:
if line.startswith(">"):
if " " in line:
seq, others = line.split(' ', 1)
sequences[seq] = ""
else:
seq = line.rstrip("\n")
sequences[seq] = ""
else:
sequences[seq] += line.rstrip("\n")
dic = {}
cluster_name = 'remained'
for contig_name in remained_contig_id:
try:
dic[cluster_name].append(contig_name)
except:
dic[cluster_name] = []
dic[cluster_name].append(contig_name)
logger.info("Writing bins:\t{}".format(outputdir))
if not os.path.exists(outputdir):
os.makedirs(outputdir)
bin_name = 0
for _, cluster in dic.items():
binfile = os.path.join(outputdir, "{}_{}.bin".format(prefix_str, bin_name))
with open(binfile, "w") as f:
for contig_name in cluster:
contig_name = ">" + contig_name
try:
sequence = sequences[contig_name]
except:
bin_name += 1
continue
f.write(contig_name + "\n")
f.write(sequence + "\n")
bin_name += 1
if __name__ == '__main__':
args = arguments()
# kmeans._k_init = _k_init
if args.log:
handler = logging.FileHandler(args.log)
handler.setLevel(logging.INFO)
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.info("Input arguments:")
logger.info("contig_file:\t" + args.contig_file)
logger.info("coverage_profiles:\t" + args.coverage_profiles)
logger.info("composition_profiles:\t" + args.composition_profiles)
logger.info("output path:\t" + args.output)
logger.info("clusters:\t" + (str(args.clusters) if args.clusters > 0 else "Auto"))
com_file = args.composition_profiles
cov_file = args.coverage_profiles
X_t, namelist, mapObj, X_cov_sr, X_com = gen_X(com_file, cov_file)
contigNum = X_t.shape[0]
contig_file = args.contig_file
if args.pacbio_read_profiles:
X_cov_pb = gen_X_cov_pb(args.pacbio_read_profiles, mapObj)
X_cov = np.hstack((X_cov_sr, X_cov_pb))
X_t = np.hstack((X_t, X_cov_pb * 1e1))
else:
X_cov = X_cov_sr
if args.use_hmm:
if not args.hmm_file:
path = args.hmm_icm_path # '/mnt/data3/wzy/ncbi_genomes_metawatt_process/filter_small_genome/' # 需要改路径
files = os.listdir(path)
par = 40
# train_imm(path, files, par) # 直接用训练好的就�?
score_reads(path, files, contig_file, par, args.output)
likelihood, read_probs = get_read_probs(path, files, args.output)
df = pd.DataFrame(read_probs)
df = df.T
df[df < 1e-8] = 0
hmm_file = os.path.join(os.path.abspath(os.path.dirname(args.output)), 'hmm_profile.tsv')
df.to_csv(hmm_file, sep='\t', header=True)
else:
hmm_file = args.hmm_file
X_hmm = gen_X_hmm(hmm_file, cov_file)
bestK = gen_bestk(args.contig_file, X_t, args.clusters)
logger.info("estimate clusters:\t" + str(bestK))
logger.info("start calculate contig length")
lengths = get_length(args.contig_file)
length_weight = []
for seq_id in namelist:
length_weight.append(lengths[seq_id])
seedURL = args.contig_file + ".seed"
# 暂时不考虑整个contig_file 找不�?seed的极端情�?
seed_list = []
with open(seedURL) as f:
for line in f:
seed_list.append(line.rstrip('\n'))
name_map = dict(zip(namelist, range(len(namelist))))
# global seed_idx
seed_idx = [name_map[seed_name] for seed_name in seed_list]
# run weight kmeans
logger.info("run kmeans with length weight")
km = KMeans(n_clusters=bestK, init='k-means++', n_jobs=-1, n_init=30, random_state=7)
km.fit(X_t, sample_weight=length_weight)
idx = km.labels_
kmeans_length_weight_output = os.path.dirname(args.output) + '/kmeans_length_weight_result.tsv'
save_result(idx, kmeans_length_weight_output, namelist)
kmeans_length_weight_output_dir = os.path.dirname(args.output) + '/kmeans_length_weight_result'
os.mkdir(kmeans_length_weight_output_dir)
gen_bins(contig_file, kmeans_length_weight_output, kmeans_length_weight_output_dir, "kmeans_weight_result")
# run weight kmeans with composition information only
logger.info("Run weight kmeans with composition information only.")
km = KMeans(n_clusters=bestK, init='k-means++', n_jobs=-1, n_init=30, random_state=7)
km.fit(X_com, sample_weight=length_weight)
idx = km.labels_
kmeans_length_weight_com_only_output = os.path.dirname(args.output) + '/kmeans_length_weight_com_only_result.tsv'
save_result(idx, kmeans_length_weight_com_only_output, namelist)
kmeans_length_weight_com_only_output_dir = os.path.dirname(args.output) + '/kmeans_length_weight_com_only_result'
os.mkdir(kmeans_length_weight_com_only_output_dir)
gen_bins(contig_file, kmeans_length_weight_com_only_output, kmeans_length_weight_com_only_output_dir, "com_result")
# run weight kmeans with coverage information only
logger.info("Run weight kmeans with coverage information only.")
km = KMeans(n_clusters=bestK, init='k-means++', n_jobs=-1, n_init=30, random_state=7)
X_cov = np.log10(X_cov * int(100) + 1) # 参考bisanity初始�?
km.fit(X_cov, sample_weight=length_weight)
idx = km.labels_
kmeans_length_weight_cov_only_output = os.path.dirname(
args.output) + '/kmeans_length_weight_cov_only_result.tsv'
save_result(idx, kmeans_length_weight_cov_only_output, namelist)
kmeans_length_weight_cov_only_output_dir = os.path.dirname(
args.output) + '/kmeans_length_weight_cov_only_result'
os.mkdir(kmeans_length_weight_cov_only_output_dir)
gen_bins(contig_file, kmeans_length_weight_cov_only_output, kmeans_length_weight_cov_only_output_dir,
"cov_result")
if args.pacbio_read_profiles: