annotate formatter.py @ 5:a177ac3c752c draft

planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit f79a5b51599254817727bc9028b9797ea994cb4e
author recetox
date Tue, 27 Jun 2023 14:25:01 +0000
parents
children 13de8005adba
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5
a177ac3c752c planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit f79a5b51599254817727bc9028b9797ea994cb4e
recetox
parents:
diff changeset
1 import click
a177ac3c752c 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
a177ac3c752c planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit f79a5b51599254817727bc9028b9797ea994cb4e
recetox
parents:
diff changeset
3 from pandas import DataFrame
a177ac3c752c planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit f79a5b51599254817727bc9028b9797ea994cb4e
recetox
parents:
diff changeset
4
a177ac3c752c planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit f79a5b51599254817727bc9028b9797ea994cb4e
recetox
parents:
diff changeset
5
a177ac3c752c 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):
a177ac3c752c 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.
a177ac3c752c planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit f79a5b51599254817727bc9028b9797ea994cb4e
recetox
parents:
diff changeset
8
a177ac3c752c planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit f79a5b51599254817727bc9028b9797ea994cb4e
recetox
parents:
diff changeset
9 Args:
a177ac3c752c 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.
a177ac3c752c planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit f79a5b51599254817727bc9028b9797ea994cb4e
recetox
parents:
diff changeset
11
a177ac3c752c planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit f79a5b51599254817727bc9028b9797ea994cb4e
recetox
parents:
diff changeset
12 Returns:
a177ac3c752c planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit f79a5b51599254817727bc9028b9797ea994cb4e
recetox
parents:
diff changeset
13 DataFrame: Scores
a177ac3c752c planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit f79a5b51599254817727bc9028b9797ea994cb4e
recetox
parents:
diff changeset
14 DataFrame: Matches
a177ac3c752c planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit f79a5b51599254817727bc9028b9797ea994cb4e
recetox
parents:
diff changeset
15 """
a177ac3c752c 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])
a177ac3c752c planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit f79a5b51599254817727bc9028b9797ea994cb4e
recetox
parents:
diff changeset
17
a177ac3c752c 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)):
a177ac3c752c 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]]
a177ac3c752c planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit f79a5b51599254817727bc9028b9797ea994cb4e
recetox
parents:
diff changeset
20
a177ac3c752c planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit f79a5b51599254817727bc9028b9797ea994cb4e
recetox
parents:
diff changeset
21 return dataframe
a177ac3c752c planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit f79a5b51599254817727bc9028b9797ea994cb4e
recetox
parents:
diff changeset
22
a177ac3c752c planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit f79a5b51599254817727bc9028b9797ea994cb4e
recetox
parents:
diff changeset
23
a177ac3c752c 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:
a177ac3c752c 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.
a177ac3c752c planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit f79a5b51599254817727bc9028b9797ea994cb4e
recetox
parents:
diff changeset
26
a177ac3c752c planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit f79a5b51599254817727bc9028b9797ea994cb4e
recetox
parents:
diff changeset
27 Args:
a177ac3c752c 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.
a177ac3c752c planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit f79a5b51599254817727bc9028b9797ea994cb4e
recetox
parents:
diff changeset
29
a177ac3c752c planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit f79a5b51599254817727bc9028b9797ea994cb4e
recetox
parents:
diff changeset
30 Returns:
a177ac3c752c 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.
a177ac3c752c planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit f79a5b51599254817727bc9028b9797ea994cb4e
recetox
parents:
diff changeset
32 """
a177ac3c752c 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)
a177ac3c752c 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)
a177ac3c752c planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit f79a5b51599254817727bc9028b9797ea994cb4e
recetox
parents:
diff changeset
35
a177ac3c752c planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit f79a5b51599254817727bc9028b9797ea994cb4e
recetox
parents:
diff changeset
36 return scores
a177ac3c752c planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit f79a5b51599254817727bc9028b9797ea994cb4e
recetox
parents:
diff changeset
37
a177ac3c752c planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit f79a5b51599254817727bc9028b9797ea994cb4e
recetox
parents:
diff changeset
38
a177ac3c752c 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)
a177ac3c752c 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)
a177ac3c752c 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)
a177ac3c752c 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):
a177ac3c752c 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)
a177ac3c752c 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)
a177ac3c752c planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit f79a5b51599254817727bc9028b9797ea994cb4e
recetox
parents:
diff changeset
45 pass
a177ac3c752c planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit f79a5b51599254817727bc9028b9797ea994cb4e
recetox
parents:
diff changeset
46
a177ac3c752c planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit f79a5b51599254817727bc9028b9797ea994cb4e
recetox
parents:
diff changeset
47
a177ac3c752c planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit f79a5b51599254817727bc9028b9797ea994cb4e
recetox
parents:
diff changeset
48 if __name__ == '__main__':
a177ac3c752c planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit f79a5b51599254817727bc9028b9797ea994cb4e
recetox
parents:
diff changeset
49 cli()