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