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
|
# ----------------------------------------------------------------------------
# Copyright (c) 2016-2022, QIIME 2 development team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file LICENSE, distributed with this software.
# ----------------------------------------------------------------------------
import unittest
import biom
import qiime2
import pandas as pd
import numpy as np
from q2_feature_table import group
class TestGroup(unittest.TestCase):
def test_identity_groups(self):
# These map to the same values as before
sample_mc = qiime2.CategoricalMetadataColumn(
pd.Series(['a', 'b', 'c'], name='foo',
index=pd.Index(['a', 'b', 'c'], name='sampleid')))
feature_mc = qiime2.CategoricalMetadataColumn(
pd.Series(['x', 'y'], name='foo',
index=pd.Index(['x', 'y'], name='featureid')))
table = biom.Table(np.array([[1, 2, 3], [30, 20, 10]]),
sample_ids=sample_mc.to_series().index,
observation_ids=feature_mc.to_series().index)
# Sample x Sum
result = group(table, axis='sample', metadata=sample_mc, mode='sum')
self.assertEqual(table, result)
# Sample x Mean
result = group(table, axis='sample', metadata=sample_mc,
mode='mean-ceiling')
self.assertEqual(table, result)
# Sample x Median
result = group(table, axis='sample', metadata=sample_mc,
mode='median-ceiling')
self.assertEqual(table, result)
# Feature x Sum
result = group(table, axis='feature', metadata=feature_mc, mode='sum')
self.assertEqual(table, result)
# Feature x Mean
result = group(table, axis='feature', metadata=feature_mc,
mode='mean-ceiling')
self.assertEqual(table, result)
# Feature x Median
result = group(table, axis='feature', metadata=feature_mc,
mode='median-ceiling')
self.assertEqual(table, result)
def test_one_to_one_rename(self):
sample_mc = qiime2.CategoricalMetadataColumn(
pd.Series(['a_new', 'b_new', 'c_new'], name='foo',
index=pd.Index(['a', 'b', 'c'], name='sampleid')))
original_sample_ids = sample_mc.to_series().index
new_sample_ids = list(sample_mc.to_series())
feature_mc = qiime2.CategoricalMetadataColumn(
pd.Series(['x_new', 'y_new'], name='foo',
index=pd.Index(['x', 'y'], name='featureid')))
original_feature_ids = feature_mc.to_series().index
new_feature_ids = list(feature_mc.to_series())
data = np.array([[1, 2, 3], [30, 20, 10]])
table = biom.Table(data, sample_ids=original_sample_ids,
observation_ids=original_feature_ids)
# Sample renames
expected = biom.Table(data, sample_ids=new_sample_ids,
observation_ids=original_feature_ids)
# Sample x Sum
result = group(table, axis='sample', metadata=sample_mc, mode='sum')
self.assertEqual(expected, result)
# Sample X Mean
result = group(table, axis='sample', metadata=sample_mc,
mode='mean-ceiling')
self.assertEqual(expected, result)
# Sample X Mean
result = group(table, axis='sample', metadata=sample_mc,
mode='median-ceiling')
self.assertEqual(expected, result)
# Feature renames
expected = biom.Table(data, sample_ids=original_sample_ids,
observation_ids=new_feature_ids)
# Feature X Sum
result = group(table, axis='feature', metadata=feature_mc, mode='sum')
self.assertEqual(expected, result)
# Feature X Mean
result = group(table, axis='feature', metadata=feature_mc,
mode='mean-ceiling')
self.assertEqual(expected, result)
# Feature X Median
result = group(table, axis='feature', metadata=feature_mc,
mode='median-ceiling')
self.assertEqual(expected, result)
def test_superset_feature_group(self):
feature_mc = qiime2.CategoricalMetadataColumn(
pd.Series(
['g0', 'g0', 'g1', 'g2', 'g1', 'g2', 'extra'], name='foo',
index=pd.Index(['a', 'b', 'c', 'd', 'e', 'f', 'g'],
name='featureid')))
data = np.array([
[1, 0, 0],
[1, 10, 10],
[0, 0, 100],
[5, 5, 5],
[0, 1, 100],
[7, 8, 9]])
# g is missing on purpose
table = biom.Table(data, sample_ids=['s1', 's2', 's3'],
observation_ids=['a', 'b', 'c', 'd', 'e', 'f'])
expected = biom.Table(
np.array([[2, 10, 10], [0, 1, 200], [12, 13, 14]]),
sample_ids=['s1', 's2', 's3'],
observation_ids=['g0', 'g1', 'g2'])
result = group(table, axis='feature', metadata=feature_mc, mode='sum')
self.assertEqual(expected, result)
def test_superset_sample_group(self):
sample_mc = qiime2.CategoricalMetadataColumn(
pd.Series(['g0', 'g1', 'g2', 'g0', 'g1', 'g2'], name='foo',
index=pd.Index(['s1', 's2', 's3', 's4', 's5', 's6'],
name='sampleid')))
data = np.array([
[0, 1, 2, 3],
[10, 11, 12, 13],
[100, 110, 120, 130]])
table = biom.Table(data, sample_ids=['s1', 's2', 's4', 's5'],
observation_ids=['x', 'y', 'z'])
expected = biom.Table(
np.array([[2, 4], [22, 24], [220, 240]]),
sample_ids=['g0', 'g1'],
observation_ids=['x', 'y', 'z'])
result = group(table, axis='sample', metadata=sample_mc, mode='sum')
self.assertEqual(expected, result)
def test_missing_feature_ids(self):
feature_mc = qiime2.CategoricalMetadataColumn(
pd.Series(['g0', 'g1', 'g2', 'g1', 'g2', 'extra'], name='foo',
index=pd.Index(['a', 'c', 'd', 'e', 'f', 'g'],
name='featureid')))
data = np.array([
[1, 0, 0],
[1, 10, 10],
[0, 0, 100],
[5, 5, 5],
[0, 1, 100],
[7, 8, 9]])
# g is missing on purpose
table = biom.Table(data, sample_ids=['s1', 's2', 's3'],
observation_ids=['a', 'b', 'c', 'd', 'e', 'f'])
with self.assertRaisesRegex(ValueError, "not present.*'b'"):
group(table, axis='feature', metadata=feature_mc, mode='sum')
def test_missing_sample_ids(self):
sample_mc = qiime2.CategoricalMetadataColumn(
pd.Series(['g0', 'g2', 'g0', 'g2'], name='foo',
index=pd.Index(['s1', 's3', 's4', 's6'],
name='sampleid')))
data = np.array([
[0, 1, 2, 3],
[10, 11, 12, 13],
[100, 110, 120, 130]])
table = biom.Table(data, sample_ids=['s1', 's2', 's4', 's5'],
observation_ids=['x', 'y', 'z'])
with self.assertRaisesRegex(ValueError, 'not present.*s2.*s5') as e:
group(table, axis='sample', metadata=sample_mc, mode='sum')
self.assertIn('s2', str(e.exception))
self.assertIn('s5', str(e.exception))
def test_reorder(self):
sample_mc = qiime2.CategoricalMetadataColumn(
pd.Series(['c', 'b', 'a'], name='foo',
index=pd.Index(['c', 'b', 'a'], name='sampleid')))
data = np.array([[1, 2, 3], [30, 20, 10]])
table = biom.Table(data, sample_ids=['a', 'b', 'c'],
observation_ids=['x', 'y'])
expected = biom.Table(np.array([[3, 2, 1], [10, 20, 30]]),
sample_ids=['c', 'b', 'a'],
observation_ids=['x', 'y'])
result = group(table, axis='sample', metadata=sample_mc, mode='sum')
self.assertEqual(expected, result)
def test_group_to_single_id(self):
sample_mc = qiime2.CategoricalMetadataColumn(
pd.Series(['all_samples', 'all_samples', 'all_samples'],
name='foo', index=pd.Index(['a', 'b', 'c'],
name='sampleid')))
data = np.array([[1, 2, 3], [30, 20, 10]])
table = biom.Table(data, sample_ids=['a', 'b', 'c'],
observation_ids=['x', 'y'])
expected = biom.Table(np.array([[6], [60]]),
sample_ids=['all_samples'],
observation_ids=['x', 'y'])
result = group(table, axis='sample', metadata=sample_mc, mode='sum')
self.assertEqual(expected, result)
def test_empty_metadata_values(self):
# Trusting that the code is sane enough to not invent a distinction
# between feature and sample metadata where there is none
sample_mc = qiime2.CategoricalMetadataColumn(
pd.Series(['a_new', 'a_new', None], name='foo',
index=pd.Index(['a', 'b', 'c'], name='sampleid')))
sample_ids = sample_mc.to_series().index
data = np.array([[1, 2, 3], [30, 20, 10]])
table = biom.Table(data, sample_ids=sample_ids,
observation_ids=['x', 'y'])
with self.assertRaisesRegex(ValueError, "missing.*value.*'c'"):
group(table, axis='sample', metadata=sample_mc, mode='sum')
nan_mc = qiime2.CategoricalMetadataColumn(
pd.Series(['a_new', float('nan'), 'a_new'], name='foo',
index=pd.Index(['a', 'b', 'c'], name='id')))
with self.assertRaisesRegex(ValueError, "missing.*value.*'b'"):
group(table, axis='sample', metadata=nan_mc, mode='sum')
def test_empty_only_in_superset(self):
# Trusting that the code is sane enough to not invent a distinction
# between feature and sample metadata where there is none
sample_mc = qiime2.CategoricalMetadataColumn(
pd.Series(['a_new', 'a_new', 'b_new', None], name='foo',
index=pd.Index(['a', 'b', 'c', 'd'], name='sampleid')))
data = np.array([[1, 2, 3], [30, 20, 10]])
table = biom.Table(data, sample_ids=['a', 'b', 'c'],
observation_ids=['x', 'y'])
expected = biom.Table(np.array([[2, 3], [25, 10]]),
sample_ids=['a_new', 'b_new'],
observation_ids=['x', 'y'])
result = group(table, axis='sample', metadata=sample_mc,
mode='mean-ceiling')
self.assertEqual(expected, result)
def test_numeric_strings(self):
data = np.array([[1, 2, 3], [30, 20, 10]])
table = biom.Table(data, sample_ids=['a', 'b', 'c'],
observation_ids=['x', 'y'])
sample_mc = qiime2.CategoricalMetadataColumn(
pd.Series(['-4.2', '-4.2', '-4.2'], name='foo',
index=pd.Index(['a', 'b', 'c'], name='sampleid')))
expected = biom.Table(np.array([[6], [60]]),
sample_ids=['-4.2'],
observation_ids=['x', 'y'])
result = group(table, axis='sample', metadata=sample_mc, mode='sum')
self.assertEqual(expected, result)
def test_empty_table(self):
mc = qiime2.CategoricalMetadataColumn(
pd.Series(['a_new', 'b_new'], name='foo',
index=pd.Index(['a', 'b'], name='id')))
table = biom.Table(np.array([[]]), sample_ids=[], observation_ids=[])
with self.assertRaisesRegex(ValueError, 'empty table'):
group(table, axis='sample', metadata=mc, mode='sum')
with self.assertRaisesRegex(ValueError, 'empty table'):
group(table, axis='feature', metadata=mc, mode='sum')
def _shared_setup(self):
sample_mc = qiime2.CategoricalMetadataColumn(
pd.Series(['treatment', 'treatment', 'control', 'other',
'control', 'other', 'other'], name='foo',
index=pd.Index(['a', 'b', 'c', 'd', 'e', 'f', 'g'],
name='sampleid')))
feature_mc = qiime2.CategoricalMetadataColumn(
pd.Series(['g0', 'g1', 'g1', 'g1', 'g0'], name='foo',
index=pd.Index(['v', 'w', 'x', 'y', 'z'],
name='featureid')))
data = np.array([
# t t c o c o o
# a b c d e f g
[0, 0, 0, 0, 1, 0, 2], # v g0
[10, 10, 10, 10, 10, 100, 1], # w g1
[12, 3, 14, 0, 0, 3, 34], # x g1
[1, 1, 1, 1, 1, 1, 1], # y g1
[0, 1, 11, 111, 1111, 20, 20]]) # z g0
table = biom.Table(data, sample_ids=sample_mc.to_series().index,
observation_ids=feature_mc.to_series().index)
return sample_mc, feature_mc, table
def test_feature_sum(self):
sample_mc, feature_mc, table = self._shared_setup()
expected = biom.Table(
np.array([[0, 1, 11, 111, 1112, 20, 22],
[23, 14, 25, 11, 11, 104, 36]]),
sample_ids=sample_mc.to_series().index,
observation_ids=['g0', 'g1'])
result = group(table, axis='feature', metadata=feature_mc, mode='sum')
self.assertEqual(expected, result)
def test_feature_mean_ceiling(self):
sample_mc, feature_mc, table = self._shared_setup()
expected = biom.Table(
np.array([[0, 1, 6, 56, 556, 10, 11],
[8, 5, 9, 4, 4, 35, 12]]),
sample_ids=sample_mc.to_series().index,
observation_ids=['g0', 'g1'])
result = group(table, axis='feature', metadata=feature_mc,
mode='mean-ceiling')
self.assertEqual(expected, result)
def test_feature_median_ceiling(self):
sample_mc, feature_mc, table = self._shared_setup()
expected = biom.Table(
np.array([[0, 1, 6, 56, 556, 10, 11],
[10, 3, 10, 1, 1, 3, 1]]),
sample_ids=sample_mc.to_series().index,
observation_ids=['g0', 'g1'])
result = group(table, axis='feature', metadata=feature_mc,
mode='median-ceiling')
self.assertEqual(expected, result)
def test_sample_sum(self):
sample_mc, feature_mc, table = self._shared_setup()
expected = biom.Table(
np.array([[0, 1, 2],
[20, 20, 111],
[15, 14, 37],
[2, 2, 3],
[1, 1122, 151]]),
sample_ids=['treatment', 'control', 'other'],
observation_ids=feature_mc.to_series().index)
result = group(table, axis='sample', metadata=sample_mc, mode='sum')
self.assertEqual(expected, result)
def test_sample_mean_ceiling(self):
sample_mc, feature_mc, table = self._shared_setup()
expected = biom.Table(
np.array([[0, 1, 1],
[10, 10, 37],
[8, 7, 13],
[1, 1, 1],
[1, 561, 51]]),
sample_ids=['treatment', 'control', 'other'],
observation_ids=feature_mc.to_series().index)
result = group(table, axis='sample', metadata=sample_mc,
mode='mean-ceiling')
self.assertEqual(expected, result)
def test_sample_median_ceiling(self):
sample_mc, feature_mc, table = self._shared_setup()
expected = biom.Table(
np.array([[0, 1, 0],
[10, 10, 10],
[8, 7, 3],
[1, 1, 1],
[1, 561, 20]]),
sample_ids=['treatment', 'control', 'other'],
observation_ids=feature_mc.to_series().index)
result = group(table, axis='sample', metadata=sample_mc,
mode='median-ceiling')
self.assertEqual(expected, result)
if __name__ == '__main__':
unittest.main()
|