This repository has been archived by the owner on Nov 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 269
/
beta_diversity.py
275 lines (235 loc) · 10.2 KB
/
beta_diversity.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
#!/usr/bin/env python
__author__ = "Justin Kuzynski"
__copyright__ = "Copyright 2011, The QIIME Project"
__credits__ = ["Justin Kuczynski", "Rob Knight", "Greg Caporaso",
"Jose Carlos Clemente Litran", "Jai Ram Rideout",
"Jose Antonio Navas Molina"]
__license__ = "GPL"
__version__ = "1.9.1-dev"
__maintainer__ = "Justin Kuczynski"
__email__ = "justinak@gmail.com"
"""Contains code for performing beta diversity analyses of different samples.
command line usage help: python beta_diversity.py -h
This module has the responsibility for relating samples to one another. This
is performed using the following inputs:
- a tab-delimited table of taxon x sample counts (taxa can be OTUs).
- future: will be able to supply
data in (taxon, sample, count) format, sparse representation is
important for large datasets where full table is impractical to build.
Note that this requires adapting the beta diversity metrics to work
with sparse matrices, not yet done.
The output is a sample x sample matrix of distances, incl. row/col headers.
Note that parser expects first field to be blank, i.e. first char of file
is expected to be a tab.
"""
from StringIO import StringIO
from sys import exit, stderr
import os.path
import warnings
warnings.filterwarnings('ignore', 'Not using MPI as mpi4py not found')
from numpy import asarray
import cogent.maths.distance_transform as distance_transform
from biom import load_table
from qiime.util import (FunctionWithParams, TreeMissingError,
OtuMissingError)
from qiime.format import format_matrix, format_distance_matrix
from qiime.parse import parse_newick, PhyloNode
import qiime.beta_metrics
def get_nonphylogenetic_metric(name):
"""Gets metric by name from distance_transform.
Metrics should be f(matrix) -> distances.
"""
# looks for name, inserting possible dist_ to find functions
# in distance_transform.py named e.g.:
# binary_dist_chisq / dist_bray_curtis
try:
return getattr(distance_transform, 'dist_' + name.lower())
except AttributeError:
try:
return getattr(distance_transform,
name.replace('binary', 'binary_dist').lower())
except AttributeError:
return getattr(distance_transform,
name.lower())
def get_phylogenetic_metric(name):
"""Gets metric by name from cogent.maths.unifrac.fast_unifrac
Metrics should be f(matrix) -> distances.
"""
# looks for name, inserting possible dist_ to find functions
# in qiime.beta_metrics
try:
return getattr(qiime.beta_metrics, 'dist_' + name.lower())
except AttributeError:
try:
return getattr(qiime.beta_metrics,
name.replace('binary', 'binary_dist').lower())
except AttributeError:
return getattr(qiime.beta_metrics,
name.lower())
def get_phylogenetic_row_metric(name):
"""Gets metric by name from qiime.beta_metrics
Metrics should be f(matrix) -> distance_array.
"""
# looks for name, inserting one_sample to find functions
# in qiime.beta_metrics
return getattr(qiime.beta_metrics, 'one_sample_' + name.lower())
def list_known_nonphylogenetic_metrics():
"""Lists known metrics by name from distance_transform.
Assumes that functions starting with dist_ or binary_dist are metrics.
"""
result = []
for name in dir(distance_transform):
if name.startswith('dist_'):
result.append(name[5:])
elif name.startswith('binary_dist_'):
result.append('binary_' + name[12:])
result.sort()
return result
def list_known_phylogenetic_metrics():
"""Lists known phylogenetic metrics from cogent.maths.unifrac."""
result = []
for name in dir(qiime.beta_metrics):
if name.startswith('dist_'):
result.append(name[5:])
result.sort()
return result
def list_known_metrics():
return list_known_nonphylogenetic_metrics() + \
list_known_phylogenetic_metrics()
def single_file_beta(input_path, metrics, tree_path, output_dir,
rowids=None, full_tree=False):
""" does beta diversity calc on a single otu table
uses name in metrics to name output beta diversity files
assumes input tree is already trimmed to contain only otus present in otu
table, doesn't call getSubTree()
inputs:
input_path (str)
metrics (str, comma delimited if more than 1 metric; or list)
tree_path (str)
output_dir (str)
rowids (comma separated str)
"""
metrics_list = metrics
try:
metrics_list = metrics_list.split(',')
except AttributeError:
pass
otu_table = load_table(input_path)
otumtx = asarray([v for v in otu_table.iter_data(axis='sample')])
if tree_path:
tree = parse_newick(open(tree_path, 'U'),
PhyloNode)
else:
tree = None
input_dir, input_filename = os.path.split(input_path)
input_basename, input_ext = os.path.splitext(input_filename)
for metric in metrics_list:
outfilepath = os.path.join(output_dir, metric + '_' +
input_basename + '.txt')
try:
metric_f = get_nonphylogenetic_metric(metric)
is_phylogenetic = False
except AttributeError:
try:
metric_f = get_phylogenetic_metric(metric)
is_phylogenetic = True
if tree is None:
stderr.write("metric %s requires a tree, but none found\n"
% (metric,))
exit(1)
except AttributeError:
stderr.write("Could not find metric %s.\n\nKnown metrics are: %s\n"
% (metric, ', '.join(list_known_metrics())))
exit(1)
if rowids is None:
# standard, full way
if is_phylogenetic:
dissims = metric_f(otumtx, otu_table.ids(axis='observation'),
tree, otu_table.ids(),
make_subtree=(not full_tree))
else:
dissims = metric_f(otumtx)
f = open(outfilepath, 'w')
f.write(format_distance_matrix(otu_table.ids(), dissims))
f.close()
else:
# only calc d(rowid1, *) for each rowid
rowids_list = rowids.split(',')
row_dissims = [] # same order as rowids_list
for rowid in rowids_list:
rowidx = otu_table.index(rowid, axis='sample')
# first test if we can the dissim is a fn of only the pair
# if not, just calc the whole matrix
if metric_f.__name__ == 'dist_chisq' or \
metric_f.__name__ == 'dist_gower' or \
metric_f.__name__ == 'dist_hellinger' or\
metric_f.__name__ == 'binary_dist_chisq':
warnings.warn('dissimilarity ' + metric_f.__name__ +
' is not parallelized, calculating the whole matrix...')
row_dissims.append(metric_f(otumtx)[rowidx])
else:
try:
row_metric = get_phylogenetic_row_metric(metric)
except AttributeError:
# do element by element
dissims = []
sample_ids = otu_table.ids()
observation_ids = otu_table.ids(axis='observation')
for i in range(len(sample_ids)):
if is_phylogenetic:
dissim = metric_f(
otumtx[[rowidx, i], :], observation_ids,
tree, [sample_ids[rowidx], sample_ids[i]],
make_subtree=(not full_tree))[0, 1]
else:
dissim = metric_f(otumtx[[rowidx, i], :])[0, 1]
dissims.append(dissim)
row_dissims.append(dissims)
else:
# do whole row at once
dissims = row_metric(otumtx,
otu_table.ids(axis='observation'),
tree, otu_table.ids(), rowid,
make_subtree=(not full_tree))
row_dissims.append(dissims)
with open(outfilepath, 'w') as f:
f.write(format_matrix(row_dissims, rowids_list,
otu_table.ids(),
convert_matching_names_to_zero=True))
def multiple_file_beta(input_path, output_dir, metrics, tree_path,
rowids=None, full_tree=False):
""" runs beta diversity for each input file in the input directory
performs minimal error checking on input args, then calls single_file_beta
for each file in the input directory
inputs:
input_path (str)
metrics (str, comma delimited if more than 1 metric; or list)
tree_path (str)
output_dir (str)
"""
metrics_list = metrics
try:
metrics_list = metrics_list.split(',')
except AttributeError:
pass
file_names = os.listdir(input_path)
file_names = [fname for fname in file_names if not fname.startswith('.')]
try:
os.makedirs(output_dir)
except OSError:
pass # hopefully dir exists
for metric in metrics_list:
try:
metric_f = get_nonphylogenetic_metric(metric)
except AttributeError:
try:
metric_f = get_phylogenetic_metric(metric)
if not tree_path:
raise ValueError("a tree file is required for " + metric)
except AttributeError:
raise ValueError(
"Could not find metric %s.\n\nKnown metrics are: %s\n"
% (metric, ', '.join(list_known_metrics())))
for fname in file_names:
single_file_beta(os.path.join(input_path, fname),
metrics, tree_path, output_dir, rowids, full_tree)