@@ -1183,7 +1183,7 @@ def text_to_bed_search(
11831183 if result_meta :
11841184 results_list .append (QdrantSearchResult (** result , metadata = result_meta ))
11851185 return BedListSearchResult (
1186- count = self .bb_agent .get_stats ().bedfiles_number ,
1186+ count = self .bb_agent .get_detailed_stats ().file_genome . get ( "hg38" , 0 ) ,
11871187 limit = limit ,
11881188 offset = offset ,
11891189 results = results_list ,
@@ -1218,34 +1218,49 @@ def bed_to_bed_search(
12181218 )
12191219
12201220 def sql_search (
1221- self , query : str , limit : int = 10 , offset : int = 0
1221+ self ,
1222+ query : str ,
1223+ genome : str = None ,
1224+ limit : int = 10 ,
1225+ offset : int = 0 ,
12221226 ) -> BedListSearchResult :
12231227 """
12241228 Search for bed files by using sql exact search.
12251229 This search will search files by id, name, and description
12261230
12271231 :param query: text query
1232+ :param genome: genome alias to filter results
12281233 :param limit: number of results to return
12291234 :param offset: offset to start from
12301235
12311236 :return: list of bed file metadata
12321237 """
12331238 _LOGGER .debug (f"Looking for: { query } " )
12341239
1240+ statement = select (Bed ).join (BedMetadata )
1241+
12351242 sql_search_str = f"%{ query } %"
1243+ or_statement = or_ (
1244+ Bed .id .ilike (sql_search_str ),
1245+ Bed .name .ilike (sql_search_str ),
1246+ Bed .description .ilike (sql_search_str ),
1247+ BedMetadata .cell_line .ilike (sql_search_str ),
1248+ BedMetadata .tissue .ilike (sql_search_str ),
1249+ BedMetadata .cell_type .ilike (sql_search_str ),
1250+ )
1251+
1252+ if genome_alias := genome :
1253+ _LOGGER .debug (f"Filtering by genome: { genome_alias } " )
1254+
1255+ condition_statement = and_ (Bed .genome_alias == genome_alias , or_statement )
1256+
1257+ else :
1258+ condition_statement = or_statement
1259+
1260+ statement = statement .where (condition_statement )
1261+
12361262 with Session (self ._sa_engine ) as session :
1237- statement = (
1238- select (Bed )
1239- .where (
1240- or_ (
1241- Bed .id .ilike (sql_search_str ),
1242- Bed .name .ilike (sql_search_str ),
1243- Bed .description .ilike (sql_search_str ),
1244- )
1245- )
1246- .limit (limit )
1247- .offset (offset )
1248- )
1263+ statement = statement .limit (limit ).offset (offset )
12491264 bed_objects = session .scalars (statement )
12501265 results = [
12511266 BedMetadataBasic (
@@ -1266,32 +1281,27 @@ def sql_search(
12661281 ]
12671282
12681283 return BedListSearchResult (
1269- count = self ._sql_search_count (query ),
1284+ count = self ._sql_search_count (condition_statement ),
12701285 limit = limit ,
12711286 offset = offset ,
12721287 results = result_list ,
12731288 )
12741289
1275- def _sql_search_count (self , query : str ) -> int :
1290+ def _sql_search_count (self , condition_statement ) -> int :
12761291 """
12771292 Get number of total found files in the database.
12781293
1279- :param query: text query
1294+ :param condition_statement: sql alchemy condition statement to filter results
12801295
12811296 :return: number of found files
12821297 """
1283- sql_search_str = f"% { query } %"
1298+
12841299 with Session (self ._sa_engine ) as session :
12851300 statement = (
12861301 select (func .count ())
12871302 .select_from (Bed )
1288- .where (
1289- or_ (
1290- Bed .id .ilike (sql_search_str ),
1291- Bed .name .ilike (sql_search_str ),
1292- Bed .description .ilike (sql_search_str ),
1293- )
1294- )
1303+ .join (BedMetadata )
1304+ .where (condition_statement )
12951305 )
12961306 count = session .execute (statement ).one ()
12971307 return count [0 ]
0 commit comments