Skip to content

Commit 8f48553

Browse files
committed
added option to report fullpath of url of files if urltype is local
1 parent 208ba11 commit 8f48553

File tree

1 file changed

+49
-14
lines changed

1 file changed

+49
-14
lines changed

seqspec/seqspec_file.py

+49-14
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
from argparse import RawTextHelpFormatter
77
import json
88
from seqspec import seqspec_find
9+
import os
10+
import argparse
911

1012

1113
def setup_file_args(parser):
@@ -89,33 +91,42 @@ def setup_file_args(parser):
8991
choices=choices,
9092
)
9193

94+
# option to get the full path of the file
95+
subparser.add_argument(
96+
"--fullpath",
97+
help=argparse.SUPPRESS,
98+
action="store_true",
99+
default=False,
100+
)
101+
92102
return subparser
93103

94104

95105
def validate_file_args(parser, args):
96-
spec_fn = args.yaml
106+
spec_fn = os.path.abspath(args.yaml)
97107
o = args.o
98108
m = args.m # modality
99109
idtype = args.s # selector
100110
fmt = args.f # format
101111
ids = args.i # ids
102112
k = args.k # key
113+
fp = args.fullpath
103114

104115
if (k == "filesize" or k == "filetype" or k == "urltype" or k == "md5") and (
105116
fmt == "paired" or fmt == "interleaved" or fmt == "index"
106117
):
107118
parser.error(f"-f {fmt} valid only with -k file_id, filename, url")
108119

109-
return run_file(spec_fn, m, ids, idtype, fmt, k, o)
120+
return run_file(spec_fn, m, ids, idtype, fmt, k, o, fp=fp)
110121

111122

112-
def run_file(spec_fn, m, ids, idtype, fmt, k, o):
123+
def run_file(spec_fn, m, ids, idtype, fmt, k, o, fp=False):
113124
spec = load_spec(spec_fn)
114125
if ids is None:
115126
ids = []
116127
else:
117128
ids = ids.split(",")
118-
files = file(spec, m, ids, idtype, fmt, k)
129+
files = file(spec, m, ids, idtype, fmt, k, spec_fn, fp)
119130

120131
if files:
121132
if o:
@@ -133,6 +144,8 @@ def file(
133144
idtype: str,
134145
fmt: str,
135146
k: Optional[str],
147+
spec_fn: str,
148+
fp: bool = False,
136149
):
137150
# NOTE: LIST FILES DOES NOT RESPECT ORDERING OF INPUT IDs LIST
138151
# NOTE: seqspec file -s read gets the files for the read, not the files mapped from the regions associated with the read.
@@ -164,7 +177,7 @@ def file(
164177
"json": format_json_files,
165178
}
166179

167-
x = FORMAT[fmt](files, fmt, k)
180+
x = FORMAT[fmt](files, fmt, k, spec_fn, fp)
168181
return x
169182

170183

@@ -196,7 +209,9 @@ def list_region_files(spec, modality):
196209
return list_onlist_files(spec, modality)
197210

198211

199-
def format_list_files_metadata(files: Dict[str, List[File]], fmt, k):
212+
def format_list_files_metadata(
213+
files: Dict[str, List[File]], fmt, k, spec_fn="", fp=False
214+
):
200215
x = ""
201216
if k == "all":
202217
for items in zip(*files.values()):
@@ -215,26 +230,42 @@ def format_list_files_metadata(files: Dict[str, List[File]], fmt, k):
215230
return x
216231

217232

218-
def format_json_files(files: Dict[str, List[File]], fmt, k):
233+
def format_json_files(files: Dict[str, List[File]], fmt, k, spec_fn="", fp=False):
219234
x = []
220235
for items in zip(*files.values()):
221236
if k == "all":
222237
for key, item in zip(files.keys(), items):
223-
x.append(item.to_dict())
238+
d = item.to_dict()
239+
if item.urltype == "local" and fp:
240+
d["url"] = os.path.join(os.path.dirname(spec_fn), d["url"])
241+
x.append(d)
224242
else:
225243
for key, item in zip(files.keys(), items):
226-
x.append({"file_id": item.file_id, k: getattr(item, k)})
244+
attr = getattr(item, k)
245+
if k == "url" and item.urltype == "local" and fp:
246+
attr = os.path.join(os.path.dirname(spec_fn), attr)
247+
x.append({"file_id": item.file_id, k: attr})
227248
return json.dumps(x, indent=4)
228249

229250

230-
def format_list_files(files: Dict[str, List[File]], fmt, k=None):
251+
def format_list_files(files: Dict[str, List[File]], fmt, k=None, spec_fn="", fp=False):
231252
x = ""
232253
if fmt == "paired":
233254
for items in zip(*files.values()):
234-
if k:
235-
x += "\t".join([str(getattr(i, k)) for i in items]) + "\n"
236-
else:
237-
x += "\t".join([i.filename for i in items]) + "\n"
255+
x = ""
256+
for i in items:
257+
if k:
258+
attr = str(getattr(i, k))
259+
if k == "url" and i.urltype == "local" and fp:
260+
attr = os.path.join(os.path.dirname(spec_fn), attr)
261+
x += f"{attr}\n"
262+
else:
263+
x += f"{i.filename}\n"
264+
# x = x[:-1]
265+
# if k:
266+
# x += "\t".join([str(getattr(i, k)) for i in items]) + "\n"
267+
# else:
268+
# x += "\t".join([i.filename for i in items]) + "\n"
238269
x = x[:-1]
239270

240271
elif fmt == "interleaved" or fmt == "list":
@@ -243,6 +274,8 @@ def format_list_files(files: Dict[str, List[File]], fmt, k=None):
243274
id = item.filename
244275
if k:
245276
id = str(getattr(item, k))
277+
if k == "url" and item.urltype == "local" and fp:
278+
id = os.path.join(os.path.dirname(spec_fn), id)
246279
x += id + "\n"
247280
x = x[:-1]
248281
elif fmt == "index":
@@ -251,6 +284,8 @@ def format_list_files(files: Dict[str, List[File]], fmt, k=None):
251284
id = item.filename
252285
if k:
253286
id = str(getattr(item, k))
287+
if k == "url" and item.urltype == "local" and fp:
288+
id = os.path.join(os.path.dirname(spec_fn), id)
254289
x += id + ","
255290
x = x[:-1]
256291

0 commit comments

Comments
 (0)