Mercurial > repos > recetox > matchms_formatter
annotate formatter.py @ 4:da32f57b4f88 draft
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5b13d8883fe18bee3aa992c2e34aa1fa79419442"
| author | recetox | 
|---|---|
| date | Fri, 11 Feb 2022 10:33:48 +0000 | 
| parents | 364976b9aba6 | 
| children | 4ca9807c56e6 | 
| rev | line source | 
|---|---|
| 0 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 1 import click | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 2 from pandas import DataFrame, read_csv | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 3 | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 4 | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 5 def create_long_table(data: DataFrame, value_id: str) -> DataFrame: | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 6 """Convert the table from compact into long format. | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 7 See DataFrame.melt(...). | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 8 | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 9 Args: | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 10 data (DataFrame): The data table to convert. | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 11 value_id (str): The name to assign to the added column through conversion to long format. | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 12 | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 13 Returns: | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 14 DataFrame: Table in long format. | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 15 """ | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 16 return data.transpose().melt(ignore_index=False, var_name='compound', value_name=value_id) | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 17 | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 18 | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 19 def join_df(x: DataFrame, y: DataFrame, on=[], how="inner") -> DataFrame: | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 20 """Shortcut functions to join to dataframes on columns and index | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 21 | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 22 Args: | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 23 x (DataFrame): Table X | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 24 y (DataFrame): Table Y | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 25 on (list, optional): Columns on which to join. Defaults to []. | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 26 how (str, optional): Join method, see DataFrame.join(...). Defaults to "inner". | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 27 | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 28 Returns: | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 29 DataFrame: Joined dataframe. | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 30 """ | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 31 df_x = x.set_index([x.index] + on) | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 32 df_y = y.set_index([y.index] + on) | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 33 combined = df_x.join(df_y, how=how) | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 34 return combined | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 35 | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 36 | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 37 def get_top_k_matches(data: DataFrame, k: int) -> DataFrame: | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 38 """Function to get top k matches from dataframe with scores. | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 39 | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 40 Args: | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 41 data (DataFrame): A table with score column. | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 42 k (int): Number of top scores to retrieve. | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 43 | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 44 Returns: | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 45 DataFrame: Table containing only the top k best matches for each compound. | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 46 """ | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 47 return data.groupby(level=0, group_keys=False).apply(DataFrame.nlargest, n=k, columns=['score']) | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 48 | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 49 | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 50 def filter_thresholds(data: DataFrame, t_score: float, t_matches: float) -> DataFrame: | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 51 """Filter a dataframe with scores and matches to only contain values above specified thresholds. | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 52 | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 53 Args: | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 54 data (DataFrame): Table to filter. | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 55 t_score (float): Score threshold. | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 56 t_matches (float): Matches threshold. | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 57 | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 58 Returns: | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 59 DataFrame: Filtered dataframe. | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 60 """ | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 61 filtered = data[data['score'] > t_score] | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 62 filtered = filtered[filtered['matches'] > t_matches] | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 63 return filtered | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 64 | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 65 | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 66 def load_data(scores_filename: str, matches_filename: str) -> DataFrame: | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 67 """Load data from filenames and join on compound id. | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 68 | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 69 Args: | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 70 scores_filename (str): Path to scores table. | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 71 matches_filename (str): Path to matches table. | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 72 | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 73 Returns: | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 74 DataFrame: Joined dataframe on compounds containing scores an matches in long format. | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 75 """ | 
| 1 
364976b9aba6
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 67040e686204e86c07ec46ca90129e50e40eb737"
 recetox parents: 
0diff
changeset | 76 matches = read_csv(matches_filename, sep=None, index_col=0) | 
| 
364976b9aba6
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 67040e686204e86c07ec46ca90129e50e40eb737"
 recetox parents: 
0diff
changeset | 77 scores = read_csv(scores_filename, sep=None, index_col=0) | 
| 0 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 78 | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 79 scores_long = create_long_table(scores, 'score') | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 80 matches_long = create_long_table(matches, 'matches') | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 81 | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 82 combined = join_df(matches_long, scores_long, on=['compound'], how='inner') | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 83 return combined | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 84 | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 85 | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 86 @click.group() | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 87 @click.option('--sf', 'scores_filename', type=click.Path(exists=True), required=True) | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 88 @click.option('--mf', 'matches_filename', type=click.Path(exists=True), required=True) | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 89 @click.option('--o', 'output_filename', type=click.Path(writable=True), required=True) | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 90 @click.pass_context | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 91 def cli(ctx, scores_filename, matches_filename, output_filename): | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 92 ctx.ensure_object(dict) | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 93 ctx.obj['data'] = load_data(scores_filename, matches_filename) | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 94 pass | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 95 | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 96 | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 97 @cli.command() | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 98 @click.option('--st', 'scores_threshold', type=float, required=True) | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 99 @click.option('--mt', 'matches_threshold', type=float, required=True) | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 100 @click.pass_context | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 101 def get_thresholded_data(ctx, scores_threshold, matches_threshold): | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 102 result = filter_thresholds(ctx.obj['data'], scores_threshold, matches_threshold) | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 103 return result | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 104 | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 105 | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 106 @cli.command() | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 107 @click.option('--k', 'k', type=int, required=True) | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 108 @click.pass_context | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 109 def get_top_k_data(ctx, k): | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 110 result = get_top_k_matches(ctx.obj['data'], k) | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 111 return result | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 112 | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 113 | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 114 @cli.resultcallback() | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 115 def write_output(result: DataFrame, scores_filename, matches_filename, output_filename): | 
| 1 
364976b9aba6
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 67040e686204e86c07ec46ca90129e50e40eb737"
 recetox parents: 
0diff
changeset | 116 input_file = read_csv(scores_filename, sep=None, iterator=True) | 
| 
364976b9aba6
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 67040e686204e86c07ec46ca90129e50e40eb737"
 recetox parents: 
0diff
changeset | 117 sep = input_file._engine.data.dialect.delimiter | 
| 
364976b9aba6
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 67040e686204e86c07ec46ca90129e50e40eb737"
 recetox parents: 
0diff
changeset | 118 | 
| 0 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 119 result = result.reset_index().rename(columns={'level_0': 'query', 'compound': 'reference'}) | 
| 1 
364976b9aba6
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 67040e686204e86c07ec46ca90129e50e40eb737"
 recetox parents: 
0diff
changeset | 120 result.to_csv(output_filename, sep=sep, index=False) | 
| 0 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 121 | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 122 | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 123 if __name__ == '__main__': | 
| 
0a08bed94964
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 85f60c94ccb3cb7706694cbb7ff6d59dcb41c0c9"
 recetox parents: diff
changeset | 124 cli(obj={}) | 
