This repository has been archived by the owner on Nov 9, 2023. It is now read-only.
/
beta_diversity.py
414 lines (363 loc) · 16.5 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
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
#!/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"]
__license__ = "GPL"
__version__ = "1.6.0"
__maintainer__ = "Justin Kuczynski"
__email__ = "justinak@gmail.com"
__status__ = "Release"
"""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 cogent.util.misc import unzip
from biom.parse import parse_biom_table
from biom.table import DenseTable
from qiime.util import (FunctionWithParams, TreeMissingError,
OtuMissingError, get_tmp_filename)
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()
class BetaDiversityCalc(FunctionWithParams):
"""A BetaDiversityCalc takes taxon x sample counts, returns distance mat.
Some BetaDiversityCalcs also require a tree.
typical usage is:
calc = BetaDiversityCalc(my_metric, 'metric name', \
IsPhylogenetic=False)
calc(data_path=stuff, result_path=stuff2)
"""
_tracked_properties = ['IsPhylogenetic', 'Metric']
def __init__(self, metric, name, is_phylogenetic=False, params=None):
"""Return new BetaDiversityCalc object with specified params.
Note: metric must conform to the following interface:
must return 2d sample distance matrix with sample order preserved.
* phylogenetic methods: take samples by taxa array, taxon_names,
cogent PhyloNode tree
* nonphylo methods: take only samples by taxa array, numpy 2d
Not sure what params does
"""
self.Metric = metric #should be f(table, tree) -> dist matrix
self.Name = name
self.IsPhylogenetic = is_phylogenetic
self.Params = params or {}
def getResult(self, data_path, tree_path=None):
"""Returns distance matrix from (indcidence matrix and optionally tree).
Parameters:
data_path: path to data file, matrix (samples = cols, taxa = rows)
in tab-delimited text format
tree_path: path or object.
if method is phylogenetic, must supply tree_path.
if path, path to
Newick-format tree file where taxon ids match taxon ids in the
input data file.
returns 2d dist matrix, list of sample names ordered as in dist mtx
"""
#if it's a phylogenetic metric, read the tree
if self.IsPhylogenetic:
tree = self.getTree(tree_path)
else:
tree = None
otu_table = parse_biom_table(open(data_path,'U'))
if isinstance(otu_table, DenseTable):
otumtx = otu_table._data.T
else:
otumtx = asarray([v for v in otu_table.iterSampleData()])
# get the 2d dist matrix from beta diversity analysis
if self.IsPhylogenetic:
return (self.Metric(otumtx, otu_table.ObservationIds, tree,
otu_table.SampleIds),
list(otu_table.SampleIds))
else:
return self.Metric(otumtx), list(otu_table.SampleIds)
def formatResult(self, result):
"""Generate formatted distance matrix. result is (data, sample_names)"""
data, sample_names = result
return format_distance_matrix(sample_names, data)
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)
tree_path (str)
output_dir (str)
rowids (comma separated str)
"""
otu_table = parse_biom_table(open(input_path,'U'))
if isinstance(otu_table, DenseTable):
otumtx = otu_table._data.T
else:
otumtx = asarray([v for v in otu_table.iterSampleData()])
if tree_path:
tree = parse_newick(open(tree_path, 'U'),
PhyloNode)
else:
tree = None
metrics_list = metrics.split(',')
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 == 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 == None:
# standard, full way
if is_phylogenetic:
dissims = metric_f(otumtx, otu_table.ObservationIds, tree, otu_table.SampleIds, make_subtree = (not full_tree))
else:
dissims = metric_f(otumtx)
f = open(outfilepath,'w')
f.write(format_distance_matrix(otu_table.SampleIds, 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.SampleIds.index(rowid)
# 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 = []
for i in range(len(otu_table.SampleIds)):
if is_phylogenetic:
dissim = metric_f(otumtx[[rowidx,i],:],
otu_table.ObservationIds, tree, [otu_table.SampleIds[rowidx],otu_table.SampleIds[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.ObservationIds, tree, otu_table.SampleIds, rowid,
make_subtree = (not full_tree))
row_dissims.append(dissims)
# rows_outfilepath = os.path.join(output_dir, metric + '_' +\
# '_'.join(rowids_list) + '_' + os.path.split(input_path)[1])
f = open(outfilepath,'w')
f.write(format_matrix(row_dissims,rowids_list,otu_table.SampleIds))
f.close()
def single_object_beta(otu_table, metrics, tr, rowids=None,
full_tree=False):
"""mod of single_file_beta to recieve and return otu obj, tree str
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:
otu_table -- a otu_table in the biom format
metrics -- metrics (str, comma delimited if more than 1 metric)
tr -- a phylonode cogent tree object if needed by the chosen beta
diversity metric
rowids -- comma seperated string
"""
if isinstance(otu_table, DenseTable):
otumtx = otu_table._data.T
else:
otumtx = asarray([v for v in otu_table.iterSampleData()])
if tr:
tree = tr
else:
tree = None
metrics_list = metrics.split(',')
for metric in metrics_list:
try:
metric_f = get_nonphylogenetic_metric(metric)
is_phylogenetic = False
except AttributeError:
try:
metric_f = get_phylogenetic_metric(metric)
is_phylogenetic = True
if tree == 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 == None:
# standard, full way
if is_phylogenetic:
dissims = metric_f(otumtx, otu_table.ObservationIds, tree, otu_table.SampleIds, make_subtree = (not full_tree))
else:
dissims = metric_f(otumtx)
return format_distance_matrix(otu_table.SampleIds, dissims).split('\n')
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.SampleIds.index(rowid)
# 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 = []
for i in range(len(otu_table.SampleIds)):
if is_phylogenetic:
dissim = metric_f(otumtx[[rowidx,i],:],
otu_table.ObservationIds, tree, [otu_table.SampleIds[rowidx],otu_table.SampleIds[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.ObservationIds, tree, otu_table.SampleIds, rowid,
make_subtree = (not full_tree))
row_dissims.append(dissims)
return format_matrix(row_dissims,rowids_list,otu_table.SampleIds)
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)
tree_path (str)
output_dir (str)
"""
metrics_list = metrics.split(',')
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)