Debian Codegraph @

biom/cli/metadata_adder.py

raw
1 # -----------------------------------------------------------------------------
2 # Copyright (c) 2011-2017, The BIOM Format Development Team.
3 #
4 # Distributed under the terms of the Modified BSD License.
5 #
6 # The full license is in the file COPYING.txt, distributed with this software.
7 # -----------------------------------------------------------------------------
8
9
10 import click
11
12 from biom import load_table
13 from biom.cli import cli
14 from biom.cli.util import write_biom_table
15 from biom.parse import MetadataMap
16
17
18 @cli.command(name='add-metadata')
19 @click.option('-i', '--input-fp', required=True,
20 type=click.Path(exists=True, dir_okay=False),
21 help='The input BIOM table')
22 @click.option('-o', '--output-fp', required=True,
23 type=click.Path(exists=False, dir_okay=False),
24 help='The output BIOM table')
25 @click.option('-m', '--sample-metadata-fp', required=False,
26 type=click.Path(exists=True, dir_okay=False),
27 help='The sample metadata mapping file (will add sample '
28 'metadata to the input BIOM table, if provided).')
29 @click.option('--observation-metadata-fp', required=False,
30 type=click.Path(exists=True, dir_okay=False),
31 help='The observation metadata mapping file (will add '
32 'observation metadata to the input BIOM table, if '
33 'provided).')
34 @click.option('--sc-separated', required=False, type=click.STRING,
35 help='Comma-separated list of the metadata fields to split '
36 'on semicolons. This is useful for hierarchical data such '
37 'as taxonomy or functional categories.')
38 @click.option('--sc-pipe-separated', required=False, type=click.STRING,
39 help='Comma-separated list of the metadata fields to split '
40 'on semicolons and pipes ("|"). This is useful for '
41 'hierarchical data such as functional categories with '
42 'one-to-many mappings (e.g. x;y;z|x;y;w)).')
43 @click.option('--int-fields', required=False, type=click.STRING,
44 help='Comma-separated list of the metadata fields to cast '
45 'to integers. This is useful for integer data such as '
46 '"DaysSinceStart".')
47 @click.option('--float-fields', required=False, type=click.STRING,
48 help='Comma-separated list of the metadata fields to cast '
49 'to floating point numbers. This is useful for real number '
50 'data such as "pH".')
51 @click.option('--sample-header', required=False, type=click.STRING,
52 help='Comma-separated list of the sample metadata field '
53 'names. This is useful if a header line is not provided '
54 'with the metadata, if you want to rename the fields, or '
55 'if you want to include only the first n fields where n is '
56 'the number of entries provided here.')
57 @click.option('--observation-header', required=False, type=click.STRING,
58 help='Comma-separated list of the observation metadata '
59 'field names. This is useful if a header line is not '
60 'provided with the metadata, if you want to rename the '
61 'fields, or if you want to include only the first n fields '
62 'where n is the number of entries provided here.')
63 @click.option('--output-as-json', default=False, is_flag=True,
64 help='Write the output file in JSON format.')
65 def add_metadata(input_fp, output_fp, sample_metadata_fp,
66 observation_metadata_fp, sc_separated, sc_pipe_separated,
67 int_fields, float_fields, sample_header, observation_header,
68 output_as_json):
69 """Add metadata to a BIOM table.
70
71 Add sample and/or observation metadata to BIOM-formatted files. See
72 examples here: http://biom-format.org/documentation/adding_metadata.html
73
74 Example usage:
75
76 Add sample metadata to a BIOM table:
77
78 $ biom add-metadata -i otu_table.biom -o table_with_sample_metadata.biom
79 -m sample_metadata.txt
80 """
81 table = load_table(input_fp)
82 if sample_metadata_fp is not None:
83 sample_metadata_f = open(sample_metadata_fp)
84 else:
85 sample_metadata_f = None
86 if observation_metadata_fp is not None:
87 observation_metadata_f = open(observation_metadata_fp)
88 else:
89 observation_metadata_f = None
90 if sc_separated is not None:
91 sc_separated = sc_separated.split(',')
92 if sc_pipe_separated is not None:
93 sc_pipe_separated = sc_pipe_separated.split(',')
94 if int_fields is not None:
95 int_fields = int_fields.split(',')
96 if float_fields is not None:
97 float_fields = float_fields.split(',')
98 if sample_header is not None:
99 sample_header = sample_header.split(',')
100 if observation_header is not None:
101 observation_header = observation_header.split(',')
102
103 result = _add_metadata(table, sample_metadata_f, observation_metadata_f,
104 sc_separated, sc_pipe_separated, int_fields,
105 float_fields, sample_header, observation_header)
106
107 if output_as_json:
108 fmt = 'json'
109 else:
110 fmt = 'hdf5'
111
112 write_biom_table(result, fmt, output_fp)
113
114
115 def _split_on_semicolons(x):
116 return [e.strip() for e in x.split(';')]
117
118
119 def _split_on_semicolons_and_pipes(x):
120 return [[e.strip() for e in y.split(';')] for y in x.split('|')]
121
122
123 def _int(x):
124 try:
125 return int(x)
126 except ValueError:
127 return x
128
129
130 def _float(x):
131 try:
132 return float(x)
133 except ValueError:
134 return x
135
136
137 def _add_metadata(table, sample_metadata=None, observation_metadata=None,
138 sc_separated=None, sc_pipe_separated=None, int_fields=None,
139 float_fields=None, sample_header=None,
140 observation_header=None):
141
142 if sample_metadata is None and observation_metadata is None:
143 raise ValueError('Must specify sample_metadata and/or '
144 'observation_metadata.')
145
146 # define metadata processing functions, if any
147 process_fns = {}
148 if sc_separated is not None:
149 process_fns.update(dict.fromkeys(sc_separated,
150 _split_on_semicolons))
151
152 if sc_pipe_separated is not None:
153 process_fns.update(dict.fromkeys(sc_pipe_separated,
154 _split_on_semicolons_and_pipes))
155
156 if int_fields is not None:
157 process_fns.update(dict.fromkeys(int_fields, _int))
158
159 if float_fields is not None:
160 process_fns.update(dict.fromkeys(float_fields, _float))
161
162 # parse mapping files
163 if sample_metadata is not None:
164 sample_metadata = MetadataMap.from_file(sample_metadata,
165 process_fns=process_fns,
166 header=sample_header)
167
168 if observation_metadata is not None:
169 observation_metadata = MetadataMap.from_file(
170 observation_metadata,
171 process_fns=process_fns,
172 header=observation_header)
173
174 # NAUGHTY: this is modifying the input table IN PLACE!!! And then
175 # RETURNING IT! MetadataAdder is angry!
176
177 # add metadata as necessary
178 if sample_metadata:
179 table.add_metadata(sample_metadata, axis='sample')
180
181 if observation_metadata:
182 table.add_metadata(observation_metadata, axis='observation')
183
184 return table