Skip to content

Commit d6d80ee

Browse files
committed
canges after review
1 parent f59a206 commit d6d80ee

File tree

2 files changed

+16
-13
lines changed

2 files changed

+16
-13
lines changed

q2_types/_util.py

+12-11
Original file line numberDiff line numberDiff line change
@@ -143,34 +143,33 @@ def _validate_mag_ids(
143143

144144

145145
class FileDictMixin:
146-
def file_dict(self, relative=False, suffixes=None):
146+
def file_dict(self, relative=False):
147147
"""
148148
For per sample directories it returns a mapping of sample id to
149149
another dictionary where keys represent the file name and values
150150
correspond to the filepath for each file matching the pathspec.
151151
For files, it returns a mapping of file name to filepath for each
152-
file matching the pathspec. The specified suffixes are removed
153-
from filenames.
152+
file matching the pathspec. If the dir format has the attribute
153+
'suffixes', then these are removed from filenames.
154154
155155
Parameters
156156
---------
157157
relative : bool
158158
Whether to return filepaths relative to the directory's location.
159159
Returns absolute filepaths by default.
160-
suffixes : List
161-
A list of suffixes that should be removed from the filenames to
162-
generate the ID.
163160
164161
Returns
165162
-------
166163
dict
167-
Mapping of filename -> filepath as described above.
164+
Mapping of sample id -> filepath as described above.
168165
Or mapping of sample id -> dict {filename: filepath} as
169166
described above.
170167
Both levels of the dictionary are sorted alphabetically by key.
171168
"""
169+
suffixes = getattr(self, "suffixes", [])
172170
file_pattern = re.compile(self.pathspec)
173171
ids = defaultdict(dict)
172+
174173
for entry in self.path.iterdir():
175174
if entry.is_dir():
176175
outer_id = entry.name
@@ -184,7 +183,7 @@ def file_dict(self, relative=False, suffixes=None):
184183
suffixes=suffixes,
185184
)
186185

187-
ids[outer_id][inner_id] = str(file_path)
186+
ids[outer_id][inner_id] = file_path
188187
ids[outer_id] = dict(sorted(ids[outer_id].items()))
189188
else:
190189
if file_pattern.match(entry.name):
@@ -196,7 +195,7 @@ def file_dict(self, relative=False, suffixes=None):
196195
suffixes=suffixes,
197196
)
198197

199-
ids[inner_id] = str(file_path)
198+
ids[inner_id] = file_path
200199

201200
return dict(sorted(ids.items()))
202201

@@ -217,17 +216,19 @@ def _process_path(path, relative, dir_format, suffixes):
217216
to the directory formats path or absolute.
218217
dir_format : model.DirectoryFormat.
219218
Any object of class model.DirectoryFormat.
219+
suffixes : List
220+
A list of suffixes that should be removed from the filenames to
221+
generate the ID.
220222
221223
Returns:
222224
-------
223225
processed_path : str
224-
The full relative or absolut path to the file.
226+
The full relative or absolute path to the file.
225227
_id : str
226228
The ID derived from the file name. ID will be "" if the filename
227229
consists only of the suffix.
228230
"""
229231
file_name = path.stem
230-
231232
_id = file_name
232233

233234
if suffixes:

q2_types/tests/test_util.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,10 @@ def setUp(self):
6767
self.TestClass.pathspec = r'.+\.(txt|tsv)$'
6868

6969
def test_file_dict_mixin(self):
70+
self.TestClass.suffixes = ["_suffix"]
7071
fmt = self.TestClass(self.get_data_path("per_sample"), mode='r')
7172

72-
obs = fmt.file_dict(suffixes=["_suffix"])
73+
obs = fmt.file_dict()
7374
exp = {
7475
"sample1": {
7576
"id1": os.path.join(str(fmt), "sample1", "id1_suffix.txt"),
@@ -92,9 +93,10 @@ def test_file_dict_mixin(self):
9293
self.assertDictEqual(obs, exp)
9394

9495
def test_genes_dirfmt_genome_dict(self):
96+
self.TestClass.suffixes = ["_suffix1", "_suffix2"]
9597
fmt = self.TestClass(self.get_data_path("not_per_sample"), mode='r')
9698

97-
obs = fmt.file_dict(suffixes=["_suffix1", "_suffix2"])
99+
obs = fmt.file_dict()
98100
exp = {
99101
"id1": os.path.join(str(fmt), "id1_suffix1.txt"),
100102
"id2": os.path.join(str(fmt), "id2_suffix2.txt"),

0 commit comments

Comments
 (0)