/usr/lib/python3/dist-packages/biom/util.py is in python3-biom-format 2.1.5+dfsg-7.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | #!/usr/bin/env python
# -*- coding: utf-8 -*-
# ----------------------------------------------------------------------------
# Copyright (c) 2011-2013, The BIOM Format Development Team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file COPYING.txt, distributed with this software.
# ----------------------------------------------------------------------------
import os
import sys
import inspect
from contextlib import contextmanager
from collections import defaultdict
from os import getenv
from os.path import abspath, dirname, exists
import re
from hashlib import md5
from gzip import open as gzip_open
try:
import h5py
HAVE_H5PY = True
if sys.version_info.major == 2:
H5PY_VLEN_STR = h5py.special_dtype(vlen=unicode) # noqa
H5PY_VLEN_UNICODE = h5py.special_dtype(vlen=unicode) # noqa
else:
H5PY_VLEN_STR = h5py.special_dtype(vlen=str)
H5PY_VLEN_UNICODE = h5py.special_dtype(vlen=str)
except ImportError:
HAVE_H5PY = False
H5PY_VLEN_STR = None
H5PY_VLEN_UNICODE = None
from numpy import mean, median, min, max
__author__ = "Daniel McDonald"
__copyright__ = "Copyright 2011-2013, The BIOM Format Development Team"
__credits__ = ["Daniel McDonald", "Jai Ram Rideout", "Greg Caporaso",
"Jose Clemente", "Justin Kuczynski", "Jorge CaƱardo Alastuey"]
__license__ = "BSD"
__url__ = "http://biom-format.org"
__maintainer__ = "Daniel McDonald"
__email__ = "daniel.mcdonald@colorado.edu"
__format_version__ = (2, 1)
__version__ = "2.1.5"
def generate_subsamples(table, n, axis='sample', by_id=False):
"""Indefinitely generate random subsamples
Parameters
----------
table : Table
The table to subsample from
n : int
The size of the subsample
axis : {'sample', 'observation'}, optional
The axis to operate on, defaults to 'sample'.
by_id : bool, optional
If `True`, operate on IDs, if `False`, operate on values. Defaults to
`False`.
Returns
-------
GeneratorType
Each subsequent call to .next() will yield a new randomly subsampled
table
Examples
--------
Randomly subsample the samples in the example_table 10 times:
>>> from biom import example_table
>>> gen = generate_subsamples(example_table, 2, by_id=True)
>>> observed_ids = []
>>> for _, table in zip(range(100), gen):
... observed_ids.append(tuple(table.ids()))
>>> print sorted(set(observed_ids))
[('S1', 'S2'), ('S1', 'S3'), ('S2', 'S3')]
"""
while True:
yield table.subsample(n, axis, by_id)
def get_biom_format_version_string(version=None):
"""Returns the current Biom file format version.
Parameters
----------
version : tuple
a tuple containing the version number of the biom table
"""
if version is None:
return "Biological Observation Matrix 1.0.0"
else:
return "Biological Observation Matrix %s.%s.0" % (version[0],
version[1])
def get_biom_format_url_string():
"""Returns the current Biom file format description URL."""
return __url__
def unzip(items):
"""Performs the reverse of zip, i.e. produces separate lists from tuples.
items should be list of k-element tuples. Will raise exception if any
tuples contain more items than the first one.
Conceptually equivalent to transposing the matrix of tuples.
Returns list of lists in which the ith list contains the ith element of
each tuple.
Note: zip expects *items rather than items, such that unzip(zip(*items))
returns something that compares equal to items.
Always returns lists: does not check original data type, but will accept
any sequence.
This function is ported from PyCogent (http://www.pycogent.org). PyCogent
is a GPL project, but we obtained permission from the authors of this
function to port it to the BIOM Format project (and keep it under BIOM's
BSD license).
"""
if items:
return list(map(list, zip(*list(items))))
else:
return []
def flatten(items):
"""Removes one level of nesting from items.
items can be any sequence, but flatten always returns a list.
This function is ported from PyCogent (http://www.pycogent.org). PyCogent
is a GPL project, but we obtained permission from the authors of this
function to port it to the BIOM Format project (and keep it under BIOM's
BSD license).
"""
result = []
for i in items:
try:
result.extend(i)
except:
result.append(i)
return result
def _natsort_key(item):
"""Provides normalized version of item for sorting with digits.
This function is ported from QIIME (http://www.qiime.org) and is based on:
http://lists.canonical.org/pipermail/kragen-hacks/2005-October/000419.html
QIIME is a GPL project, but we obtained permission from the authors of this
function to port it to the BIOM Format project (and keep it under BIOM's
BSD license).
"""
item = str(item)
try:
chunks = re.split('(\d+(?:\.\d+)?)', item)
except TypeError:
# if item is a tuple or list (i.e., indexable, but not a string)
# work with the first element
chunks = re.split('(\d+(?:\.\d+)?)', item[0])
for ii in range(len(chunks)):
if chunks[ii] and chunks[ii][0] in '0123456789':
if '.' in chunks[ii]:
numtype = float
else:
numtype = int
# wrap in tuple with '0' to explicitly specify numbers come first
chunks[ii] = (0, numtype(chunks[ii]))
else:
chunks[ii] = (1, chunks[ii])
return (chunks, item)
def natsort(seq):
"""Sort a sequence of text strings in a reasonable order.
This function is ported from QIIME (http://www.qiime.org) and is based on:
http://lists.canonical.org/pipermail/kragen-hacks/2005-October/000419.html
QIIME is a GPL project, but we obtained permission from the authors of this
function to port it to the BIOM Format project (and keep it under BIOM's
BSD license).
"""
alist = list(seq)
alist.sort(key=_natsort_key)
return alist
def prefer_self(x, y):
"""Merge metadata method, return X if X else Y"""
return x if x is not None else y
def index_list(l):
"""Takes a list and returns {l[idx]:idx}"""
return dict([(id_, idx) for idx, id_ in enumerate(l)])
def load_biom_config():
"""Returns biom-format configuration read in from file.
This function is ported from QIIME (http://www.qiime.org), previously named
load_qiime_config. QIIME is a GPL project, but we obtained permission from
the authors of this function to port it to the BIOM Format project (and
keep it under BIOM's BSD license).
"""
biom_config_fps = []
biom_project_dir = get_biom_project_dir()
biom_config_fps.append(biom_project_dir + '/support_files/biom_config')
biom_config_env_fp = getenv('BIOM_CONFIG_FP')
if biom_config_env_fp:
biom_config_fps.append(biom_config_env_fp)
home_dir = getenv('HOME')
if home_dir:
biom_config_home_fp = home_dir + '/.biom_config'
biom_config_fps.append(biom_config_home_fp)
biom_config_files = []
for biom_config_fp in biom_config_fps:
if exists(biom_config_fp):
biom_config_files.append(open(biom_config_fp))
return parse_biom_config_files(biom_config_files)
def get_biom_project_dir():
"""Returns the top-level biom-format directory.
This function is ported from QIIME (http://www.qiime.org), previously named
get_qiime_project_dir. QIIME is a GPL project, but we obtained permission
from the authors of this function to port it to the BIOM Format project
(and keep it under BIOM's BSD license).
"""
# Get the full path of util.py.
current_fp = abspath(__file__)
# Get the directory containing util.py.
current_dir = dirname(current_fp)
# Get the directory containing that directory.
current_dir = dirname(current_dir)
# Return the directory containing that directory.
return dirname(current_dir)
def parse_biom_config_files(biom_config_files):
"""Parses files in (ordered!) list of biom_config_files.
The order of files must be least important to most important. Values
defined in earlier files will be overwritten if the same values are defined
in later files.
This function is ported from QIIME (http://www.qiime.org), previously named
parse_qiime_config_files. QIIME is a GPL project, but we obtained
permission from the authors of this function to port it to the BIOM Format
project (and keep it under BIOM's BSD license).
"""
# The biom_config object is a default dict: if keys are not present, None
# is returned.
def return_none():
return None
results = defaultdict(return_none)
for biom_config_file in biom_config_files:
try:
results.update(parse_biom_config_file(biom_config_file))
except IOError:
pass
return results
def parse_biom_config_file(biom_config_file):
"""Parses lines in a biom_config file.
This function is ported from QIIME (http://www.qiime.org), previously named
parse_qiime_config_file. QIIME is a GPL project, but we obtained permission
from the authors of this function to port it to the BIOM Format project
(and keep it under BIOM's BSD license).
"""
result = {}
for line in biom_config_file:
line = line.strip()
# Ignore blank lines or lines beginning with '#'.
if not line or line.startswith('#'):
continue
fields = line.split()
param_id = fields[0]
param_value = ' '.join(fields[1:]) or None
result[param_id] = param_value
return result
def compute_counts_per_sample_stats(table, binary_counts=False):
"""Return summary statistics on per-sample observation counts
table: a BIOM table object
binary_counts: count the number of unique observations per
sample, rather than the sum of the total counts (i.e., counts
are qualitative rather than quantitative)
This function is ported from QIIME (http://www.qiime.org), previously named
compute_seqs_per_library_stats. QIIME is a GPL project, but we obtained
permission from the authors of this function to port it to the BIOM Format
project (and keep it under BIOM's BSD license).
"""
sample_counts = {}
for count_vector, sample_id, metadata in table.iter():
if binary_counts:
sample_counts[sample_id] = (count_vector != 0).sum()
else:
sample_counts[sample_id] = float(count_vector.sum())
counts = list(sample_counts.values())
if len(counts) == 0:
return (0, 0, 0, 0, sample_counts)
else:
return (min(counts),
max(counts),
median(counts),
mean(counts),
sample_counts)
def safe_md5(open_file, block_size=2 ** 20):
"""Computes an md5 sum without loading the file into memory
This method is based on the answers given in:
http://stackoverflow.com/questions/1131220/get-md5-hash-of-a-files-without-open-it-in-python
This function is ported from PyCogent (http://www.pycogent.org). PyCogent
is a GPL project, but we obtained permission from the authors of this
function to port it to the BIOM Format project (and keep it under BIOM's
BSD license).
"""
data = True
result = md5()
# While a little hackish, this allows this code to
# safely work either with a file object or a list of lines.
if hasattr(open_file, 'read'):
data_getter = open_file.read
data_getter_i = block_size
elif isinstance(open_file, list):
def f(i):
try:
return open_file.pop(i)
except IndexError:
return None
data_getter = f
data_getter_i = 0
else:
raise TypeError("safe_md5 can only handle a file handle or list of "
"lines but recieved %r." % type(open_file))
while data:
data = data_getter(data_getter_i)
if data:
result.update(data.encode('utf-8'))
return result.hexdigest()
def is_gzip(fp):
"""Checks the first two bytes of the file for the gzip magic number
If the first two bytes of the file are 1f 8b (the "magic number" of a
gzip file), return True; otherwise, return false.
This function is ported from QIIME (http://www.qiime.org). QIIME is a GPL
project, but we obtained permission from the authors of this function to
port it to the BIOM Format project (and keep it under BIOM's BSD license).
"""
return open(fp, 'rb').read(2) == b'\x1f\x8b'
@contextmanager
def biom_open(fp, permission='U'):
"""Wrapper to allow opening of gzipped or non-compressed files
Read or write the contents of a file
Parameters
----------
file_fp : file path
permission : str, {'r', 'w', 'wb', 'rb', 'U'}
Returns
-------
[h5py.File, file, gzip.GzipFile]
Notes
-----
If the file is binary, be sure to pass in a binary mode (append 'b' to
the mode); opening a binary file in text mode (e.g., in default mode 'U')
will have unpredictable results.
If h5py is available on the system, you cannot use biom_open to create a
writable ASCII file handle. You can use it to create writable GZIP handles
and HDF5 handles, however.
This function is ported from QIIME (http://www.qiime.org), previously named
qiime_open. QIIME is a GPL project, but we obtained permission from the
authors of this function to port it to the BIOM Format project (and keep it
under BIOM's BSD license).
Raises
------
RuntimeError
If the user tries to parse an HDF5 file without having h5py installed.
ValueError
If the user tries to read an empty file.
"""
if permission not in ['r', 'w', 'U', 'rb', 'wb']:
raise IOError("Unknown mode: %s" % permission)
opener = open
mode = permission
# don't try to open an HDF5 file if H5PY is not installed, this can only
# happen if we are reading a file
if mode in {'r', 'rb', 'U'}:
if os.path.getsize(fp) == 0:
raise ValueError("The file '%s' is empty and can't be parsed" % fp)
if is_hdf5_file(fp) and not HAVE_H5PY:
raise RuntimeError("h5py is not installed, cannot parse HDF5 "
"BIOM file")
if HAVE_H5PY:
if mode in ['U', 'r', 'rb'] and h5py.is_hdf5(fp):
opener = h5py.File
mode = 'r' if permission == 'U' else permission
elif mode == 'w':
opener = h5py.File
if mode in ['U', 'r', 'rb'] and is_gzip(fp):
opener = gzip_open
mode = 'rb' if permission in ['U', 'r'] else permission
elif mode in ['w', 'wb'] and fp.endswith('.gz'):
opener = gzip_open
f = opener(fp, mode)
try:
yield f
finally:
f.close()
def get_data_path(fn):
"""Return path to filename ``fn`` in the data folder.
During testing it is often necessary to load data files. This
function returns the full path to files in the ``data`` subfolder.
Parameters
----------
fn : str
File name.
Returns
-------
str
Inferred absolute path to the test data for the module where
``get_data_path(fn)`` is called.
Notes
-----
The requested path may not point to an existing file, as its
existence is not checked.
This method was adapted from scikit-bio, specifically `skbio.util.testing`.
"""
# getouterframes returns a list of tuples: the second tuple
# contains info about the caller, and the second element is its
# filename
callers_filename = inspect.getouterframes(inspect.currentframe())[1][1]
path = os.path.dirname(os.path.abspath(callers_filename))
data_path = os.path.join(path, 'test_data', fn)
return data_path
def is_hdf5_file(fp):
"""Guess if file is HDF5.
Parameters
----------
fn : str
File name
Returns
-------
bool
Whether the file is an HDF5 file
"""
with open(fp, 'rb') as f:
# from the HDF5 documentation about format signature
return f.read(8) == b'\x89HDF\r\n\x1a\n'
|