1- import os
2-
31import numpy as np
4- import pandas as pd
52from scipy .sparse import csr_matrix
63
7- from TreeMS2 .groups .groups import Groups
84from TreeMS2 .logger_config import get_logger
95from TreeMS2 .similarity_matrix .filters .mask_filter import MaskFilter
106from TreeMS2 .similarity_matrix .similarity_matrix import SimilarityMatrix
1511
1612
1713class PrecursorMzFilter (MaskFilter ):
18- def __init__ (self , groups : Groups , vector_store : VectorStore ,
14+ def __init__ (self , vector_store : VectorStore ,
1915 precursor_mz_window : float ):
2016 self .precursor_mz_window = precursor_mz_window
2117 self .vector_store = vector_store
22- self .groups = groups
2318 super ().__init__ (None )
2419
2520 def construct_mask (self , similarity_matrix : SimilarityMatrix ) -> SpectraMatrix :
@@ -42,67 +37,3 @@ def construct_mask(self, similarity_matrix: SimilarityMatrix) -> SpectraMatrix:
4237 dtype = np .bool_ )
4338 mask = SpectraMatrix (m )
4439 return mask
45-
46- def write_filter_statistics (self , target_dir : str , total_spectra ):
47- if self .mask is None :
48- raise ValueError ("No mask has been constructed" )
49-
50- rows , cols = self .mask .matrix .nonzero ()
51-
52- row_ids = self .vector_store .get_data (rows , ["global_id" ])["global_id" ].to_numpy (dtype = np .int32 )
53- col_ids = self .vector_store .get_data (cols , ["global_id" ])["global_id" ].to_numpy (dtype = np .int32 )
54- m = csr_matrix ((self .mask .matrix .data , (row_ids , col_ids )), shape = (total_spectra , total_spectra ),
55- dtype = np .bool_ )
56-
57- nr_groups = self .groups .get_size ()
58- s = np .zeros ((nr_groups , nr_groups ), dtype = np .uint64 )
59- # loop over groups representing the rows in s
60- for row_group in self .groups .get_groups ():
61- row_group_id = row_group .get_id ()
62- # retrieve id of first and last spectrum in group
63- row_begin = row_group .begin
64- row_end = row_group .end
65- # loop over groups representing the columns in s
66- for col_group in self .groups .get_groups ():
67- col_group_id = col_group .get_id ()
68- # retrieve id of first and last spectrum in group
69- col_begin = col_group .begin
70- col_end = col_group .end
71- # count the number of spectra that have been filtered between group A and group B
72- filtered = m [row_begin :row_end + 1 , col_begin :col_end + 1 ].nnz
73- s [row_group_id , col_group_id ] = filtered
74-
75- # create a dataframe
76- group_names = [group .get_group_name () for group in self .groups .get_groups ()]
77- df = pd .DataFrame (s , index = group_names , columns = group_names )
78-
79- # create path
80- filters_dir = os .path .join (target_dir , "filters" )
81- os .makedirs (filters_dir , exist_ok = True )
82- path = os .path .join (filters_dir , "precursor_mz.txt" )
83-
84- # write statistics to disk
85- with open (path , 'w' ) as f :
86- # Write a header explanation
87- f .write (
88- f"Number of spectra considered similar between each pair of groups with a precursor m/z difference larger than { self .precursor_mz_window } :\n \n " )
89- # Write the matrix
90- f .write (df .to_string ())
91- logger .info (
92- f"Overview of the number of similarities filtered due to precursor m/z difference written to '{ path } '" )
93- return s
94-
95- def save_mask (self , target_dir : str ):
96- # save mask
97- path = self .mask .write (os .path .join (target_dir , "precursor_mz" ))
98- logger .info (f"Precursor mz mask has been written to '{ path } '." )
99- return path
100-
101- def save_mask_global (self , target_dir : str , total_spectra : int ):
102- # save mask
103- path = self .mask .write_global (os .path .join (target_dir , "precursor_mz_global" ), total_spectra , self .vector_store )
104- logger .info (f"Precursor mz mask has been written to '{ path } '." )
105- return path
106-
107- def __repr__ (self ) -> str :
108- return f"{ self .__class__ .__name__ } (precursor_mz_window={ self .precursor_mz_window :.3f} )"
0 commit comments