Mercurial > repos > recetox > matchms_split
annotate formatter.py @ 4:49f9aa5cfa21 draft default tip
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit f79a5b51599254817727bc9028b9797ea994cb4e
| author | recetox | 
|---|---|
| date | Tue, 27 Jun 2023 14:18:56 +0000 | 
| parents | |
| children | 
| rev | line source | 
|---|---|
| 4 
49f9aa5cfa21
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit f79a5b51599254817727bc9028b9797ea994cb4e
 recetox parents: diff
changeset | 1 import click | 
| 
49f9aa5cfa21
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit f79a5b51599254817727bc9028b9797ea994cb4e
 recetox parents: diff
changeset | 2 from matchms.importing import scores_from_json | 
| 
49f9aa5cfa21
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit f79a5b51599254817727bc9028b9797ea994cb4e
 recetox parents: diff
changeset | 3 from pandas import DataFrame | 
| 
49f9aa5cfa21
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit f79a5b51599254817727bc9028b9797ea994cb4e
 recetox parents: diff
changeset | 4 | 
| 
49f9aa5cfa21
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit f79a5b51599254817727bc9028b9797ea994cb4e
 recetox parents: diff
changeset | 5 | 
| 
49f9aa5cfa21
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit f79a5b51599254817727bc9028b9797ea994cb4e
 recetox parents: diff
changeset | 6 def scores_to_dataframe(scores): | 
| 
49f9aa5cfa21
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit f79a5b51599254817727bc9028b9797ea994cb4e
 recetox parents: diff
changeset | 7 """Unpack scores from matchms.scores into two dataframes of scores and matches. | 
| 
49f9aa5cfa21
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit f79a5b51599254817727bc9028b9797ea994cb4e
 recetox parents: diff
changeset | 8 | 
| 
49f9aa5cfa21
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit f79a5b51599254817727bc9028b9797ea994cb4e
 recetox parents: diff
changeset | 9 Args: | 
| 
49f9aa5cfa21
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit f79a5b51599254817727bc9028b9797ea994cb4e
 recetox parents: diff
changeset | 10 scores (matchms.scores): matchms.scores object. | 
| 
49f9aa5cfa21
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit f79a5b51599254817727bc9028b9797ea994cb4e
 recetox parents: diff
changeset | 11 | 
| 
49f9aa5cfa21
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit f79a5b51599254817727bc9028b9797ea994cb4e
 recetox parents: diff
changeset | 12 Returns: | 
| 
49f9aa5cfa21
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit f79a5b51599254817727bc9028b9797ea994cb4e
 recetox parents: diff
changeset | 13 DataFrame: Scores | 
| 
49f9aa5cfa21
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit f79a5b51599254817727bc9028b9797ea994cb4e
 recetox parents: diff
changeset | 14 DataFrame: Matches | 
| 
49f9aa5cfa21
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit f79a5b51599254817727bc9028b9797ea994cb4e
 recetox parents: diff
changeset | 15 """ | 
| 
49f9aa5cfa21
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit f79a5b51599254817727bc9028b9797ea994cb4e
 recetox parents: diff
changeset | 16 dataframe = DataFrame(columns=['query', 'reference', *scores.scores.score_names]) | 
| 
49f9aa5cfa21
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit f79a5b51599254817727bc9028b9797ea994cb4e
 recetox parents: diff
changeset | 17 | 
| 
49f9aa5cfa21
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit f79a5b51599254817727bc9028b9797ea994cb4e
 recetox parents: diff
changeset | 18 for i, (row, col) in enumerate(zip(scores.scores.row, scores.scores.col)): | 
| 
49f9aa5cfa21
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit f79a5b51599254817727bc9028b9797ea994cb4e
 recetox parents: diff
changeset | 19 dataframe.loc[i] = [scores.queries[col].metadata['compound_name'], scores.references[row].metadata['compound_name'], *scores.scores.data[i]] | 
| 
49f9aa5cfa21
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit f79a5b51599254817727bc9028b9797ea994cb4e
 recetox parents: diff
changeset | 20 | 
| 
49f9aa5cfa21
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit f79a5b51599254817727bc9028b9797ea994cb4e
 recetox parents: diff
changeset | 21 return dataframe | 
| 
49f9aa5cfa21
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit f79a5b51599254817727bc9028b9797ea994cb4e
 recetox parents: diff
changeset | 22 | 
| 
49f9aa5cfa21
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit f79a5b51599254817727bc9028b9797ea994cb4e
 recetox parents: diff
changeset | 23 | 
| 
49f9aa5cfa21
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit f79a5b51599254817727bc9028b9797ea994cb4e
 recetox parents: diff
changeset | 24 def load_data(scores_filename: str) -> DataFrame: | 
| 
49f9aa5cfa21
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit f79a5b51599254817727bc9028b9797ea994cb4e
 recetox parents: diff
changeset | 25 """Load data from filenames and join on compound id. | 
| 
49f9aa5cfa21
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit f79a5b51599254817727bc9028b9797ea994cb4e
 recetox parents: diff
changeset | 26 | 
| 
49f9aa5cfa21
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit f79a5b51599254817727bc9028b9797ea994cb4e
 recetox parents: diff
changeset | 27 Args: | 
| 
49f9aa5cfa21
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit f79a5b51599254817727bc9028b9797ea994cb4e
 recetox parents: diff
changeset | 28 scores_filename (str): Path to json file with serialized scores. | 
| 
49f9aa5cfa21
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit f79a5b51599254817727bc9028b9797ea994cb4e
 recetox parents: diff
changeset | 29 | 
| 
49f9aa5cfa21
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit f79a5b51599254817727bc9028b9797ea994cb4e
 recetox parents: diff
changeset | 30 Returns: | 
| 
49f9aa5cfa21
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit f79a5b51599254817727bc9028b9797ea994cb4e
 recetox parents: diff
changeset | 31 DataFrame: Joined dataframe on compounds containing scores and matches in long format. | 
| 
49f9aa5cfa21
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit f79a5b51599254817727bc9028b9797ea994cb4e
 recetox parents: diff
changeset | 32 """ | 
| 
49f9aa5cfa21
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit f79a5b51599254817727bc9028b9797ea994cb4e
 recetox parents: diff
changeset | 33 scores = scores_from_json(scores_filename) | 
| 
49f9aa5cfa21
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit f79a5b51599254817727bc9028b9797ea994cb4e
 recetox parents: diff
changeset | 34 scores = scores_to_dataframe(scores) | 
| 
49f9aa5cfa21
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit f79a5b51599254817727bc9028b9797ea994cb4e
 recetox parents: diff
changeset | 35 | 
| 
49f9aa5cfa21
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit f79a5b51599254817727bc9028b9797ea994cb4e
 recetox parents: diff
changeset | 36 return scores | 
| 
49f9aa5cfa21
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit f79a5b51599254817727bc9028b9797ea994cb4e
 recetox parents: diff
changeset | 37 | 
| 
49f9aa5cfa21
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit f79a5b51599254817727bc9028b9797ea994cb4e
 recetox parents: diff
changeset | 38 | 
| 
49f9aa5cfa21
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit f79a5b51599254817727bc9028b9797ea994cb4e
 recetox parents: diff
changeset | 39 @click.group(invoke_without_command=True) | 
| 
49f9aa5cfa21
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit f79a5b51599254817727bc9028b9797ea994cb4e
 recetox parents: diff
changeset | 40 @click.option('--sf', 'scores_filename', type=click.Path(exists=True), required=True) | 
| 
49f9aa5cfa21
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit f79a5b51599254817727bc9028b9797ea994cb4e
 recetox parents: diff
changeset | 41 @click.option('--o', 'output_filename', type=click.Path(writable=True), required=True) | 
| 
49f9aa5cfa21
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit f79a5b51599254817727bc9028b9797ea994cb4e
 recetox parents: diff
changeset | 42 def cli(scores_filename, output_filename): | 
| 
49f9aa5cfa21
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit f79a5b51599254817727bc9028b9797ea994cb4e
 recetox parents: diff
changeset | 43 result = load_data(scores_filename) | 
| 
49f9aa5cfa21
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit f79a5b51599254817727bc9028b9797ea994cb4e
 recetox parents: diff
changeset | 44 result.to_csv(output_filename, sep="\t", index=False) | 
| 
49f9aa5cfa21
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit f79a5b51599254817727bc9028b9797ea994cb4e
 recetox parents: diff
changeset | 45 pass | 
| 
49f9aa5cfa21
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit f79a5b51599254817727bc9028b9797ea994cb4e
 recetox parents: diff
changeset | 46 | 
| 
49f9aa5cfa21
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit f79a5b51599254817727bc9028b9797ea994cb4e
 recetox parents: diff
changeset | 47 | 
| 
49f9aa5cfa21
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit f79a5b51599254817727bc9028b9797ea994cb4e
 recetox parents: diff
changeset | 48 if __name__ == '__main__': | 
| 
49f9aa5cfa21
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit f79a5b51599254817727bc9028b9797ea994cb4e
 recetox parents: diff
changeset | 49 cli() | 
