annotate pima_report.py @ 26:46edd7435555 draft

Uploaded
author greg
date Tue, 25 Apr 2023 20:31:35 +0000
parents 4986a7fb2145
children ddc056cf16bf
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
14
95b1d1a9497d Uploaded
greg
parents: 13
diff changeset
12 # FIXME: TableOfContents doesn't work.
95b1d1a9497d Uploaded
greg
parents: 13
diff changeset
13 # from mdutils.tools import TableOfContents
0
0a558f444c98 Uploaded
greg
parents:
diff changeset
14
0a558f444c98 Uploaded
greg
parents:
diff changeset
15 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
16
0a558f444c98 Uploaded
greg
parents:
diff changeset
17
0a558f444c98 Uploaded
greg
parents:
diff changeset
18 class PimaReport:
0a558f444c98 Uploaded
greg
parents:
diff changeset
19
21
667b253329c6 Uploaded
greg
parents: 20
diff changeset
20 def __init__(self, analysis_name=None, amr_deletions_file=None, amr_matrix_files=None, assembler_version=None,
667b253329c6 Uploaded
greg
parents: 20
diff changeset
21 assembly_fasta_file=None, assembly_name=None, bedtools_version=None, blastn_version=None,
667b253329c6 Uploaded
greg
parents: 20
diff changeset
22 circos_files=None, compute_sequence_length_file=None, contig_coverage_file=None, dbkey=None,
26
46edd7435555 Uploaded
greg
parents: 25
diff changeset
23 dnadiff_snps_file=None, dnadiff_version=None, errors_file=None, feature_bed_files=None,
21
667b253329c6 Uploaded
greg
parents: 20
diff changeset
24 feature_png_files=None, flye_assembly_info_file=None, genome_insertions_file=None, gzipped=None,
26
46edd7435555 Uploaded
greg
parents: 25
diff changeset
25 illumina_forward_read_file=None, illumina_reverse_read_file=None, kraken2_report_file=None,
46edd7435555 Uploaded
greg
parents: 25
diff changeset
26 kraken2_version=None, minimap2_version=None, mutation_regions_bed_file=None,
46edd7435555 Uploaded
greg
parents: 25
diff changeset
27 mutation_regions_tsv_files=None, ont_file=None, pima_css=None, plasmids_file=None, quast_report_file=None,
21
667b253329c6 Uploaded
greg
parents: 20
diff changeset
28 read_type=None, reference_insertions_file=None, samtools_version=None, varscan_version=None):
0
0a558f444c98 Uploaded
greg
parents:
diff changeset
29 self.ofh = open("process_log.txt", "w")
0a558f444c98 Uploaded
greg
parents:
diff changeset
30
1
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
31 self.ofh.write("amr_deletions_file: %s\n" % str(amr_deletions_file))
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
32 self.ofh.write("amr_matrix_files: %s\n" % str(amr_matrix_files))
0
0a558f444c98 Uploaded
greg
parents:
diff changeset
33 self.ofh.write("analysis_name: %s\n" % str(analysis_name))
21
667b253329c6 Uploaded
greg
parents: 20
diff changeset
34 self.ofh.write("assembler_version: %s\n" % str(assembler_version))
0
0a558f444c98 Uploaded
greg
parents:
diff changeset
35 self.ofh.write("assembly_fasta_file: %s\n" % str(assembly_fasta_file))
0a558f444c98 Uploaded
greg
parents:
diff changeset
36 self.ofh.write("assembly_name: %s\n" % str(assembly_name))
12
99613333fd1f Uploaded
greg
parents: 11
diff changeset
37 self.ofh.write("bedtools_version: %s\n" % str(bedtools_version))
2
9cb62054a87a Uploaded
greg
parents: 1
diff changeset
38 self.ofh.write("blastn_version: %s\n" % str(blastn_version))
13
f03c80bb22e9 Uploaded
greg
parents: 12
diff changeset
39 self.ofh.write("circos_files: %s\n" % str(circos_files))
1
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
40 self.ofh.write("compute_sequence_length_file: %s\n" % str(compute_sequence_length_file))
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
41 self.ofh.write("contig_coverage_file: %s\n" % str(contig_coverage_file))
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
42 self.ofh.write("dbkey: %s\n" % str(dbkey))
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
43 self.ofh.write("dnadiff_snps_file: %s\n" % str(dnadiff_snps_file))
2
9cb62054a87a Uploaded
greg
parents: 1
diff changeset
44 self.ofh.write("dnadiff_version: %s\n" % str(dnadiff_version))
18
e948214a9e3c Uploaded
greg
parents: 17
diff changeset
45 self.ofh.write("errors_file: %s\n" % str(errors_file))
0
0a558f444c98 Uploaded
greg
parents:
diff changeset
46 self.ofh.write("feature_bed_files: %s\n" % str(feature_bed_files))
0a558f444c98 Uploaded
greg
parents:
diff changeset
47 self.ofh.write("feature_png_files: %s\n" % str(feature_png_files))
1
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
48 self.ofh.write("flye_assembly_info_file: %s\n" % str(flye_assembly_info_file))
0
0a558f444c98 Uploaded
greg
parents:
diff changeset
49 self.ofh.write("gzipped: %s\n" % str(gzipped))
1
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
50 self.ofh.write("genome_insertions_file: %s\n" % str(genome_insertions_file))
26
46edd7435555 Uploaded
greg
parents: 25
diff changeset
51 self.ofh.write("illumina_forward_read_file: %s\n" % str(illumina_forward_read_file))
46edd7435555 Uploaded
greg
parents: 25
diff changeset
52 self.ofh.write("illumina_reverse_read_file: %s\n" % str(illumina_reverse_read_file))
2
9cb62054a87a Uploaded
greg
parents: 1
diff changeset
53 self.ofh.write("kraken2_report_file: %s\n" % str(kraken2_report_file))
9cb62054a87a Uploaded
greg
parents: 1
diff changeset
54 self.ofh.write("kraken2_version: %s\n" % str(kraken2_version))
12
99613333fd1f Uploaded
greg
parents: 11
diff changeset
55 self.ofh.write("minimap2_version: %s\n" % str(minimap2_version))
0
0a558f444c98 Uploaded
greg
parents:
diff changeset
56 self.ofh.write("mutation_regions_bed_file: %s\n" % str(mutation_regions_bed_file))
0a558f444c98 Uploaded
greg
parents:
diff changeset
57 self.ofh.write("mutation_regions_tsv_files: %s\n" % str(mutation_regions_tsv_files))
26
46edd7435555 Uploaded
greg
parents: 25
diff changeset
58 self.ofh.write("ont_file: %s\n" % str(ont_file))
0
0a558f444c98 Uploaded
greg
parents:
diff changeset
59 self.ofh.write("pima_css: %s\n" % str(pima_css))
1
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
60 self.ofh.write("plasmids_file: %s\n" % str(plasmids_file))
13
f03c80bb22e9 Uploaded
greg
parents: 12
diff changeset
61 self.ofh.write("quast_report_file: %s\n" % str(quast_report_file))
18
e948214a9e3c Uploaded
greg
parents: 17
diff changeset
62 self.ofh.write("read_type: %s\n" % str(read_type))
1
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
63 self.ofh.write("reference_insertions_file: %s\n" % str(reference_insertions_file))
12
99613333fd1f Uploaded
greg
parents: 11
diff changeset
64 self.ofh.write("samtools_version: %s\n" % str(samtools_version))
99613333fd1f Uploaded
greg
parents: 11
diff changeset
65 self.ofh.write("varscan_version: %s\n" % str(varscan_version))
0
0a558f444c98 Uploaded
greg
parents:
diff changeset
66
0a558f444c98 Uploaded
greg
parents:
diff changeset
67 # General
0a558f444c98 Uploaded
greg
parents:
diff changeset
68 self.doc = None
0a558f444c98 Uploaded
greg
parents:
diff changeset
69 self.report_md = 'pima_report.md'
0a558f444c98 Uploaded
greg
parents:
diff changeset
70
0a558f444c98 Uploaded
greg
parents:
diff changeset
71 # Inputs
1
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
72 self.amr_deletions_file = amr_deletions_file
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
73 self.amr_matrix_files = amr_matrix_files
13
f03c80bb22e9 Uploaded
greg
parents: 12
diff changeset
74 self.analysis_name = analysis_name.split('_')[0]
f03c80bb22e9 Uploaded
greg
parents: 12
diff changeset
75 self.ofh.write("self.analysis_name: %s\n" % str(self.analysis_name))
21
667b253329c6 Uploaded
greg
parents: 20
diff changeset
76 if assembler_version is None:
667b253329c6 Uploaded
greg
parents: 20
diff changeset
77 self.assembler_version = 'assembler (version unknown)'
667b253329c6 Uploaded
greg
parents: 20
diff changeset
78 else:
667b253329c6 Uploaded
greg
parents: 20
diff changeset
79 if read_type == 'ont':
667b253329c6 Uploaded
greg
parents: 20
diff changeset
80 # Assembler is flye.
667b253329c6 Uploaded
greg
parents: 20
diff changeset
81 assembler_version = assembler_version.rstrip(' _assembly info_')
667b253329c6 Uploaded
greg
parents: 20
diff changeset
82 else:
667b253329c6 Uploaded
greg
parents: 20
diff changeset
83 # Assembler is spades.
667b253329c6 Uploaded
greg
parents: 20
diff changeset
84 assembler_version = assembler_version.rstrip(' _contigs')
667b253329c6 Uploaded
greg
parents: 20
diff changeset
85 self.assembler_version = re.sub('_', '.', assembler_version)
0
0a558f444c98 Uploaded
greg
parents:
diff changeset
86 self.assembly_fasta_file = assembly_fasta_file
12
99613333fd1f Uploaded
greg
parents: 11
diff changeset
87 self.assembly_name = re.sub('_', '.', assembly_name.rstrip(' _consensus_'))
99613333fd1f Uploaded
greg
parents: 11
diff changeset
88 if bedtools_version is None:
99613333fd1f Uploaded
greg
parents: 11
diff changeset
89 self.bedtools_version = 'bedtools (version unknown)'
99613333fd1f Uploaded
greg
parents: 11
diff changeset
90 else:
99613333fd1f Uploaded
greg
parents: 11
diff changeset
91 self.bedtools_version = re.sub('_', '.', bedtools_version.rstrip(' _genome insertions'))
99613333fd1f Uploaded
greg
parents: 11
diff changeset
92 if blastn_version is None:
99613333fd1f Uploaded
greg
parents: 11
diff changeset
93 self.blastn_version = 'blastn (version unknown)'
99613333fd1f Uploaded
greg
parents: 11
diff changeset
94 else:
99613333fd1f Uploaded
greg
parents: 11
diff changeset
95 self.blastn_version = re.sub('_', '.', blastn_version.rstrip(' _features_'))
13
f03c80bb22e9 Uploaded
greg
parents: 12
diff changeset
96 self.circos_files = circos_files
1
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
97 self.compute_sequence_length_file = compute_sequence_length_file
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
98 self.contig_coverage_file = contig_coverage_file
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
99 self.dbkey = dbkey
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
100 self.dnadiff_snps_file = dnadiff_snps_file
12
99613333fd1f Uploaded
greg
parents: 11
diff changeset
101 if dnadiff_version is None:
99613333fd1f Uploaded
greg
parents: 11
diff changeset
102 self.dnadiff_version = 'dnadiff (version unknown)'
99613333fd1f Uploaded
greg
parents: 11
diff changeset
103 else:
99613333fd1f Uploaded
greg
parents: 11
diff changeset
104 self.dnadiff_version = re.sub('_', '.', dnadiff_version.rstrip(' _snps_'))
18
e948214a9e3c Uploaded
greg
parents: 17
diff changeset
105 self.errors_file = errors_file
0
0a558f444c98 Uploaded
greg
parents:
diff changeset
106 self.feature_bed_files = feature_bed_files
0a558f444c98 Uploaded
greg
parents:
diff changeset
107 self.feature_png_files = feature_png_files
1
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
108 self.flye_assembly_info_file = flye_assembly_info_file
0
0a558f444c98 Uploaded
greg
parents:
diff changeset
109 self.gzipped = gzipped
1
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
110 self.genome_insertions_file = genome_insertions_file
26
46edd7435555 Uploaded
greg
parents: 25
diff changeset
111 self.illumina_forward_read_file = illumina_forward_read_file
46edd7435555 Uploaded
greg
parents: 25
diff changeset
112 self.illumina_reverse_read_file = illumina_reverse_read_file
2
9cb62054a87a Uploaded
greg
parents: 1
diff changeset
113 self.kraken2_report_file = kraken2_report_file
12
99613333fd1f Uploaded
greg
parents: 11
diff changeset
114 if kraken2_version is None:
99613333fd1f Uploaded
greg
parents: 11
diff changeset
115 self.kraken2_version = 'kraken2 (version unknown)'
99613333fd1f Uploaded
greg
parents: 11
diff changeset
116 else:
99613333fd1f Uploaded
greg
parents: 11
diff changeset
117 self.kraken2_version = re.sub('_', '.', kraken2_version.rstrip(' _report_'))
99613333fd1f Uploaded
greg
parents: 11
diff changeset
118 if minimap2_version is None:
99613333fd1f Uploaded
greg
parents: 11
diff changeset
119 self.minimap2_version = 'minimap2 (version unknown)'
99613333fd1f Uploaded
greg
parents: 11
diff changeset
120 else:
99613333fd1f Uploaded
greg
parents: 11
diff changeset
121 self.minimap2_version = re.sub('_', '.', minimap2_version)
0
0a558f444c98 Uploaded
greg
parents:
diff changeset
122 self.mutation_regions_bed_file = mutation_regions_bed_file
0a558f444c98 Uploaded
greg
parents:
diff changeset
123 self.mutation_regions_tsv_files = mutation_regions_tsv_files
0a558f444c98 Uploaded
greg
parents:
diff changeset
124 self.pima_css = pima_css
1
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
125 self.plasmids_file = plasmids_file
13
f03c80bb22e9 Uploaded
greg
parents: 12
diff changeset
126 self.quast_report_file = quast_report_file
18
e948214a9e3c Uploaded
greg
parents: 17
diff changeset
127 self.read_type = read_type.upper()
13
f03c80bb22e9 Uploaded
greg
parents: 12
diff changeset
128 self.reference_insertions_file = reference_insertions_file
1
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
129 self.reference_insertions_file = reference_insertions_file
12
99613333fd1f Uploaded
greg
parents: 11
diff changeset
130 if samtools_version is None:
99613333fd1f Uploaded
greg
parents: 11
diff changeset
131 self.samtools_version = 'samtools (version unknown)'
99613333fd1f Uploaded
greg
parents: 11
diff changeset
132 else:
99613333fd1f Uploaded
greg
parents: 11
diff changeset
133 self.samtools_version = re.sub('_', '.', samtools_version)
99613333fd1f Uploaded
greg
parents: 11
diff changeset
134 if varscan_version is None:
99613333fd1f Uploaded
greg
parents: 11
diff changeset
135 self.varscan_version = 'varscan (version unknown)'
99613333fd1f Uploaded
greg
parents: 11
diff changeset
136 else:
99613333fd1f Uploaded
greg
parents: 11
diff changeset
137 self.varscan_version = re.sub('_', '.', varscan_version)
0
0a558f444c98 Uploaded
greg
parents:
diff changeset
138
0a558f444c98 Uploaded
greg
parents:
diff changeset
139 # Titles
0a558f444c98 Uploaded
greg
parents:
diff changeset
140 self.alignment_title = 'Comparison with reference'
0a558f444c98 Uploaded
greg
parents:
diff changeset
141 self.alignment_notes_title = 'Alignment notes'
0a558f444c98 Uploaded
greg
parents:
diff changeset
142 self.amr_matrix_title = 'AMR matrix'
0a558f444c98 Uploaded
greg
parents:
diff changeset
143 self.assembly_methods_title = 'Assembly'
0a558f444c98 Uploaded
greg
parents:
diff changeset
144 self.assembly_notes_title = 'Assembly notes'
0a558f444c98 Uploaded
greg
parents:
diff changeset
145 self.basecalling_title = 'Basecalling'
0a558f444c98 Uploaded
greg
parents:
diff changeset
146 self.basecalling_methods_title = 'Basecalling'
0a558f444c98 Uploaded
greg
parents:
diff changeset
147 self.contamination_methods_title = 'Contamination check'
0a558f444c98 Uploaded
greg
parents:
diff changeset
148 self.contig_alignment_title = 'Alignment vs. reference contigs'
0a558f444c98 Uploaded
greg
parents:
diff changeset
149 self.feature_title = 'Features found in the assembly'
0a558f444c98 Uploaded
greg
parents:
diff changeset
150 self.feature_methods_title = 'Feature annotation'
0a558f444c98 Uploaded
greg
parents:
diff changeset
151 self.feature_plot_title = 'Feature annotation plots'
0a558f444c98 Uploaded
greg
parents:
diff changeset
152 self.large_indel_title = 'Large insertions & deletions'
0a558f444c98 Uploaded
greg
parents:
diff changeset
153 self.methods_title = 'Methods'
18
e948214a9e3c Uploaded
greg
parents: 17
diff changeset
154 self.mutation_errors_title = 'Errors finding mutations in the sample'
0
0a558f444c98 Uploaded
greg
parents:
diff changeset
155 self.mutation_title = 'Mutations found in the sample'
0a558f444c98 Uploaded
greg
parents:
diff changeset
156 self.mutation_methods_title = 'Mutation screening'
0a558f444c98 Uploaded
greg
parents:
diff changeset
157 self.plasmid_methods_title = 'Plasmid annotation'
1
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
158 self.plasmid_title = 'Plasmid annotation'
21
667b253329c6 Uploaded
greg
parents: 20
diff changeset
159 self.reference_genome_title = 'Reference genome'
0
0a558f444c98 Uploaded
greg
parents:
diff changeset
160 self.reference_methods_title = 'Reference comparison'
0a558f444c98 Uploaded
greg
parents:
diff changeset
161 self.snp_indel_title = 'SNPs and small indels'
13
f03c80bb22e9 Uploaded
greg
parents: 12
diff changeset
162 self.summary_title = 'Summary'
0
0a558f444c98 Uploaded
greg
parents:
diff changeset
163
0a558f444c98 Uploaded
greg
parents:
diff changeset
164 # Methods
0a558f444c98 Uploaded
greg
parents:
diff changeset
165 self.methods = pandas.Series(dtype='float64')
0a558f444c98 Uploaded
greg
parents:
diff changeset
166 self.methods[self.contamination_methods_title] = pandas.Series(dtype='float64')
0a558f444c98 Uploaded
greg
parents:
diff changeset
167 self.methods[self.assembly_methods_title] = pandas.Series(dtype='float64')
21
667b253329c6 Uploaded
greg
parents: 20
diff changeset
168 self.methods[self.reference_genome_title] = pandas.Series(dtype='float64')
0
0a558f444c98 Uploaded
greg
parents:
diff changeset
169 self.methods[self.reference_methods_title] = pandas.Series(dtype='float64')
0a558f444c98 Uploaded
greg
parents:
diff changeset
170 self.methods[self.mutation_methods_title] = pandas.Series(dtype='float64')
0a558f444c98 Uploaded
greg
parents:
diff changeset
171 self.methods[self.feature_methods_title] = pandas.Series(dtype='float64')
0a558f444c98 Uploaded
greg
parents:
diff changeset
172 self.methods[self.plasmid_methods_title] = pandas.Series(dtype='float64')
0a558f444c98 Uploaded
greg
parents:
diff changeset
173
0a558f444c98 Uploaded
greg
parents:
diff changeset
174 # Notes
0a558f444c98 Uploaded
greg
parents:
diff changeset
175 self.assembly_notes = pandas.Series(dtype=object)
0a558f444c98 Uploaded
greg
parents:
diff changeset
176 self.alignment_notes = pandas.Series(dtype=object)
0a558f444c98 Uploaded
greg
parents:
diff changeset
177 self.contig_alignment = pandas.Series(dtype=object)
0a558f444c98 Uploaded
greg
parents:
diff changeset
178
0a558f444c98 Uploaded
greg
parents:
diff changeset
179 # Values
0a558f444c98 Uploaded
greg
parents:
diff changeset
180 self.assembly_size = 0
0a558f444c98 Uploaded
greg
parents:
diff changeset
181 self.contig_info = None
0a558f444c98 Uploaded
greg
parents:
diff changeset
182 self.feature_hits = pandas.Series(dtype='float64')
26
46edd7435555 Uploaded
greg
parents: 25
diff changeset
183 self.ont_fast5 = None
46edd7435555 Uploaded
greg
parents: 25
diff changeset
184 self.ont_file = ont_file
46edd7435555 Uploaded
greg
parents: 25
diff changeset
185 self.ont_n50 = None
46edd7435555 Uploaded
greg
parents: 25
diff changeset
186 self.ont_read_count = None
14
95b1d1a9497d Uploaded
greg
parents: 13
diff changeset
187 # TODO: should the following be passed as a parameter?
95b1d1a9497d Uploaded
greg
parents: 13
diff changeset
188 self.ont_coverage_min = 30
95b1d1a9497d Uploaded
greg
parents: 13
diff changeset
189 # TODO: should the following be passed as a parameter?
1
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
190 self.ont_n50_min = 2500
26
46edd7435555 Uploaded
greg
parents: 25
diff changeset
191
21
667b253329c6 Uploaded
greg
parents: 20
diff changeset
192 if self.read_type == 'ONT':
667b253329c6 Uploaded
greg
parents: 20
diff changeset
193 self.ont_raw_fastq = self.analysis_name
26
46edd7435555 Uploaded
greg
parents: 25
diff changeset
194 self.ont_bases = 0
46edd7435555 Uploaded
greg
parents: 25
diff changeset
195 self.illumina_bases = None
21
667b253329c6 Uploaded
greg
parents: 20
diff changeset
196 self.illumina_fastq = None
26
46edd7435555 Uploaded
greg
parents: 25
diff changeset
197 self.illumina_length_mean = None
46edd7435555 Uploaded
greg
parents: 25
diff changeset
198 self.illumina_read_count = None
21
667b253329c6 Uploaded
greg
parents: 20
diff changeset
199 else:
26
46edd7435555 Uploaded
greg
parents: 25
diff changeset
200 self.illumina_fastq = self.analysis_name
46edd7435555 Uploaded
greg
parents: 25
diff changeset
201 self.illumina_bases = 0
46edd7435555 Uploaded
greg
parents: 25
diff changeset
202 self.illumina_length_mean = 0
46edd7435555 Uploaded
greg
parents: 25
diff changeset
203 self.illumina_read_count = 0
46edd7435555 Uploaded
greg
parents: 25
diff changeset
204 self.ont_bases = None
21
667b253329c6 Uploaded
greg
parents: 20
diff changeset
205 self.ont_raw_fastq = None
0
0a558f444c98 Uploaded
greg
parents:
diff changeset
206
0a558f444c98 Uploaded
greg
parents:
diff changeset
207 # Actions
0a558f444c98 Uploaded
greg
parents:
diff changeset
208 self.did_guppy_ont_fast5 = False
0a558f444c98 Uploaded
greg
parents:
diff changeset
209 self.did_qcat_ont_fastq = False
21
667b253329c6 Uploaded
greg
parents: 20
diff changeset
210 self.ofh.write("self.read_type: %s\n" % str(self.read_type))
667b253329c6 Uploaded
greg
parents: 20
diff changeset
211 if self.read_type == 'ONT':
26
46edd7435555 Uploaded
greg
parents: 25
diff changeset
212 self.info_ont_fastq(self.ont_file)
21
667b253329c6 Uploaded
greg
parents: 20
diff changeset
213 else:
26
46edd7435555 Uploaded
greg
parents: 25
diff changeset
214 self.info_illumina_fastq([self.illumina_forward_read_file, self.illumina_reverse_read_file])
0
0a558f444c98 Uploaded
greg
parents:
diff changeset
215 self.load_contig_info()
0a558f444c98 Uploaded
greg
parents:
diff changeset
216
0a558f444c98 Uploaded
greg
parents:
diff changeset
217 def run_command(self, command):
0a558f444c98 Uploaded
greg
parents:
diff changeset
218 self.ofh.write("\nXXXXXX In run_command, command:\n%s\n\n" % str(command))
0a558f444c98 Uploaded
greg
parents:
diff changeset
219 try:
0a558f444c98 Uploaded
greg
parents:
diff changeset
220 return re.split('\\n', subprocess.check_output(command, shell=True).decode('utf-8'))
0a558f444c98 Uploaded
greg
parents:
diff changeset
221 except Exception:
0a558f444c98 Uploaded
greg
parents:
diff changeset
222 message = 'Command %s failed: exiting...' % command
0a558f444c98 Uploaded
greg
parents:
diff changeset
223 sys.exit(message)
0a558f444c98 Uploaded
greg
parents:
diff changeset
224
0a558f444c98 Uploaded
greg
parents:
diff changeset
225 def format_kmg(self, number, decimals=0):
0a558f444c98 Uploaded
greg
parents:
diff changeset
226 self.ofh.write("\nXXXXXX In format_kmg, number:\n%s\n" % str(number))
0a558f444c98 Uploaded
greg
parents:
diff changeset
227 self.ofh.write("XXXXXX In format_kmg, decimals:\n%s\n\n" % str(decimals))
0a558f444c98 Uploaded
greg
parents:
diff changeset
228 if number == 0:
0a558f444c98 Uploaded
greg
parents:
diff changeset
229 return '0'
0a558f444c98 Uploaded
greg
parents:
diff changeset
230 magnitude_powers = [10**9, 10**6, 10**3, 1]
0a558f444c98 Uploaded
greg
parents:
diff changeset
231 magnitude_units = ['G', 'M', 'K', '']
0a558f444c98 Uploaded
greg
parents:
diff changeset
232 for i in range(len(magnitude_units)):
0a558f444c98 Uploaded
greg
parents:
diff changeset
233 if number >= magnitude_powers[i]:
0a558f444c98 Uploaded
greg
parents:
diff changeset
234 magnitude_power = magnitude_powers[i]
0a558f444c98 Uploaded
greg
parents:
diff changeset
235 magnitude_unit = magnitude_units[i]
0a558f444c98 Uploaded
greg
parents:
diff changeset
236 return ('{:0.' + str(decimals) + 'f}').format(number / magnitude_power) + magnitude_unit
0a558f444c98 Uploaded
greg
parents:
diff changeset
237
0a558f444c98 Uploaded
greg
parents:
diff changeset
238 def load_contig_info(self):
0a558f444c98 Uploaded
greg
parents:
diff changeset
239 self.contig_info = pandas.Series(dtype=object)
0a558f444c98 Uploaded
greg
parents:
diff changeset
240 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
241 self.contig_info[self.read_type].columns = ['contig', 'size', 'coverage']
26
46edd7435555 Uploaded
greg
parents: 25
diff changeset
242 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()
46edd7435555 Uploaded
greg
parents: 25
diff changeset
243 if mean_coverage <= self.ont_coverage_min:
46edd7435555 Uploaded
greg
parents: 25
diff changeset
244 warning = '%s mean coverage ({:.0f}X) is less than the recommended minimum ({:.0f}X).'.format(mean_coverage, self.ont_coverage_min) % self.read_type
1
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
245 self.assembly_notes = self.assembly_notes.append(pandas.Series(warning))
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
246 # Report if some contigs have low coverage.
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
247 low_coverage = self.contig_info[self.read_type].loc[self.contig_info[self.read_type]['coverage'] < self.ont_coverage_min, :]
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
248 if low_coverage.shape[0] >= 0:
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
249 for contig_i in range(low_coverage.shape[0]):
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
250 warning = '%s coverage of {:s} ({:.0f}X) is less than the recommended minimum ({:.0f}X).'.format(low_coverage.iloc[contig_i, 0], low_coverage.iloc[contig_i, 2], self.ont_coverage_min) % self.read_type
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
251 self.assembly_notes = self.assembly_notes.append(pandas.Series(warning))
26
46edd7435555 Uploaded
greg
parents: 25
diff changeset
252 # See if some contigs have anonymously low coverage.
46edd7435555 Uploaded
greg
parents: 25
diff changeset
253 fold_coverage = self.contig_info[self.read_type]['coverage'] / mean_coverage
1
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
254 low_coverage = self.contig_info[self.read_type].loc[fold_coverage < 1 / 5, :]
8
66c7cdf7e93f Uploaded
greg
parents: 7
diff changeset
255 if low_coverage.shape[0] >= 0:
1
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
256 for contig_i in range(low_coverage.shape[0]):
26
46edd7435555 Uploaded
greg
parents: 25
diff changeset
257 warning = '%s coverage of {:s} ({:.0f}X) is less than 1/5 the mean coverage ({:.0f}X).'.format(low_coverage.iloc[contig_i, 0], low_coverage.iloc[contig_i, 2], mean_coverage) % self.read_type
1
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
258 self.assembly_notes = self.assembly_notes.append(pandas.Series(warning))
0
0a558f444c98 Uploaded
greg
parents:
diff changeset
259
0a558f444c98 Uploaded
greg
parents:
diff changeset
260 def load_fasta(self, fasta):
0a558f444c98 Uploaded
greg
parents:
diff changeset
261 sequence = pandas.Series(dtype=object)
0a558f444c98 Uploaded
greg
parents:
diff changeset
262 for contig in SeqIO.parse(fasta, 'fasta'):
0a558f444c98 Uploaded
greg
parents:
diff changeset
263 sequence[contig.id] = contig
0a558f444c98 Uploaded
greg
parents:
diff changeset
264 return sequence
0a558f444c98 Uploaded
greg
parents:
diff changeset
265
0a558f444c98 Uploaded
greg
parents:
diff changeset
266 def load_assembly(self):
0a558f444c98 Uploaded
greg
parents:
diff changeset
267 self.assembly = self.load_fasta(self.assembly_fasta_file)
0a558f444c98 Uploaded
greg
parents:
diff changeset
268 self.num_assembly_contigs = len(self.assembly)
26
46edd7435555 Uploaded
greg
parents: 25
diff changeset
269 self.assembly_size = self.format_kmg(sum([len(x) for x in self.assembly]), decimals=1)
0
0a558f444c98 Uploaded
greg
parents:
diff changeset
270
26
46edd7435555 Uploaded
greg
parents: 25
diff changeset
271 def info_illumina_fastq(self, illumina_read_files):
0
0a558f444c98 Uploaded
greg
parents:
diff changeset
272 self.ofh.write("\nXXXXXX In info_illumina_fastq\n\n")
0a558f444c98 Uploaded
greg
parents:
diff changeset
273 if self.gzipped:
0a558f444c98 Uploaded
greg
parents:
diff changeset
274 opener = 'gunzip -c'
0a558f444c98 Uploaded
greg
parents:
diff changeset
275 else:
0a558f444c98 Uploaded
greg
parents:
diff changeset
276 opener = 'cat'
26
46edd7435555 Uploaded
greg
parents: 25
diff changeset
277 for fastq_file in illumina_read_files:
46edd7435555 Uploaded
greg
parents: 25
diff changeset
278 command = ' '.join([opener,
46edd7435555 Uploaded
greg
parents: 25
diff changeset
279 fastq_file,
46edd7435555 Uploaded
greg
parents: 25
diff changeset
280 '| awk \'{getline;s += length($1);getline;getline;}END{print s/(NR/4)"\t"(NR/4)"\t"s}\''])
46edd7435555 Uploaded
greg
parents: 25
diff changeset
281 output = self.run_command(command)
46edd7435555 Uploaded
greg
parents: 25
diff changeset
282 self.ofh.write("output:\n%s\n" % str(output))
46edd7435555 Uploaded
greg
parents: 25
diff changeset
283 self.ofh.write("re.split('\\t', self.run_command(command)[0]:\n%s\n" % str(re.split('\\t', self.run_command(command)[0])))
46edd7435555 Uploaded
greg
parents: 25
diff changeset
284 values = []
46edd7435555 Uploaded
greg
parents: 25
diff changeset
285 for i in re.split('\\t', self.run_command(command)[0]):
46edd7435555 Uploaded
greg
parents: 25
diff changeset
286 if i == '':
46edd7435555 Uploaded
greg
parents: 25
diff changeset
287 values.append(float('nan'))
46edd7435555 Uploaded
greg
parents: 25
diff changeset
288 else:
46edd7435555 Uploaded
greg
parents: 25
diff changeset
289 values.append(float(i))
46edd7435555 Uploaded
greg
parents: 25
diff changeset
290 self.ofh.write("values:\n%s\n" % str(values))
46edd7435555 Uploaded
greg
parents: 25
diff changeset
291 self.ofh.write("values[0]:\n%s\n" % str(values[0]))
46edd7435555 Uploaded
greg
parents: 25
diff changeset
292 self.illumina_length_mean += values[0]
46edd7435555 Uploaded
greg
parents: 25
diff changeset
293 self.ofh.write("values[1]:\n%s\n" % str(values[1]))
46edd7435555 Uploaded
greg
parents: 25
diff changeset
294 self.illumina_read_count += int(values[1])
46edd7435555 Uploaded
greg
parents: 25
diff changeset
295 self.ofh.write("values[2]:\n%s\n" % str(values[2]))
46edd7435555 Uploaded
greg
parents: 25
diff changeset
296 self.illumina_bases += int(values[2])
46edd7435555 Uploaded
greg
parents: 25
diff changeset
297 self.illumina_length_mean /= 2
0
0a558f444c98 Uploaded
greg
parents:
diff changeset
298 self.illumina_bases = self.format_kmg(self.illumina_bases, decimals=1)
0a558f444c98 Uploaded
greg
parents:
diff changeset
299
0a558f444c98 Uploaded
greg
parents:
diff changeset
300 def start_doc(self):
13
f03c80bb22e9 Uploaded
greg
parents: 12
diff changeset
301 header_text = 'Analysis of ' + self.analysis_name
f03c80bb22e9 Uploaded
greg
parents: 12
diff changeset
302 self.doc = MdUtils(file_name=self.report_md, title=header_text)
0
0a558f444c98 Uploaded
greg
parents:
diff changeset
303
14
95b1d1a9497d Uploaded
greg
parents: 13
diff changeset
304 def add_table_of_contents(self):
95b1d1a9497d Uploaded
greg
parents: 13
diff changeset
305 self.doc.create_marker(text_marker="TableOfContents")
95b1d1a9497d Uploaded
greg
parents: 13
diff changeset
306 self.doc.new_line()
95b1d1a9497d Uploaded
greg
parents: 13
diff changeset
307 self.doc.new_line('<div style="page-break-after: always;"></div>')
95b1d1a9497d Uploaded
greg
parents: 13
diff changeset
308 self.doc.new_line()
95b1d1a9497d Uploaded
greg
parents: 13
diff changeset
309
0
0a558f444c98 Uploaded
greg
parents:
diff changeset
310 def add_run_information(self):
0a558f444c98 Uploaded
greg
parents:
diff changeset
311 self.ofh.write("\nXXXXXX In add_run_information\n\n")
0a558f444c98 Uploaded
greg
parents:
diff changeset
312 self.doc.new_line()
0a558f444c98 Uploaded
greg
parents:
diff changeset
313 self.doc.new_header(1, 'Run information')
0a558f444c98 Uploaded
greg
parents:
diff changeset
314 # Tables in md.utils are implemented as a wrapping function.
0a558f444c98 Uploaded
greg
parents:
diff changeset
315 Table_list = [
0a558f444c98 Uploaded
greg
parents:
diff changeset
316 "Category",
0a558f444c98 Uploaded
greg
parents:
diff changeset
317 "Information",
0a558f444c98 Uploaded
greg
parents:
diff changeset
318 "Date",
0a558f444c98 Uploaded
greg
parents:
diff changeset
319 date.today(),
0a558f444c98 Uploaded
greg
parents:
diff changeset
320 "ONT FAST5",
14
95b1d1a9497d Uploaded
greg
parents: 13
diff changeset
321 self.wordwrap_markdown(self.ont_fast5),
0
0a558f444c98 Uploaded
greg
parents:
diff changeset
322 "ONT FASTQ",
14
95b1d1a9497d Uploaded
greg
parents: 13
diff changeset
323 self.wordwrap_markdown(self.ont_raw_fastq),
0
0a558f444c98 Uploaded
greg
parents:
diff changeset
324 "Illumina FASTQ",
21
667b253329c6 Uploaded
greg
parents: 20
diff changeset
325 self.wordwrap_markdown(self.illumina_fastq),
0
0a558f444c98 Uploaded
greg
parents:
diff changeset
326 "Assembly",
0a558f444c98 Uploaded
greg
parents:
diff changeset
327 self.wordwrap_markdown(self.assembly_name),
0a558f444c98 Uploaded
greg
parents:
diff changeset
328 "Reference",
0a558f444c98 Uploaded
greg
parents:
diff changeset
329 self.wordwrap_markdown(self.dbkey),
0a558f444c98 Uploaded
greg
parents:
diff changeset
330 ]
0a558f444c98 Uploaded
greg
parents:
diff changeset
331 self.doc.new_table(columns=2, rows=7, text=Table_list, text_align='left')
0a558f444c98 Uploaded
greg
parents:
diff changeset
332 self.doc.new_line()
14
95b1d1a9497d Uploaded
greg
parents: 13
diff changeset
333 # FIXME: the following doesn't work.
95b1d1a9497d Uploaded
greg
parents: 13
diff changeset
334 # self.add_table_of_contents()
0
0a558f444c98 Uploaded
greg
parents:
diff changeset
335 self.doc.new_line()
0a558f444c98 Uploaded
greg
parents:
diff changeset
336
0a558f444c98 Uploaded
greg
parents:
diff changeset
337 def add_ont_library_information(self):
0a558f444c98 Uploaded
greg
parents:
diff changeset
338 self.ofh.write("\nXXXXXX In add_ont_library_information\n\n")
0a558f444c98 Uploaded
greg
parents:
diff changeset
339 if self.ont_n50 is None:
0a558f444c98 Uploaded
greg
parents:
diff changeset
340 return
0a558f444c98 Uploaded
greg
parents:
diff changeset
341 self.doc.new_line()
0a558f444c98 Uploaded
greg
parents:
diff changeset
342 self.doc.new_header(2, 'ONT library statistics')
0a558f444c98 Uploaded
greg
parents:
diff changeset
343 Table_List = [
0a558f444c98 Uploaded
greg
parents:
diff changeset
344 "Category",
0a558f444c98 Uploaded
greg
parents:
diff changeset
345 "Quantity",
0a558f444c98 Uploaded
greg
parents:
diff changeset
346 "ONT N50",
0a558f444c98 Uploaded
greg
parents:
diff changeset
347 '{:,}'.format(self.ont_n50),
0a558f444c98 Uploaded
greg
parents:
diff changeset
348 "ONT reads",
0a558f444c98 Uploaded
greg
parents:
diff changeset
349 '{:,}'.format(self.ont_read_count),
0a558f444c98 Uploaded
greg
parents:
diff changeset
350 "ONT bases",
0a558f444c98 Uploaded
greg
parents:
diff changeset
351 '{:s}'.format(self.ont_bases),
0a558f444c98 Uploaded
greg
parents:
diff changeset
352 "Illumina FASTQ",
17
b4ed9f55de13 Uploaded
greg
parents: 16
diff changeset
353 "N/A",
0
0a558f444c98 Uploaded
greg
parents:
diff changeset
354 "Assembly",
0a558f444c98 Uploaded
greg
parents:
diff changeset
355 self.wordwrap_markdown(self.assembly_name),
0a558f444c98 Uploaded
greg
parents:
diff changeset
356 "Reference",
0a558f444c98 Uploaded
greg
parents:
diff changeset
357 self.wordwrap_markdown(self.dbkey),
0a558f444c98 Uploaded
greg
parents:
diff changeset
358 ]
0a558f444c98 Uploaded
greg
parents:
diff changeset
359 self.doc.new_table(columns=2, rows=7, text=Table_List, text_align='left')
0a558f444c98 Uploaded
greg
parents:
diff changeset
360 self.doc.new_line()
0a558f444c98 Uploaded
greg
parents:
diff changeset
361
0a558f444c98 Uploaded
greg
parents:
diff changeset
362 def add_illumina_library_information(self):
0a558f444c98 Uploaded
greg
parents:
diff changeset
363 self.ofh.write("\nXXXXXX In add_illumina_library_information\n\n")
14
95b1d1a9497d Uploaded
greg
parents: 13
diff changeset
364 if self.illumina_length_mean is None:
0
0a558f444c98 Uploaded
greg
parents:
diff changeset
365 return
0a558f444c98 Uploaded
greg
parents:
diff changeset
366 self.doc.new_line()
0a558f444c98 Uploaded
greg
parents:
diff changeset
367 self.doc.new_header(2, 'Illumina library statistics')
0a558f444c98 Uploaded
greg
parents:
diff changeset
368 Table_List = [
0a558f444c98 Uploaded
greg
parents:
diff changeset
369 "Illumina Info.",
0a558f444c98 Uploaded
greg
parents:
diff changeset
370 "Quantity",
0a558f444c98 Uploaded
greg
parents:
diff changeset
371 'Illumina mean length',
0a558f444c98 Uploaded
greg
parents:
diff changeset
372 '{:.1f}'.format(self.illumina_length_mean),
0a558f444c98 Uploaded
greg
parents:
diff changeset
373 'Illumina reads',
0a558f444c98 Uploaded
greg
parents:
diff changeset
374 '{:,}'.format(self.illumina_read_count),
0a558f444c98 Uploaded
greg
parents:
diff changeset
375 'Illumina bases',
0a558f444c98 Uploaded
greg
parents:
diff changeset
376 '{:s}'.format(self.illumina_bases)
0a558f444c98 Uploaded
greg
parents:
diff changeset
377 ]
0a558f444c98 Uploaded
greg
parents:
diff changeset
378 self.doc.new_table(columns=2, rows=4, text=Table_List, text_align='left')
0a558f444c98 Uploaded
greg
parents:
diff changeset
379
8
66c7cdf7e93f Uploaded
greg
parents: 7
diff changeset
380 def evaluate_assembly(self):
1
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
381 assembly_info = pandas.read_csv(self.compute_sequence_length_file, sep='\t', header=None)
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
382 assembly_info.columns = ['contig', 'length']
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
383 self.contig_sizes = assembly_info
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
384 # Take a look at the number of contigs, their sizes,
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
385 # and circularity. Warn if things don't look good.
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
386 if assembly_info.shape[0] > 4:
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
387 warning = 'Assembly produced {:d} contigs, more than ususally expected; assembly may be fragmented'.format(assembly_info.shape[0])
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
388 self.assembly_notes = self.assembly_notes.append(pandas.Series(warning))
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
389 small_contigs = assembly_info.loc[assembly_info['length'] <= 3000, :]
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
390 if small_contigs.shape[0] > 0:
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
391 warning = 'Assembly produced {:d} small contigs ({:s}); assembly may include spurious sequences.'.format(small_contigs.shape[0], ', '.join(small_contigs['contig']))
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
392 self.assembly_notes = self.assembly_notes.append(pandas.Series(warning))
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
393
0
0a558f444c98 Uploaded
greg
parents:
diff changeset
394 def add_assembly_information(self):
0a558f444c98 Uploaded
greg
parents:
diff changeset
395 self.ofh.write("\nXXXXXX In add_assembly_information\n\n")
0a558f444c98 Uploaded
greg
parents:
diff changeset
396 if self.assembly_fasta_file is None:
0a558f444c98 Uploaded
greg
parents:
diff changeset
397 return
0a558f444c98 Uploaded
greg
parents:
diff changeset
398 self.load_assembly()
0a558f444c98 Uploaded
greg
parents:
diff changeset
399 self.doc.new_line()
0a558f444c98 Uploaded
greg
parents:
diff changeset
400 self.doc.new_header(2, 'Assembly statistics')
0a558f444c98 Uploaded
greg
parents:
diff changeset
401 Table_List = [
0a558f444c98 Uploaded
greg
parents:
diff changeset
402 "Category",
0a558f444c98 Uploaded
greg
parents:
diff changeset
403 "Information",
0a558f444c98 Uploaded
greg
parents:
diff changeset
404 "Contigs",
0a558f444c98 Uploaded
greg
parents:
diff changeset
405 str(self.num_assembly_contigs),
0a558f444c98 Uploaded
greg
parents:
diff changeset
406 "Assembly size",
0a558f444c98 Uploaded
greg
parents:
diff changeset
407 str(self.assembly_size),
0a558f444c98 Uploaded
greg
parents:
diff changeset
408 ]
0a558f444c98 Uploaded
greg
parents:
diff changeset
409 self.doc.new_table(columns=2, rows=3, text=Table_List, text_align='left')
0a558f444c98 Uploaded
greg
parents:
diff changeset
410
0a558f444c98 Uploaded
greg
parents:
diff changeset
411 def info_ont_fastq(self, fastq_file):
0a558f444c98 Uploaded
greg
parents:
diff changeset
412 self.ofh.write("\nXXXXXX In info_ont_fastq, fastq_file:\n%s\n\n" % str(fastq_file))
0a558f444c98 Uploaded
greg
parents:
diff changeset
413 opener = 'cat'
0a558f444c98 Uploaded
greg
parents:
diff changeset
414 if self.gzipped:
0a558f444c98 Uploaded
greg
parents:
diff changeset
415 opener = 'gunzip -c'
0a558f444c98 Uploaded
greg
parents:
diff changeset
416 else:
0a558f444c98 Uploaded
greg
parents:
diff changeset
417 opener = 'cat'
0a558f444c98 Uploaded
greg
parents:
diff changeset
418 command = ' '.join([opener,
0a558f444c98 Uploaded
greg
parents:
diff changeset
419 fastq_file,
0a558f444c98 Uploaded
greg
parents:
diff changeset
420 '| awk \'{getline;print length($0);s += length($1);getline;getline;}END{print "+"s}\'',
0a558f444c98 Uploaded
greg
parents:
diff changeset
421 '| sort -gr',
0a558f444c98 Uploaded
greg
parents:
diff changeset
422 '| awk \'BEGIN{bp = 0;f = 0}',
0a558f444c98 Uploaded
greg
parents:
diff changeset
423 '{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
424 'END{printf "%d\\t%d\\t%d\\n", n50, (NR - 1), s;exit}\''])
0a558f444c98 Uploaded
greg
parents:
diff changeset
425 result = list(re.split('\\t', self.run_command(command)[0]))
0a558f444c98 Uploaded
greg
parents:
diff changeset
426 if result[1] == '0':
21
667b253329c6 Uploaded
greg
parents: 20
diff changeset
427 warning = 'No ONT reads found'
667b253329c6 Uploaded
greg
parents: 20
diff changeset
428 self.assembly_notes = self.assembly_notes.append(pandas.Series(warning))
14
95b1d1a9497d Uploaded
greg
parents: 13
diff changeset
429 self.ont_n50, self.ont_read_count, ont_raw_bases = [int(i) for i in result]
0
0a558f444c98 Uploaded
greg
parents:
diff changeset
430 command = ' '.join([opener,
0a558f444c98 Uploaded
greg
parents:
diff changeset
431 fastq_file,
0a558f444c98 Uploaded
greg
parents:
diff changeset
432 '| awk \'{getline;print length($0);getline;getline;}\''])
0a558f444c98 Uploaded
greg
parents:
diff changeset
433 result = self.run_command(command)
0a558f444c98 Uploaded
greg
parents:
diff changeset
434 result = list(filter(lambda x: x != '', result))
14
95b1d1a9497d Uploaded
greg
parents: 13
diff changeset
435 self.ont_bases = self.format_kmg(ont_raw_bases, decimals=1)
95b1d1a9497d Uploaded
greg
parents: 13
diff changeset
436 if self.ont_n50 <= self.ont_n50_min:
95b1d1a9497d Uploaded
greg
parents: 13
diff changeset
437 warning = 'ONT N50 (%s) is less than the recommended minimum (%s)' % (str(self.ont_n50), str(self.ont_n50_min))
1
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
438 self.assembly_notes = self.assembly_notes.append(pandas.Series(warning))
0
0a558f444c98 Uploaded
greg
parents:
diff changeset
439
0a558f444c98 Uploaded
greg
parents:
diff changeset
440 def wordwrap_markdown(self, string):
0a558f444c98 Uploaded
greg
parents:
diff changeset
441 if string:
0a558f444c98 Uploaded
greg
parents:
diff changeset
442 if len(string) < 35:
0a558f444c98 Uploaded
greg
parents:
diff changeset
443 return string
0a558f444c98 Uploaded
greg
parents:
diff changeset
444 else:
0a558f444c98 Uploaded
greg
parents:
diff changeset
445 if '/' in string:
0a558f444c98 Uploaded
greg
parents:
diff changeset
446 adjust = string.split('/')
0a558f444c98 Uploaded
greg
parents:
diff changeset
447 out = ''
0a558f444c98 Uploaded
greg
parents:
diff changeset
448 max = 35
0a558f444c98 Uploaded
greg
parents:
diff changeset
449 for i in adjust:
0a558f444c98 Uploaded
greg
parents:
diff changeset
450 out = out + '/' + i
0a558f444c98 Uploaded
greg
parents:
diff changeset
451 if len(out) > max:
0a558f444c98 Uploaded
greg
parents:
diff changeset
452 out += '<br>'
0a558f444c98 Uploaded
greg
parents:
diff changeset
453 max += 35
0a558f444c98 Uploaded
greg
parents:
diff changeset
454 return out
0a558f444c98 Uploaded
greg
parents:
diff changeset
455 else:
0a558f444c98 Uploaded
greg
parents:
diff changeset
456 out = [string[i:i + 35] for i in range(0, len(string), 50)]
0a558f444c98 Uploaded
greg
parents:
diff changeset
457 return '<br>'.join(out)
0a558f444c98 Uploaded
greg
parents:
diff changeset
458 else:
0a558f444c98 Uploaded
greg
parents:
diff changeset
459 return string
0a558f444c98 Uploaded
greg
parents:
diff changeset
460
0a558f444c98 Uploaded
greg
parents:
diff changeset
461 def add_contig_info(self):
0a558f444c98 Uploaded
greg
parents:
diff changeset
462 self.ofh.write("\nXXXXXX In add_contig_info\n\n")
26
46edd7435555 Uploaded
greg
parents: 25
diff changeset
463 if self.contig_info is None or self.read_type not in self.contig_info.index:
0
0a558f444c98 Uploaded
greg
parents:
diff changeset
464 return
26
46edd7435555 Uploaded
greg
parents: 25
diff changeset
465 self.doc.new_line()
46edd7435555 Uploaded
greg
parents: 25
diff changeset
466 self.doc.new_header(2, 'Assembly coverage by ' + self.read_type)
46edd7435555 Uploaded
greg
parents: 25
diff changeset
467 Table_List = ["Contig", "Length (bp)", "Coverage (X)"]
46edd7435555 Uploaded
greg
parents: 25
diff changeset
468 formatted = self.contig_info[self.read_type].copy()
46edd7435555 Uploaded
greg
parents: 25
diff changeset
469 formatted.iloc[:, 1] = formatted.iloc[:, 1].apply(lambda x: '{:,}'.format(x))
46edd7435555 Uploaded
greg
parents: 25
diff changeset
470 for i in range(self.contig_info[self.read_type].shape[0]):
46edd7435555 Uploaded
greg
parents: 25
diff changeset
471 Table_List = Table_List + formatted.iloc[i, :].values.tolist()
46edd7435555 Uploaded
greg
parents: 25
diff changeset
472 row_count = int(len(Table_List) / 3)
46edd7435555 Uploaded
greg
parents: 25
diff changeset
473 self.doc.new_table(columns=3, rows=row_count, text=Table_List, text_align='left')
0
0a558f444c98 Uploaded
greg
parents:
diff changeset
474
0a558f444c98 Uploaded
greg
parents:
diff changeset
475 def add_assembly_notes(self):
0a558f444c98 Uploaded
greg
parents:
diff changeset
476 self.ofh.write("\nXXXXXX In add_assembly_notes\n\n")
0a558f444c98 Uploaded
greg
parents:
diff changeset
477 if len(self.assembly_notes) == 0:
0a558f444c98 Uploaded
greg
parents:
diff changeset
478 return
0a558f444c98 Uploaded
greg
parents:
diff changeset
479 self.doc.new_line()
0a558f444c98 Uploaded
greg
parents:
diff changeset
480 self.doc.new_line('<div style="page-break-after: always;"></div>')
0a558f444c98 Uploaded
greg
parents:
diff changeset
481 self.doc.new_line()
0a558f444c98 Uploaded
greg
parents:
diff changeset
482 self.doc.new_header(2, self.assembly_notes_title)
1
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
483 for note in self.assembly_notes:
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
484 self.doc.new_line(note)
0
0a558f444c98 Uploaded
greg
parents:
diff changeset
485
0a558f444c98 Uploaded
greg
parents:
diff changeset
486 def add_contamination(self):
0a558f444c98 Uploaded
greg
parents:
diff changeset
487 self.ofh.write("\nXXXXXX In add_contamination\n\n")
2
9cb62054a87a Uploaded
greg
parents: 1
diff changeset
488 if self.kraken2_report_file is None:
0
0a558f444c98 Uploaded
greg
parents:
diff changeset
489 return
2
9cb62054a87a Uploaded
greg
parents: 1
diff changeset
490 # Read in the Kraken fractions and pull out the useful parts
8
66c7cdf7e93f Uploaded
greg
parents: 7
diff changeset
491 kraken_fracs = pandas.read_csv(self.kraken2_report_file, delimiter='\t', header=None)
66c7cdf7e93f Uploaded
greg
parents: 7
diff changeset
492 kraken_fracs.index = kraken_fracs.iloc[:, 4].values
66c7cdf7e93f Uploaded
greg
parents: 7
diff changeset
493 kraken_fracs = kraken_fracs.loc[kraken_fracs.iloc[:, 3].str.match('[UG]1?'), :]
66c7cdf7e93f Uploaded
greg
parents: 7
diff changeset
494 kraken_fracs = kraken_fracs.loc[(kraken_fracs.iloc[:, 0] >= 1) | (kraken_fracs.iloc[:, 3] == 'U'), :]
66c7cdf7e93f Uploaded
greg
parents: 7
diff changeset
495 kraken_fracs = kraken_fracs.iloc[:, [0, 1, 3, 5]]
66c7cdf7e93f Uploaded
greg
parents: 7
diff changeset
496 kraken_fracs.columns = ['Fraction', 'Reads', 'Level', 'Taxa']
66c7cdf7e93f Uploaded
greg
parents: 7
diff changeset
497 kraken_fracs['Fraction'] = (kraken_fracs['Fraction'] / 100).round(4)
66c7cdf7e93f Uploaded
greg
parents: 7
diff changeset
498 kraken_fracs.sort_values(by='Fraction', inplace=True, ascending=False)
66c7cdf7e93f Uploaded
greg
parents: 7
diff changeset
499 kraken_fracs['Taxa'] = kraken_fracs['Taxa'].str.lstrip()
0
0a558f444c98 Uploaded
greg
parents:
diff changeset
500 self.doc.new_line()
0a558f444c98 Uploaded
greg
parents:
diff changeset
501 self.doc.new_header(2, 'Contamination check')
10
7c4077c9b3ad Uploaded
greg
parents: 9
diff changeset
502 self.doc.new_line(self.read_type + ' classifications')
7c4077c9b3ad Uploaded
greg
parents: 9
diff changeset
503 self.doc.new_line()
7c4077c9b3ad Uploaded
greg
parents: 9
diff changeset
504 Table_List = ["Percent of Reads", "Reads", "Level", "Label"]
7c4077c9b3ad Uploaded
greg
parents: 9
diff changeset
505 for index, row in kraken_fracs.iterrows():
7c4077c9b3ad Uploaded
greg
parents: 9
diff changeset
506 Table_List = Table_List + row.tolist()
7c4077c9b3ad Uploaded
greg
parents: 9
diff changeset
507 row_count = int(len(Table_List) / 4)
7c4077c9b3ad Uploaded
greg
parents: 9
diff changeset
508 self.doc.new_table(columns=4, rows=row_count, text=Table_List, text_align='left')
7c4077c9b3ad Uploaded
greg
parents: 9
diff changeset
509 if self.contamination_methods_title not in self.methods:
7c4077c9b3ad Uploaded
greg
parents: 9
diff changeset
510 self.methods[self.contamination_methods_title] = ''
11
4a7c31f28197 Uploaded
greg
parents: 10
diff changeset
511 method = '%s was used to assign the raw reads into taxa.' % self.kraken2_version.rstrip('report')
0
0a558f444c98 Uploaded
greg
parents:
diff changeset
512 self.methods[self.contamination_methods_title] = self.methods[self.contamination_methods_title].append(pandas.Series(method))
0a558f444c98 Uploaded
greg
parents:
diff changeset
513
0a558f444c98 Uploaded
greg
parents:
diff changeset
514 def add_alignment(self):
0a558f444c98 Uploaded
greg
parents:
diff changeset
515 self.ofh.write("\nXXXXXX In add_alignment\n\n")
13
f03c80bb22e9 Uploaded
greg
parents: 12
diff changeset
516 if self.quast_report_file is not None:
f03c80bb22e9 Uploaded
greg
parents: 12
diff changeset
517 # Process quast values.
f03c80bb22e9 Uploaded
greg
parents: 12
diff changeset
518 quast_report = pandas.read_csv(self.quast_report_file, header=0, index_col=0, sep='\t')
f03c80bb22e9 Uploaded
greg
parents: 12
diff changeset
519 quast_mismatches = int(float(quast_report.loc['# mismatches per 100 kbp', :][0]) * (float(quast_report.loc['Total length (>= 0 bp)', :][0]) / 100000.))
f03c80bb22e9 Uploaded
greg
parents: 12
diff changeset
520 quast_indels = int(float(quast_report.loc['# indels per 100 kbp', :][0]) * (float(quast_report.loc['Total length (>= 0 bp)', :][0]) / 100000.))
f03c80bb22e9 Uploaded
greg
parents: 12
diff changeset
521 self.doc.new_line()
f03c80bb22e9 Uploaded
greg
parents: 12
diff changeset
522 self.doc.new_header(level=2, title=self.alignment_title)
f03c80bb22e9 Uploaded
greg
parents: 12
diff changeset
523 self.doc.new_line()
f03c80bb22e9 Uploaded
greg
parents: 12
diff changeset
524 self.doc.new_header(level=3, title=self.snp_indel_title)
f03c80bb22e9 Uploaded
greg
parents: 12
diff changeset
525 Table_1 = [
f03c80bb22e9 Uploaded
greg
parents: 12
diff changeset
526 "Category",
f03c80bb22e9 Uploaded
greg
parents: 12
diff changeset
527 "Quantity",
f03c80bb22e9 Uploaded
greg
parents: 12
diff changeset
528 'SNPs',
f03c80bb22e9 Uploaded
greg
parents: 12
diff changeset
529 '{:,}'.format(quast_mismatches),
f03c80bb22e9 Uploaded
greg
parents: 12
diff changeset
530 'Small indels',
f03c80bb22e9 Uploaded
greg
parents: 12
diff changeset
531 '{:,}'.format(quast_indels)
f03c80bb22e9 Uploaded
greg
parents: 12
diff changeset
532 ]
0
0a558f444c98 Uploaded
greg
parents:
diff changeset
533 self.doc.new_table(columns=2, rows=3, text=Table_1, text_align='left')
0a558f444c98 Uploaded
greg
parents:
diff changeset
534 self.doc.new_line('<div style="page-break-after: always;"></div>')
0a558f444c98 Uploaded
greg
parents:
diff changeset
535 self.doc.new_line()
13
f03c80bb22e9 Uploaded
greg
parents: 12
diff changeset
536 # TODO: self.alignment_notes is not currently populated.
0
0a558f444c98 Uploaded
greg
parents:
diff changeset
537 if len(self.alignment_notes) > 0:
0a558f444c98 Uploaded
greg
parents:
diff changeset
538 self.doc.new_header(level=3, title=self.alignment_notes_title)
0a558f444c98 Uploaded
greg
parents:
diff changeset
539 for note in self.alignment_notes:
0a558f444c98 Uploaded
greg
parents:
diff changeset
540 self.doc.new_line(note)
13
f03c80bb22e9 Uploaded
greg
parents: 12
diff changeset
541 if len(self.circos_files) > 0:
f03c80bb22e9 Uploaded
greg
parents: 12
diff changeset
542 # Add circos PNG files.
f03c80bb22e9 Uploaded
greg
parents: 12
diff changeset
543 for circos_file in self.circos_files:
f03c80bb22e9 Uploaded
greg
parents: 12
diff changeset
544 contig = os.path.basename(circos_file)
f03c80bb22e9 Uploaded
greg
parents: 12
diff changeset
545 contig_title = 'Alignment to %s' % contig
f03c80bb22e9 Uploaded
greg
parents: 12
diff changeset
546 self.doc.new_line()
f03c80bb22e9 Uploaded
greg
parents: 12
diff changeset
547 self.doc.new_header(level=3, title=contig_title)
25
4986a7fb2145 Uploaded
greg
parents: 24
diff changeset
548 self.doc.new_line('Blue color indicates query sequences aligned to the reference sequence, which is shown in yellow')
13
f03c80bb22e9 Uploaded
greg
parents: 12
diff changeset
549 self.doc.new_line(self.doc.new_inline_image(text='contig_title', path=os.path.abspath(circos_file)))
f03c80bb22e9 Uploaded
greg
parents: 12
diff changeset
550 self.doc.new_line('<div style="page-break-after: always;"></div>')
f03c80bb22e9 Uploaded
greg
parents: 12
diff changeset
551 self.doc.new_line()
21
667b253329c6 Uploaded
greg
parents: 20
diff changeset
552 if self.dbkey == 'ref_genome':
667b253329c6 Uploaded
greg
parents: 20
diff changeset
553 headers = ["* Chromosome - NC_007530.2 Bacillus anthracis str. 'Ames Ancestor', complete sequence",
667b253329c6 Uploaded
greg
parents: 20
diff changeset
554 "* pXO1 - NC_007322.2 Bacillus anthracis str. 'Ames Ancestor' plasmid pXO1, complete sequence",
667b253329c6 Uploaded
greg
parents: 20
diff changeset
555 "* pXO2 - NC_007323.3 Bacillus anthracis str. 'Ames Ancestor' plasmid pXO2, complete sequence"]
667b253329c6 Uploaded
greg
parents: 20
diff changeset
556 method = '\n'.join(headers)
667b253329c6 Uploaded
greg
parents: 20
diff changeset
557 self.methods[self.reference_genome_title] = self.methods[self.reference_genome_title].append(pandas.Series(method))
667b253329c6 Uploaded
greg
parents: 20
diff changeset
558 method = 'The genome assembly was aligned against the reference sequence using %s.' % self.dnadiff_version
0
0a558f444c98 Uploaded
greg
parents:
diff changeset
559 self.methods[self.reference_methods_title] = self.methods[self.reference_methods_title].append(pandas.Series(method))
0a558f444c98 Uploaded
greg
parents:
diff changeset
560
0a558f444c98 Uploaded
greg
parents:
diff changeset
561 def add_features(self):
0a558f444c98 Uploaded
greg
parents:
diff changeset
562 self.ofh.write("\nXXXXXX In add_features\n\n")
0a558f444c98 Uploaded
greg
parents:
diff changeset
563 if len(self.feature_bed_files) == 0:
0a558f444c98 Uploaded
greg
parents:
diff changeset
564 return
0a558f444c98 Uploaded
greg
parents:
diff changeset
565 for bbf in self.feature_bed_files:
0a558f444c98 Uploaded
greg
parents:
diff changeset
566 if os.path.getsize(bbf) > 0:
0a558f444c98 Uploaded
greg
parents:
diff changeset
567 best = pandas.read_csv(filepath_or_buffer=bbf, sep='\t', header=None)
0a558f444c98 Uploaded
greg
parents:
diff changeset
568 self.feature_hits[os.path.basename(bbf)] = best
0a558f444c98 Uploaded
greg
parents:
diff changeset
569 if len(self.feature_hits) == 0:
0a558f444c98 Uploaded
greg
parents:
diff changeset
570 return
0a558f444c98 Uploaded
greg
parents:
diff changeset
571 self.ofh.write("self.feature_hits: %s\n" % str(self.feature_hits))
0a558f444c98 Uploaded
greg
parents:
diff changeset
572 self.doc.new_line()
0a558f444c98 Uploaded
greg
parents:
diff changeset
573 self.doc.new_header(level=2, title=self.feature_title)
0a558f444c98 Uploaded
greg
parents:
diff changeset
574 for feature_name in self.feature_hits.index.tolist():
0a558f444c98 Uploaded
greg
parents:
diff changeset
575 self.ofh.write("feature_name: %s\n" % str(feature_name))
0a558f444c98 Uploaded
greg
parents:
diff changeset
576 features = self.feature_hits[feature_name].copy()
0a558f444c98 Uploaded
greg
parents:
diff changeset
577 self.ofh.write("features: %s\n" % str(features))
0a558f444c98 Uploaded
greg
parents:
diff changeset
578 if features.shape[0] == 0:
0a558f444c98 Uploaded
greg
parents:
diff changeset
579 continue
0a558f444c98 Uploaded
greg
parents:
diff changeset
580 features.iloc[:, 1] = features.iloc[:, 1].apply(lambda x: '{:,}'.format(x))
0a558f444c98 Uploaded
greg
parents:
diff changeset
581 features.iloc[:, 2] = features.iloc[:, 2].apply(lambda x: '{:,}'.format(x))
0a558f444c98 Uploaded
greg
parents:
diff changeset
582 self.doc.new_line()
0a558f444c98 Uploaded
greg
parents:
diff changeset
583 self.doc.new_header(level=3, title=feature_name)
0a558f444c98 Uploaded
greg
parents:
diff changeset
584 if (features.shape[0] == 0):
0a558f444c98 Uploaded
greg
parents:
diff changeset
585 continue
0a558f444c98 Uploaded
greg
parents:
diff changeset
586 for contig in pandas.unique(features.iloc[:, 0]):
0a558f444c98 Uploaded
greg
parents:
diff changeset
587 self.ofh.write("contig: %s\n" % str(contig))
0a558f444c98 Uploaded
greg
parents:
diff changeset
588 self.doc.new_line(contig)
0a558f444c98 Uploaded
greg
parents:
diff changeset
589 contig_features = features.loc[(features.iloc[:, 0] == contig), :]
0a558f444c98 Uploaded
greg
parents:
diff changeset
590 self.ofh.write("contig_features: %s\n" % str(contig_features))
0a558f444c98 Uploaded
greg
parents:
diff changeset
591 Table_List = ['Start', 'Stop', 'Feature', 'Identity (%)', 'Strand']
0a558f444c98 Uploaded
greg
parents:
diff changeset
592 for i in range(contig_features.shape[0]):
0a558f444c98 Uploaded
greg
parents:
diff changeset
593 self.ofh.write("i: %s\n" % str(i))
0a558f444c98 Uploaded
greg
parents:
diff changeset
594 feature = contig_features.iloc[i, :].copy(deep=True)
0a558f444c98 Uploaded
greg
parents:
diff changeset
595 self.ofh.write("feature: %s\n" % str(feature))
0a558f444c98 Uploaded
greg
parents:
diff changeset
596 feature[4] = '{:.3f}'.format(feature[4])
2
9cb62054a87a Uploaded
greg
parents: 1
diff changeset
597 self.ofh.write("feature[1:].values.tolist(): %s\n" % str(feature[1:].values.tolist()))
9cb62054a87a Uploaded
greg
parents: 1
diff changeset
598 Table_List = Table_List + feature[1:].values.tolist()
0
0a558f444c98 Uploaded
greg
parents:
diff changeset
599 self.ofh.write("Table_List: %s\n" % str(Table_List))
0a558f444c98 Uploaded
greg
parents:
diff changeset
600 row_count = int(len(Table_List) / 5)
0a558f444c98 Uploaded
greg
parents:
diff changeset
601 self.ofh.write("row_count: %s\n" % str(row_count))
0a558f444c98 Uploaded
greg
parents:
diff changeset
602 self.doc.new_line()
1
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
603 self.ofh.write("Before new_table, len(Table_List):: %s\n" % str(len(Table_List)))
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
604 self.doc.new_table(columns=5, rows=row_count, text=Table_List, text_align='left')
12
99613333fd1f Uploaded
greg
parents: 11
diff changeset
605 blastn_version = 'The genome assembly was queried for features using %s.' % self.blastn_version
99613333fd1f Uploaded
greg
parents: 11
diff changeset
606 bedtools_version = 'Feature hits were clustered using %s and the highest scoring hit for each cluster was reported.' % self.bedtools_version
0
0a558f444c98 Uploaded
greg
parents:
diff changeset
607 method = '%s %s' % (blastn_version, bedtools_version)
0a558f444c98 Uploaded
greg
parents:
diff changeset
608 self.methods[self.feature_methods_title] = self.methods[self.feature_methods_title].append(pandas.Series(method))
0a558f444c98 Uploaded
greg
parents:
diff changeset
609
0a558f444c98 Uploaded
greg
parents:
diff changeset
610 def add_feature_plots(self):
0a558f444c98 Uploaded
greg
parents:
diff changeset
611 self.ofh.write("\nXXXXXX In add_feature_plots\n\n")
0a558f444c98 Uploaded
greg
parents:
diff changeset
612 if len(self.feature_png_files) == 0:
0a558f444c98 Uploaded
greg
parents:
diff changeset
613 return
0a558f444c98 Uploaded
greg
parents:
diff changeset
614 self.doc.new_line()
0a558f444c98 Uploaded
greg
parents:
diff changeset
615 self.doc.new_header(level=2, title='Feature Plots')
0a558f444c98 Uploaded
greg
parents:
diff changeset
616 self.doc.new_paragraph('Only contigs with features are shown')
0a558f444c98 Uploaded
greg
parents:
diff changeset
617 for feature_png_file in self.feature_png_files:
0a558f444c98 Uploaded
greg
parents:
diff changeset
618 self.doc.new_line(self.doc.new_inline_image(text='Analysis', path=os.path.abspath(feature_png_file)))
0a558f444c98 Uploaded
greg
parents:
diff changeset
619
0a558f444c98 Uploaded
greg
parents:
diff changeset
620 def add_mutations(self):
0a558f444c98 Uploaded
greg
parents:
diff changeset
621 self.ofh.write("\nXXXXXX In add_mutations\n\n")
0a558f444c98 Uploaded
greg
parents:
diff changeset
622 if len(self.mutation_regions_tsv_files) == 0:
0a558f444c98 Uploaded
greg
parents:
diff changeset
623 return
8
66c7cdf7e93f Uploaded
greg
parents: 7
diff changeset
624 try:
0
0a558f444c98 Uploaded
greg
parents:
diff changeset
625 mutation_regions = pandas.read_csv(self.mutation_regions_bed_file, sep='\t', header=0, index_col=False)
0a558f444c98 Uploaded
greg
parents:
diff changeset
626 except Exception:
0a558f444c98 Uploaded
greg
parents:
diff changeset
627 # Likely an empty file.
0a558f444c98 Uploaded
greg
parents:
diff changeset
628 return
0a558f444c98 Uploaded
greg
parents:
diff changeset
629 amr_mutations = pandas.Series(dtype=object)
0a558f444c98 Uploaded
greg
parents:
diff changeset
630 for region_i in range(mutation_regions.shape[0]):
0a558f444c98 Uploaded
greg
parents:
diff changeset
631 region = mutation_regions.iloc[region_i, :]
0a558f444c98 Uploaded
greg
parents:
diff changeset
632 region_name = str(region['name'])
0a558f444c98 Uploaded
greg
parents:
diff changeset
633 self.ofh.write("Processing mutations for region %s\n" % region_name)
0a558f444c98 Uploaded
greg
parents:
diff changeset
634 region_mutations_tsv_name = '%s_mutations.tsv' % region_name
0a558f444c98 Uploaded
greg
parents:
diff changeset
635 if region_mutations_tsv_name not in self.mutation_regions_tsv_files:
0a558f444c98 Uploaded
greg
parents:
diff changeset
636 continue
0a558f444c98 Uploaded
greg
parents:
diff changeset
637 region_mutations_tsv = self.mutation_regions_tsv_files[region_mutations_tsv_name]
8
66c7cdf7e93f Uploaded
greg
parents: 7
diff changeset
638 try:
0
0a558f444c98 Uploaded
greg
parents:
diff changeset
639 region_mutations = pandas.read_csv(region_mutations_tsv, sep='\t', header=0, index_col=False)
0a558f444c98 Uploaded
greg
parents:
diff changeset
640 except Exception:
0a558f444c98 Uploaded
greg
parents:
diff changeset
641 region_mutations = pandas.DataFrame()
0a558f444c98 Uploaded
greg
parents:
diff changeset
642 if region_mutations.shape[0] == 0:
0a558f444c98 Uploaded
greg
parents:
diff changeset
643 continue
1
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
644 # Figure out what kind of mutations are in this region.
0
0a558f444c98 Uploaded
greg
parents:
diff changeset
645 region_mutation_types = pandas.Series(['snp'] * region_mutations.shape[0], name='TYPE', index=region_mutations.index)
0a558f444c98 Uploaded
greg
parents:
diff changeset
646 region_mutation_types[region_mutations['REF'].str.len() != region_mutations['ALT'].str.len()] = 'small-indel'
0a558f444c98 Uploaded
greg
parents:
diff changeset
647 region_mutation_drugs = pandas.Series(region['drug'] * region_mutations.shape[0], name='DRUG', index=region_mutations.index)
0a558f444c98 Uploaded
greg
parents:
diff changeset
648 region_notes = pandas.Series(region['note'] * region_mutations.shape[0], name='NOTE', index=region_mutations.index)
0a558f444c98 Uploaded
greg
parents:
diff changeset
649 region_mutations = pandas.concat([region_mutations, region_mutation_types, region_mutation_drugs, region_notes], axis=1)
0a558f444c98 Uploaded
greg
parents:
diff changeset
650 region_mutations = region_mutations[['#CHROM', 'POS', 'TYPE', 'REF', 'ALT', 'DRUG', 'NOTE']]
0a558f444c98 Uploaded
greg
parents:
diff changeset
651 amr_mutations[region['name']] = region_mutations
2
9cb62054a87a Uploaded
greg
parents: 1
diff changeset
652 if (amr_mutations.shape[0] > 0):
9cb62054a87a Uploaded
greg
parents: 1
diff changeset
653 # Report the mutations.
0
0a558f444c98 Uploaded
greg
parents:
diff changeset
654 self.doc.new_line()
2
9cb62054a87a Uploaded
greg
parents: 1
diff changeset
655 self.doc.new_header(level=2, title=self.mutation_title)
9cb62054a87a Uploaded
greg
parents: 1
diff changeset
656 for region_name in amr_mutations.index.tolist():
9cb62054a87a Uploaded
greg
parents: 1
diff changeset
657 region_mutations = amr_mutations[region_name].copy()
9cb62054a87a Uploaded
greg
parents: 1
diff changeset
658 self.doc.new_line()
9cb62054a87a Uploaded
greg
parents: 1
diff changeset
659 self.doc.new_header(level=3, title=region_name)
9cb62054a87a Uploaded
greg
parents: 1
diff changeset
660 if (region_mutations.shape[0] == 0):
9cb62054a87a Uploaded
greg
parents: 1
diff changeset
661 self.doc.append('None')
9cb62054a87a Uploaded
greg
parents: 1
diff changeset
662 continue
9cb62054a87a Uploaded
greg
parents: 1
diff changeset
663 region_mutations.iloc[:, 1] = region_mutations.iloc[:, 1].apply(lambda x: '{:,}'.format(x))
9cb62054a87a Uploaded
greg
parents: 1
diff changeset
664 Table_List = ['Reference contig', 'Position', 'Reference', 'Alternate', 'Drug', 'Note']
9cb62054a87a Uploaded
greg
parents: 1
diff changeset
665 for i in range(region_mutations.shape[0]):
9cb62054a87a Uploaded
greg
parents: 1
diff changeset
666 Table_List = Table_List + region_mutations.iloc[i, [0, 1, 3, 4, 5, 6]].values.tolist()
9cb62054a87a Uploaded
greg
parents: 1
diff changeset
667 row_count = int(len(Table_List) / 6)
9cb62054a87a Uploaded
greg
parents: 1
diff changeset
668 self.doc.new_table(columns=6, rows=row_count, text=Table_List, text_align='left')
18
e948214a9e3c Uploaded
greg
parents: 17
diff changeset
669 if os.path.getsize(self.errors_file) > 0:
e948214a9e3c Uploaded
greg
parents: 17
diff changeset
670 # Report the errors encountered when attempting
e948214a9e3c Uploaded
greg
parents: 17
diff changeset
671 # to find mutations in the sample.
e948214a9e3c Uploaded
greg
parents: 17
diff changeset
672 self.doc.new_line()
e948214a9e3c Uploaded
greg
parents: 17
diff changeset
673 self.doc.new_header(level=2, title=self.mutation_errors_title)
e948214a9e3c Uploaded
greg
parents: 17
diff changeset
674 with open(self.errors_file, 'r') as efh:
e948214a9e3c Uploaded
greg
parents: 17
diff changeset
675 for i, line in enumerate(efh):
e948214a9e3c Uploaded
greg
parents: 17
diff changeset
676 line = line.strip()
e948214a9e3c Uploaded
greg
parents: 17
diff changeset
677 if line:
e948214a9e3c Uploaded
greg
parents: 17
diff changeset
678 self.doc.new_line('* %s' % line)
12
99613333fd1f Uploaded
greg
parents: 11
diff changeset
679 method = '%s reads were mapped to the reference sequence using %s.' % (self.read_type, self.minimap2_version)
0
0a558f444c98 Uploaded
greg
parents:
diff changeset
680 self.methods[self.mutation_methods_title] = self.methods[self.mutation_methods_title].append(pandas.Series(method))
13
f03c80bb22e9 Uploaded
greg
parents: 12
diff changeset
681 method = 'Mutations were identified using %s and %s.' % (self.samtools_version, self.varscan_version)
0
0a558f444c98 Uploaded
greg
parents:
diff changeset
682 self.methods[self.mutation_methods_title] = self.methods[self.mutation_methods_title].append(pandas.Series(method))
0a558f444c98 Uploaded
greg
parents:
diff changeset
683
0a558f444c98 Uploaded
greg
parents:
diff changeset
684 def add_amr_matrix(self):
0a558f444c98 Uploaded
greg
parents:
diff changeset
685 self.ofh.write("\nXXXXXX In add_amr_matrix\n\n")
0a558f444c98 Uploaded
greg
parents:
diff changeset
686 # Make sure that we have an AMR matrix to plot
1
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
687 if len(self.amr_matrix_files) == 0:
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
688 return
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
689 self.doc.new_line()
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
690 self.doc.new_header(level=2, title=self.amr_matrix_title)
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
691 self.doc.new_line('AMR genes and mutations with their corresponding drugs')
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
692 for amr_matrix_file in self.amr_matrix_files:
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
693 self.doc.new_line(self.doc.new_inline_image(text='AMR genes and mutations with their corresponding drugs',
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
694 path=os.path.abspath(amr_matrix_file)))
0
0a558f444c98 Uploaded
greg
parents:
diff changeset
695
0a558f444c98 Uploaded
greg
parents:
diff changeset
696 def add_large_indels(self):
0a558f444c98 Uploaded
greg
parents:
diff changeset
697 self.ofh.write("\nXXXXXX In add_large_indels\n\n")
1
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
698 large_indels = pandas.Series(dtype='float64')
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
699 # Pull in insertions.
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
700 try:
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
701 reference_insertions = pandas.read_csv(filepath_or_buffer=self.reference_insertions_file, sep='\t', header=None)
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
702 except Exception:
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
703 reference_insertions = pandas.DataFrame()
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
704 try:
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
705 genome_insertions = pandas.read_csv(filepath_or_buffer=self.genome_insertions_file, sep='\t', header=None)
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
706 except Exception:
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
707 genome_insertions = pandas.DataFrame()
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
708 large_indels['Reference insertions'] = reference_insertions
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
709 large_indels['Query insertions'] = genome_insertions
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
710 # Pull in deletions.
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
711 try:
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
712 amr_deletions = pandas.read_csv(filepath_or_buffer=self.amr_deletion_file, sep='\t', header=None)
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
713 except Exception:
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
714 amr_deletions = pandas.DataFrame()
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
715 if amr_deletions.shape[0] > 0:
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
716 amr_deletions.columns = ['contig', 'start', 'stop', 'name', 'type', 'drug', 'note']
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
717 amr_deletions = amr_deletions.loc[amr_deletions['type'].isin(['large-deletion', 'any']), :]
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
718 self.doc.new_line()
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
719 self.doc.new_header(level=2, title=self.large_indel_title)
25
4986a7fb2145 Uploaded
greg
parents: 24
diff changeset
720 self.doc.new_line('This section is informative only when your isolates were identified as *Bacillus anthracis* strains')
1
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
721 for genome in ['Reference insertions', 'Query insertions']:
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
722 genome_indels = large_indels[genome].copy()
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
723 self.doc.new_line()
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
724 self.doc.new_header(level=3, title=genome)
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
725 if (genome_indels.shape[0] == 0):
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
726 continue
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
727 genome_indels.iloc[:, 1] = genome_indels.iloc[:, 1].apply(lambda x: '{:,}'.format(x))
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
728 genome_indels.iloc[:, 2] = genome_indels.iloc[:, 2].apply(lambda x: '{:,}'.format(x))
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
729 genome_indels.iloc[:, 3] = genome_indels.iloc[:, 3].apply(lambda x: '{:,}'.format(x))
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
730 Table_List = [
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
731 'Reference contig', 'Start', 'Stop', 'Size (bp)'
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
732 ]
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
733 for i in range(genome_indels.shape[0]):
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
734 Table_List = Table_List + genome_indels.iloc[i, :].values.tolist()
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
735 row_count = int(len(Table_List) / 4)
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
736 self.doc.new_table(columns=4, rows=row_count, text=Table_List, text_align='left')
12
99613333fd1f Uploaded
greg
parents: 11
diff changeset
737 method = 'Large insertions or deletions were found as the complement of aligned regions using %s.' % self.bedtools_version
1
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
738 self.methods[self.reference_methods_title] = self.methods[self.reference_methods_title].append(pandas.Series(method))
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
739 self.doc.new_line()
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
740 self.doc.new_line('<div style="page-break-after: always;"></div>')
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
741 self.doc.new_line()
0
0a558f444c98 Uploaded
greg
parents:
diff changeset
742
0a558f444c98 Uploaded
greg
parents:
diff changeset
743 def add_plasmids(self):
8
66c7cdf7e93f Uploaded
greg
parents: 7
diff changeset
744 try:
1
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
745 plasmids = pandas.read_csv(filepath_or_buffer=self.plasmids_file, sep='\t', header=0)
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
746 except Exception:
0
0a558f444c98 Uploaded
greg
parents:
diff changeset
747 return
0a558f444c98 Uploaded
greg
parents:
diff changeset
748 plasmids = plasmids.copy()
0a558f444c98 Uploaded
greg
parents:
diff changeset
749 self.doc.new_line()
1
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
750 self.doc.new_header(level=2, title=self.plasmid_title)
0
0a558f444c98 Uploaded
greg
parents:
diff changeset
751 if (plasmids.shape[0] == 0):
0a558f444c98 Uploaded
greg
parents:
diff changeset
752 self.doc.new_line('None')
0a558f444c98 Uploaded
greg
parents:
diff changeset
753 return
0a558f444c98 Uploaded
greg
parents:
diff changeset
754 plasmids.iloc[:, 3] = plasmids.iloc[:, 3].apply(lambda x: '{:,}'.format(x))
0a558f444c98 Uploaded
greg
parents:
diff changeset
755 plasmids.iloc[:, 4] = plasmids.iloc[:, 4].apply(lambda x: '{:,}'.format(x))
0a558f444c98 Uploaded
greg
parents:
diff changeset
756 plasmids.iloc[:, 5] = plasmids.iloc[:, 5].apply(lambda x: '{:,}'.format(x))
1
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
757 Table_List = ['Genome contig', 'Plasmid hit', 'Plasmid acc.', 'Contig size', 'Aliged', 'Plasmid size']
0
0a558f444c98 Uploaded
greg
parents:
diff changeset
758 for i in range(plasmids.shape[0]):
0a558f444c98 Uploaded
greg
parents:
diff changeset
759 Table_List = Table_List + plasmids.iloc[i, 0:6].values.tolist()
0a558f444c98 Uploaded
greg
parents:
diff changeset
760 row_count = int(len(Table_List) / 6)
0a558f444c98 Uploaded
greg
parents:
diff changeset
761 self.doc.new_table(columns=6, rows=row_count, text=Table_List, text_align='left')
12
99613333fd1f Uploaded
greg
parents: 11
diff changeset
762 method = 'The plasmid reference database was queried against the genome assembly using %s.' % self.minimap2_version
0
0a558f444c98 Uploaded
greg
parents:
diff changeset
763 self.methods[self.plasmid_methods_title] = self.methods[self.plasmid_methods_title].append(pandas.Series(method))
2
9cb62054a87a Uploaded
greg
parents: 1
diff changeset
764 method = 'The resulting BAM was converted to a PSL using a custom version of sam2psl.'
0
0a558f444c98 Uploaded
greg
parents:
diff changeset
765 self.methods[self.plasmid_methods_title] = self.methods[self.plasmid_methods_title].append(pandas.Series(method))
0a558f444c98 Uploaded
greg
parents:
diff changeset
766 method = 'Plasmid-to-genome hits were resolved using the pChunks algorithm.'
0a558f444c98 Uploaded
greg
parents:
diff changeset
767 self.methods[self.plasmid_methods_title] = self.methods[self.plasmid_methods_title].append(pandas.Series(method))
0a558f444c98 Uploaded
greg
parents:
diff changeset
768
0a558f444c98 Uploaded
greg
parents:
diff changeset
769 def add_methods(self):
0a558f444c98 Uploaded
greg
parents:
diff changeset
770 self.ofh.write("\nXXXXXX In add_methods\n\n")
0a558f444c98 Uploaded
greg
parents:
diff changeset
771 if len(self.methods) == 0:
0a558f444c98 Uploaded
greg
parents:
diff changeset
772 return
0a558f444c98 Uploaded
greg
parents:
diff changeset
773 self.doc.new_line()
0a558f444c98 Uploaded
greg
parents:
diff changeset
774 self.doc.new_header(level=2, title=self.methods_title)
0a558f444c98 Uploaded
greg
parents:
diff changeset
775 for methods_section in self.methods.index.tolist():
0a558f444c98 Uploaded
greg
parents:
diff changeset
776 if self.methods[methods_section] is None or len(self.methods[methods_section]) == 0:
0a558f444c98 Uploaded
greg
parents:
diff changeset
777 continue
0a558f444c98 Uploaded
greg
parents:
diff changeset
778 self.doc.new_line()
0a558f444c98 Uploaded
greg
parents:
diff changeset
779 self.doc.new_header(level=3, title=methods_section)
0a558f444c98 Uploaded
greg
parents:
diff changeset
780 self.doc.new_paragraph(' '.join(self.methods[methods_section]))
24
803e5965ed20 Uploaded
greg
parents: 23
diff changeset
781 self.doc.new_line('<div style="page-break-after: always;"></div>')
803e5965ed20 Uploaded
greg
parents: 23
diff changeset
782 self.doc.new_line()
0
0a558f444c98 Uploaded
greg
parents:
diff changeset
783
0a558f444c98 Uploaded
greg
parents:
diff changeset
784 def add_summary(self):
0a558f444c98 Uploaded
greg
parents:
diff changeset
785 self.ofh.write("\nXXXXXX In add_summary\n\n")
0a558f444c98 Uploaded
greg
parents:
diff changeset
786 # Add summary title
0a558f444c98 Uploaded
greg
parents:
diff changeset
787 self.doc.new_header(level=1, title=self.summary_title)
0a558f444c98 Uploaded
greg
parents:
diff changeset
788 # First section of Summary
0a558f444c98 Uploaded
greg
parents:
diff changeset
789 self.doc.new_header(level=1, title='CDC Advisory')
0a558f444c98 Uploaded
greg
parents:
diff changeset
790 self.doc.new_paragraph(CDC_ADVISORY)
0a558f444c98 Uploaded
greg
parents:
diff changeset
791 self.doc.new_line()
0a558f444c98 Uploaded
greg
parents:
diff changeset
792 self.add_run_information()
0a558f444c98 Uploaded
greg
parents:
diff changeset
793 self.add_ont_library_information()
0a558f444c98 Uploaded
greg
parents:
diff changeset
794 methods = []
0a558f444c98 Uploaded
greg
parents:
diff changeset
795 if self.did_guppy_ont_fast5:
0a558f444c98 Uploaded
greg
parents:
diff changeset
796 methods += ['ONT reads were basecalled using guppy']
0a558f444c98 Uploaded
greg
parents:
diff changeset
797 if self.did_qcat_ont_fastq:
0a558f444c98 Uploaded
greg
parents:
diff changeset
798 methods += ['ONT reads were demultiplexed and trimmed using qcat']
0a558f444c98 Uploaded
greg
parents:
diff changeset
799 self.methods[self.basecalling_methods_title] = pandas.Series(methods)
26
46edd7435555 Uploaded
greg
parents: 25
diff changeset
800 self.add_illumina_library_information()
46edd7435555 Uploaded
greg
parents: 25
diff changeset
801 self.add_assembly_information()
1
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
802 self.add_contig_info()
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
803 self.evaluate_assembly()
21
667b253329c6 Uploaded
greg
parents: 20
diff changeset
804 if self.assembler_version is not None:
667b253329c6 Uploaded
greg
parents: 20
diff changeset
805 if self.read_type == 'ONT':
667b253329c6 Uploaded
greg
parents: 20
diff changeset
806 method = 'ONT reads were assembled using %s' % self.assembler_version
667b253329c6 Uploaded
greg
parents: 20
diff changeset
807 self.methods[self.assembly_methods_title] = self.methods[self.assembly_methods_title].append(pandas.Series(method))
667b253329c6 Uploaded
greg
parents: 20
diff changeset
808 # Pull in the assembly summary and look at the coverage.
667b253329c6 Uploaded
greg
parents: 20
diff changeset
809 assembly_info = pandas.read_csv(self.flye_assembly_info_file, header=0, index_col=0, sep='\t')
667b253329c6 Uploaded
greg
parents: 20
diff changeset
810 # Look for non-circular contigs.
667b253329c6 Uploaded
greg
parents: 20
diff changeset
811 open_contigs = assembly_info.loc[assembly_info['circ.'] == 'N', :]
667b253329c6 Uploaded
greg
parents: 20
diff changeset
812 if open_contigs.shape[0] > 0:
667b253329c6 Uploaded
greg
parents: 20
diff changeset
813 open_contig_ids = open_contigs.index.values
667b253329c6 Uploaded
greg
parents: 20
diff changeset
814 warning = 'Flye reported {:d} open contigs ({:s}); assembly may be incomplete.'.format(open_contigs.shape[0], ', '.join(open_contig_ids))
667b253329c6 Uploaded
greg
parents: 20
diff changeset
815 self.assembly_notes = self.assembly_notes.append(pandas.Series(warning))
667b253329c6 Uploaded
greg
parents: 20
diff changeset
816 else:
667b253329c6 Uploaded
greg
parents: 20
diff changeset
817 method = 'Illumina reads were assembled using %s' % self.assembler_version
26
46edd7435555 Uploaded
greg
parents: 25
diff changeset
818 method = 'The genome assembly was polished using ONT reads and medaka.'
46edd7435555 Uploaded
greg
parents: 25
diff changeset
819 self.methods[self.assembly_methods_title] = self.methods[self.assembly_methods_title].append(pandas.series(method))
1
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
820 self.add_assembly_notes()
0
0a558f444c98 Uploaded
greg
parents:
diff changeset
821
0a558f444c98 Uploaded
greg
parents:
diff changeset
822 def make_tex(self):
0a558f444c98 Uploaded
greg
parents:
diff changeset
823 self.doc.new_table_of_contents(table_title='detailed run information', depth=2, marker="tableofcontents")
0a558f444c98 Uploaded
greg
parents:
diff changeset
824 text = self.doc.file_data_text
0a558f444c98 Uploaded
greg
parents:
diff changeset
825 text = text.replace("##--[", "")
0a558f444c98 Uploaded
greg
parents:
diff changeset
826 text = text.replace("]--##", "")
0a558f444c98 Uploaded
greg
parents:
diff changeset
827 self.doc.file_data_text = text
0a558f444c98 Uploaded
greg
parents:
diff changeset
828 self.doc.create_md_file()
0a558f444c98 Uploaded
greg
parents:
diff changeset
829
0a558f444c98 Uploaded
greg
parents:
diff changeset
830 def make_report(self):
0a558f444c98 Uploaded
greg
parents:
diff changeset
831 self.ofh.write("\nXXXXXX In make_report\n\n")
0a558f444c98 Uploaded
greg
parents:
diff changeset
832 self.start_doc()
0a558f444c98 Uploaded
greg
parents:
diff changeset
833 self.add_summary()
0a558f444c98 Uploaded
greg
parents:
diff changeset
834 self.add_contamination()
0a558f444c98 Uploaded
greg
parents:
diff changeset
835 self.add_alignment()
0a558f444c98 Uploaded
greg
parents:
diff changeset
836 self.add_features()
0a558f444c98 Uploaded
greg
parents:
diff changeset
837 self.add_feature_plots()
0a558f444c98 Uploaded
greg
parents:
diff changeset
838 self.add_mutations()
0a558f444c98 Uploaded
greg
parents:
diff changeset
839 self.add_large_indels()
0a558f444c98 Uploaded
greg
parents:
diff changeset
840 self.add_plasmids()
0a558f444c98 Uploaded
greg
parents:
diff changeset
841 self.add_amr_matrix()
0a558f444c98 Uploaded
greg
parents:
diff changeset
842 # self.add_snps()
0a558f444c98 Uploaded
greg
parents:
diff changeset
843 self.add_methods()
0a558f444c98 Uploaded
greg
parents:
diff changeset
844 self.make_tex()
0a558f444c98 Uploaded
greg
parents:
diff changeset
845 # It took me quite a long time to find out that the value of the -t
0a558f444c98 Uploaded
greg
parents:
diff changeset
846 # (implied) argument in the following command must be 'html' instead of
0a558f444c98 Uploaded
greg
parents:
diff changeset
847 # the more logical 'pdf'. see the answer from snsn in this thread:
0a558f444c98 Uploaded
greg
parents:
diff changeset
848 # https://github.com/jessicategner/pypandoc/issues/186
0a558f444c98 Uploaded
greg
parents:
diff changeset
849 self.ofh.write("\nXXXXX In make_report, calling pypandoc.convert_file...\n\n")
0a558f444c98 Uploaded
greg
parents:
diff changeset
850 pypandoc.convert_file(self.report_md,
0a558f444c98 Uploaded
greg
parents:
diff changeset
851 'html',
0a558f444c98 Uploaded
greg
parents:
diff changeset
852 extra_args=['--pdf-engine=weasyprint', '-V', '-css=%s' % self.pima_css],
0a558f444c98 Uploaded
greg
parents:
diff changeset
853 outputfile='pima_report.pdf')
0a558f444c98 Uploaded
greg
parents:
diff changeset
854 self.ofh.close()
0a558f444c98 Uploaded
greg
parents:
diff changeset
855
0a558f444c98 Uploaded
greg
parents:
diff changeset
856
0a558f444c98 Uploaded
greg
parents:
diff changeset
857 parser = argparse.ArgumentParser()
0a558f444c98 Uploaded
greg
parents:
diff changeset
858
1
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
859 parser.add_argument('--amr_deletions_file', action='store', dest='amr_deletions_file', help='AMR deletions BED file')
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
860 parser.add_argument('--amr_matrix_png_dir', action='store', dest='amr_matrix_png_dir', help='Directory of AMR matrix PNG files')
0
0a558f444c98 Uploaded
greg
parents:
diff changeset
861 parser.add_argument('--analysis_name', action='store', dest='analysis_name', help='Sample identifier')
21
667b253329c6 Uploaded
greg
parents: 20
diff changeset
862 parser.add_argument('--assembler_version', action='store', dest='assembler_version', default=None, help='Assembler version string')
0
0a558f444c98 Uploaded
greg
parents:
diff changeset
863 parser.add_argument('--assembly_fasta_file', action='store', dest='assembly_fasta_file', help='Assembly fasta file')
0a558f444c98 Uploaded
greg
parents:
diff changeset
864 parser.add_argument('--assembly_name', action='store', dest='assembly_name', help='Assembly identifier')
12
99613333fd1f Uploaded
greg
parents: 11
diff changeset
865 parser.add_argument('--bedtools_version', action='store', dest='bedtools_version', default=None, help='Bedtools version string')
2
9cb62054a87a Uploaded
greg
parents: 1
diff changeset
866 parser.add_argument('--blastn_version', action='store', dest='blastn_version', default=None, help='Blastn version string')
13
f03c80bb22e9 Uploaded
greg
parents: 12
diff changeset
867 parser.add_argument('--circos_png_dir', action='store', dest='circos_png_dir', help='Directory of circos PNG files')
1
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
868 parser.add_argument('--compute_sequence_length_file', action='store', dest='compute_sequence_length_file', help='Comnpute sequence length tabular file')
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
869 parser.add_argument('--contig_coverage_file', action='store', dest='contig_coverage_file', help='Contig coverage TSV file')
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
870 parser.add_argument('--dbkey', action='store', dest='dbkey', help='Reference genome identifier')
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
871 parser.add_argument('--dnadiff_snps_file', action='store', dest='dnadiff_snps_file', help='DNAdiff snps tabular file')
12
99613333fd1f Uploaded
greg
parents: 11
diff changeset
872 parser.add_argument('--dnadiff_version', action='store', dest='dnadiff_version', default=None, help='DNAdiff version string')
18
e948214a9e3c Uploaded
greg
parents: 17
diff changeset
873 parser.add_argument('--errors_file', action='store', dest='errors_file', default=None, help='AMR mutations errors encountered txt file')
0
0a558f444c98 Uploaded
greg
parents:
diff changeset
874 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
875 parser.add_argument('--feature_png_dir', action='store', dest='feature_png_dir', help='Directory of best feature hits png files')
1
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
876 parser.add_argument('--flye_assembly_info_file', action='store', dest='flye_assembly_info_file', default=None, help='Flye assembly info tabular file')
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
877 parser.add_argument('--genome_insertions_file', action='store', dest='genome_insertions_file', help='Genome insertions BED file')
26
46edd7435555 Uploaded
greg
parents: 25
diff changeset
878 parser.add_argument('--gzipped', action='store_true', dest='gzipped', default=False, help='Sample(s) is/are gzipped')
46edd7435555 Uploaded
greg
parents: 25
diff changeset
879 parser.add_argument('--illumina_forward_read_file', action='store', dest='illumina_forward_read_file', help='Illumina forward read file')
46edd7435555 Uploaded
greg
parents: 25
diff changeset
880 parser.add_argument('--illumina_reverse_read_file', action='store', dest='illumina_reverse_read_file', help='Illumina reverse read file')
2
9cb62054a87a Uploaded
greg
parents: 1
diff changeset
881 parser.add_argument('--kraken2_report_file', action='store', dest='kraken2_report_file', default=None, help='kraken2 report file')
9cb62054a87a Uploaded
greg
parents: 1
diff changeset
882 parser.add_argument('--kraken2_version', action='store', dest='kraken2_version', default=None, help='kraken2 version string')
12
99613333fd1f Uploaded
greg
parents: 11
diff changeset
883 parser.add_argument('--minimap2_version', action='store', dest='minimap2_version', default=None, help='minimap2 version string')
0
0a558f444c98 Uploaded
greg
parents:
diff changeset
884 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
885 parser.add_argument('--mutation_regions_dir', action='store', dest='mutation_regions_dir', help='Directory of mutation regions TSV files')
26
46edd7435555 Uploaded
greg
parents: 25
diff changeset
886 parser.add_argument('--ont_file', action='store', dest='ont_file', help='ONT single read file')
0
0a558f444c98 Uploaded
greg
parents:
diff changeset
887 parser.add_argument('--pima_css', action='store', dest='pima_css', help='PIMA css stypesheet')
23
d89b7d47a85a Uploaded
greg
parents: 22
diff changeset
888 parser.add_argument('--plasmids_file', action='store', dest='plasmids_file', default=None, help='pChunks plasmids TSV file')
13
f03c80bb22e9 Uploaded
greg
parents: 12
diff changeset
889 parser.add_argument('--quast_report_file', action='store', dest='quast_report_file', help='Quast report tabular file')
18
e948214a9e3c Uploaded
greg
parents: 17
diff changeset
890 parser.add_argument('--read_type', action='store', dest='read_type', help='Sample read type (ONT or Illumina)')
1
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
891 parser.add_argument('--reference_insertions_file', action='store', dest='reference_insertions_file', help='Reference insertions BED file')
12
99613333fd1f Uploaded
greg
parents: 11
diff changeset
892 parser.add_argument('--samtools_version', action='store', dest='samtools_version', default=None, help='Samtools version string')
99613333fd1f Uploaded
greg
parents: 11
diff changeset
893 parser.add_argument('--varscan_version', action='store', dest='varscan_version', default=None, help='Varscan version string')
0
0a558f444c98 Uploaded
greg
parents:
diff changeset
894
0a558f444c98 Uploaded
greg
parents:
diff changeset
895 args = parser.parse_args()
0a558f444c98 Uploaded
greg
parents:
diff changeset
896
1
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
897 # Prepare the AMR matrix PNG files.
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
898 amr_matrix_files = []
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
899 for file_name in sorted(os.listdir(args.amr_matrix_png_dir)):
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
900 file_path = os.path.abspath(os.path.join(args.amr_matrix_png_dir, file_name))
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
901 amr_matrix_files.append(file_path)
13
f03c80bb22e9 Uploaded
greg
parents: 12
diff changeset
902 # Prepare the circos PNG files.
f03c80bb22e9 Uploaded
greg
parents: 12
diff changeset
903 circos_files = []
f03c80bb22e9 Uploaded
greg
parents: 12
diff changeset
904 for file_name in sorted(os.listdir(args.circos_png_dir)):
f03c80bb22e9 Uploaded
greg
parents: 12
diff changeset
905 file_path = os.path.abspath(os.path.join(args.circos_png_dir, file_name))
f03c80bb22e9 Uploaded
greg
parents: 12
diff changeset
906 circos_files.append(file_path)
0
0a558f444c98 Uploaded
greg
parents:
diff changeset
907 # Prepare the features BED files.
0a558f444c98 Uploaded
greg
parents:
diff changeset
908 feature_bed_files = []
0a558f444c98 Uploaded
greg
parents:
diff changeset
909 for file_name in sorted(os.listdir(args.feature_bed_dir)):
0a558f444c98 Uploaded
greg
parents:
diff changeset
910 file_path = os.path.abspath(os.path.join(args.feature_bed_dir, file_name))
0a558f444c98 Uploaded
greg
parents:
diff changeset
911 feature_bed_files.append(file_path)
0a558f444c98 Uploaded
greg
parents:
diff changeset
912 # Prepare the features PNG files.
0a558f444c98 Uploaded
greg
parents:
diff changeset
913 feature_png_files = []
0a558f444c98 Uploaded
greg
parents:
diff changeset
914 for file_name in sorted(os.listdir(args.feature_png_dir)):
0a558f444c98 Uploaded
greg
parents:
diff changeset
915 file_path = os.path.abspath(os.path.join(args.feature_png_dir, file_name))
0a558f444c98 Uploaded
greg
parents:
diff changeset
916 feature_png_files.append(file_path)
0a558f444c98 Uploaded
greg
parents:
diff changeset
917 # Prepare the mutation regions TSV files.
0a558f444c98 Uploaded
greg
parents:
diff changeset
918 mutation_regions_files = []
0a558f444c98 Uploaded
greg
parents:
diff changeset
919 for file_name in sorted(os.listdir(args.mutation_regions_dir)):
0a558f444c98 Uploaded
greg
parents:
diff changeset
920 file_path = os.path.abspath(os.path.join(args.feature_png_dir, file_name))
0a558f444c98 Uploaded
greg
parents:
diff changeset
921 mutation_regions_files.append(file_path)
0a558f444c98 Uploaded
greg
parents:
diff changeset
922
0a558f444c98 Uploaded
greg
parents:
diff changeset
923 markdown_report = PimaReport(args.analysis_name,
1
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
924 args.amr_deletions_file,
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
925 amr_matrix_files,
21
667b253329c6 Uploaded
greg
parents: 20
diff changeset
926 args.assembler_version,
0
0a558f444c98 Uploaded
greg
parents:
diff changeset
927 args.assembly_fasta_file,
0a558f444c98 Uploaded
greg
parents:
diff changeset
928 args.assembly_name,
12
99613333fd1f Uploaded
greg
parents: 11
diff changeset
929 args.bedtools_version,
2
9cb62054a87a Uploaded
greg
parents: 1
diff changeset
930 args.blastn_version,
13
f03c80bb22e9 Uploaded
greg
parents: 12
diff changeset
931 circos_files,
1
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
932 args.compute_sequence_length_file,
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
933 args.contig_coverage_file,
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
934 args.dbkey,
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
935 args.dnadiff_snps_file,
2
9cb62054a87a Uploaded
greg
parents: 1
diff changeset
936 args.dnadiff_version,
18
e948214a9e3c Uploaded
greg
parents: 17
diff changeset
937 args.errors_file,
0
0a558f444c98 Uploaded
greg
parents:
diff changeset
938 feature_bed_files,
0a558f444c98 Uploaded
greg
parents:
diff changeset
939 feature_png_files,
1
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
940 args.flye_assembly_info_file,
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
941 args.genome_insertions_file,
0
0a558f444c98 Uploaded
greg
parents:
diff changeset
942 args.gzipped,
26
46edd7435555 Uploaded
greg
parents: 25
diff changeset
943 args.illumina_forward_read_file,
46edd7435555 Uploaded
greg
parents: 25
diff changeset
944 args.illumina_reverse_read_file,
2
9cb62054a87a Uploaded
greg
parents: 1
diff changeset
945 args.kraken2_report_file,
9cb62054a87a Uploaded
greg
parents: 1
diff changeset
946 args.kraken2_version,
12
99613333fd1f Uploaded
greg
parents: 11
diff changeset
947 args.minimap2_version,
0
0a558f444c98 Uploaded
greg
parents:
diff changeset
948 args.mutation_regions_bed_file,
0a558f444c98 Uploaded
greg
parents:
diff changeset
949 mutation_regions_files,
26
46edd7435555 Uploaded
greg
parents: 25
diff changeset
950 args.ont_file,
1
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
951 args.pima_css,
67d0939b56b0 Uploaded
greg
parents: 0
diff changeset
952 args.plasmids_file,
13
f03c80bb22e9 Uploaded
greg
parents: 12
diff changeset
953 args.quast_report_file,
18
e948214a9e3c Uploaded
greg
parents: 17
diff changeset
954 args.read_type,
12
99613333fd1f Uploaded
greg
parents: 11
diff changeset
955 args.reference_insertions_file,
99613333fd1f Uploaded
greg
parents: 11
diff changeset
956 args.samtools_version,
99613333fd1f Uploaded
greg
parents: 11
diff changeset
957 args.varscan_version)
0
0a558f444c98 Uploaded
greg
parents:
diff changeset
958 markdown_report.make_report()