|
7 | 7 | # ----------------------------------------------------------------------------
|
8 | 8 | import os
|
9 | 9 | import re
|
| 10 | +from collections import defaultdict |
10 | 11 |
|
11 | 12 | import qiime2.plugin.model as model
|
12 | 13 | from qiime2.plugin import ValidationError
|
@@ -40,34 +41,25 @@ def genome_dict(self, relative=False):
|
40 | 41 | described above.
|
41 | 42 | Both levels of the dictionary are sorted alphabetically by key.
|
42 | 43 | """
|
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) |
61 | 55 | ids[sample_id] = dict(sorted(ids[sample_id].items()))
|
62 | 56 | 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) |
71 | 63 |
|
72 | 64 | return dict(sorted(ids.items()))
|
73 | 65 |
|
|
0 commit comments