annotate formatter.py @ 0:ea891750acfc draft

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