From 193e20d3afd229d7639f0fc0a61fedeafc7d76ba Mon Sep 17 00:00:00 2001 From: Haibao Tang Date: Sat, 11 May 2024 00:35:37 -0700 Subject: [PATCH 1/2] markup ks --- jcvi/graphics/base.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/jcvi/graphics/base.py b/jcvi/graphics/base.py index f1ed271c..c956b3ba 100644 --- a/jcvi/graphics/base.py +++ b/jcvi/graphics/base.py @@ -708,7 +708,7 @@ def draw_cmap(ax, cmap_text, vmin, vmax, cmap=None, reverse=False): ax.text(x, ymin - 0.005, "%.1f" % v, ha="center", va="top", size=10) -def write_messages(ax, messages, ypad=0.04): +def write_messages(ax, messages: List[str], ypad: float=0.04): """ Write text on canvas, usually on the top right corner. """ @@ -716,7 +716,7 @@ def write_messages(ax, messages, ypad=0.04): axt = ax.transAxes yy = 0.95 for msg in messages: - ax.text(0.95, yy, msg, color=tc, transform=axt, ha="right") + ax.text(0.95, yy, markup(msg), color=tc, transform=axt, ha="right") yy -= ypad From f76075bb6a5ae1ee2d004fff660924ee021f0b5a Mon Sep 17 00:00:00 2001 From: Haibao Tang Date: Sat, 11 May 2024 00:40:34 -0700 Subject: [PATCH 2/2] use f-strings --- jcvi/assembly/kmer.py | 34 +++++++++++++++------------------- 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/jcvi/assembly/kmer.py b/jcvi/assembly/kmer.py index d1c355f8..9f43c2a4 100644 --- a/jcvi/assembly/kmer.py +++ b/jcvi/assembly/kmer.py @@ -260,7 +260,7 @@ def run_optimization(termination=0.999, maxiter=100): print(ll, rr, GG, file=sys.stderr) # Ready for genome summary - m = "\n==> Kmer (K={0}) Spectrum Analysis\n".format(K) + m = f"\n==> Kmer (K={K}) Spectrum Analysis\n" genome_size = int(round(self.totalKmers / ll)) inferred_genome_size = 0 @@ -270,7 +270,7 @@ def run_optimization(termination=0.999, maxiter=100): inferred_genome_size += g * mid * (end - start + 1) inferred_genome_size = int(round(inferred_genome_size)) genome_size = max(genome_size, inferred_genome_size) - m += "Genome size estimate = {0}\n".format(thousands(genome_size)) + m += f"Genome size estimate = {thousands(genome_size)}\n" copy_series = [] copy_messages = [] for i, g in enumerate(GG): @@ -279,7 +279,7 @@ def run_optimization(termination=0.999, maxiter=100): copy_num = start if start == end else "{}-{}".format(start, end) g_copies = int(round(g * mid * (end - start + 1))) copy_series.append((mid, copy_num, g_copies, g)) - copy_message = f"CN {copy_num}: {g_copies / 1e6:.1f} Mb ({ g_copies * 100 / genome_size:.1f} percent)" + copy_message = f"CN {copy_num}: {g_copies / 1e6:.1f} Mb ({ g_copies * 100 / genome_size:.1f} %)" copy_messages.append(copy_message) m += copy_message + "\n" @@ -287,7 +287,7 @@ def run_optimization(termination=0.999, maxiter=100): g_copies = genome_size - inferred_genome_size copy_num = "{}+".format(end + 1) copy_series.append((end + 1, copy_num, g_copies, g_copies / (end + 1))) - m += f"CN {copy_num}: {g_copies / 1e6:.1f} Mb ({ g_copies * 100 / genome_size:.1f} percent)\n" + m += f"CN {copy_num}: {g_copies / 1e6:.1f} Mb ({ g_copies * 100 / genome_size:.1f} %)\n" # Determine ploidy def determine_ploidy(copy_series, threshold=0.15): @@ -302,7 +302,7 @@ def determine_ploidy(copy_series, threshold=0.15): ploidy = determine_ploidy(copy_series) self.ploidy = ploidy - self.ploidy_message = "Ploidy: {}".format(ploidy) + self.ploidy_message = f"Ploidy: {ploidy}" m += self.ploidy_message + "\n" self.copy_messages = copy_messages[:ploidy] @@ -317,7 +317,7 @@ def calc_repeats(copy_series, ploidy, genome_size): return 1 - unique / genome_size repeats = calc_repeats(copy_series, ploidy, genome_size) - self.repetitive = "Repeats: {:.1f} percent".format(repeats * 100) + self.repetitive = f"Repeats: {repeats * 100:.1f} %" m += self.repetitive + "\n" # SNP rate @@ -336,7 +336,7 @@ def calc_snp_rate(copy_series, ploidy, genome_size, K): return 1 - (1 - L / genome_size) ** (1 / K) snp_rate = calc_snp_rate(copy_series, ploidy, genome_size, K) - self.snprate = "SNP rate: {:.2f} percent".format(snp_rate * 100) + self.snprate = f"SNP rate: {snp_rate * 100:.2f} %" m += self.snprate + "\n" print(m, file=sys.stderr) @@ -475,22 +475,18 @@ def analyze_allpaths(self, ploidy=2, K=23, covmax=1000000): GR = int(_genome_size_repetitive) coverage = int(_coverage) - m = "Kmer (K={0}) Spectrum Analysis\n".format(K) - m += "Genome size estimate = {0}\n".format(thousands(G)) - m += "Genome size estimate CN = 1 = {0} ({1})\n".format( - thousands(G1), percentage(G1, G) - ) - m += "Genome size estimate CN > 1 = {0} ({1})\n".format( - thousands(GR), percentage(GR, G) - ) - m += "Coverage estimate: {0} x\n".format(coverage) - self.repetitive = "Repeats: {0} percent".format(GR * 100 // G) + m = f"Kmer (K={K}) Spectrum Analysis\n" + m += f"Genome size estimate = {thousands(G)}\n" + m += f"Genome size estimate CN = 1 = {thousands(G1)} ({percentage(G1, G)})\n" + m += f"Genome size estimate CN > 1 = {thousands(GR)} ({percentage(GR, G)})\n" + m += f"Coverage estimate: {coverage} x\n" + self.repetitive = f"Repeats: {GR * 100 // G} %" if ploidy == 2: d_SNP = int(_d_SNP) - self.snprate = "SNP rate ~= 1/{0}".format(d_SNP) + self.snprate = f"SNP rate ~= 1/{d_SNP}" else: - self.snprate = "SNP rate not computed (Ploidy = {0})".format(ploidy) + self.snprate = f"SNP rate not computed (Ploidy = {ploidy})" m += self.snprate + "\n" self.genomesize = int(round(self.totalKmers * 1.0 / self.max2))