Mercurial > repos > immport-devteam > flowcl
comparison getOntology.py @ 1:f70f75e89890 draft default tip
"planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/flowcl commit d59d95d2bc6a64eb5c37b8291a7c314754c2067f"
author | azomics |
---|---|
date | Thu, 23 Jul 2020 08:50:18 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
0:fb0ee82f686d | 1:f70f75e89890 |
---|---|
1 #!/usr/bin/env python | |
2 ###################################################################### | |
3 # Copyright (c) 2016 Northrop Grumman. | |
4 # All rights reserved. | |
5 ###################################################################### | |
6 from __future__ import print_function | |
7 import sys | |
8 import os | |
9 | |
10 from collections import defaultdict | |
11 from argparse import ArgumentParser | |
12 from jinja2 import Environment, FileSystemLoader | |
13 | |
14 | |
15 def generate_flowCL_query(list_markers, list_types): | |
16 if (len(list_markers) != len(list_types)): | |
17 return("pb with headers") | |
18 query = [] | |
19 # go through both lists, remove fsc/ssc | |
20 for i in range(0, len(list_markers)): | |
21 if not list_markers[i].startswith("FSC") and not list_markers[i].startswith("SSC"): | |
22 query.append(list_markers[i].upper()) | |
23 query.append(list_types[i]) | |
24 # return concatenated string | |
25 return("".join(query)) | |
26 | |
27 | |
28 def run_flowCL(phenotype, output_file, output_dir, tool_dir): | |
29 os.mkdir(output_dir) | |
30 tool = "/".join([tool_dir, "getOntology.R"]) | |
31 output_txt = "".join([output_dir, "/flowCL_run_summary.txt"]) | |
32 output_table = "".join([output_dir, "/flowCL_table.txt"]) | |
33 output_pdf = "".join([output_dir, "/flowCL_res.pdf"]) | |
34 run_command = " ". join(["Rscript --slave --vanilla", tool, output_txt, phenotype]) | |
35 os.system(run_command) | |
36 | |
37 table = defaultdict(list) | |
38 labels = [] | |
39 nb_match = 0 | |
40 if os.path.isfile(output_txt): | |
41 with open(output_txt, "r") as txt: | |
42 check = txt.readline().strip() | |
43 if (not check): | |
44 sys.exit(2) | |
45 else: | |
46 i = -1 | |
47 for lines in txt: | |
48 data = lines.strip("\n").split("\"") | |
49 if data[0].strip(): | |
50 labels.append(data[0].strip()) | |
51 i += 1 | |
52 if data[0].startswith("Score"): | |
53 count_matches = data[1].split(") ") | |
54 nb_match = len(count_matches) - 1 | |
55 table[i].append(data[1]) | |
56 else: | |
57 sys.stderr.write("There are no results with this query. Please check your markers if you believe there should be.") | |
58 sys.exit(2) | |
59 | |
60 with open(output_table, "w") as tbl: | |
61 tbl.write("1\t2\nQuery\t" + phenotype + "\n") | |
62 for j in table: | |
63 newline = " ".join(table[j]) | |
64 for k in range(1, nb_match + 1): | |
65 cur_stg = "".join([str(k+1), ")"]) | |
66 new_stg = "".join(["<br>", cur_stg]) | |
67 newline = newline.replace(cur_stg, new_stg) | |
68 | |
69 if labels[j] == "Cell ID": | |
70 cls = newline.split(" ") | |
71 for m in range(0, len(cls)): | |
72 if cls[m].startswith("CL"): | |
73 cl_id = cls[m].replace("_", ":") | |
74 link = "".join(['<a href="http://www.immport-labs.org/immport-ontology/public/home/home/', cl_id, '" target="_blank">']) | |
75 cls[m] = "".join([link, cls[m], "</a>"]) | |
76 newline = " ".join(cls) | |
77 tbl.write("\t".join([labels[j], newline]) + "\n") | |
78 | |
79 get_graph = " ".join(["mv flowCL_results/*.pdf", output_pdf]) | |
80 os.system(get_graph) | |
81 | |
82 env = Environment(loader=FileSystemLoader(tool_dir + "/templates")) | |
83 template = env.get_template("flowCL.template") | |
84 | |
85 real_directory = output_dir.replace("/job_working_directory", "") | |
86 context = {'outputDirectory': real_directory} | |
87 overview = template.render(**context) | |
88 with open(output_file, "w") as outf: | |
89 outf.write(overview) | |
90 return | |
91 | |
92 | |
93 if __name__ == "__main__": | |
94 parser = ArgumentParser( | |
95 prog="getOntology", | |
96 description="runs flowCL on a set of markers.") | |
97 | |
98 parser.add_argument( | |
99 '-m', | |
100 dest="markers", | |
101 required=True, | |
102 action='append', | |
103 help="marker queries.") | |
104 | |
105 parser.add_argument( | |
106 '-y', | |
107 dest="marker_types", | |
108 required=True, | |
109 action='append', | |
110 help="marker queries.") | |
111 | |
112 parser.add_argument( | |
113 '-o', | |
114 dest="output_file", | |
115 required=True, | |
116 help="Name of the output html file.") | |
117 | |
118 parser.add_argument( | |
119 '-d', | |
120 dest="output_dir", | |
121 required=True, | |
122 help="Path to the html supporting directory") | |
123 | |
124 parser.add_argument( | |
125 '-t', | |
126 dest="tool_dir", | |
127 required=True, | |
128 help="Path to the tool directory") | |
129 | |
130 args = parser.parse_args() | |
131 | |
132 markers = [m.strip() for m in args.markers] | |
133 query = generate_flowCL_query(markers, args.marker_types) | |
134 run_flowCL(query, args.output_file, args.output_dir, args.tool_dir) |