Skip to content

Commit c11450b

Browse files
committed
fix merge for BIOM files exported from UI
1 parent 4f5bbd4 commit c11450b

1 file changed

Lines changed: 22 additions & 1 deletion

File tree

mglib/mglib.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,15 +249,36 @@ def metadata_from_biom(biom, term):
249249
vals.append(value)
250250
return vals
251251

252+
# turn profile format BIOM into matrix format, use only abundances
253+
def profile_to_matrix(p):
254+
if p['columns'][0]['id'] != 'abundance':
255+
# not a profile
256+
return p
257+
trim = True if len(p['columns']) > 1 else False
258+
p['columns'][0]['id'] = p['id']
259+
p['matrix_element_type'] = 'int'
260+
p['matrix_element_value'] = 'abundance'
261+
p['date'] = time.strftime("%Y-%m-%d %H:%M:%S")
262+
if p['matrix_type'] == 'sparse':
263+
p['data'] = sparse_to_dense(p['data'], p['shape'][0], p['shape'][1])
264+
if trim:
265+
p['columns'] = p['columns'][:1]
266+
for i in range(len(p['rows'])):
267+
p['data'][i] = p['data'][i][:1]
268+
return p
269+
252270
# merge two BIOM objects
253271
def merge_biom(b1, b2):
254272
"""input: 2 biom objects of same 'type', 'matrix_element_type', and 'matrix_element_value'
255273
return: merged biom object, duplicate columns skipped, duplicate rows added"""
256-
# hack for using in loop when one oif 2 is empty
274+
# hack for using in loop when one of 2 is empty
257275
if b1 and (not b2):
258276
return b1
259277
if b2 and (not b1):
260278
return b2
279+
# transform profile BIOM from UI export into matrix BIOM
280+
b1 = profile_to_matrix(b1)
281+
b2 = profile_to_matrix(b2)
261282
# validate
262283
if not (b1 and b2 and (b1['type'] == b2['type']) and (b1['matrix_element_type'] == b2['matrix_element_type']) and (b1['matrix_element_value'] == b2['matrix_element_value'])):
263284
sys.stderr.write("The inputed biom objects are not compatable for merging\n")

0 commit comments

Comments
 (0)