6
6
from argparse import RawTextHelpFormatter
7
7
import json
8
8
from seqspec import seqspec_find
9
+ import os
10
+ import argparse
9
11
10
12
11
13
def setup_file_args (parser ):
@@ -89,33 +91,42 @@ def setup_file_args(parser):
89
91
choices = choices ,
90
92
)
91
93
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
+
92
102
return subparser
93
103
94
104
95
105
def validate_file_args (parser , args ):
96
- spec_fn = args .yaml
106
+ spec_fn = os . path . abspath ( args .yaml )
97
107
o = args .o
98
108
m = args .m # modality
99
109
idtype = args .s # selector
100
110
fmt = args .f # format
101
111
ids = args .i # ids
102
112
k = args .k # key
113
+ fp = args .fullpath
103
114
104
115
if (k == "filesize" or k == "filetype" or k == "urltype" or k == "md5" ) and (
105
116
fmt == "paired" or fmt == "interleaved" or fmt == "index"
106
117
):
107
118
parser .error (f"-f { fmt } valid only with -k file_id, filename, url" )
108
119
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 )
110
121
111
122
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 ):
113
124
spec = load_spec (spec_fn )
114
125
if ids is None :
115
126
ids = []
116
127
else :
117
128
ids = ids .split ("," )
118
- files = file (spec , m , ids , idtype , fmt , k )
129
+ files = file (spec , m , ids , idtype , fmt , k , spec_fn , fp )
119
130
120
131
if files :
121
132
if o :
@@ -133,6 +144,8 @@ def file(
133
144
idtype : str ,
134
145
fmt : str ,
135
146
k : Optional [str ],
147
+ spec_fn : str ,
148
+ fp : bool = False ,
136
149
):
137
150
# NOTE: LIST FILES DOES NOT RESPECT ORDERING OF INPUT IDs LIST
138
151
# 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(
164
177
"json" : format_json_files ,
165
178
}
166
179
167
- x = FORMAT [fmt ](files , fmt , k )
180
+ x = FORMAT [fmt ](files , fmt , k , spec_fn , fp )
168
181
return x
169
182
170
183
@@ -196,7 +209,9 @@ def list_region_files(spec, modality):
196
209
return list_onlist_files (spec , modality )
197
210
198
211
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
+ ):
200
215
x = ""
201
216
if k == "all" :
202
217
for items in zip (* files .values ()):
@@ -215,26 +230,42 @@ def format_list_files_metadata(files: Dict[str, List[File]], fmt, k):
215
230
return x
216
231
217
232
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 ):
219
234
x = []
220
235
for items in zip (* files .values ()):
221
236
if k == "all" :
222
237
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 )
224
242
else :
225
243
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 })
227
248
return json .dumps (x , indent = 4 )
228
249
229
250
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 ):
231
252
x = ""
232
253
if fmt == "paired" :
233
254
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"
238
269
x = x [:- 1 ]
239
270
240
271
elif fmt == "interleaved" or fmt == "list" :
@@ -243,6 +274,8 @@ def format_list_files(files: Dict[str, List[File]], fmt, k=None):
243
274
id = item .filename
244
275
if k :
245
276
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 )
246
279
x += id + "\n "
247
280
x = x [:- 1 ]
248
281
elif fmt == "index" :
@@ -251,6 +284,8 @@ def format_list_files(files: Dict[str, List[File]], fmt, k=None):
251
284
id = item .filename
252
285
if k :
253
286
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 )
254
289
x += id + ","
255
290
x = x [:- 1 ]
256
291
0 commit comments