Skip to content

Commit 05d433c

Browse files
authored
NEW: adds ImmutableMetadata semantic type and supporting formats (#293)
1 parent d18a420 commit 05d433c

File tree

12 files changed

+262
-0
lines changed

12 files changed

+262
-0
lines changed

q2_types/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,4 @@
2424
importlib.import_module('q2_types.sample_data')
2525
importlib.import_module('q2_types.per_sample_sequences')
2626
importlib.import_module('q2_types.bowtie2')
27+
importlib.import_module('q2_types.metadata')

q2_types/metadata/__init__.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# ----------------------------------------------------------------------------
2+
# Copyright (c) 2016-2023, QIIME 2 development team.
3+
#
4+
# Distributed under the terms of the Modified BSD License.
5+
#
6+
# The full license is in the file LICENSE, distributed with this software.
7+
# ----------------------------------------------------------------------------
8+
9+
import importlib
10+
11+
from ._format import ImmutableMetadataFormat, ImmutableMetadataDirectoryFormat
12+
from ._type import ImmutableMetadata
13+
14+
__all__ = ['ImmutableMetadataFormat',
15+
'ImmutableMetadataDirectoryFormat',
16+
'ImmutableMetadata']
17+
18+
importlib.import_module('q2_types.metadata._transformer')

q2_types/metadata/_format.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# ----------------------------------------------------------------------------
2+
# Copyright (c) 2016-2023, QIIME 2 development team.
3+
#
4+
# Distributed under the terms of the Modified BSD License.
5+
#
6+
# The full license is in the file LICENSE, distributed with this software.
7+
# ----------------------------------------------------------------------------
8+
9+
import qiime2.plugin.model as model
10+
from qiime2.plugin import ValidationError
11+
from qiime2 import Metadata
12+
from qiime2.metadata import MetadataFileError
13+
14+
from ..plugin_setup import plugin
15+
16+
17+
class ImmutableMetadataFormat(model.TextFileFormat):
18+
def _validate_(self, level):
19+
try:
20+
Metadata.load(str(self))
21+
except (MetadataFileError,) as e:
22+
raise ValidationError(str(e))
23+
24+
25+
ImmutableMetadataDirectoryFormat = model.SingleFileDirectoryFormat(
26+
'ImmutableMetadataDirectoryFormat', 'metadata.tsv',
27+
ImmutableMetadataFormat)
28+
29+
30+
plugin.register_formats(ImmutableMetadataFormat,
31+
ImmutableMetadataDirectoryFormat)

q2_types/metadata/_transformer.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# ----------------------------------------------------------------------------
2+
# Copyright (c) 2016-2023, QIIME 2 development team.
3+
#
4+
# Distributed under the terms of the Modified BSD License.
5+
#
6+
# The full license is in the file LICENSE, distributed with this software.
7+
# ----------------------------------------------------------------------------
8+
9+
import qiime2
10+
11+
from ..plugin_setup import plugin
12+
from . import ImmutableMetadataFormat
13+
14+
15+
@plugin.register_transformer
16+
def _1(ff: ImmutableMetadataFormat) -> qiime2.Metadata:
17+
return qiime2.Metadata.load(str(ff))

q2_types/metadata/_type.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# ----------------------------------------------------------------------------
2+
# Copyright (c) 2016-2023, QIIME 2 development team.
3+
#
4+
# Distributed under the terms of the Modified BSD License.
5+
#
6+
# The full license is in the file LICENSE, distributed with this software.
7+
# ----------------------------------------------------------------------------
8+
9+
from qiime2.plugin import SemanticType
10+
11+
from ..plugin_setup import plugin
12+
from . import ImmutableMetadataDirectoryFormat
13+
14+
15+
ImmutableMetadata = SemanticType('ImmutableMetadata')
16+
17+
plugin.register_semantic_types(ImmutableMetadata)
18+
19+
plugin.register_artifact_class(
20+
ImmutableMetadata,
21+
directory_format=ImmutableMetadataDirectoryFormat,
22+
description=("Immutable sample or feature metadata.")
23+
)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# ----------------------------------------------------------------------------
2+
# Copyright (c) 2016-2023, QIIME 2 development team.
3+
#
4+
# Distributed under the terms of the Modified BSD License.
5+
#
6+
# The full license is in the file LICENSE, distributed with this software.
7+
# ----------------------------------------------------------------------------
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
bad-id-label barcode-sequence body-site year month day subject reported-antibiotic-usage days-since-experiment-start
2+
#q2:types categorical categorical numeric numeric numeric categorical categorical numeric
3+
L1S8 AGCTGACTAGTC gut 2008 10 28 subject-1 Yes 0
4+
L1S57 ACACACTATGGC gut 2009 1 20 subject-1 No 84
5+
L1S76 ACTACGTGTGGT gut 2009 2 17 subject-1 No 112
6+
L1S105 AGTGCGATGCGT gut 2009 3 17 subject-1 No 140
7+
L2S155 ACGATGCGACCA left palm 2009 1 20 subject-1 No 84
8+
L2S175 AGCTATCCACGA left palm 2009 2 17 subject-1 No 112
9+
L2S204 ATGCAGCTCAGT left palm 2009 3 17 subject-1 No 140
10+
L2S222 CACGTGACATGT left palm 2009 4 14 subject-1 No 168
11+
L3S242 ACAGTTGCGCGA right palm 2008 10 28 subject-1 Yes 0
12+
L3S294 CACGACAGGCTA right palm 2009 1 20 subject-1 No 84
13+
L3S313 AGTGTCACGGTG right palm 2009 2 17 subject-1 No 112
14+
L3S341 CAAGTGAGAGAG right palm 2009 3 17 subject-1 No 140
15+
L3S360 CATCGTATCAAC right palm 2009 4 14 subject-1 No 168
16+
L5S104 CAGTGTCAGGAC tongue 2008 10 28 subject-1 Yes 0
17+
L5S155 ATCTTAGACTGC tongue 2009 1 20 subject-1 No 84
18+
L5S174 CAGACATTGCGT tongue 2009 2 17 subject-1 No 112
19+
L5S203 CGATGCACCAGA tongue 2009 3 17 subject-1 No 140
20+
L5S222 CTAGAGACTCTT tongue 2009 4 14 subject-1 No 168
21+
L1S140 ATGGCAGCTCTA gut 2008 10 28 subject-2 Yes 0
22+
L1S208 CTGAGATACGCG gut 2009 1 20 subject-2 No 84
23+
L1S257 CCGACTGAGATG gut 2009 3 17 subject-2 No 140
24+
L1S281 CCTCTCGTGATC gut 2009 4 14 subject-2 No 168
25+
L2S240 CATATCGCAGTT left palm 2008 10 28 subject-2 Yes 0
26+
L2S309 CGTGCATTATCA left palm 2009 1 20 subject-2 No 84
27+
L2S357 CTAACGCAGTCA left palm 2009 3 17 subject-2 No 140
28+
L2S382 CTCAATGACTCA left palm 2009 4 14 subject-2 No 168
29+
L3S378 ATCGATCTGTGG right palm 2008 10 28 subject-2 Yes 0
30+
L4S63 CTCGTGGAGTAG right palm 2009 1 20 subject-2 No 84
31+
L4S112 GCGTTACACACA right palm 2009 3 17 subject-2 No 140
32+
L4S137 GAACTGTATCTC right palm 2009 4 14 subject-2 No 168
33+
L5S240 CTGGACTCATAG tongue 2008 10 28 subject-2 Yes 0
34+
L6S20 GAGGCTCATCAT tongue 2009 1 20 subject-2 No 84
35+
L6S68 GATACGTCCTGA tongue 2009 3 17 subject-2 No 140
36+
L6S93 GATTAGCACTCT tongue 2009 4 14 subject-2 No 168
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
sample-id barcode-sequence body-site year month day subject reported-antibiotic-usage days-since-experiment-start
2+
#q2:types categorical categorical numeric numeric numeric categorical categorical numeric
3+
L1S8 AGCTGACTAGTC gut 2008 10 28 subject-1 Yes 0
4+
L1S57 ACACACTATGGC gut 2009 1 20 subject-1 No 84
5+
L1S76 ACTACGTGTGGT gut 2009 2 17 subject-1 No 112
6+
L1S105 AGTGCGATGCGT gut 2009 3 17 subject-1 No 140
7+
L2S155 ACGATGCGACCA left palm 2009 1 20 subject-1 No 84
8+
L2S175 AGCTATCCACGA left palm 2009 2 17 subject-1 No 112
9+
L2S204 ATGCAGCTCAGT left palm 2009 3 17 subject-1 No 140
10+
L2S222 CACGTGACATGT left palm 2009 4 14 subject-1 No 168
11+
L3S242 ACAGTTGCGCGA right palm 2008 10 28 subject-1 Yes 0
12+
L3S294 CACGACAGGCTA right palm 2009 1 20 subject-1 No 84
13+
L3S313 AGTGTCACGGTG right palm 2009 2 17 subject-1 No 112
14+
L3S341 CAAGTGAGAGAG right palm 2009 3 17 subject-1 No 140
15+
L3S360 CATCGTATCAAC right palm 2009 4 14 subject-1 No 168
16+
L5S104 CAGTGTCAGGAC tongue 2008 10 28 subject-1 Yes 0
17+
L5S155 ATCTTAGACTGC tongue 2009 1 20 subject-1 No 84
18+
L5S174 CAGACATTGCGT tongue 2009 2 17 subject-1 No 112
19+
L5S203 CGATGCACCAGA tongue 2009 3 17 subject-1 No 140
20+
L5S222 CTAGAGACTCTT tongue 2009 4 14 subject-1 No 168
21+
L1S140 ATGGCAGCTCTA gut 2008 10 28 subject-2 Yes 0
22+
L1S208 CTGAGATACGCG gut 2009 1 20 subject-2 No 84
23+
L1S257 CCGACTGAGATG gut 2009 3 17 subject-2 No 140
24+
L1S281 CCTCTCGTGATC gut 2009 4 14 subject-2 No 168
25+
L2S240 CATATCGCAGTT left palm 2008 10 28 subject-2 Yes 0
26+
L2S309 CGTGCATTATCA left palm 2009 1 20 subject-2 No 84
27+
L2S357 CTAACGCAGTCA left palm 2009 3 17 subject-2 No 140
28+
L2S382 CTCAATGACTCA left palm 2009 4 14 subject-2 No 168
29+
L3S378 ATCGATCTGTGG right palm 2008 10 28 subject-2 Yes 0
30+
L4S63 CTCGTGGAGTAG right palm 2009 1 20 subject-2 No 84
31+
L4S112 GCGTTACACACA right palm 2009 3 17 subject-2 No 140
32+
L4S137 GAACTGTATCTC right palm 2009 4 14 subject-2 No 168
33+
L5S240 CTGGACTCATAG tongue 2008 10 28 subject-2 Yes 0
34+
L6S20 GAGGCTCATCAT tongue 2009 1 20 subject-2 No 84
35+
L6S68 GATACGTCCTGA tongue 2009 3 17 subject-2 No 140
36+
L6S93 GATTAGCACTCT tongue 2009 4 14 subject-2 No 168
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# ----------------------------------------------------------------------------
2+
# Copyright (c) 2016-2023, QIIME 2 development team.
3+
#
4+
# Distributed under the terms of the Modified BSD License.
5+
#
6+
# The full license is in the file LICENSE, distributed with this software.
7+
# ----------------------------------------------------------------------------
8+
9+
import shutil
10+
11+
from q2_types.metadata import (ImmutableMetadataDirectoryFormat,
12+
ImmutableMetadataFormat)
13+
from qiime2.plugin.testing import TestPluginBase
14+
from qiime2.plugin import ValidationError
15+
16+
17+
class TestFormats(TestPluginBase):
18+
package = "q2_types.metadata.tests"
19+
20+
def test_metadata_format_validate_positive(self):
21+
filepath = self.get_data_path('metadata.tsv')
22+
format = ImmutableMetadataFormat(filepath, mode='r')
23+
24+
format.validate()
25+
26+
def test_metadata_dir_format_validate_positive(self):
27+
filepath = self.get_data_path('metadata.tsv')
28+
shutil.copy(filepath, self.temp_dir.name)
29+
format = ImmutableMetadataDirectoryFormat(self.temp_dir.name, mode='r')
30+
31+
format.validate()
32+
33+
def test_metadata_format_validate_negative(self):
34+
filepath = self.get_data_path('invalid-metadata-1.tsv')
35+
format = ImmutableMetadataFormat(filepath, mode='r')
36+
37+
with self.assertRaisesRegex(ValidationError,
38+
"column name 'bad-id-label'"):
39+
format.validate()
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# ----------------------------------------------------------------------------
2+
# Copyright (c) 2016-2023, QIIME 2 development team.
3+
#
4+
# Distributed under the terms of the Modified BSD License.
5+
#
6+
# The full license is in the file LICENSE, distributed with this software.
7+
# ----------------------------------------------------------------------------
8+
9+
import qiime2
10+
from q2_types.metadata import ImmutableMetadataFormat
11+
from qiime2.plugin.testing import TestPluginBase
12+
from qiime2.metadata import MetadataFileError
13+
14+
15+
class TestTransformers(TestPluginBase):
16+
package = "q2_types.metadata.tests"
17+
18+
def test_metadata_format_to_metadata(self):
19+
filename = 'metadata.tsv'
20+
_, obs = self.transform_format(ImmutableMetadataFormat,
21+
qiime2.Metadata,
22+
filename)
23+
exp_md = qiime2.Metadata.load(self.get_data_path(filename))
24+
self.assertEqual(obs, exp_md)
25+
26+
def test_non_metadata(self):
27+
filename = 'invalid-metadata-1.tsv'
28+
with self.assertRaisesRegex(MetadataFileError,
29+
"column name 'bad-id-label'"):
30+
self.transform_format(ImmutableMetadataFormat, qiime2.Metadata,
31+
filename)

0 commit comments

Comments
 (0)