Mercurial > repos > bioit_sciensano > phagetermvirome
comparison _modules/generate_report.py @ 0:69e8f12c8b31 draft
"planemo upload"
author | bioit_sciensano |
---|---|
date | Fri, 11 Mar 2022 15:06:20 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:69e8f12c8b31 |
---|---|
1 from __future__ import print_function | |
2 import os | |
3 import pickle | |
4 from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Image, Table, TableStyle, PageBreak | |
5 from reportlab.lib.pagesizes import letter, landscape | |
6 from _modules.functions_PhageTerm import SummaryReport,WorkflowReport,ExportCohesiveSeq,ExportPhageSequence,CreateReport | |
7 | |
8 | |
9 def loadDR(DR_path,DR): | |
10 for d in os.listdir(DR_path): # iterate over P_class subdirectories. | |
11 if not os.path.isdir(os.path.join(DR_path,d)): | |
12 err_str=DR_path+" should contain only directories." | |
13 raise RuntimeError(err_str) | |
14 for fic_name in os.listdir(os.path.join(DR_path,d)): # iterate over all files for a given P_class | |
15 p=os.path.join(DR_path,d) | |
16 fname=os.path.join(p,fic_name) | |
17 with open(fname, 'rb') as f: | |
18 loaded_items=pickle.load(f) | |
19 # d is P_class name, fic_name is phagename. | |
20 dict_tmp=dict() | |
21 dict_tmp["phagename"]=loaded_items[0] | |
22 dict_tmp["seed"]=loaded_items[1] | |
23 dict_tmp["added_whole_coverage"]=loaded_items[2] | |
24 dict_tmp["Redundant"]=loaded_items[3] | |
25 dict_tmp["P_left"]=loaded_items[4] | |
26 print("P_left=",dict_tmp["P_left"],type(dict_tmp["P_left"])) | |
27 dict_tmp["P_right"] = loaded_items[5] | |
28 print("P_right=",dict_tmp["P_right"],type(dict_tmp["P_right"])) | |
29 dict_tmp["Permuted"]=loaded_items[6] | |
30 dict_tmp["P_orient"] =loaded_items[7] | |
31 dict_tmp["termini_coverage_norm_close"] =loaded_items[8] | |
32 dict_tmp["picMaxPlus_norm_close"] =loaded_items[9] | |
33 dict_tmp["picMaxMinus_norm_close"] =loaded_items[10] | |
34 dict_tmp["gen_len"] =loaded_items[11] | |
35 dict_tmp["tot_reads"] =loaded_items[12] | |
36 dict_tmp["P_seqcoh"] =loaded_items[13] | |
37 dict_tmp["phage_plus_norm"] =loaded_items[14] | |
38 dict_tmp["phage_minus_norm"] =loaded_items[15] | |
39 dict_tmp["ArtPackmode"] = loaded_items[16] | |
40 dict_tmp["termini"] = loaded_items[17] | |
41 dict_tmp["forward"] = loaded_items[18] | |
42 dict_tmp["reverse"] = loaded_items[19] | |
43 dict_tmp["ArtOrient"] = loaded_items[20] | |
44 dict_tmp["ArtcohesiveSeq"] = loaded_items[21] | |
45 dict_tmp["termini_coverage_close"] = loaded_items[22] | |
46 dict_tmp["picMaxPlus_close"] = loaded_items[23] | |
47 dict_tmp["picMaxMinus_close"] = loaded_items[24] | |
48 dict_tmp["picOUT_norm_forw"] = loaded_items[25] | |
49 dict_tmp["picOUT_norm_rev"] = loaded_items[26] | |
50 dict_tmp["picOUT_forw"] = loaded_items[27] | |
51 dict_tmp["picOUT_rev"] = loaded_items[28] | |
52 dict_tmp["lost_perc"] = loaded_items[29] | |
53 dict_tmp["ave_whole_cov"] = loaded_items[30] | |
54 dict_tmp["R1"] = loaded_items[31] | |
55 dict_tmp["R2"] = loaded_items[32] | |
56 dict_tmp["R3"] = loaded_items[33] | |
57 dict_tmp["host"] = loaded_items[34] | |
58 dict_tmp["host_len"] = loaded_items[35] | |
59 dict_tmp["host_whole_coverage"] = loaded_items[36] | |
60 dict_tmp["picMaxPlus_host"] = loaded_items[37] | |
61 dict_tmp["picMaxMinus_host"] = loaded_items[38] | |
62 dict_tmp["surrounding"] = loaded_items[39] | |
63 dict_tmp["drop_cov"] = loaded_items[40] | |
64 dict_tmp["paired"] = loaded_items[41] | |
65 dict_tmp["insert"] = loaded_items[42] | |
66 dict_tmp["phage_hybrid_coverage"] = loaded_items[43] | |
67 dict_tmp["host_hybrid_coverage"] = loaded_items[44] | |
68 dict_tmp["added_paired_whole_coverage"] = loaded_items[45] | |
69 dict_tmp["Mu_like"] = loaded_items[46] | |
70 dict_tmp["test_run"] = loaded_items[47] | |
71 dict_tmp["P_class"] = loaded_items[48] | |
72 dict_tmp["P_type"] = loaded_items[49] | |
73 dict_tmp["P_concat"] = loaded_items[50] | |
74 dict_tmp["idx_refseq_in_list"] = loaded_items[51] | |
75 DR [d][fic_name]=dict_tmp | |
76 f.close() | |
77 | |
78 | |
79 | |
80 | |
81 def genReport(fParms,inDArgs,inRawDArgs,no_match,DR): | |
82 # Test No Match | |
83 if len(no_match) == inDArgs.nbr_virome: | |
84 print("\n\nERROR: No reads match, please check your reference file.") | |
85 exit() | |
86 | |
87 # Report Resume | |
88 multiReport = SummaryReport(inRawDArgs.phagename, DR, no_match) | |
89 multiCohSeq = "" | |
90 multiPhageSeq = "" | |
91 multiWorkflow = "#phagename\tClass\tLeft\tRight\tType\tOrient\tCoverage\tComments\n" | |
92 | |
93 # No Match in workflow | |
94 if fParms.workflow: | |
95 for no_match_contig in no_match: | |
96 multiWorkflow += WorkflowReport(no_match_contig, "-", "-", "-", "-", "-", 0, 1) | |
97 | |
98 for DPC in DR: | |
99 for DC in DR[DPC]: | |
100 # Text report | |
101 if fParms.workflow: # phagename, P_class, P_left, P_right, P_type, P_orient, ave_whole_cov, multi = 0 | |
102 multiWorkflow += WorkflowReport(DC, DR[DPC][DC]["P_class"], DR[DPC][DC]["P_left"], | |
103 DR[DPC][DC]["P_right"], | |
104 DR[DPC][DC]["P_type"], DR[DPC][DC]["P_orient"], | |
105 DR[DPC][DC]["ave_whole_cov"], 1) | |
106 | |
107 # Sequence | |
108 idx_refseq = DR[DPC][DC]["idx_refseq_in_list"] | |
109 refseq = inDArgs.refseq_liste[idx_refseq] | |
110 multiCohSeq += ExportCohesiveSeq(DC, DR[DPC][DC]["ArtcohesiveSeq"], DR[DPC][DC]["P_seqcoh"], fParms.test_run, 1) | |
111 multiPhageSeq += ExportPhageSequence(DC, DR[DPC][DC]["P_left"], DR[DPC][DC]["P_right"], refseq, | |
112 DR[DPC][DC]["P_orient"], DR[DPC][DC]["Redundant"], DR[DPC][DC]["Mu_like"], | |
113 DR[DPC][DC]["P_class"], DR[DPC][DC]["P_seqcoh"], fParms.test_run, 1) | |
114 | |
115 # Report | |
116 draw=0 # TODO VL: ask what is the use of this parameter that is alwayes 0... | |
117 multiReport = CreateReport(DC, DR[DPC][DC]["seed"], DR[DPC][DC]["added_whole_coverage"], draw, | |
118 DR[DPC][DC]["Redundant"], DR[DPC][DC]["P_left"], DR[DPC][DC]["P_right"], | |
119 DR[DPC][DC]["Permuted"], DR[DPC][DC]["P_orient"], | |
120 DR[DPC][DC]["termini_coverage_norm_close"], DR[DPC][DC]["picMaxPlus_norm_close"], | |
121 DR[DPC][DC]["picMaxMinus_norm_close"], DR[DPC][DC]["gen_len"], | |
122 DR[DPC][DC]["tot_reads"], DR[DPC][DC]["P_seqcoh"], DR[DPC][DC]["phage_plus_norm"], | |
123 DR[DPC][DC]["phage_minus_norm"], DR[DPC][DC]["ArtPackmode"], DR[DPC][DC]["termini"], | |
124 DR[DPC][DC]["forward"], DR[DPC][DC]["reverse"], DR[DPC][DC]["ArtOrient"], | |
125 DR[DPC][DC]["ArtcohesiveSeq"], DR[DPC][DC]["termini_coverage_close"], | |
126 DR[DPC][DC]["picMaxPlus_close"], DR[DPC][DC]["picMaxMinus_close"], | |
127 DR[DPC][DC]["picOUT_norm_forw"], DR[DPC][DC]["picOUT_norm_rev"], | |
128 DR[DPC][DC]["picOUT_forw"], DR[DPC][DC]["picOUT_rev"], DR[DPC][DC]["lost_perc"], | |
129 DR[DPC][DC]["ave_whole_cov"], DR[DPC][DC]["R1"], DR[DPC][DC]["R2"], | |
130 DR[DPC][DC]["R3"], DR[DPC][DC]["host"], DR[DPC][DC]["host_len"], | |
131 DR[DPC][DC]["host_whole_coverage"], DR[DPC][DC]["picMaxPlus_host"], | |
132 DR[DPC][DC]["picMaxMinus_host"], DR[DPC][DC]["surrounding"], DR[DPC][DC]["drop_cov"], | |
133 DR[DPC][DC]["paired"], DR[DPC][DC]["insert"], DR[DPC][DC]["phage_hybrid_coverage"], | |
134 DR[DPC][DC]["host_hybrid_coverage"], DR[DPC][DC]["added_paired_whole_coverage"], | |
135 DR[DPC][DC]["Mu_like"], fParms.test_run, DR[DPC][DC]["P_class"], | |
136 DR[DPC][DC]["P_type"], DR[DPC][DC]["P_concat"], 1, multiReport) | |
137 | |
138 # Workflow | |
139 if not fParms.test: | |
140 if fParms.workflow: | |
141 filoutWorkflow = open(inRawDArgs.phagename + "_workflow.txt", "w") | |
142 filoutWorkflow.write(multiWorkflow) | |
143 filoutWorkflow.close() | |
144 | |
145 # Concatene Sequences | |
146 filoutCohSeq = open(inRawDArgs.phagename + "_cohesive-sequence.fasta", "w") | |
147 filoutCohSeq.write(multiCohSeq) | |
148 filoutCohSeq.close() | |
149 | |
150 filoutPhageSeq = open(inRawDArgs.phagename + "_sequence.fasta", "w") | |
151 filoutPhageSeq.write(multiPhageSeq) | |
152 filoutPhageSeq.close() | |
153 | |
154 # Concatene Report | |
155 doc = SimpleDocTemplate("%s_PhageTerm_report.pdf" % inRawDArgs.phagename, pagesize=letter, rightMargin=10, | |
156 leftMargin=10, topMargin=5, bottomMargin=10) | |
157 doc.build(multiReport) |