Skip to content

Commit 673f82e

Browse files
committed
simplifacation of function
1 parent b47518a commit 673f82e

File tree

1 file changed

+18
-26
lines changed

1 file changed

+18
-26
lines changed

q2_types/genome_data/_formats.py

+18-26
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
# ----------------------------------------------------------------------------
88
import os
99
import re
10+
from collections import defaultdict
1011

1112
import qiime2.plugin.model as model
1213
from qiime2.plugin import ValidationError
@@ -40,34 +41,25 @@ def genome_dict(self, relative=False):
4041
described above.
4142
Both levels of the dictionary are sorted alphabetically by key.
4243
"""
43-
ids = {}
44-
for d in self.path.iterdir():
45-
if d.is_dir():
46-
47-
sample_id = d.name.rsplit('/', 1)[0]
48-
if sample_id not in ids:
49-
ids[sample_id] = {}
50-
51-
for path in d.iterdir():
52-
mag_id = os.path.splitext(os.path.basename(path.name))[0]
53-
absolute_path = path.absolute()
54-
if relative:
55-
ids[sample_id][mag_id] = str(
56-
absolute_path.relative_to(self.path.absolute())
57-
)
58-
else:
59-
ids[sample_id][mag_id] = str(absolute_path)
60-
44+
ids = defaultdict(dict)
45+
for entry in self.path.iterdir():
46+
if entry.is_dir():
47+
sample_id = entry.name
48+
for path in entry.iterdir():
49+
file_name = path.stem
50+
file_path = (
51+
path.absolute().relative_to(self.path.absolute())
52+
if relative else path.absolute()
53+
)
54+
ids[sample_id][file_name] = str(file_path)
6155
ids[sample_id] = dict(sorted(ids[sample_id].items()))
6256
else:
63-
_id = d.stem
64-
absolute_path = d.absolute()
65-
if relative:
66-
ids[_id] = str(
67-
absolute_path.relative_to(self.path.absolute())
68-
)
69-
else:
70-
ids[_id] = str(absolute_path)
57+
file_name = entry.stem
58+
file_path = (
59+
entry.absolute().relative_to(self.path.absolute())
60+
if relative else entry.absolute()
61+
)
62+
ids[file_name] = str(file_path)
7163

7264
return dict(sorted(ids.items()))
7365

0 commit comments

Comments
 (0)