-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcluster.py
179 lines (158 loc) · 7.3 KB
/
cluster.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
import os
import sys
import Bio
import scipy as sp
import numpy as np
import pandas as pd
import pickle as pkl
import networkx as nx
import scipy.stats as stats
import scipy.sparse as sparse
from Bio import SeqIO
from Bio.Seq import Seq
from Bio.SeqRecord import SeqRecord
def make_diamond_db(cpu: int):
diamond_db_bp = "database/database.dmnd"
aa_fp = "database/protein.fasta"
make_diamond_cmd = ['diamond', 'makedb', '--threads', str(cpu), '--in', aa_fp, '-d', diamond_db_bp]
print("Creating Diamond database...")
res = subprocess.run(make_diamond_cmd, check=True, stdout=subprocess.PIPE)
if res.returncode != 0:
print('Error creating Diamond database')
exit(1)
diamond_db_fp = diamond_db_bp + '.dmnd'
return diamond_db_fp
def run_diamond(aa_fp, db_fp, cpu: int, diamond_out_fnn):
# More sensitive as an option?
diamond_cmd = ['diamond', 'blastp', '--threads', str(cpu), '--sensitive', '-d', db_fp, '-q', aa_fp,
'-o', diamond_out_fnn]
print("Running Diamond...")
res = subprocess.run(diamond_cmd, check=True, stdout=subprocess.PIPE)
if res.returncode != 0:
print('Error running Diamond')
exit(1)
return diamond_out_fnn
def make_protein_clusters_mcl(abc_fp, out_p, inflation=2):
print("Running MCL...")
abc_fn = "merged"
mci_fn = '{}.mci'.format(abc_fn)
mci_fp = os.path.join(out_p, mci_fn)
mcxload_fn = '{}_mcxload.tab'.format(abc_fn)
mcxload_fp = os.path.join(out_p, mcxload_fn)
subprocess.check_call("mcxload -abc {0} --stream-mirror --stream-neg-log10 -stream-tf 'ceil(200)' -o {1}"
" -write-tab {2}".format(abc_fp, mci_fp, mcxload_fp), shell=True)
mcl_clstr_fn = "{0}_mcl{1}.clusters".format(abc_fn, int(inflation*10))
mcl_clstr_fp = os.path.join(out_p, mcl_clstr_fn)
subprocess.check_call("mcl {0} -I {1} -use-tab {2} -o {3}".format(
mci_fp, inflation, mcxload_fp, mcl_clstr_fp), shell=True)
return mcl_clstr_fp
def load_mcl_clusters(fi):
with open(fi) as f:
c = [line.rstrip("\n").split("\t") for line in f]
c = [x for x in c if len(c) > 1]
nb_clusters = len(c)
formatter = "PC_{{:>0{}}}".format(int(round(np.log10(nb_clusters))+1))
name = [formatter.format(str(i)) for i in range(nb_clusters)]
size = [len(i) for i in c]
clusters_df = pd.DataFrame({"size": size, "pc_id": name}).set_index("pc_id")
return clusters_df, name, c
def build_clusters(fp, gene2genome):
# Read MCL
clusters_df, name, c = load_mcl_clusters(fp)
print("Using MCL to generate PCs.")
# Assign each prot to its cluster
gene2genome.set_index("protein_id", inplace=True) # id, contig, keywords, cluster
for prots, clust in zip(c, name):
try:
gene2genome.loc[prots, "cluster"] = clust
except KeyError:
prots_in = [p for p in prots if p in gene2genome.index]
not_in = frozenset(prots) - frozenset(prots_in)
print("{} protein(s) without contig: {}".format(len(not_in), not_in))
gene2genome.loc[prots_in, "cluster"] = clust
# Keys
for clust, prots in gene2genome.groupby("cluster"):
clusters_df.loc[clust, "annotated"] = prots.keywords.count()
if prots.keywords.count():
keys = ";".join(prots.keywords.dropna().values).split(";")
key_count = {}
for k in keys:
k = k.strip()
try:
key_count[k] += 1
except KeyError:
key_count[k] = 1
clusters_df.loc[clust, "keys"] = "; ".join(["{} ({})".format(x, y) for x, y in key_count.items()])
gene2genome.reset_index(inplace=True)
clusters_df.reset_index(inplace=True)
profiles_df = gene2genome.loc[:, ["contig_id", "cluster"]].drop_duplicates()
profiles_df.columns = ["contig_id", "pc_id"]
contigs_df = pd.DataFrame(gene2genome.fillna(0).groupby("contig_id").count().protein_id)
contigs_df.index.name = "contig_id"
contigs_df.columns = ["proteins"]
contigs_df.reset_index(inplace=True)
return gene2genome, clusters_df, profiles_df, contigs_df
# Create P-edges
def build_pc_matrices(profiles, contigs, pcs):
pc_by_cont = profiles.groupby("contig_id").count().pc_id
pc_by_cont = pd.merge(contigs.sort_values("pos").loc[:, ["pos", "contig_id", "proteins"]], pc_by_cont.to_frame(), how="left",
left_on="contig_id", right_on="contig_id").fillna(0)
singletons = (pc_by_cont.proteins - pc_by_cont.pc_id).values
singletons = sparse.lil_matrix(singletons).transpose()
# Matrix
profiles.index.name = "pos"
profiles.reset_index(inplace=True)
profiles = pd.merge(profiles, pcs.loc[:, ["pc_id", "pos"]], left_on="pc_id", right_on="pc_id", how="inner",
suffixes=["", "_pc"]) # pos, contig_id, pc_id, id (pc), pos_pc
profiles = pd.merge(profiles, contigs.loc[:, ["contig_id", "pos"]], left_on="contig_id", right_on="contig_id", how="inner",
suffixes=["", "_contig"])
profiles = profiles.loc[:, ["pos_contig", "pos_pc"]]
matrix = sparse.coo_matrix(([1]*len(profiles), (zip(*profiles.values))), shape=(len(contigs), len(pcs)),
dtype="bool")
return matrix.tocsr(), singletons.tocsr()
def to_clusterer(matrix, fi, contigs=None,names=None):
names = contigs if names is None else names
names = names.set_index("pos").contig_id
with open(fi, "wt") as f:
matrix = sparse.dok_matrix(matrix)
for r, c in zip(*matrix.nonzero()):
f.write(" ".join([str(x) for x in (names[r], names[c], matrix[r, c])]))
f.write("\n")
print("Saving network in file {0} ({1} lines).".format(fi, matrix.getnnz()))
return fi
def create_network(matrix, singletons, thres=1, max_sig=1000):
contigs, pcs = matrix.shape
pcs += singletons.sum()
# Number of comparisons
T = 0.5 * contigs * (contigs - 1)
logT = np.log10(T)
# Number of protein clusters in each contig
# = # shared pcs + #singletons
number_of_pc = matrix.sum(1) + singletons
number_of_pc = number_of_pc.A1 # Transform into a flat array
# Number of common protein clusters between two contigs, tuple + commons
commons_pc = matrix.dot(sparse.csr_matrix(matrix.transpose(), dtype=int))
S = sparse.lil_matrix((contigs, contigs))
total_c = float(commons_pc.getnnz())
i = 0 # Display
for A, B in zip(*commons_pc.nonzero()): # For A & B sharing contigs
if A != B:
a, b = sorted([number_of_pc[A], number_of_pc[B]])
pval = stats.hypergeom.sf(commons_pc[A, B] - 1, pcs, a, b)
sig = min(max_sig, np.nan_to_num(-np.log10(pval) - logT))
if sig > thres:
S[min(A, B), max(A, B)] = sig
# Display
i += 1
if i % 1000 == 0:
sys.stdout.write(".")
if i % 10000 == 0:
sys.stdout.write("{:6.2%} {}/{}\n".format(i / total_c, i, total_c))
S += S.T
S = S.tocsr()
if len(S.data) != 0:
print("Hypergeometric contig-similarity network:\n {0:10} contigs,\n {1:10} edges (min:{2:.2}"
"max: {3:.2}, threshold was {4})".format(contigs, S.getnnz(), S.data.min(), S.data.max(), thres))
else:
raise ValueError("No edge in the similarity network !")
return S