annotate pima_report.py @ 0:0a558f444c98 draft

Uploaded
author greg
date Fri, 03 Mar 2023 22:06:23 +0000
parents
children 67d0939b56b0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
0a558f444c98 Uploaded
greg
parents:
diff changeset
1 import argparse
0a558f444c98 Uploaded
greg
parents:
diff changeset
2 import os
0a558f444c98 Uploaded
greg
parents:
diff changeset
3 import pandas
0a558f444c98 Uploaded
greg
parents:
diff changeset
4 import pypandoc
0a558f444c98 Uploaded
greg
parents:
diff changeset
5 import re
0a558f444c98 Uploaded
greg
parents:
diff changeset
6 import subprocess
0a558f444c98 Uploaded
greg
parents:
diff changeset
7 import sys
0a558f444c98 Uploaded
greg
parents:
diff changeset
8
0a558f444c98 Uploaded
greg
parents:
diff changeset
9 from Bio import SeqIO
0a558f444c98 Uploaded
greg
parents:
diff changeset
10 from datetime import date
0a558f444c98 Uploaded
greg
parents:
diff changeset
11 from mdutils.mdutils import MdUtils
0a558f444c98 Uploaded
greg
parents:
diff changeset
12
0a558f444c98 Uploaded
greg
parents:
diff changeset
13 CDC_ADVISORY = 'The analysis and report presented here should be treated as preliminary. Please contact the CDC/BDRD with any results regarding _Bacillus anthracis_.'
0a558f444c98 Uploaded
greg
parents:
diff changeset
14
0a558f444c98 Uploaded
greg
parents:
diff changeset
15
0a558f444c98 Uploaded
greg
parents:
diff changeset
16 class PimaReport:
0a558f444c98 Uploaded
greg
parents:
diff changeset
17
0a558f444c98 Uploaded
greg
parents:
diff changeset
18 def __init__(self, analysis_name=None, assembly_fasta_file=None, assembly_name=None, feature_bed_files=None, feature_png_files=None,
0a558f444c98 Uploaded
greg
parents:
diff changeset
19 contig_coverage_file=None, dbkey=None, gzipped=None, illumina_fastq_file=None, mutation_regions_bed_file=None,
0a558f444c98 Uploaded
greg
parents:
diff changeset
20 mutation_regions_tsv_files=None, pima_css=None):
0a558f444c98 Uploaded
greg
parents:
diff changeset
21 self.ofh = open("process_log.txt", "w")
0a558f444c98 Uploaded
greg
parents:
diff changeset
22
0a558f444c98 Uploaded
greg
parents:
diff changeset
23 self.ofh.write("analysis_name: %s\n" % str(analysis_name))
0a558f444c98 Uploaded
greg
parents:
diff changeset
24 self.ofh.write("assembly_fasta_file: %s\n" % str(assembly_fasta_file))
0a558f444c98 Uploaded
greg
parents:
diff changeset
25 self.ofh.write("assembly_name: %s\n" % str(assembly_name))
0a558f444c98 Uploaded
greg
parents:
diff changeset
26 self.ofh.write("feature_bed_files: %s\n" % str(feature_bed_files))
0a558f444c98 Uploaded
greg
parents:
diff changeset
27 self.ofh.write("feature_png_files: %s\n" % str(feature_png_files))
0a558f444c98 Uploaded
greg
parents:
diff changeset
28 self.ofh.write("contig_coverage_file: %s\n" % str(contig_coverage_file))
0a558f444c98 Uploaded
greg
parents:
diff changeset
29 self.ofh.write("dbkey: %s\n" % str(dbkey))
0a558f444c98 Uploaded
greg
parents:
diff changeset
30 self.ofh.write("gzipped: %s\n" % str(gzipped))
0a558f444c98 Uploaded
greg
parents:
diff changeset
31 self.ofh.write("illumina_fastq_file: %s\n" % str(illumina_fastq_file))
0a558f444c98 Uploaded
greg
parents:
diff changeset
32 self.ofh.write("mutation_regions_bed_file: %s\n" % str(mutation_regions_bed_file))
0a558f444c98 Uploaded
greg
parents:
diff changeset
33 self.ofh.write("mutation_regions_tsv_files: %s\n" % str(mutation_regions_tsv_files))
0a558f444c98 Uploaded
greg
parents:
diff changeset
34 self.ofh.write("pima_css: %s\n" % str(pima_css))
0a558f444c98 Uploaded
greg
parents:
diff changeset
35
0a558f444c98 Uploaded
greg
parents:
diff changeset
36 # General
0a558f444c98 Uploaded
greg
parents:
diff changeset
37 self.doc = None
0a558f444c98 Uploaded
greg
parents:
diff changeset
38 self.report_md = 'pima_report.md'
0a558f444c98 Uploaded
greg
parents:
diff changeset
39
0a558f444c98 Uploaded
greg
parents:
diff changeset
40 # Inputs
0a558f444c98 Uploaded
greg
parents:
diff changeset
41 self.analysis_name = analysis_name
0a558f444c98 Uploaded
greg
parents:
diff changeset
42 self.assembly_fasta_file = assembly_fasta_file
0a558f444c98 Uploaded
greg
parents:
diff changeset
43 self.assembly_name = assembly_name
0a558f444c98 Uploaded
greg
parents:
diff changeset
44 self.feature_bed_files = feature_bed_files
0a558f444c98 Uploaded
greg
parents:
diff changeset
45 self.feature_png_files = feature_png_files
0a558f444c98 Uploaded
greg
parents:
diff changeset
46 self.contig_coverage_file = contig_coverage_file
0a558f444c98 Uploaded
greg
parents:
diff changeset
47 self.dbkey = dbkey
0a558f444c98 Uploaded
greg
parents:
diff changeset
48 self.gzipped = gzipped
0a558f444c98 Uploaded
greg
parents:
diff changeset
49 self.illumina_fastq_file = illumina_fastq_file
0a558f444c98 Uploaded
greg
parents:
diff changeset
50 self.mutation_regions_bed_file = mutation_regions_bed_file
0a558f444c98 Uploaded
greg
parents:
diff changeset
51 self.mutation_regions_tsv_files = mutation_regions_tsv_files
0a558f444c98 Uploaded
greg
parents:
diff changeset
52 self.read_type = 'Illumina'
0a558f444c98 Uploaded
greg
parents:
diff changeset
53 self.ont_bases = None
0a558f444c98 Uploaded
greg
parents:
diff changeset
54 self.ont_n50 = None
0a558f444c98 Uploaded
greg
parents:
diff changeset
55 self.ont_read_count = None
0a558f444c98 Uploaded
greg
parents:
diff changeset
56 self.pima_css = pima_css
0a558f444c98 Uploaded
greg
parents:
diff changeset
57
0a558f444c98 Uploaded
greg
parents:
diff changeset
58 # Titles
0a558f444c98 Uploaded
greg
parents:
diff changeset
59 self.alignment_title = 'Comparison with reference'
0a558f444c98 Uploaded
greg
parents:
diff changeset
60 self.alignment_notes_title = 'Alignment notes'
0a558f444c98 Uploaded
greg
parents:
diff changeset
61 self.amr_matrix_title = 'AMR matrix'
0a558f444c98 Uploaded
greg
parents:
diff changeset
62 self.assembly_methods_title = 'Assembly'
0a558f444c98 Uploaded
greg
parents:
diff changeset
63 self.assembly_notes_title = 'Assembly notes'
0a558f444c98 Uploaded
greg
parents:
diff changeset
64 self.basecalling_title = 'Basecalling'
0a558f444c98 Uploaded
greg
parents:
diff changeset
65 self.basecalling_methods_title = 'Basecalling'
0a558f444c98 Uploaded
greg
parents:
diff changeset
66 self.contamination_methods_title = 'Contamination check'
0a558f444c98 Uploaded
greg
parents:
diff changeset
67 self.contig_alignment_title = 'Alignment vs. reference contigs'
0a558f444c98 Uploaded
greg
parents:
diff changeset
68 self.feature_title = 'Features found in the assembly'
0a558f444c98 Uploaded
greg
parents:
diff changeset
69 self.feature_methods_title = 'Feature annotation'
0a558f444c98 Uploaded
greg
parents:
diff changeset
70 self.feature_plot_title = 'Feature annotation plots'
0a558f444c98 Uploaded
greg
parents:
diff changeset
71 self.large_indel_title = 'Large insertions & deletions'
0a558f444c98 Uploaded
greg
parents:
diff changeset
72 self.methods_title = 'Methods'
0a558f444c98 Uploaded
greg
parents:
diff changeset
73 self.mutation_title = 'Mutations found in the sample'
0a558f444c98 Uploaded
greg
parents:
diff changeset
74 self.mutation_methods_title = 'Mutation screening'
0a558f444c98 Uploaded
greg
parents:
diff changeset
75 self.plasmid_methods_title = 'Plasmid annotation'
0a558f444c98 Uploaded
greg
parents:
diff changeset
76 self.reference_methods_title = 'Reference comparison'
0a558f444c98 Uploaded
greg
parents:
diff changeset
77 self.snp_indel_title = 'SNPs and small indels'
0a558f444c98 Uploaded
greg
parents:
diff changeset
78 #self.summary_title = 'Summary'
0a558f444c98 Uploaded
greg
parents:
diff changeset
79 self.summary_title = 'Analysis of %s' % analysis_name
0a558f444c98 Uploaded
greg
parents:
diff changeset
80
0a558f444c98 Uploaded
greg
parents:
diff changeset
81 # Methods
0a558f444c98 Uploaded
greg
parents:
diff changeset
82 self.methods = pandas.Series(dtype='float64')
0a558f444c98 Uploaded
greg
parents:
diff changeset
83 self.methods[self.contamination_methods_title] = pandas.Series(dtype='float64')
0a558f444c98 Uploaded
greg
parents:
diff changeset
84 self.methods[self.assembly_methods_title] = pandas.Series(dtype='float64')
0a558f444c98 Uploaded
greg
parents:
diff changeset
85 self.methods[self.reference_methods_title] = pandas.Series(dtype='float64')
0a558f444c98 Uploaded
greg
parents:
diff changeset
86 self.methods[self.mutation_methods_title] = pandas.Series(dtype='float64')
0a558f444c98 Uploaded
greg
parents:
diff changeset
87 self.methods[self.feature_methods_title] = pandas.Series(dtype='float64')
0a558f444c98 Uploaded
greg
parents:
diff changeset
88 self.methods[self.plasmid_methods_title] = pandas.Series(dtype='float64')
0a558f444c98 Uploaded
greg
parents:
diff changeset
89
0a558f444c98 Uploaded
greg
parents:
diff changeset
90 # Contamination
0a558f444c98 Uploaded
greg
parents:
diff changeset
91 self.kraken_fracs = pandas.Series(dtype=object)
0a558f444c98 Uploaded
greg
parents:
diff changeset
92
0a558f444c98 Uploaded
greg
parents:
diff changeset
93 # Notes
0a558f444c98 Uploaded
greg
parents:
diff changeset
94 self.assembly_notes = pandas.Series(dtype=object)
0a558f444c98 Uploaded
greg
parents:
diff changeset
95 self.alignment_notes = pandas.Series(dtype=object)
0a558f444c98 Uploaded
greg
parents:
diff changeset
96 self.contig_alignment = pandas.Series(dtype=object)
0a558f444c98 Uploaded
greg
parents:
diff changeset
97
0a558f444c98 Uploaded
greg
parents:
diff changeset
98 # Values
0a558f444c98 Uploaded
greg
parents:
diff changeset
99 self.assembly_size = 0
0a558f444c98 Uploaded
greg
parents:
diff changeset
100 self.contig_info = None
0a558f444c98 Uploaded
greg
parents:
diff changeset
101 self.did_flye_ont_fastq = False
0a558f444c98 Uploaded
greg
parents:
diff changeset
102 self.did_medaka_ont_assembly = False
0a558f444c98 Uploaded
greg
parents:
diff changeset
103 self.feature_hits = pandas.Series(dtype='float64')
0a558f444c98 Uploaded
greg
parents:
diff changeset
104 self.illumina_length_mean = 0
0a558f444c98 Uploaded
greg
parents:
diff changeset
105 self.illumina_read_count = 0
0a558f444c98 Uploaded
greg
parents:
diff changeset
106 self.illumina_bases = 0
0a558f444c98 Uploaded
greg
parents:
diff changeset
107 self.mean_coverage = 0
0a558f444c98 Uploaded
greg
parents:
diff changeset
108 self.num_assembly_contigs = 0
0a558f444c98 Uploaded
greg
parents:
diff changeset
109
0a558f444c98 Uploaded
greg
parents:
diff changeset
110 # Actions
0a558f444c98 Uploaded
greg
parents:
diff changeset
111 self.did_guppy_ont_fast5 = False
0a558f444c98 Uploaded
greg
parents:
diff changeset
112 self.did_qcat_ont_fastq = False
0a558f444c98 Uploaded
greg
parents:
diff changeset
113 self.info_illumina_fastq()
0a558f444c98 Uploaded
greg
parents:
diff changeset
114 self.load_contig_info()
0a558f444c98 Uploaded
greg
parents:
diff changeset
115
0a558f444c98 Uploaded
greg
parents:
diff changeset
116 def run_command(self, command):
0a558f444c98 Uploaded
greg
parents:
diff changeset
117 self.ofh.write("\nXXXXXX In run_command, command:\n%s\n\n" % str(command))
0a558f444c98 Uploaded
greg
parents:
diff changeset
118 try:
0a558f444c98 Uploaded
greg
parents:
diff changeset
119 return re.split('\\n', subprocess.check_output(command, shell=True).decode('utf-8'))
0a558f444c98 Uploaded
greg
parents:
diff changeset
120 except Exception:
0a558f444c98 Uploaded
greg
parents:
diff changeset
121 message = 'Command %s failed: exiting...' % command
0a558f444c98 Uploaded
greg
parents:
diff changeset
122 sys.exit(message)
0a558f444c98 Uploaded
greg
parents:
diff changeset
123
0a558f444c98 Uploaded
greg
parents:
diff changeset
124 def format_kmg(self, number, decimals=0):
0a558f444c98 Uploaded
greg
parents:
diff changeset
125 self.ofh.write("\nXXXXXX In format_kmg, number:\n%s\n" % str(number))
0a558f444c98 Uploaded
greg
parents:
diff changeset
126 self.ofh.write("XXXXXX In format_kmg, decimals:\n%s\n\n" % str(decimals))
0a558f444c98 Uploaded
greg
parents:
diff changeset
127 if number == 0:
0a558f444c98 Uploaded
greg
parents:
diff changeset
128 return '0'
0a558f444c98 Uploaded
greg
parents:
diff changeset
129 magnitude_powers = [10**9, 10**6, 10**3, 1]
0a558f444c98 Uploaded
greg
parents:
diff changeset
130 magnitude_units = ['G', 'M', 'K', '']
0a558f444c98 Uploaded
greg
parents:
diff changeset
131 for i in range(len(magnitude_units)):
0a558f444c98 Uploaded
greg
parents:
diff changeset
132 if number >= magnitude_powers[i]:
0a558f444c98 Uploaded
greg
parents:
diff changeset
133 magnitude_power = magnitude_powers[i]
0a558f444c98 Uploaded
greg
parents:
diff changeset
134 magnitude_unit = magnitude_units[i]
0a558f444c98 Uploaded
greg
parents:
diff changeset
135 return ('{:0.' + str(decimals) + 'f}').format(number / magnitude_power) + magnitude_unit
0a558f444c98 Uploaded
greg
parents:
diff changeset
136
0a558f444c98 Uploaded
greg
parents:
diff changeset
137 def load_contig_info(self):
0a558f444c98 Uploaded
greg
parents:
diff changeset
138 self.contig_info = pandas.Series(dtype=object)
0a558f444c98 Uploaded
greg
parents:
diff changeset
139 self.contig_info[self.read_type] = pandas.read_csv(self.contig_coverage_file, header=None, index_col=None, sep='\t').sort_values(1, axis=0, ascending=False)
0a558f444c98 Uploaded
greg
parents:
diff changeset
140 self.contig_info[self.read_type].columns = ['contig', 'size', 'coverage']
0a558f444c98 Uploaded
greg
parents:
diff changeset
141 self.mean_coverage = (self.contig_info[self.read_type].iloc[:, 1] * self.contig_info[self.read_type].iloc[:, 2]).sum() / self.contig_info[self.read_type].iloc[:, 1].sum()
0a558f444c98 Uploaded
greg
parents:
diff changeset
142
0a558f444c98 Uploaded
greg
parents:
diff changeset
143 def load_fasta(self, fasta):
0a558f444c98 Uploaded
greg
parents:
diff changeset
144 sequence = pandas.Series(dtype=object)
0a558f444c98 Uploaded
greg
parents:
diff changeset
145 for contig in SeqIO.parse(fasta, 'fasta'):
0a558f444c98 Uploaded
greg
parents:
diff changeset
146 sequence[contig.id] = contig
0a558f444c98 Uploaded
greg
parents:
diff changeset
147 return sequence
0a558f444c98 Uploaded
greg
parents:
diff changeset
148
0a558f444c98 Uploaded
greg
parents:
diff changeset
149 def load_assembly(self):
0a558f444c98 Uploaded
greg
parents:
diff changeset
150 self.assembly = self.load_fasta(self.assembly_fasta_file)
0a558f444c98 Uploaded
greg
parents:
diff changeset
151 self.num_assembly_contigs = len(self.assembly)
0a558f444c98 Uploaded
greg
parents:
diff changeset
152 for i in self.assembly:
0a558f444c98 Uploaded
greg
parents:
diff changeset
153 self.assembly_size += len(i.seq)
0a558f444c98 Uploaded
greg
parents:
diff changeset
154 self.assembly_size = self.format_kmg(self.assembly_size, decimals=1)
0a558f444c98 Uploaded
greg
parents:
diff changeset
155
0a558f444c98 Uploaded
greg
parents:
diff changeset
156 def info_illumina_fastq(self):
0a558f444c98 Uploaded
greg
parents:
diff changeset
157 self.ofh.write("\nXXXXXX In info_illumina_fastq\n\n")
0a558f444c98 Uploaded
greg
parents:
diff changeset
158 if self.gzipped:
0a558f444c98 Uploaded
greg
parents:
diff changeset
159 opener = 'gunzip -c'
0a558f444c98 Uploaded
greg
parents:
diff changeset
160 else:
0a558f444c98 Uploaded
greg
parents:
diff changeset
161 opener = 'cat'
0a558f444c98 Uploaded
greg
parents:
diff changeset
162 command = ' '.join([opener,
0a558f444c98 Uploaded
greg
parents:
diff changeset
163 self.illumina_fastq_file,
0a558f444c98 Uploaded
greg
parents:
diff changeset
164 '| awk \'{getline;s += length($1);getline;getline;}END{print s/(NR/4)"\t"(NR/4)"\t"s}\''])
0a558f444c98 Uploaded
greg
parents:
diff changeset
165 output = self.run_command(command)
0a558f444c98 Uploaded
greg
parents:
diff changeset
166 self.ofh.write("output:\n%s\n" % str(output))
0a558f444c98 Uploaded
greg
parents:
diff changeset
167 self.ofh.write("re.split('\\t', self.run_command(command)[0]:\n%s\n" % str(re.split('\\t', self.run_command(command)[0])))
0a558f444c98 Uploaded
greg
parents:
diff changeset
168 values = []
0a558f444c98 Uploaded
greg
parents:
diff changeset
169 for i in re.split('\\t', self.run_command(command)[0]):
0a558f444c98 Uploaded
greg
parents:
diff changeset
170 if i == '':
0a558f444c98 Uploaded
greg
parents:
diff changeset
171 values.append(float('nan'))
0a558f444c98 Uploaded
greg
parents:
diff changeset
172 else:
0a558f444c98 Uploaded
greg
parents:
diff changeset
173 values.append(float(i))
0a558f444c98 Uploaded
greg
parents:
diff changeset
174 self.ofh.write("values:\n%s\n" % str(values))
0a558f444c98 Uploaded
greg
parents:
diff changeset
175 self.ofh.write("values[0]:\n%s\n" % str(values[0]))
0a558f444c98 Uploaded
greg
parents:
diff changeset
176 self.illumina_length_mean += values[0]
0a558f444c98 Uploaded
greg
parents:
diff changeset
177 self.ofh.write("values[1]:\n%s\n" % str(values[1]))
0a558f444c98 Uploaded
greg
parents:
diff changeset
178 self.illumina_read_count += int(values[1])
0a558f444c98 Uploaded
greg
parents:
diff changeset
179 self.ofh.write("values[2]:\n%s\n" % str(values[2]))
0a558f444c98 Uploaded
greg
parents:
diff changeset
180 self.illumina_bases += int(values[2])
0a558f444c98 Uploaded
greg
parents:
diff changeset
181 # The original PIMA code inserts self.illumina_fastq into
0a558f444c98 Uploaded
greg
parents:
diff changeset
182 # a list for no apparent reason. We don't do that here.
0a558f444c98 Uploaded
greg
parents:
diff changeset
183 # self.illumina_length_mean /= len(self.illumina_fastq)
0a558f444c98 Uploaded
greg
parents:
diff changeset
184 self.illumina_length_mean /= 1
0a558f444c98 Uploaded
greg
parents:
diff changeset
185 self.illumina_bases = self.format_kmg(self.illumina_bases, decimals=1)
0a558f444c98 Uploaded
greg
parents:
diff changeset
186
0a558f444c98 Uploaded
greg
parents:
diff changeset
187 def start_doc(self):
0a558f444c98 Uploaded
greg
parents:
diff changeset
188 #header_text = 'Analysis of %s' % self.analysis_name
0a558f444c98 Uploaded
greg
parents:
diff changeset
189 self.doc = MdUtils(file_name=self.report_md, title='')
0a558f444c98 Uploaded
greg
parents:
diff changeset
190
0a558f444c98 Uploaded
greg
parents:
diff changeset
191 def add_run_information(self):
0a558f444c98 Uploaded
greg
parents:
diff changeset
192 self.ofh.write("\nXXXXXX In add_run_information\n\n")
0a558f444c98 Uploaded
greg
parents:
diff changeset
193 self.doc.new_line()
0a558f444c98 Uploaded
greg
parents:
diff changeset
194 self.doc.new_header(1, 'Run information')
0a558f444c98 Uploaded
greg
parents:
diff changeset
195 # Tables in md.utils are implemented as a wrapping function.
0a558f444c98 Uploaded
greg
parents:
diff changeset
196 Table_list = [
0a558f444c98 Uploaded
greg
parents:
diff changeset
197 "Category",
0a558f444c98 Uploaded
greg
parents:
diff changeset
198 "Information",
0a558f444c98 Uploaded
greg
parents:
diff changeset
199 "Date",
0a558f444c98 Uploaded
greg
parents:
diff changeset
200 date.today(),
0a558f444c98 Uploaded
greg
parents:
diff changeset
201 "ONT FAST5",
0a558f444c98 Uploaded
greg
parents:
diff changeset
202 "N/A",
0a558f444c98 Uploaded
greg
parents:
diff changeset
203 "ONT FASTQ",
0a558f444c98 Uploaded
greg
parents:
diff changeset
204 "N/A",
0a558f444c98 Uploaded
greg
parents:
diff changeset
205 "Illumina FASTQ",
0a558f444c98 Uploaded
greg
parents:
diff changeset
206 self.wordwrap_markdown(self.analysis_name),
0a558f444c98 Uploaded
greg
parents:
diff changeset
207 "Assembly",
0a558f444c98 Uploaded
greg
parents:
diff changeset
208 self.wordwrap_markdown(self.assembly_name),
0a558f444c98 Uploaded
greg
parents:
diff changeset
209 "Reference",
0a558f444c98 Uploaded
greg
parents:
diff changeset
210 self.wordwrap_markdown(self.dbkey),
0a558f444c98 Uploaded
greg
parents:
diff changeset
211 ]
0a558f444c98 Uploaded
greg
parents:
diff changeset
212 self.doc.new_table(columns=2, rows=7, text=Table_list, text_align='left')
0a558f444c98 Uploaded
greg
parents:
diff changeset
213 self.doc.new_line()
0a558f444c98 Uploaded
greg
parents:
diff changeset
214 self.doc.new_line()
0a558f444c98 Uploaded
greg
parents:
diff changeset
215
0a558f444c98 Uploaded
greg
parents:
diff changeset
216 def add_ont_library_information(self):
0a558f444c98 Uploaded
greg
parents:
diff changeset
217 self.ofh.write("\nXXXXXX In add_ont_library_information\n\n")
0a558f444c98 Uploaded
greg
parents:
diff changeset
218 if self.ont_n50 is None:
0a558f444c98 Uploaded
greg
parents:
diff changeset
219 return
0a558f444c98 Uploaded
greg
parents:
diff changeset
220 self.doc.new_line()
0a558f444c98 Uploaded
greg
parents:
diff changeset
221 self.doc.new_header(2, 'ONT library statistics')
0a558f444c98 Uploaded
greg
parents:
diff changeset
222 Table_List = [
0a558f444c98 Uploaded
greg
parents:
diff changeset
223 "Category",
0a558f444c98 Uploaded
greg
parents:
diff changeset
224 "Quantity",
0a558f444c98 Uploaded
greg
parents:
diff changeset
225 "ONT N50",
0a558f444c98 Uploaded
greg
parents:
diff changeset
226 '{:,}'.format(self.ont_n50),
0a558f444c98 Uploaded
greg
parents:
diff changeset
227 "ONT reads",
0a558f444c98 Uploaded
greg
parents:
diff changeset
228 '{:,}'.format(self.ont_read_count),
0a558f444c98 Uploaded
greg
parents:
diff changeset
229 "ONT bases",
0a558f444c98 Uploaded
greg
parents:
diff changeset
230 '{:s}'.format(self.ont_bases),
0a558f444c98 Uploaded
greg
parents:
diff changeset
231 "Illumina FASTQ",
0a558f444c98 Uploaded
greg
parents:
diff changeset
232 self.wordwrap_markdown(self.illumina_fastq_file),
0a558f444c98 Uploaded
greg
parents:
diff changeset
233 "Assembly",
0a558f444c98 Uploaded
greg
parents:
diff changeset
234 self.wordwrap_markdown(self.assembly_name),
0a558f444c98 Uploaded
greg
parents:
diff changeset
235 "Reference",
0a558f444c98 Uploaded
greg
parents:
diff changeset
236 self.wordwrap_markdown(self.dbkey),
0a558f444c98 Uploaded
greg
parents:
diff changeset
237 ]
0a558f444c98 Uploaded
greg
parents:
diff changeset
238 self.doc.new_table(columns=2, rows=7, text=Table_List, text_align='left')
0a558f444c98 Uploaded
greg
parents:
diff changeset
239 self.doc.new_line()
0a558f444c98 Uploaded
greg
parents:
diff changeset
240
0a558f444c98 Uploaded
greg
parents:
diff changeset
241 def add_illumina_library_information(self):
0a558f444c98 Uploaded
greg
parents:
diff changeset
242 self.ofh.write("\nXXXXXX In add_illumina_library_information\n\n")
0a558f444c98 Uploaded
greg
parents:
diff changeset
243 if self.illumina_length_mean is None:
0a558f444c98 Uploaded
greg
parents:
diff changeset
244 return
0a558f444c98 Uploaded
greg
parents:
diff changeset
245 self.doc.new_line()
0a558f444c98 Uploaded
greg
parents:
diff changeset
246 self.doc.new_header(2, 'Illumina library statistics')
0a558f444c98 Uploaded
greg
parents:
diff changeset
247 Table_List = [
0a558f444c98 Uploaded
greg
parents:
diff changeset
248 "Illumina Info.",
0a558f444c98 Uploaded
greg
parents:
diff changeset
249 "Quantity",
0a558f444c98 Uploaded
greg
parents:
diff changeset
250 'Illumina mean length',
0a558f444c98 Uploaded
greg
parents:
diff changeset
251 '{:.1f}'.format(self.illumina_length_mean),
0a558f444c98 Uploaded
greg
parents:
diff changeset
252 'Illumina reads',
0a558f444c98 Uploaded
greg
parents:
diff changeset
253 '{:,}'.format(self.illumina_read_count),
0a558f444c98 Uploaded
greg
parents:
diff changeset
254 'Illumina bases',
0a558f444c98 Uploaded
greg
parents:
diff changeset
255 '{:s}'.format(self.illumina_bases)
0a558f444c98 Uploaded
greg
parents:
diff changeset
256 ]
0a558f444c98 Uploaded
greg
parents:
diff changeset
257 self.doc.new_table(columns=2, rows=4, text=Table_List, text_align='left')
0a558f444c98 Uploaded
greg
parents:
diff changeset
258
0a558f444c98 Uploaded
greg
parents:
diff changeset
259 def add_assembly_information(self):
0a558f444c98 Uploaded
greg
parents:
diff changeset
260 self.ofh.write("\nXXXXXX In add_assembly_information\n\n")
0a558f444c98 Uploaded
greg
parents:
diff changeset
261 if self.assembly_fasta_file is None:
0a558f444c98 Uploaded
greg
parents:
diff changeset
262 return
0a558f444c98 Uploaded
greg
parents:
diff changeset
263 self.load_assembly()
0a558f444c98 Uploaded
greg
parents:
diff changeset
264 self.doc.new_line()
0a558f444c98 Uploaded
greg
parents:
diff changeset
265 self.doc.new_header(2, 'Assembly statistics')
0a558f444c98 Uploaded
greg
parents:
diff changeset
266 Table_List = [
0a558f444c98 Uploaded
greg
parents:
diff changeset
267 "Category",
0a558f444c98 Uploaded
greg
parents:
diff changeset
268 "Information",
0a558f444c98 Uploaded
greg
parents:
diff changeset
269 "Contigs",
0a558f444c98 Uploaded
greg
parents:
diff changeset
270 str(self.num_assembly_contigs),
0a558f444c98 Uploaded
greg
parents:
diff changeset
271 "Assembly size",
0a558f444c98 Uploaded
greg
parents:
diff changeset
272 str(self.assembly_size),
0a558f444c98 Uploaded
greg
parents:
diff changeset
273 ]
0a558f444c98 Uploaded
greg
parents:
diff changeset
274 self.doc.new_table(columns=2, rows=3, text=Table_List, text_align='left')
0a558f444c98 Uploaded
greg
parents:
diff changeset
275
0a558f444c98 Uploaded
greg
parents:
diff changeset
276 def info_ont_fastq(self, fastq_file):
0a558f444c98 Uploaded
greg
parents:
diff changeset
277 self.ofh.write("\nXXXXXX In info_ont_fastq, fastq_file:\n%s\n\n" % str(fastq_file))
0a558f444c98 Uploaded
greg
parents:
diff changeset
278 opener = 'cat'
0a558f444c98 Uploaded
greg
parents:
diff changeset
279 if self.gzipped:
0a558f444c98 Uploaded
greg
parents:
diff changeset
280 opener = 'gunzip -c'
0a558f444c98 Uploaded
greg
parents:
diff changeset
281 else:
0a558f444c98 Uploaded
greg
parents:
diff changeset
282 opener = 'cat'
0a558f444c98 Uploaded
greg
parents:
diff changeset
283 command = ' '.join([opener,
0a558f444c98 Uploaded
greg
parents:
diff changeset
284 fastq_file,
0a558f444c98 Uploaded
greg
parents:
diff changeset
285 '| awk \'{getline;print length($0);s += length($1);getline;getline;}END{print "+"s}\'',
0a558f444c98 Uploaded
greg
parents:
diff changeset
286 '| sort -gr',
0a558f444c98 Uploaded
greg
parents:
diff changeset
287 '| awk \'BEGIN{bp = 0;f = 0}',
0a558f444c98 Uploaded
greg
parents:
diff changeset
288 '{if(NR == 1){sub(/+/, "", $1);s=$1}else{bp += $1;if(bp > s / 2 && f == 0){n50 = $1;f = 1}}}',
0a558f444c98 Uploaded
greg
parents:
diff changeset
289 'END{printf "%d\\t%d\\t%d\\n", n50, (NR - 1), s;exit}\''])
0a558f444c98 Uploaded
greg
parents:
diff changeset
290 result = list(re.split('\\t', self.run_command(command)[0]))
0a558f444c98 Uploaded
greg
parents:
diff changeset
291 if result[1] == '0':
0a558f444c98 Uploaded
greg
parents:
diff changeset
292 self.error_out('No ONT reads found')
0a558f444c98 Uploaded
greg
parents:
diff changeset
293 ont_n50, ont_read_count, ont_raw_bases = [int(i) for i in result]
0a558f444c98 Uploaded
greg
parents:
diff changeset
294
0a558f444c98 Uploaded
greg
parents:
diff changeset
295 command = ' '.join([opener,
0a558f444c98 Uploaded
greg
parents:
diff changeset
296 fastq_file,
0a558f444c98 Uploaded
greg
parents:
diff changeset
297 '| awk \'{getline;print length($0);getline;getline;}\''])
0a558f444c98 Uploaded
greg
parents:
diff changeset
298 result = self.run_command(command)
0a558f444c98 Uploaded
greg
parents:
diff changeset
299 result = list(filter(lambda x: x != '', result))
0a558f444c98 Uploaded
greg
parents:
diff changeset
300 ont_read_lengths = [int(i) for i in result]
0a558f444c98 Uploaded
greg
parents:
diff changeset
301
0a558f444c98 Uploaded
greg
parents:
diff changeset
302 return ([ont_n50, ont_read_count, ont_raw_bases, ont_read_lengths])
0a558f444c98 Uploaded
greg
parents:
diff changeset
303
0a558f444c98 Uploaded
greg
parents:
diff changeset
304 def wordwrap_markdown(self, string):
0a558f444c98 Uploaded
greg
parents:
diff changeset
305 if string:
0a558f444c98 Uploaded
greg
parents:
diff changeset
306 if len(string) < 35:
0a558f444c98 Uploaded
greg
parents:
diff changeset
307 return string
0a558f444c98 Uploaded
greg
parents:
diff changeset
308 else:
0a558f444c98 Uploaded
greg
parents:
diff changeset
309 if '/' in string:
0a558f444c98 Uploaded
greg
parents:
diff changeset
310 adjust = string.split('/')
0a558f444c98 Uploaded
greg
parents:
diff changeset
311 out = ''
0a558f444c98 Uploaded
greg
parents:
diff changeset
312 max = 35
0a558f444c98 Uploaded
greg
parents:
diff changeset
313 for i in adjust:
0a558f444c98 Uploaded
greg
parents:
diff changeset
314 out = out + '/' + i
0a558f444c98 Uploaded
greg
parents:
diff changeset
315 if len(out) > max:
0a558f444c98 Uploaded
greg
parents:
diff changeset
316 out += '<br>'
0a558f444c98 Uploaded
greg
parents:
diff changeset
317 max += 35
0a558f444c98 Uploaded
greg
parents:
diff changeset
318 return out
0a558f444c98 Uploaded
greg
parents:
diff changeset
319 else:
0a558f444c98 Uploaded
greg
parents:
diff changeset
320 out = [string[i:i + 35] for i in range(0, len(string), 50)]
0a558f444c98 Uploaded
greg
parents:
diff changeset
321 return '<br>'.join(out)
0a558f444c98 Uploaded
greg
parents:
diff changeset
322 else:
0a558f444c98 Uploaded
greg
parents:
diff changeset
323 return string
0a558f444c98 Uploaded
greg
parents:
diff changeset
324
0a558f444c98 Uploaded
greg
parents:
diff changeset
325 def add_contig_info(self):
0a558f444c98 Uploaded
greg
parents:
diff changeset
326 self.ofh.write("\nXXXXXX In add_contig_info\n\n")
0a558f444c98 Uploaded
greg
parents:
diff changeset
327 if self.contig_info is None:
0a558f444c98 Uploaded
greg
parents:
diff changeset
328 return
0a558f444c98 Uploaded
greg
parents:
diff changeset
329 for method in ['ONT', 'Illumina']:
0a558f444c98 Uploaded
greg
parents:
diff changeset
330 if method not in self.contig_info.index:
0a558f444c98 Uploaded
greg
parents:
diff changeset
331 continue
0a558f444c98 Uploaded
greg
parents:
diff changeset
332 self.doc.new_line()
0a558f444c98 Uploaded
greg
parents:
diff changeset
333 self.doc.new_header(2, 'Assembly coverage by ' + method)
0a558f444c98 Uploaded
greg
parents:
diff changeset
334 Table_List = ["Contig", "Length (bp)", "Coverage (X)"]
0a558f444c98 Uploaded
greg
parents:
diff changeset
335 formatted = self.contig_info[method].copy()
0a558f444c98 Uploaded
greg
parents:
diff changeset
336 formatted.iloc[:, 1] = formatted.iloc[:, 1].apply(lambda x: '{:,}'.format(x))
0a558f444c98 Uploaded
greg
parents:
diff changeset
337 for i in range(self.contig_info[method].shape[0]):
0a558f444c98 Uploaded
greg
parents:
diff changeset
338 Table_List = Table_List + formatted.iloc[i, :].values.tolist()
0a558f444c98 Uploaded
greg
parents:
diff changeset
339 row_count = int(len(Table_List) / 3)
0a558f444c98 Uploaded
greg
parents:
diff changeset
340 self.doc.new_table(columns=3, rows=row_count, text=Table_List, text_align='left')
0a558f444c98 Uploaded
greg
parents:
diff changeset
341
0a558f444c98 Uploaded
greg
parents:
diff changeset
342 def add_assembly_notes(self):
0a558f444c98 Uploaded
greg
parents:
diff changeset
343 self.ofh.write("\nXXXXXX In add_assembly_notes\n\n")
0a558f444c98 Uploaded
greg
parents:
diff changeset
344 if len(self.assembly_notes) == 0:
0a558f444c98 Uploaded
greg
parents:
diff changeset
345 return
0a558f444c98 Uploaded
greg
parents:
diff changeset
346 self.doc.new_line()
0a558f444c98 Uploaded
greg
parents:
diff changeset
347 self.doc.new_line('<div style="page-break-after: always;"></div>')
0a558f444c98 Uploaded
greg
parents:
diff changeset
348 self.doc.new_line()
0a558f444c98 Uploaded
greg
parents:
diff changeset
349 self.doc.new_header(2, self.assembly_notes_title)
0a558f444c98 Uploaded
greg
parents:
diff changeset
350 #for note in self.analysis.assembly_notes:
0a558f444c98 Uploaded
greg
parents:
diff changeset
351 # self.doc.new_line(note)
0a558f444c98 Uploaded
greg
parents:
diff changeset
352
0a558f444c98 Uploaded
greg
parents:
diff changeset
353 def add_contamination(self):
0a558f444c98 Uploaded
greg
parents:
diff changeset
354 self.ofh.write("\nXXXXXX In add_contamination\n\n")
0a558f444c98 Uploaded
greg
parents:
diff changeset
355 if len(self.kraken_fracs) == 0:
0a558f444c98 Uploaded
greg
parents:
diff changeset
356 return
0a558f444c98 Uploaded
greg
parents:
diff changeset
357 self.doc.new_line()
0a558f444c98 Uploaded
greg
parents:
diff changeset
358 self.doc.new_header(2, 'Contamination check')
0a558f444c98 Uploaded
greg
parents:
diff changeset
359 for read_type, kraken_fracs in self.kraken_fracs.iteritems():
0a558f444c98 Uploaded
greg
parents:
diff changeset
360 self.doc.new_line(read_type + ' classifications')
0a558f444c98 Uploaded
greg
parents:
diff changeset
361 self.doc.new_line()
0a558f444c98 Uploaded
greg
parents:
diff changeset
362 Table_List = ["Percent of Reads", "Reads", "Level", "Label"]
0a558f444c98 Uploaded
greg
parents:
diff changeset
363 for index, row in kraken_fracs.iterrows():
0a558f444c98 Uploaded
greg
parents:
diff changeset
364 Table_List = Table_List + row.tolist()
0a558f444c98 Uploaded
greg
parents:
diff changeset
365 row_count = int(len(Table_List) / 4)
0a558f444c98 Uploaded
greg
parents:
diff changeset
366 self.doc.new_table(columns=4, rows=row_count, text=Table_List, text_align='left')
0a558f444c98 Uploaded
greg
parents:
diff changeset
367 if self.contamination_methods_title not in self.methods:
0a558f444c98 Uploaded
greg
parents:
diff changeset
368 self.methods[self.contamination_methods_title] = ''
0a558f444c98 Uploaded
greg
parents:
diff changeset
369 method = 'Kraken2 was used to assign the raw reads into taxa.'
0a558f444c98 Uploaded
greg
parents:
diff changeset
370 self.methods[self.contamination_methods_title] = self.methods[self.contamination_methods_title].append(pandas.Series(method))
0a558f444c98 Uploaded
greg
parents:
diff changeset
371
0a558f444c98 Uploaded
greg
parents:
diff changeset
372 def add_alignment(self):
0a558f444c98 Uploaded
greg
parents:
diff changeset
373 self.ofh.write("\nXXXXXX In add_alignment\n\n")
0a558f444c98 Uploaded
greg
parents:
diff changeset
374 # TODO: implement the draw_circos function for this.
0a558f444c98 Uploaded
greg
parents:
diff changeset
375 if len(self.contig_alignment) > 0:
0a558f444c98 Uploaded
greg
parents:
diff changeset
376 alignments = self.contig_alignment
0a558f444c98 Uploaded
greg
parents:
diff changeset
377 else:
0a558f444c98 Uploaded
greg
parents:
diff changeset
378 return
0a558f444c98 Uploaded
greg
parents:
diff changeset
379 self.doc.new_line()
0a558f444c98 Uploaded
greg
parents:
diff changeset
380 self.doc.new_header(level=2, title=self.alignment_title)
0a558f444c98 Uploaded
greg
parents:
diff changeset
381 self.doc.new_line()
0a558f444c98 Uploaded
greg
parents:
diff changeset
382 self.doc.new_header(level=3, title=self.snp_indel_title)
0a558f444c98 Uploaded
greg
parents:
diff changeset
383 Table_1 = [
0a558f444c98 Uploaded
greg
parents:
diff changeset
384 "Category",
0a558f444c98 Uploaded
greg
parents:
diff changeset
385 "Quantity",
0a558f444c98 Uploaded
greg
parents:
diff changeset
386 'SNPs',
0a558f444c98 Uploaded
greg
parents:
diff changeset
387 #'{:,}'.format(self.analysis.quast_mismatches),
0a558f444c98 Uploaded
greg
parents:
diff changeset
388 'NA'
0a558f444c98 Uploaded
greg
parents:
diff changeset
389 'Small indels',
0a558f444c98 Uploaded
greg
parents:
diff changeset
390 #'{:,}'.format(self.analysis.quast_indels)
0a558f444c98 Uploaded
greg
parents:
diff changeset
391 'NA'
0a558f444c98 Uploaded
greg
parents:
diff changeset
392 ]
0a558f444c98 Uploaded
greg
parents:
diff changeset
393 self.doc.new_table(columns=2, rows=3, text=Table_1, text_align='left')
0a558f444c98 Uploaded
greg
parents:
diff changeset
394 self.doc.new_line('<div style="page-break-after: always;"></div>')
0a558f444c98 Uploaded
greg
parents:
diff changeset
395 self.doc.new_line()
0a558f444c98 Uploaded
greg
parents:
diff changeset
396 if len(self.alignment_notes) > 0:
0a558f444c98 Uploaded
greg
parents:
diff changeset
397 self.doc.new_header(level=3, title=self.alignment_notes_title)
0a558f444c98 Uploaded
greg
parents:
diff changeset
398 for note in self.alignment_notes:
0a558f444c98 Uploaded
greg
parents:
diff changeset
399 self.doc.new_line(note)
0a558f444c98 Uploaded
greg
parents:
diff changeset
400 for contig in alignments.index.tolist():
0a558f444c98 Uploaded
greg
parents:
diff changeset
401 contig_title = 'Alignment to %s' % contig
0a558f444c98 Uploaded
greg
parents:
diff changeset
402 image_png = alignments[contig]
0a558f444c98 Uploaded
greg
parents:
diff changeset
403 self.doc.new_line()
0a558f444c98 Uploaded
greg
parents:
diff changeset
404 self.doc.new_header(level=3, title=contig_title)
0a558f444c98 Uploaded
greg
parents:
diff changeset
405 self.doc.new_line(self.doc.new_inline_image(text='contig_title', path=os.path.abspath(image_png)))
0a558f444c98 Uploaded
greg
parents:
diff changeset
406 self.doc.new_line('<div style="page-break-after: always;"></div>')
0a558f444c98 Uploaded
greg
parents:
diff changeset
407 self.doc.new_line()
0a558f444c98 Uploaded
greg
parents:
diff changeset
408 method = 'The genome assembly was aligned against the reference sequencing using dnadiff.'
0a558f444c98 Uploaded
greg
parents:
diff changeset
409 self.methods[self.reference_methods_title] = self.methods[self.reference_methods_title].append(pandas.Series(method))
0a558f444c98 Uploaded
greg
parents:
diff changeset
410
0a558f444c98 Uploaded
greg
parents:
diff changeset
411 def add_features(self):
0a558f444c98 Uploaded
greg
parents:
diff changeset
412 self.ofh.write("\nXXXXXX In add_features\n\n")
0a558f444c98 Uploaded
greg
parents:
diff changeset
413 if len(self.feature_bed_files) == 0:
0a558f444c98 Uploaded
greg
parents:
diff changeset
414 return
0a558f444c98 Uploaded
greg
parents:
diff changeset
415 for bbf in self.feature_bed_files:
0a558f444c98 Uploaded
greg
parents:
diff changeset
416 if os.path.getsize(bbf) > 0:
0a558f444c98 Uploaded
greg
parents:
diff changeset
417 best = pandas.read_csv(filepath_or_buffer=bbf, sep='\t', header=None)
0a558f444c98 Uploaded
greg
parents:
diff changeset
418 self.feature_hits[os.path.basename(bbf)] = best
0a558f444c98 Uploaded
greg
parents:
diff changeset
419 if len(self.feature_hits) == 0:
0a558f444c98 Uploaded
greg
parents:
diff changeset
420 return
0a558f444c98 Uploaded
greg
parents:
diff changeset
421 self.ofh.write("self.feature_hits: %s\n" % str(self.feature_hits))
0a558f444c98 Uploaded
greg
parents:
diff changeset
422 self.doc.new_line()
0a558f444c98 Uploaded
greg
parents:
diff changeset
423 self.doc.new_header(level=2, title=self.feature_title)
0a558f444c98 Uploaded
greg
parents:
diff changeset
424 for feature_name in self.feature_hits.index.tolist():
0a558f444c98 Uploaded
greg
parents:
diff changeset
425 self.ofh.write("feature_name: %s\n" % str(feature_name))
0a558f444c98 Uploaded
greg
parents:
diff changeset
426 features = self.feature_hits[feature_name].copy()
0a558f444c98 Uploaded
greg
parents:
diff changeset
427 self.ofh.write("features: %s\n" % str(features))
0a558f444c98 Uploaded
greg
parents:
diff changeset
428 if features.shape[0] == 0:
0a558f444c98 Uploaded
greg
parents:
diff changeset
429 continue
0a558f444c98 Uploaded
greg
parents:
diff changeset
430 features.iloc[:, 1] = features.iloc[:, 1].apply(lambda x: '{:,}'.format(x))
0a558f444c98 Uploaded
greg
parents:
diff changeset
431 features.iloc[:, 2] = features.iloc[:, 2].apply(lambda x: '{:,}'.format(x))
0a558f444c98 Uploaded
greg
parents:
diff changeset
432 self.doc.new_line()
0a558f444c98 Uploaded
greg
parents:
diff changeset
433 self.doc.new_header(level=3, title=feature_name)
0a558f444c98 Uploaded
greg
parents:
diff changeset
434 if (features.shape[0] == 0):
0a558f444c98 Uploaded
greg
parents:
diff changeset
435 continue
0a558f444c98 Uploaded
greg
parents:
diff changeset
436 for contig in pandas.unique(features.iloc[:, 0]):
0a558f444c98 Uploaded
greg
parents:
diff changeset
437 self.ofh.write("contig: %s\n" % str(contig))
0a558f444c98 Uploaded
greg
parents:
diff changeset
438 self.doc.new_line(contig)
0a558f444c98 Uploaded
greg
parents:
diff changeset
439 contig_features = features.loc[(features.iloc[:, 0] == contig), :]
0a558f444c98 Uploaded
greg
parents:
diff changeset
440 self.ofh.write("contig_features: %s\n" % str(contig_features))
0a558f444c98 Uploaded
greg
parents:
diff changeset
441 Table_List = ['Start', 'Stop', 'Feature', 'Identity (%)', 'Strand']
0a558f444c98 Uploaded
greg
parents:
diff changeset
442 for i in range(contig_features.shape[0]):
0a558f444c98 Uploaded
greg
parents:
diff changeset
443 self.ofh.write("i: %s\n" % str(i))
0a558f444c98 Uploaded
greg
parents:
diff changeset
444 feature = contig_features.iloc[i, :].copy(deep=True)
0a558f444c98 Uploaded
greg
parents:
diff changeset
445 self.ofh.write("feature: %s\n" % str(feature))
0a558f444c98 Uploaded
greg
parents:
diff changeset
446 feature[4] = '{:.3f}'.format(feature[4])
0a558f444c98 Uploaded
greg
parents:
diff changeset
447 Table_List = Table_List + feature[1:].values.tolist()
0a558f444c98 Uploaded
greg
parents:
diff changeset
448 self.ofh.write("Table_List: %s\n" % str(Table_List))
0a558f444c98 Uploaded
greg
parents:
diff changeset
449 row_count = int(len(Table_List) / 5)
0a558f444c98 Uploaded
greg
parents:
diff changeset
450 self.ofh.write("row_count: %s\n" % str(row_count))
0a558f444c98 Uploaded
greg
parents:
diff changeset
451 self.doc.new_line()
0a558f444c98 Uploaded
greg
parents:
diff changeset
452 self.doc.new_table(columns=7, rows=row_count, text=Table_List, text_align='left')
0a558f444c98 Uploaded
greg
parents:
diff changeset
453 blastn_version = 'The genome assembly was queried for features using blastn.'
0a558f444c98 Uploaded
greg
parents:
diff changeset
454 bedtools_version = 'Feature hits were clustered using bedtools and the highest scoring hit for each cluster was reported.'
0a558f444c98 Uploaded
greg
parents:
diff changeset
455 method = '%s %s' % (blastn_version, bedtools_version)
0a558f444c98 Uploaded
greg
parents:
diff changeset
456 self.methods[self.feature_methods_title] = self.methods[self.feature_methods_title].append(pandas.Series(method))
0a558f444c98 Uploaded
greg
parents:
diff changeset
457
0a558f444c98 Uploaded
greg
parents:
diff changeset
458 def add_feature_plots(self):
0a558f444c98 Uploaded
greg
parents:
diff changeset
459 self.ofh.write("\nXXXXXX In add_feature_plots\n\n")
0a558f444c98 Uploaded
greg
parents:
diff changeset
460 if len(self.feature_png_files) == 0:
0a558f444c98 Uploaded
greg
parents:
diff changeset
461 return
0a558f444c98 Uploaded
greg
parents:
diff changeset
462 self.doc.new_line()
0a558f444c98 Uploaded
greg
parents:
diff changeset
463 self.doc.new_header(level=2, title='Feature Plots')
0a558f444c98 Uploaded
greg
parents:
diff changeset
464 self.doc.new_paragraph('Only contigs with features are shown')
0a558f444c98 Uploaded
greg
parents:
diff changeset
465 for feature_png_file in self.feature_png_files:
0a558f444c98 Uploaded
greg
parents:
diff changeset
466 self.doc.new_line(self.doc.new_inline_image(text='Analysis', path=os.path.abspath(feature_png_file)))
0a558f444c98 Uploaded
greg
parents:
diff changeset
467
0a558f444c98 Uploaded
greg
parents:
diff changeset
468 def add_mutations(self):
0a558f444c98 Uploaded
greg
parents:
diff changeset
469 self.ofh.write("\nXXXXXX In add_mutations\n\n")
0a558f444c98 Uploaded
greg
parents:
diff changeset
470 if len(self.mutation_regions_tsv_files) == 0:
0a558f444c98 Uploaded
greg
parents:
diff changeset
471 return
0a558f444c98 Uploaded
greg
parents:
diff changeset
472 try:
0a558f444c98 Uploaded
greg
parents:
diff changeset
473 mutation_regions = pandas.read_csv(self.mutation_regions_bed_file, sep='\t', header=0, index_col=False)
0a558f444c98 Uploaded
greg
parents:
diff changeset
474 except Exception:
0a558f444c98 Uploaded
greg
parents:
diff changeset
475 # Likely an empty file.
0a558f444c98 Uploaded
greg
parents:
diff changeset
476 return
0a558f444c98 Uploaded
greg
parents:
diff changeset
477 amr_mutations = pandas.Series(dtype=object)
0a558f444c98 Uploaded
greg
parents:
diff changeset
478 for region_i in range(mutation_regions.shape[0]):
0a558f444c98 Uploaded
greg
parents:
diff changeset
479 region = mutation_regions.iloc[region_i, :]
0a558f444c98 Uploaded
greg
parents:
diff changeset
480 region_name = str(region['name'])
0a558f444c98 Uploaded
greg
parents:
diff changeset
481 self.ofh.write("Processing mutations for region %s\n" % region_name)
0a558f444c98 Uploaded
greg
parents:
diff changeset
482 region_mutations_tsv_name = '%s_mutations.tsv' % region_name
0a558f444c98 Uploaded
greg
parents:
diff changeset
483 if region_mutations_tsv_name not in self.mutation_regions_tsv_files:
0a558f444c98 Uploaded
greg
parents:
diff changeset
484 continue
0a558f444c98 Uploaded
greg
parents:
diff changeset
485 region_mutations_tsv = self.mutation_regions_tsv_files[region_mutations_tsv_name]
0a558f444c98 Uploaded
greg
parents:
diff changeset
486 try:
0a558f444c98 Uploaded
greg
parents:
diff changeset
487 region_mutations = pandas.read_csv(region_mutations_tsv, sep='\t', header=0, index_col=False)
0a558f444c98 Uploaded
greg
parents:
diff changeset
488 except Exception:
0a558f444c98 Uploaded
greg
parents:
diff changeset
489 region_mutations = pandas.DataFrame()
0a558f444c98 Uploaded
greg
parents:
diff changeset
490 if region_mutations.shape[0] == 0:
0a558f444c98 Uploaded
greg
parents:
diff changeset
491 continue
0a558f444c98 Uploaded
greg
parents:
diff changeset
492 # Figure out what kind of mutations are in this region
0a558f444c98 Uploaded
greg
parents:
diff changeset
493 region_mutation_types = pandas.Series(['snp'] * region_mutations.shape[0], name='TYPE', index=region_mutations.index)
0a558f444c98 Uploaded
greg
parents:
diff changeset
494 region_mutation_types[region_mutations['REF'].str.len() != region_mutations['ALT'].str.len()] = 'small-indel'
0a558f444c98 Uploaded
greg
parents:
diff changeset
495 region_mutation_drugs = pandas.Series(region['drug'] * region_mutations.shape[0], name='DRUG', index=region_mutations.index)
0a558f444c98 Uploaded
greg
parents:
diff changeset
496 region_notes = pandas.Series(region['note'] * region_mutations.shape[0], name='NOTE', index=region_mutations.index)
0a558f444c98 Uploaded
greg
parents:
diff changeset
497 region_mutations = pandas.concat([region_mutations, region_mutation_types, region_mutation_drugs, region_notes], axis=1)
0a558f444c98 Uploaded
greg
parents:
diff changeset
498 region_mutations = region_mutations[['#CHROM', 'POS', 'TYPE', 'REF', 'ALT', 'DRUG', 'NOTE']]
0a558f444c98 Uploaded
greg
parents:
diff changeset
499 amr_mutations[region['name']] = region_mutations
0a558f444c98 Uploaded
greg
parents:
diff changeset
500 # Report the mutations.
0a558f444c98 Uploaded
greg
parents:
diff changeset
501 self.doc.new_line()
0a558f444c98 Uploaded
greg
parents:
diff changeset
502 self.doc.new_header(level=2, title=self.mutation_title)
0a558f444c98 Uploaded
greg
parents:
diff changeset
503 for region_name in amr_mutations.index.tolist():
0a558f444c98 Uploaded
greg
parents:
diff changeset
504 region_mutations = amr_mutations[region_name].copy()
0a558f444c98 Uploaded
greg
parents:
diff changeset
505 self.doc.new_line()
0a558f444c98 Uploaded
greg
parents:
diff changeset
506 self.doc.new_header(level=3, title=region_name)
0a558f444c98 Uploaded
greg
parents:
diff changeset
507 if (region_mutations.shape[0] == 0):
0a558f444c98 Uploaded
greg
parents:
diff changeset
508 self.doc.append('None')
0a558f444c98 Uploaded
greg
parents:
diff changeset
509 continue
0a558f444c98 Uploaded
greg
parents:
diff changeset
510 region_mutations.iloc[:, 1] = region_mutations.iloc[:, 1].apply(lambda x: '{:,}'.format(x))
0a558f444c98 Uploaded
greg
parents:
diff changeset
511 Table_List = ['Reference contig', 'Position', 'Reference', 'Alternate', 'Drug', 'Note']
0a558f444c98 Uploaded
greg
parents:
diff changeset
512 for i in range(region_mutations.shape[0]):
0a558f444c98 Uploaded
greg
parents:
diff changeset
513 Table_List = Table_List + region_mutations.iloc[i, [0, 1, 3, 4, 5, 6]].values.tolist()
0a558f444c98 Uploaded
greg
parents:
diff changeset
514 row_count = int(len(Table_List) / 6)
0a558f444c98 Uploaded
greg
parents:
diff changeset
515 self.doc.new_table(columns=6, rows=row_count, text=Table_List, text_align='left')
0a558f444c98 Uploaded
greg
parents:
diff changeset
516 method = '%s reads were mapped to the reference sequence using minimap2.' % self.read_type
0a558f444c98 Uploaded
greg
parents:
diff changeset
517 self.methods[self.mutation_methods_title] = self.methods[self.mutation_methods_title].append(pandas.Series(method))
0a558f444c98 Uploaded
greg
parents:
diff changeset
518 method = 'Mutations were identified using samtools mpileup and varscan.'
0a558f444c98 Uploaded
greg
parents:
diff changeset
519 self.methods[self.mutation_methods_title] = self.methods[self.mutation_methods_title].append(pandas.Series(method))
0a558f444c98 Uploaded
greg
parents:
diff changeset
520
0a558f444c98 Uploaded
greg
parents:
diff changeset
521 def add_amr_matrix(self):
0a558f444c98 Uploaded
greg
parents:
diff changeset
522 self.ofh.write("\nXXXXXX In add_amr_matrix\n\n")
0a558f444c98 Uploaded
greg
parents:
diff changeset
523 # Make sure that we have an AMR matrix to plot
0a558f444c98 Uploaded
greg
parents:
diff changeset
524 #if not getattr(self.analysis, 'did_draw_amr_matrix', False):
0a558f444c98 Uploaded
greg
parents:
diff changeset
525 # return
0a558f444c98 Uploaded
greg
parents:
diff changeset
526 #amr_matrix_png = self.analysis.amr_matrix_png
0a558f444c98 Uploaded
greg
parents:
diff changeset
527 #self.doc.new_line()
0a558f444c98 Uploaded
greg
parents:
diff changeset
528 #self.doc.new_header(level=2, title=self.amr_matrix_title)
0a558f444c98 Uploaded
greg
parents:
diff changeset
529 #self.doc.new_line('AMR genes and mutations with their corresponding drugs.')
0a558f444c98 Uploaded
greg
parents:
diff changeset
530 #self.doc.new_line(
0a558f444c98 Uploaded
greg
parents:
diff changeset
531 # self.doc.new_inline_image(
0a558f444c98 Uploaded
greg
parents:
diff changeset
532 # text='AMR genes and mutations with their corresponding drugs',
0a558f444c98 Uploaded
greg
parents:
diff changeset
533 # path=amr_matrix_png
0a558f444c98 Uploaded
greg
parents:
diff changeset
534 # )
0a558f444c98 Uploaded
greg
parents:
diff changeset
535 #)
0a558f444c98 Uploaded
greg
parents:
diff changeset
536 pass
0a558f444c98 Uploaded
greg
parents:
diff changeset
537
0a558f444c98 Uploaded
greg
parents:
diff changeset
538 def add_large_indels(self):
0a558f444c98 Uploaded
greg
parents:
diff changeset
539 self.ofh.write("\nXXXXXX In add_large_indels\n\n")
0a558f444c98 Uploaded
greg
parents:
diff changeset
540 # Make sure we looked for mutations
0a558f444c98 Uploaded
greg
parents:
diff changeset
541 #if len(self.analysis.large_indels) == 0:
0a558f444c98 Uploaded
greg
parents:
diff changeset
542 # return
0a558f444c98 Uploaded
greg
parents:
diff changeset
543 #large_indels = self.analysis.large_indels
0a558f444c98 Uploaded
greg
parents:
diff changeset
544 #self.doc.new_line()
0a558f444c98 Uploaded
greg
parents:
diff changeset
545 #self.doc.new_header(level=2, title=self.large_indel_title)
0a558f444c98 Uploaded
greg
parents:
diff changeset
546 #for genome in ['Reference insertions', 'Query insertions']:
0a558f444c98 Uploaded
greg
parents:
diff changeset
547 # genome_indels = large_indels[genome].copy()
0a558f444c98 Uploaded
greg
parents:
diff changeset
548 # self.doc.new_line()
0a558f444c98 Uploaded
greg
parents:
diff changeset
549 # self.doc.new_header(level=3, title=genome)
0a558f444c98 Uploaded
greg
parents:
diff changeset
550 # if (genome_indels.shape[0] == 0):
0a558f444c98 Uploaded
greg
parents:
diff changeset
551 # continue
0a558f444c98 Uploaded
greg
parents:
diff changeset
552 # genome_indels.iloc[:, 1] = genome_indels.iloc[:, 1].apply(lambda x: '{:,}'.format(x))
0a558f444c98 Uploaded
greg
parents:
diff changeset
553 # genome_indels.iloc[:, 2] = genome_indels.iloc[:, 2].apply(lambda x: '{:,}'.format(x))
0a558f444c98 Uploaded
greg
parents:
diff changeset
554 # genome_indels.iloc[:, 3] = genome_indels.iloc[:, 3].apply(lambda x: '{:,}'.format(x))
0a558f444c98 Uploaded
greg
parents:
diff changeset
555 # Table_List = [
0a558f444c98 Uploaded
greg
parents:
diff changeset
556 # 'Reference contig', 'Start', 'Stop', 'Size (bp)'
0a558f444c98 Uploaded
greg
parents:
diff changeset
557 # ]
0a558f444c98 Uploaded
greg
parents:
diff changeset
558 # for i in range(genome_indels.shape[0]):
0a558f444c98 Uploaded
greg
parents:
diff changeset
559 # Table_List = Table_List + genome_indels.iloc[i, :].values.tolist()
0a558f444c98 Uploaded
greg
parents:
diff changeset
560 # row_count = int(len(Table_List) / 4)
0a558f444c98 Uploaded
greg
parents:
diff changeset
561 # self.doc.new_table(columns=4, rows=row_count, text=Table_List, text_align='left')
0a558f444c98 Uploaded
greg
parents:
diff changeset
562 #method = 'Large insertions or deletions were found as the complement of aligned regions using bedtools.'
0a558f444c98 Uploaded
greg
parents:
diff changeset
563 #self.methods[self.reference_methods_title] = self.methods[self.reference_methods_title].append(
0a558f444c98 Uploaded
greg
parents:
diff changeset
564 # pandas.Series(method))
0a558f444c98 Uploaded
greg
parents:
diff changeset
565 #self.doc.new_line()
0a558f444c98 Uploaded
greg
parents:
diff changeset
566 #self.doc.new_line('<div style="page-break-after: always;"></div>')
0a558f444c98 Uploaded
greg
parents:
diff changeset
567 #self.doc.new_line()
0a558f444c98 Uploaded
greg
parents:
diff changeset
568 pass
0a558f444c98 Uploaded
greg
parents:
diff changeset
569
0a558f444c98 Uploaded
greg
parents:
diff changeset
570 def add_plasmids(self):
0a558f444c98 Uploaded
greg
parents:
diff changeset
571 self.ofh.write("\nXXXXXX In add_plasmids\n\n")
0a558f444c98 Uploaded
greg
parents:
diff changeset
572 """
0a558f444c98 Uploaded
greg
parents:
diff changeset
573 if not getattr(self.analysis, 'did_call_plasmids', False):
0a558f444c98 Uploaded
greg
parents:
diff changeset
574 return
0a558f444c98 Uploaded
greg
parents:
diff changeset
575 # Make sure we looked for mutations
0a558f444c98 Uploaded
greg
parents:
diff changeset
576 #plasmids = self.analysis.plasmids
0a558f444c98 Uploaded
greg
parents:
diff changeset
577 if plasmids is None:
0a558f444c98 Uploaded
greg
parents:
diff changeset
578 return
0a558f444c98 Uploaded
greg
parents:
diff changeset
579 plasmids = plasmids.copy()
0a558f444c98 Uploaded
greg
parents:
diff changeset
580 self.doc.new_line()
0a558f444c98 Uploaded
greg
parents:
diff changeset
581 #self.doc.new_header(level=2, title=self.analysis.plasmid_title)
0a558f444c98 Uploaded
greg
parents:
diff changeset
582 if (plasmids.shape[0] == 0):
0a558f444c98 Uploaded
greg
parents:
diff changeset
583 self.doc.new_line('None')
0a558f444c98 Uploaded
greg
parents:
diff changeset
584 return
0a558f444c98 Uploaded
greg
parents:
diff changeset
585 plasmids.iloc[:, 3] = plasmids.iloc[:, 3].apply(lambda x: '{:,}'.format(x))
0a558f444c98 Uploaded
greg
parents:
diff changeset
586 plasmids.iloc[:, 4] = plasmids.iloc[:, 4].apply(lambda x: '{:,}'.format(x))
0a558f444c98 Uploaded
greg
parents:
diff changeset
587 plasmids.iloc[:, 5] = plasmids.iloc[:, 5].apply(lambda x: '{:,}'.format(x))
0a558f444c98 Uploaded
greg
parents:
diff changeset
588 Table_List = [
0a558f444c98 Uploaded
greg
parents:
diff changeset
589 'Genome contig',
0a558f444c98 Uploaded
greg
parents:
diff changeset
590 'Plasmid hit',
0a558f444c98 Uploaded
greg
parents:
diff changeset
591 'Plasmid acc.',
0a558f444c98 Uploaded
greg
parents:
diff changeset
592 'Contig size',
0a558f444c98 Uploaded
greg
parents:
diff changeset
593 'Aliged',
0a558f444c98 Uploaded
greg
parents:
diff changeset
594 'Plasmid size'
0a558f444c98 Uploaded
greg
parents:
diff changeset
595 ]
0a558f444c98 Uploaded
greg
parents:
diff changeset
596 for i in range(plasmids.shape[0]):
0a558f444c98 Uploaded
greg
parents:
diff changeset
597 Table_List = Table_List + plasmids.iloc[i, 0:6].values.tolist()
0a558f444c98 Uploaded
greg
parents:
diff changeset
598 row_count = int(len(Table_List) / 6)
0a558f444c98 Uploaded
greg
parents:
diff changeset
599 self.doc.new_table(columns=6, rows=row_count, text=Table_List, text_align='left')
0a558f444c98 Uploaded
greg
parents:
diff changeset
600 method = 'The plasmid reference database was queried against the genome assembly using minimap2.'
0a558f444c98 Uploaded
greg
parents:
diff changeset
601 self.methods[self.plasmid_methods_title] = self.methods[self.plasmid_methods_title].append(pandas.Series(method))
0a558f444c98 Uploaded
greg
parents:
diff changeset
602 method = 'The resulting SAM was converted to a PSL using a custom version of sam2psl.'
0a558f444c98 Uploaded
greg
parents:
diff changeset
603 self.methods[self.plasmid_methods_title] = self.methods[self.plasmid_methods_title].append(pandas.Series(method))
0a558f444c98 Uploaded
greg
parents:
diff changeset
604 method = 'Plasmid-to-genome hits were resolved using the pChunks algorithm.'
0a558f444c98 Uploaded
greg
parents:
diff changeset
605 self.methods[self.plasmid_methods_title] = self.methods[self.plasmid_methods_title].append(pandas.Series(method))
0a558f444c98 Uploaded
greg
parents:
diff changeset
606 """
0a558f444c98 Uploaded
greg
parents:
diff changeset
607 pass
0a558f444c98 Uploaded
greg
parents:
diff changeset
608
0a558f444c98 Uploaded
greg
parents:
diff changeset
609 def add_methods(self):
0a558f444c98 Uploaded
greg
parents:
diff changeset
610 self.ofh.write("\nXXXXXX In add_methods\n\n")
0a558f444c98 Uploaded
greg
parents:
diff changeset
611 self.doc.new_line('<div style="page-break-after: always;"></div>')
0a558f444c98 Uploaded
greg
parents:
diff changeset
612 self.doc.new_line()
0a558f444c98 Uploaded
greg
parents:
diff changeset
613 if len(self.methods) == 0:
0a558f444c98 Uploaded
greg
parents:
diff changeset
614 return
0a558f444c98 Uploaded
greg
parents:
diff changeset
615 self.doc.new_line()
0a558f444c98 Uploaded
greg
parents:
diff changeset
616 self.doc.new_header(level=2, title=self.methods_title)
0a558f444c98 Uploaded
greg
parents:
diff changeset
617
0a558f444c98 Uploaded
greg
parents:
diff changeset
618 for methods_section in self.methods.index.tolist():
0a558f444c98 Uploaded
greg
parents:
diff changeset
619 if self.methods[methods_section] is None or len(self.methods[methods_section]) == 0:
0a558f444c98 Uploaded
greg
parents:
diff changeset
620 continue
0a558f444c98 Uploaded
greg
parents:
diff changeset
621 self.doc.new_line()
0a558f444c98 Uploaded
greg
parents:
diff changeset
622 self.doc.new_header(level=3, title=methods_section)
0a558f444c98 Uploaded
greg
parents:
diff changeset
623 self.doc.new_paragraph(' '.join(self.methods[methods_section]))
0a558f444c98 Uploaded
greg
parents:
diff changeset
624
0a558f444c98 Uploaded
greg
parents:
diff changeset
625 def add_summary(self):
0a558f444c98 Uploaded
greg
parents:
diff changeset
626 self.ofh.write("\nXXXXXX In add_summary\n\n")
0a558f444c98 Uploaded
greg
parents:
diff changeset
627 # Add summary title
0a558f444c98 Uploaded
greg
parents:
diff changeset
628 self.doc.new_header(level=1, title=self.summary_title)
0a558f444c98 Uploaded
greg
parents:
diff changeset
629 # First section of Summary
0a558f444c98 Uploaded
greg
parents:
diff changeset
630 self.doc.new_header(level=1, title='CDC Advisory')
0a558f444c98 Uploaded
greg
parents:
diff changeset
631 self.doc.new_paragraph(CDC_ADVISORY)
0a558f444c98 Uploaded
greg
parents:
diff changeset
632 self.doc.new_line()
0a558f444c98 Uploaded
greg
parents:
diff changeset
633 self.add_run_information()
0a558f444c98 Uploaded
greg
parents:
diff changeset
634 self.add_ont_library_information()
0a558f444c98 Uploaded
greg
parents:
diff changeset
635 methods = []
0a558f444c98 Uploaded
greg
parents:
diff changeset
636 if self.did_guppy_ont_fast5:
0a558f444c98 Uploaded
greg
parents:
diff changeset
637 methods += ['ONT reads were basecalled using guppy']
0a558f444c98 Uploaded
greg
parents:
diff changeset
638 if self.did_qcat_ont_fastq:
0a558f444c98 Uploaded
greg
parents:
diff changeset
639 methods += ['ONT reads were demultiplexed and trimmed using qcat']
0a558f444c98 Uploaded
greg
parents:
diff changeset
640 self.methods[self.basecalling_methods_title] = pandas.Series(methods)
0a558f444c98 Uploaded
greg
parents:
diff changeset
641 self.add_illumina_library_information()
0a558f444c98 Uploaded
greg
parents:
diff changeset
642 self.add_assembly_information()
0a558f444c98 Uploaded
greg
parents:
diff changeset
643 self.add_contig_info()
0a558f444c98 Uploaded
greg
parents:
diff changeset
644 self.add_assembly_notes()
0a558f444c98 Uploaded
greg
parents:
diff changeset
645 if self.did_flye_ont_fastq:
0a558f444c98 Uploaded
greg
parents:
diff changeset
646 method = 'ONT reads were assembled using Flye.'
0a558f444c98 Uploaded
greg
parents:
diff changeset
647 self.methods[self.assembly_methods_title] = self.methods[self.assembly_methods_title].append(pandas.Series(method))
0a558f444c98 Uploaded
greg
parents:
diff changeset
648 if self.did_medaka_ont_assembly:
0a558f444c98 Uploaded
greg
parents:
diff changeset
649 method = 'the genome assembly was polished using ont reads and medaka.'
0a558f444c98 Uploaded
greg
parents:
diff changeset
650 self.methods[self.assembly_methods_title] = self.methods[self.assembly_methods_title].append(pandas.series(method))
0a558f444c98 Uploaded
greg
parents:
diff changeset
651
0a558f444c98 Uploaded
greg
parents:
diff changeset
652 def make_tex(self):
0a558f444c98 Uploaded
greg
parents:
diff changeset
653 self.doc.new_table_of_contents(table_title='detailed run information', depth=2, marker="tableofcontents")
0a558f444c98 Uploaded
greg
parents:
diff changeset
654 text = self.doc.file_data_text
0a558f444c98 Uploaded
greg
parents:
diff changeset
655 text = text.replace("##--[", "")
0a558f444c98 Uploaded
greg
parents:
diff changeset
656 text = text.replace("]--##", "")
0a558f444c98 Uploaded
greg
parents:
diff changeset
657 self.doc.file_data_text = text
0a558f444c98 Uploaded
greg
parents:
diff changeset
658 self.doc.create_md_file()
0a558f444c98 Uploaded
greg
parents:
diff changeset
659
0a558f444c98 Uploaded
greg
parents:
diff changeset
660 def make_report(self):
0a558f444c98 Uploaded
greg
parents:
diff changeset
661 self.ofh.write("\nXXXXXX In make_report\n\n")
0a558f444c98 Uploaded
greg
parents:
diff changeset
662 self.start_doc()
0a558f444c98 Uploaded
greg
parents:
diff changeset
663 self.add_summary()
0a558f444c98 Uploaded
greg
parents:
diff changeset
664 self.add_contamination()
0a558f444c98 Uploaded
greg
parents:
diff changeset
665 self.add_alignment()
0a558f444c98 Uploaded
greg
parents:
diff changeset
666 self.add_features()
0a558f444c98 Uploaded
greg
parents:
diff changeset
667 self.add_feature_plots()
0a558f444c98 Uploaded
greg
parents:
diff changeset
668 self.add_mutations()
0a558f444c98 Uploaded
greg
parents:
diff changeset
669 # TODO stuff working to here...
0a558f444c98 Uploaded
greg
parents:
diff changeset
670 self.add_large_indels()
0a558f444c98 Uploaded
greg
parents:
diff changeset
671 self.add_plasmids()
0a558f444c98 Uploaded
greg
parents:
diff changeset
672 self.add_amr_matrix()
0a558f444c98 Uploaded
greg
parents:
diff changeset
673 # self.add_snps()
0a558f444c98 Uploaded
greg
parents:
diff changeset
674 self.add_methods()
0a558f444c98 Uploaded
greg
parents:
diff changeset
675 self.make_tex()
0a558f444c98 Uploaded
greg
parents:
diff changeset
676 # It took me quite a long time to find out that the value of the -t
0a558f444c98 Uploaded
greg
parents:
diff changeset
677 # (implied) argument in the following command must be 'html' instead of
0a558f444c98 Uploaded
greg
parents:
diff changeset
678 # the more logical 'pdf'. see the answer from snsn in this thread:
0a558f444c98 Uploaded
greg
parents:
diff changeset
679 # https://github.com/jessicategner/pypandoc/issues/186
0a558f444c98 Uploaded
greg
parents:
diff changeset
680 self.ofh.write("\nXXXXX In make_report, calling pypandoc.convert_file...\n\n")
0a558f444c98 Uploaded
greg
parents:
diff changeset
681 pypandoc.convert_file(self.report_md,
0a558f444c98 Uploaded
greg
parents:
diff changeset
682 'html',
0a558f444c98 Uploaded
greg
parents:
diff changeset
683 extra_args=['--pdf-engine=weasyprint', '-V', '-css=%s' % self.pima_css],
0a558f444c98 Uploaded
greg
parents:
diff changeset
684 outputfile='pima_report.pdf')
0a558f444c98 Uploaded
greg
parents:
diff changeset
685 self.ofh.close()
0a558f444c98 Uploaded
greg
parents:
diff changeset
686
0a558f444c98 Uploaded
greg
parents:
diff changeset
687
0a558f444c98 Uploaded
greg
parents:
diff changeset
688 parser = argparse.ArgumentParser()
0a558f444c98 Uploaded
greg
parents:
diff changeset
689
0a558f444c98 Uploaded
greg
parents:
diff changeset
690 parser.add_argument('--analysis_name', action='store', dest='analysis_name', help='Sample identifier')
0a558f444c98 Uploaded
greg
parents:
diff changeset
691 parser.add_argument('--assembly_fasta_file', action='store', dest='assembly_fasta_file', help='Assembly fasta file')
0a558f444c98 Uploaded
greg
parents:
diff changeset
692 parser.add_argument('--assembly_name', action='store', dest='assembly_name', help='Assembly identifier')
0a558f444c98 Uploaded
greg
parents:
diff changeset
693 parser.add_argument('--feature_bed_dir', action='store', dest='feature_bed_dir', help='Directory of best feature hits bed files')
0a558f444c98 Uploaded
greg
parents:
diff changeset
694 parser.add_argument('--feature_png_dir', action='store', dest='feature_png_dir', help='Directory of best feature hits png files')
0a558f444c98 Uploaded
greg
parents:
diff changeset
695 parser.add_argument('--contig_coverage_file', action='store', dest='contig_coverage_file', help='Contig coverage TSV file')
0a558f444c98 Uploaded
greg
parents:
diff changeset
696 parser.add_argument('--dbkey', action='store', dest='dbkey', help='Reference genome')
0a558f444c98 Uploaded
greg
parents:
diff changeset
697 parser.add_argument('--gzipped', action='store_true', dest='gzipped', default=False, help='Input sample is gzipped')
0a558f444c98 Uploaded
greg
parents:
diff changeset
698 parser.add_argument('--illumina_fastq_file', action='store', dest='illumina_fastq_file', help='Input sample')
0a558f444c98 Uploaded
greg
parents:
diff changeset
699 parser.add_argument('--mutation_regions_bed_file', action='store', dest='mutation_regions_bed_file', help='AMR mutation regions BRD file')
0a558f444c98 Uploaded
greg
parents:
diff changeset
700 parser.add_argument('--mutation_regions_dir', action='store', dest='mutation_regions_dir', help='Directory of mutation regions TSV files')
0a558f444c98 Uploaded
greg
parents:
diff changeset
701 parser.add_argument('--pima_css', action='store', dest='pima_css', help='PIMA css stypesheet')
0a558f444c98 Uploaded
greg
parents:
diff changeset
702
0a558f444c98 Uploaded
greg
parents:
diff changeset
703 args = parser.parse_args()
0a558f444c98 Uploaded
greg
parents:
diff changeset
704
0a558f444c98 Uploaded
greg
parents:
diff changeset
705 # Prepare the features BED files.
0a558f444c98 Uploaded
greg
parents:
diff changeset
706 feature_bed_files = []
0a558f444c98 Uploaded
greg
parents:
diff changeset
707 for file_name in sorted(os.listdir(args.feature_bed_dir)):
0a558f444c98 Uploaded
greg
parents:
diff changeset
708 file_path = os.path.abspath(os.path.join(args.feature_bed_dir, file_name))
0a558f444c98 Uploaded
greg
parents:
diff changeset
709 feature_bed_files.append(file_path)
0a558f444c98 Uploaded
greg
parents:
diff changeset
710 # Prepare the features PNG files.
0a558f444c98 Uploaded
greg
parents:
diff changeset
711 feature_png_files = []
0a558f444c98 Uploaded
greg
parents:
diff changeset
712 for file_name in sorted(os.listdir(args.feature_png_dir)):
0a558f444c98 Uploaded
greg
parents:
diff changeset
713 file_path = os.path.abspath(os.path.join(args.feature_png_dir, file_name))
0a558f444c98 Uploaded
greg
parents:
diff changeset
714 feature_png_files.append(file_path)
0a558f444c98 Uploaded
greg
parents:
diff changeset
715 # Prepare the mutation regions TSV files.
0a558f444c98 Uploaded
greg
parents:
diff changeset
716 mutation_regions_files = []
0a558f444c98 Uploaded
greg
parents:
diff changeset
717 for file_name in sorted(os.listdir(args.mutation_regions_dir)):
0a558f444c98 Uploaded
greg
parents:
diff changeset
718 file_path = os.path.abspath(os.path.join(args.feature_png_dir, file_name))
0a558f444c98 Uploaded
greg
parents:
diff changeset
719 mutation_regions_files.append(file_path)
0a558f444c98 Uploaded
greg
parents:
diff changeset
720
0a558f444c98 Uploaded
greg
parents:
diff changeset
721 markdown_report = PimaReport(args.analysis_name,
0a558f444c98 Uploaded
greg
parents:
diff changeset
722 args.assembly_fasta_file,
0a558f444c98 Uploaded
greg
parents:
diff changeset
723 args.assembly_name,
0a558f444c98 Uploaded
greg
parents:
diff changeset
724 feature_bed_files,
0a558f444c98 Uploaded
greg
parents:
diff changeset
725 feature_png_files,
0a558f444c98 Uploaded
greg
parents:
diff changeset
726 args.contig_coverage_file,
0a558f444c98 Uploaded
greg
parents:
diff changeset
727 args.dbkey,
0a558f444c98 Uploaded
greg
parents:
diff changeset
728 args.gzipped,
0a558f444c98 Uploaded
greg
parents:
diff changeset
729 args.illumina_fastq_file,
0a558f444c98 Uploaded
greg
parents:
diff changeset
730 args.mutation_regions_bed_file,
0a558f444c98 Uploaded
greg
parents:
diff changeset
731 mutation_regions_files,
0a558f444c98 Uploaded
greg
parents:
diff changeset
732 args.pima_css)
0a558f444c98 Uploaded
greg
parents:
diff changeset
733 markdown_report.make_report()