annotate pima_report.py @ 4:428aed51c487 draft

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