-
Notifications
You must be signed in to change notification settings - Fork 85
/
strain_transmission.py
executable file
·324 lines (269 loc) · 11.6 KB
/
strain_transmission.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
#!/usr/bin/env python
__author__ = ('Aitor Blanco (aitor.blancomiguez@unitn.it), '
'Mireia Valles-Colomer (mireia.vallescolomer@unitn.it)')
__version__ = '4.1.1'
__date__ = '11 Mar 2024'
import os
import time
import argparse as ap
try:
from .util_fun import info, error
from .pyphlan import PpaTree, dist_matrix
except ImportError:
from util_fun import info, error
from pyphlan import PpaTree, dist_matrix
DISTRIBUTION_THRESHOLD = 0.03
metaphlan_script_install_folder = os.path.dirname(os.path.abspath(__file__))
DEFAULT_UTILS_FOLDER = os.path.join(metaphlan_script_install_folder)
PRECOMPUTED_FILE = os.path.join(
DEFAULT_UTILS_FOLDER, 'VallesColomerM_2022_Jan21_thresholds.tsv')
def read_params():
""" Reads and parses the command line arguments of the script
Returns:
namespace: The populated namespace with the command line arguments
"""
p = ap.ArgumentParser(
description="", formatter_class=ap.ArgumentDefaultsHelpFormatter)
p.add_argument('-t', '--tree', type=str, default=None,
help="The input tree file")
p.add_argument('-m', '--metadata', type=str, default=None,
help="The input metadata")
p.add_argument('-o', '--output_dir', type=str, default=None,
help="The output directory")
p.add_argument('--sgb_id', type=str, default=None,
help="[Optional] If specified, it will use the precomputed transmisison threshold for the specific SGB from the VallesColomerM_2022 study")
p.add_argument('--threshold', type=float, default=DISTRIBUTION_THRESHOLD,
help="[Optional] A custom distribution threshold value")
p.add_argument('--precomputed_thresholds_file', type=str, default=PRECOMPUTED_FILE,
help="[Optional] The file containing the pre-computed thresholds")
p.add_argument('--save_dist', action='store_true',
help="[Optional] Save the PhyPhlAn pairwise distances file")
return p.parse_args()
def check_params(args):
"""Checks the mandatory command line arguments of the script
Args:
args (namespace): the arguments to check
"""
if not args.tree and not args.dist:
error('-t (or --tree) must be specified', exit=True)
if not args.output_dir:
error('-o (or --output_dir) must be specified', exit=True)
elif not os.path.exists(args.output_dir):
error('The directory {} does not exist'.format(
args.output_dir), exit=True)
if not args.metadata:
error('-m (or --metadata) must be specified', exit=True)
def tree_pairwisedist(tree, normalise, matrix, output):
"""Calls the main function on the tree_pairwisedist.py Script of the Pyphlan tool
Args:
tree (str): the path to the input tree
normalise (bool): whether to normalize the distances
matrix (bool): whether the output is a matrix
output (str): the path to the output distances file
"""
ppatree = PpaTree(tree)
dists = dist_matrix(ppatree.tree)
tbl = ppatree.tree.total_branch_length() if normalise else 1.0
with open(output, 'w') as out:
if matrix:
keys = sorted(dists.keys())
out.write("\t".join(["ID"]+keys) + "\n")
for k1 in keys:
out.write("\t".join([k1]+[str(dists[k1][k2]/tbl)
for k2 in keys]) + "\n")
else:
for k1, v1 in dists.items():
for k2, v2 in v1.items():
if k1 < k2:
out.write("\t".join([k1, k2, str(v2/tbl)]) + "\n")
def get_metadata_info(metadata):
"""Gets all the information about the families from the metadata
Args:
metadata (str): the path to the metadata file
Returns:
(dict, dict): a tuple with the info about relations and the samples
"""
info = dict()
samples = dict()
with open(metadata, 'r') as metadata_file:
metadata_file.readline()
for line in metadata_file:
line = line.strip().split("\t")
relation = line[2]
subject = line[1]
timepoint = line[3]
sample = line[0]
if relation not in info:
info[relation] = dict()
if subject not in info[relation]:
info[relation][subject] = dict()
if timepoint not in info[relation][subject]:
info[relation][subject][timepoint] = sample
samples[sample] = [relation, subject, timepoint]
return info, samples
def parse_distances(distances_path):
"""Parses the pairwise distances from the generated distances file
Args:
distances_path (str): the path to the distance matrix
Returns:
list: a list with the distances
"""
distances = list()
with open(distances_path, 'r') as distances_file:
for line in distances_file:
line = line.strip().split("\t")
distances.append({"1": line[0], "2": line[1], "dist": line[2]})
return distances
def get_nodes(distances):
"""Gets the nodes of the tree
Args:
distances (list): a list with the distances between nodes
Returns:
set: the list nodes in the tree
"""
nodes = set()
for line in distances:
nodes.add(line["1"])
nodes.add(line["2"])
return nodes
def get_training_nodes(nodes, metadata):
"""Gets the training nodes from the metadata
Args:
nodes (set): the list of the nodes in the tree
metadata (str): the metadata file
Returns:
(dict, dict): a tuplw with the training nodes and the metadata of the samples
"""
metadata_info, metadata_samples = get_metadata_info(metadata)
training_nodes = dict()
for relation in metadata_info:
for subject in metadata_info[relation]:
for timepoint in metadata_info[relation][subject]:
sample = metadata_info[relation][subject][timepoint]
training_nodes[sample] = metadata_samples[sample]
break
for node in nodes:
if node not in metadata_samples:
training_nodes[node] = list()
return training_nodes, metadata_samples
def get_training_distances(training_nodes, pairwise_distances):
"""Gets the distances between the training nodes
Args:
training_nodes (list): the ditionary of the training nodes
pairwise_distances (list): the list of the pairwise distances
Returns:
list: the list of training distances
"""
training_distances = list()
for pair in pairwise_distances:
if pair["1"] in training_nodes and pair["2"] in training_nodes:
if not training_nodes[pair['1']] or not training_nodes[pair['2']]:
training_distances.append(pair)
elif not training_nodes[pair['1']][0] == training_nodes[pair['2']][0]:
training_distances.append(pair)
return training_distances
def get_threshold(training_distances, distr_threshold):
"""Gets the threshold defining strain transmission
Args:
training_distances (list): the list of training distances
distr_threshold (float): the distribution threshold
Returns:
float: the inferred threshold
"""
distances = list()
for distance in training_distances:
distances.append(float(distance['dist']))
distances.sort()
return distances[int(len(distances)*distr_threshold)]
def get_transmission_events(pairwise_distances, metadata_samples, threshold):
"""Gets the transmission events using a calculated threshold
Args:
pairwise_distances (list): the list of the pairwise distances
metadata_samples (dict): the dictionary with the samples metadata
threshold (float): the infered threshold
Returns:
list: the list with the transmission events
"""
transmission_events = list()
for pair in pairwise_distances:
if pair['1'] in metadata_samples and pair['2'] in metadata_samples:
if not metadata_samples[pair['1']][1] == metadata_samples[pair['2']][1] and float(pair['dist']) <= threshold:
if metadata_samples[pair['1']][0] == metadata_samples[pair['2']][0]:
transmission_events.append(pair)
return transmission_events
def write_transmission_events(transmission_events, threshold, output_dir):
"""Writes the detected transmission events to file
Args:
transmission_events (list): the list with the transmission events
threshold (float): the infered threshold
output_dir (str): the output directory
"""
with open(os.path.join(output_dir, "transmission_events.info"), 'w') as report:
report.write(
"Selected strain-transmission threshold: {}\n".format(threshold))
report.write("Number of transmission events: {}\n\n".format(
len(transmission_events)))
for event in transmission_events:
report.write(event['1']+" <-> "+event['2']+"\n")
def get_precomputed_threshold(sgb_id, precomputed_thresholds_file):
"""Gets the precomputed threshold from VallesColomerM_2022 study
Args:
sgb_id (str): the SGB id
precomputed_thresholds_file (str): the path to the precomputed thresholds
Returns:
float: the precomputed threshold
"""
sgb2thres = dict()
with open(precomputed_thresholds_file, 'r') as rf:
rf.readline()
for line in rf:
line = line.strip().split('\t')
sgb2thres[line[0]] = line[1]
if sgb_id not in sgb2thres:
error('The SGB specified "{}" has not been precomputed'.format(
sgb_id), exit=True)
else:
return float(sgb2thres[sgb_id])
def strain_transmission(tree, metadata, distr_threshold, sgb_id, precomputed_thresholds_file, save_dist, output_dir):
"""Identifies transmission events in phylogenetic trees
Args:
tree (str): The path to the tree
metadata (str): The path to the metadata file
distr_threshold (float): the distribution threshold
sgb_id (str): the SGB id
precomputed_thresholds_file (str): the path to the precomputed thresholds
save_dist (bool): whether to save the distance matrix
output_dir (str): the output directory
"""
normalise = True
matrix = False
distances_file = "{}.dist".format(tree)
tree_pairwisedist(tree, normalise, matrix,
os.path.join(output_dir, distances_file))
pairwise_distances = parse_distances(
os.path.join(output_dir, distances_file))
if not save_dist:
os.remove(os.path.join(output_dir, distances_file))
nodes = get_nodes(pairwise_distances)
training_nodes, metadata_samples = get_training_nodes(nodes, metadata)
training_distances = get_training_distances(
training_nodes, pairwise_distances)
if sgb_id is None:
threshold = get_threshold(training_distances, distr_threshold)
else:
threshold = get_threshold(training_distances, get_precomputed_threshold(
sgb_id, precomputed_thresholds_file))
transmission_events = get_transmission_events(
pairwise_distances, metadata_samples, threshold)
write_transmission_events(transmission_events, threshold, output_dir)
def main():
t0 = time.time()
args = read_params()
info("Start execution")
check_params(args)
strain_transmission(args.tree, args.metadata, args.threshold, args.sgb_id,
args.precomputed_thresholds_file, args.save_dist, args.output_dir)
exec_time = time.time() - t0
info("Finish execution ({} seconds)".format(round(exec_time, 2)))
if __name__ == "__main__":
main()