1010from metapang .logger import mp_log
1111from metapang .pg import compat
1212from metapang .pg .api import PanGBank_Cache , parse_collection_name_version
13+ from metapang .pg .local import (
14+ LOCAL_COLLECTION ,
15+ build_local_pangenome ,
16+ is_built ,
17+ local_dir ,
18+ local_root ,
19+ )
1320
1421
1522def _cache (ctx ) -> PanGBank_Cache :
@@ -67,6 +74,54 @@ def path(ctx) -> None:
6774 click .echo (str (_cache (ctx ).directory ))
6875
6976
77+ @cache .command ()
78+ @click .argument (
79+ "pangenome" , type = click .Path (exists = True , dir_okay = False , path_type = Path )
80+ )
81+ @click .option (
82+ "--name" ,
83+ "-n" ,
84+ required = True ,
85+ help = "Name to register the pangenome under (referenced as 'local:<name>')." ,
86+ )
87+ @click .option (
88+ "--kmer-size" ,
89+ "-k" ,
90+ type = int ,
91+ help = "K-mer size for the de Bruijn graph __placeholder__." ,
92+ )
93+ @click .option ("--threads" , "-t" , type = int , help = "Build threads __placeholder__." )
94+ @click .option (
95+ "--metagraph-path" ,
96+ help = "Path to the metagraph binary __placeholder__." ,
97+ )
98+ @click .option ("--force" , "-f" , is_flag = True , help = "Rebuild even if already present." )
99+ @click .pass_context
100+ def add (ctx , pangenome , name , kmer_size , threads , metagraph_path , force ) -> None :
101+ """
102+ Build a local pangenome (.h5) into the cache as 'local:<name>'.
103+
104+ \b
105+ Profile against it with:
106+ metapang profile reads.fastq.gz -b local:<name>
107+ """
108+ import tempfile
109+
110+ store = _cache (ctx )
111+ with tempfile .TemporaryDirectory (prefix = "metapang-build-" ) as tmp :
112+ dbg_dir = build_local_pangenome (
113+ pangenome ,
114+ name ,
115+ kmer_size ,
116+ store .directory ,
117+ Path (tmp ),
118+ threads ,
119+ metagraph_path ,
120+ force = force ,
121+ )
122+ mp_log .info (f"Added local:{ name } : { dbg_dir } " )
123+
124+
70125@cache .command (name = "list" )
71126@click .pass_context
72127def list_ (ctx ) -> None :
@@ -75,40 +130,55 @@ def list_(ctx) -> None:
75130 root = store .collection_directory
76131 console = Console ()
77132
78- if not root .exists () or not any (root .iterdir ()):
79- console .print (f"Cache is empty ({ store .directory } )" )
80- return
81-
82133 table = Table ("collection" , "version" , "item" , "kind" , "status" , "size" )
83- for coll in sorted (p for p in root .iterdir () if p .is_dir ()):
84- for ver in sorted (p for p in coll .iterdir () if p .is_dir ()):
85- if (ver / "bank_index" ).exists ():
86- table .add_row (
87- coll .name ,
88- ver .name ,
89- "bank_index" ,
90- "index" ,
91- "-" ,
92- _human (_dir_size (ver / "bank_index" )),
93- )
94- pangenomes = ver / "pangenomes"
95- if pangenomes .exists ():
96- for h5 in sorted (pangenomes .glob ("*.h5" )):
134+ if root .exists ():
135+ for coll in sorted (p for p in root .iterdir () if p .is_dir ()):
136+ for ver in sorted (p for p in coll .iterdir () if p .is_dir ()):
137+ if (ver / "bank_index" ).exists ():
97138 table .add_row (
98139 coll .name ,
99140 ver .name ,
100- h5 . stem ,
101- "pangenome " ,
141+ "bank_index" ,
142+ "index " ,
102143 "-" ,
103- _human (h5 .stat ().st_size ),
104- )
105- dbg = ver / "dbg"
106- if dbg .exists ():
107- for d in sorted (p for p in dbg .iterdir () if p .is_dir ()):
108- status = "complete" if _dbg_complete (d ) else "partial"
109- table .add_row (
110- coll .name , ver .name , d .name , "dbg" , status , _human (_dir_size (d ))
144+ _human (_dir_size (ver / "bank_index" )),
111145 )
146+ pangenomes = ver / "pangenomes"
147+ if pangenomes .exists ():
148+ for h5 in sorted (pangenomes .glob ("*.h5" )):
149+ table .add_row (
150+ coll .name ,
151+ ver .name ,
152+ h5 .stem ,
153+ "pangenome" ,
154+ "-" ,
155+ _human (h5 .stat ().st_size ),
156+ )
157+ dbg = ver / "dbg"
158+ if dbg .exists ():
159+ for d in sorted (p for p in dbg .iterdir () if p .is_dir ()):
160+ status = "complete" if _dbg_complete (d ) else "partial"
161+ table .add_row (
162+ coll .name ,
163+ ver .name ,
164+ d .name ,
165+ "dbg" ,
166+ status ,
167+ _human (_dir_size (d )),
168+ )
169+
170+ lroot = local_root (store .directory )
171+ if lroot .exists ():
172+ for d in sorted (p for p in lroot .iterdir () if p .is_dir ()):
173+ status = "complete" if is_built (store .directory , d .name ) else "partial"
174+ table .add_row (
175+ LOCAL_COLLECTION , "-" , d .name , "local" , status , _human (_dir_size (d ))
176+ )
177+
178+ if not table .rows :
179+ console .print (f"Cache is empty ({ store .directory } )" )
180+ return
181+
112182 console .print (table )
113183 console .print (f"Total: { _human (_dir_size (store .directory ))} ({ store .directory } )" )
114184
@@ -127,12 +197,22 @@ def clear(ctx, target, yes) -> None:
127197 metapang cache clear GTDB_refseq
128198 metapang cache clear GTDB_refseq@2.0.0
129199 metapang cache clear GTDB_refseq@2.0.0:s__Abiotrophia_defectiva
200+ metapang cache clear local
201+ metapang cache clear local:my_pangenome
130202 """
131203 store = _cache (ctx )
132204
133205 if target is None :
134206 to_remove = [store .directory ]
135207 label = f"the entire cache ({ store .directory } )"
208+ elif (parsed := parse_collection_name_version (target ))[0 ] == LOCAL_COLLECTION :
209+ _ , _ , pangenome = parsed
210+ if pangenome :
211+ to_remove = [local_dir (store .directory , pangenome )]
212+ label = f"local:{ pangenome } "
213+ else :
214+ to_remove = [local_root (store .directory )]
215+ label = "all local pangenomes"
136216 else :
137217 name , version , pangenome = parse_collection_name_version (target )
138218 base = store .collection_directory / name
@@ -181,19 +261,19 @@ def _remove(path: Path) -> None:
181261@cache .command ()
182262@click .argument ("target" )
183263@click .option (
184- "--index/--no-index" , "do_index" , default = True , help = "Fetch the release bank index."
264+ "--index/--no-index" ,
265+ "do_index" ,
266+ help = "Fetch the release bank index __placeholder__." ,
185267)
186268@click .option (
187269 "--dbg/--no-dbg" ,
188270 "do_dbg" ,
189- default = True ,
190- help = "Fetch the de Bruijn graph of each named species." ,
271+ help = "Fetch the de Bruijn graph of each named species __placeholder__." ,
191272)
192273@click .option (
193274 "--pangenome/--no-pangenome" ,
194275 "do_pangenome" ,
195- default = True ,
196- help = "Fetch the .h5 pangenome of each named species." ,
276+ help = "Fetch the .h5 pangenome of each named species __placeholder__." ,
197277)
198278@click .option ("--force" , "-f" , is_flag = True , help = "Re-download even if already cached." )
199279@click .pass_context
0 commit comments