annotate formatter.py @ 13:fc1bc38ede0b draft

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