annotate formatter.py @ 6:1387786b6206 draft

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