33from metapang .logger import mp_log
44from metapang .pg .api import PanGBank_Cache , parse_collection_name_version
55from metapang .core .index .dbg import MetagraphCLI , MetagraphQueryOptions
6+ from metapang .core .graph import PWGraph , PWGraphAnnotator
67from metapang .utils .time import timer
78import sys
89
4546 type = click .Path (path_type = str ),
4647 help = "Output file ('stdout' or '-' for standard output) __placeholder__." ,
4748)
49+ @click .option (
50+ "--annotate" ,
51+ type = click .Path (path_type = Path ),
52+ help = "Also write a graph_tool annotated pangenome graph (.gt) to this path,"
53+ "with per-family read and k-mer counts" ,
54+ )
4855@click .pass_context
49- def pangenome (ctx , dbg , annotations , pangbank , query , threads , metagraph_path , output ) -> None :
56+ def pangenome (ctx , dbg , annotations , pangbank , query , threads , metagraph_path , output , annotate ) -> None :
5057 """
5158 [bold]Search a in a MetaPanG pangenome index[/bold]
5259
@@ -70,6 +77,11 @@ def pangenome(ctx, dbg, annotations, pangbank, query, threads, metagraph_path, o
7077 if not (dbg and annotations ):
7178 raise click .UsageError ("You must provide either --pangbank OR (--dbg AND --annotations)." )
7279
80+ if annotate and not pangbank :
81+ raise click .UsageError ("--annotate requires --pangbank: the annotated graph is built on the prebuilt .gt downloaded from PanGBank" )
82+ if annotate and output in ("stdout" , "-" ):
83+ raise click .UsageError ("--annotate needs the query results in a file. Set --output to a file, not stdout" )
84+
7385 config = ctx .obj .get ("config" )
7486
7587 if pangbank :
@@ -95,9 +107,11 @@ def pangenome(ctx, dbg, annotations, pangbank, query, threads, metagraph_path, o
95107 dbg_directory = proxy .get_dbg (pangenome_name )
96108 dbg_file = dbg_directory / f"{ pangenome_name } .dbg"
97109 annotations_file = dbg_directory / f"{ pangenome_name } .row_diff_brwt.annodbg"
110+ pangenome_gt = dbg_directory / f"{ pangenome_name } .gt"
98111 else :
99112 dbg_file = dbg
100113 annotations_file = annotations
114+ pangenome_gt = None
101115
102116 mcli = MetagraphCLI (metagraph_path )
103117
@@ -117,10 +131,25 @@ def pangenome(ctx, dbg, annotations, pangbank, query, threads, metagraph_path, o
117131
118132 mcli .query (query_opt )
119133
134+ if annotate :
135+ assert pangenome_gt is not None
136+ if not pangenome_gt .exists ():
137+ mp_log .error (f"Cannot annotate: pangenome graph '{ pangenome_gt } ' not found" )
138+ sys .exit (1 )
139+ mp_log .info (f"Annotating pangenome graph '{ pangenome_gt .name } '" )
140+ pwg = PWGraph (pangenome_gt )
141+ report = PWGraphAnnotator (pwg , Path (output )).annotate ()
142+ ratio = (f" ({ 100 * report .total_seq_mapped / report .total_seq :.1f} %)"
143+ if report .total_seq else "" )
144+ pwg .save (annotate )
145+ mp_log .info (f"Reads mapped: { report .total_seq_mapped } /{ report .total_seq } { ratio } " )
146+
120147 mp_log .info (f"Done ({ t .format ()} )" )
121148
122149 if output not in ("stdout" , "-" ):
123150 mp_log .info (f"Output available at '{ output } '" )
151+ if annotate :
152+ mp_log .info (f"Annotated graph available at '{ annotate } '" )
124153
125154
126155
0 commit comments