Mercurial > repos > recetox > matchms_add_key
annotate matchms_networking_wrapper.py @ 3:43fbb784d546 draft
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 0f836e483fe3727580d6d1239135203b20812504
| author | recetox | 
|---|---|
| date | Wed, 22 Nov 2023 14:22:24 +0000 | 
| parents | 5f6707072df7 | 
| children | 
| rev | line source | 
|---|---|
| 0 
ea00a749ec1f
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit b1cc1aebf796f170d93e3dd46ffcdefdc7b8018a
 recetox parents: diff
changeset | 1 import argparse | 
| 
ea00a749ec1f
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit b1cc1aebf796f170d93e3dd46ffcdefdc7b8018a
 recetox parents: diff
changeset | 2 import sys | 
| 
ea00a749ec1f
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit b1cc1aebf796f170d93e3dd46ffcdefdc7b8018a
 recetox parents: diff
changeset | 3 | 
| 
ea00a749ec1f
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit b1cc1aebf796f170d93e3dd46ffcdefdc7b8018a
 recetox parents: diff
changeset | 4 from matchms.importing import scores_from_json | 
| 
ea00a749ec1f
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit b1cc1aebf796f170d93e3dd46ffcdefdc7b8018a
 recetox parents: diff
changeset | 5 from matchms.networking import SimilarityNetwork | 
| 
ea00a749ec1f
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit b1cc1aebf796f170d93e3dd46ffcdefdc7b8018a
 recetox parents: diff
changeset | 6 | 
| 
ea00a749ec1f
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit b1cc1aebf796f170d93e3dd46ffcdefdc7b8018a
 recetox parents: diff
changeset | 7 | 
| 
ea00a749ec1f
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit b1cc1aebf796f170d93e3dd46ffcdefdc7b8018a
 recetox parents: diff
changeset | 8 def main(argv): | 
| 
ea00a749ec1f
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit b1cc1aebf796f170d93e3dd46ffcdefdc7b8018a
 recetox parents: diff
changeset | 9 parser = argparse.ArgumentParser(description="Create network-graph from similarity scores.") | 
| 
ea00a749ec1f
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit b1cc1aebf796f170d93e3dd46ffcdefdc7b8018a
 recetox parents: diff
changeset | 10 parser.add_argument("--graph_format", type=str, help="Format of the output similarity network.") | 
| 
ea00a749ec1f
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit b1cc1aebf796f170d93e3dd46ffcdefdc7b8018a
 recetox parents: diff
changeset | 11 parser.add_argument("--score_name", type=str, help="Name of the score layer to use for creating the network graph.") | 
| 
ea00a749ec1f
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit b1cc1aebf796f170d93e3dd46ffcdefdc7b8018a
 recetox parents: diff
changeset | 12 parser.add_argument("--identifier", type=str, help="Unique metadata identifier of each spectrum from which scores are computed.") | 
| 
ea00a749ec1f
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit b1cc1aebf796f170d93e3dd46ffcdefdc7b8018a
 recetox parents: diff
changeset | 13 parser.add_argument("--top_n", type=int, help="Number of highest-score edges to keep.") | 
| 
ea00a749ec1f
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit b1cc1aebf796f170d93e3dd46ffcdefdc7b8018a
 recetox parents: diff
changeset | 14 parser.add_argument("--max_links", type=int, help="Maximum number of links to add per node.") | 
| 
ea00a749ec1f
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit b1cc1aebf796f170d93e3dd46ffcdefdc7b8018a
 recetox parents: diff
changeset | 15 parser.add_argument("--score_cutoff", type=float, help="Minimum similarity score value to link two spectra.") | 
| 
ea00a749ec1f
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit b1cc1aebf796f170d93e3dd46ffcdefdc7b8018a
 recetox parents: diff
changeset | 16 parser.add_argument("--link_method", type=str, help="Method for selecting top N edges for each node.") | 
| 
ea00a749ec1f
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit b1cc1aebf796f170d93e3dd46ffcdefdc7b8018a
 recetox parents: diff
changeset | 17 parser.add_argument("--keep_unconnected_nodes", help="Keep unconnected nodes in the network.", action="store_true") | 
| 
ea00a749ec1f
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit b1cc1aebf796f170d93e3dd46ffcdefdc7b8018a
 recetox parents: diff
changeset | 18 parser.add_argument("scores", type=str, help="Path to matchms similarity-scores .json file.") | 
| 
ea00a749ec1f
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit b1cc1aebf796f170d93e3dd46ffcdefdc7b8018a
 recetox parents: diff
changeset | 19 parser.add_argument("output_filename", type=str, help="Path where to store the output similarity network.") | 
| 
ea00a749ec1f
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit b1cc1aebf796f170d93e3dd46ffcdefdc7b8018a
 recetox parents: diff
changeset | 20 args = parser.parse_args() | 
| 
ea00a749ec1f
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit b1cc1aebf796f170d93e3dd46ffcdefdc7b8018a
 recetox parents: diff
changeset | 21 | 
| 
ea00a749ec1f
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit b1cc1aebf796f170d93e3dd46ffcdefdc7b8018a
 recetox parents: diff
changeset | 22 scores = scores_from_json(args.scores) | 
| 
ea00a749ec1f
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit b1cc1aebf796f170d93e3dd46ffcdefdc7b8018a
 recetox parents: diff
changeset | 23 | 
| 
ea00a749ec1f
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit b1cc1aebf796f170d93e3dd46ffcdefdc7b8018a
 recetox parents: diff
changeset | 24 network = SimilarityNetwork(identifier_key=args.identifier, | 
| 
ea00a749ec1f
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit b1cc1aebf796f170d93e3dd46ffcdefdc7b8018a
 recetox parents: diff
changeset | 25 top_n=args.top_n, | 
| 
ea00a749ec1f
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit b1cc1aebf796f170d93e3dd46ffcdefdc7b8018a
 recetox parents: diff
changeset | 26 max_links=args.max_links, | 
| 
ea00a749ec1f
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit b1cc1aebf796f170d93e3dd46ffcdefdc7b8018a
 recetox parents: diff
changeset | 27 score_cutoff=args.score_cutoff, | 
| 
ea00a749ec1f
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit b1cc1aebf796f170d93e3dd46ffcdefdc7b8018a
 recetox parents: diff
changeset | 28 link_method=args.link_method, | 
| 
ea00a749ec1f
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit b1cc1aebf796f170d93e3dd46ffcdefdc7b8018a
 recetox parents: diff
changeset | 29 keep_unconnected_nodes=args.keep_unconnected_nodes) | 
| 2 
5f6707072df7
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 13f4f7af12ec694a5f4ab7b3e52f46a4f67a245e
 recetox parents: 
0diff
changeset | 30 score_name = next((s for s in scores.score_names if args.score_name in s and "score" in s), None) | 
| 
5f6707072df7
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 13f4f7af12ec694a5f4ab7b3e52f46a4f67a245e
 recetox parents: 
0diff
changeset | 31 if score_name is None: | 
| 
5f6707072df7
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 13f4f7af12ec694a5f4ab7b3e52f46a4f67a245e
 recetox parents: 
0diff
changeset | 32 raise ValueError(f"Could not find any score name containing '{args.score_name}'.") | 
| 0 
ea00a749ec1f
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit b1cc1aebf796f170d93e3dd46ffcdefdc7b8018a
 recetox parents: diff
changeset | 33 | 
| 2 
5f6707072df7
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 13f4f7af12ec694a5f4ab7b3e52f46a4f67a245e
 recetox parents: 
0diff
changeset | 34 network.create_network(scores, score_name) | 
| 0 
ea00a749ec1f
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit b1cc1aebf796f170d93e3dd46ffcdefdc7b8018a
 recetox parents: diff
changeset | 35 network.export_to_file(filename=args.output_filename, graph_format=args.graph_format) | 
| 
ea00a749ec1f
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit b1cc1aebf796f170d93e3dd46ffcdefdc7b8018a
 recetox parents: diff
changeset | 36 | 
| 
ea00a749ec1f
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit b1cc1aebf796f170d93e3dd46ffcdefdc7b8018a
 recetox parents: diff
changeset | 37 return 0 | 
| 
ea00a749ec1f
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit b1cc1aebf796f170d93e3dd46ffcdefdc7b8018a
 recetox parents: diff
changeset | 38 | 
| 
ea00a749ec1f
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit b1cc1aebf796f170d93e3dd46ffcdefdc7b8018a
 recetox parents: diff
changeset | 39 | 
| 
ea00a749ec1f
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit b1cc1aebf796f170d93e3dd46ffcdefdc7b8018a
 recetox parents: diff
changeset | 40 if __name__ == "__main__": | 
| 
ea00a749ec1f
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit b1cc1aebf796f170d93e3dd46ffcdefdc7b8018a
 recetox parents: diff
changeset | 41 main(argv=sys.argv[1:]) | 
| 
ea00a749ec1f
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit b1cc1aebf796f170d93e3dd46ffcdefdc7b8018a
 recetox parents: diff
changeset | 42 pass | 
