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