From 20ad9bea6da9bb77aa4bc1651acfcf827f795918 Mon Sep 17 00:00:00 2001 From: Yen-Chung Chen Date: Sat, 27 Sep 2025 03:03:53 -0400 Subject: [PATCH] Handle possible non-existent attributes in clusterbuster.py (#5074) * Handle possible non-existent attrs in clusterbuster.py In commit bc0d58b, support was added for gap and weight paramters, but since these two attributes are not guaranteed to exist by parse and read methods, an AttributeError could be raised for non-clusterbuster motifs. An attribute check was added to allow motifs lacking gap and weight to be formatted properly. Co-authored-by: Ee Shan Liau Shirley <69098782+shirleyliau@users.noreply.github.com> * Add names to CONTRIB.rst * Use try-except to handle missing attributes Co-authored-by: Ee Shan Liau Shirley <69098782+shirleyliau@users.noreply.github.com> * Avoid CB output when an attribute is None In the previous commit, the skipping mechanism was switched to try-except blocks, and the cases when an attribute is None are no longer dealt with. This patch re-introduced `if m.weight` to prevent empty export fields in such cases. Co-authored-by: Ee Shan Liau Shirley <69098782+shirleyliau@users.noreply.github.com> --------- Co-authored-by: Ee Shan Liau Shirley <69098782+shirleyliau@users.noreply.github.com> --- Bio/motifs/clusterbuster.py | 18 ++++++++++++++---- CONTRIB.rst | 2 ++ 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/Bio/motifs/clusterbuster.py b/Bio/motifs/clusterbuster.py index 3b4344aee..baf8f07c2 100644 --- a/Bio/motifs/clusterbuster.py +++ b/Bio/motifs/clusterbuster.py @@ -88,10 +88,20 @@ def write(motifs, precision=0): lines = [] for m in motifs: lines.append(f">{m.name}\n") - if m.weight: - lines.append(f"# WEIGHT: {m.weight}\n") - if m.gap: - lines.append(f"# GAP: {m.gap}\n") + try: + weight = m.weight + except AttributeError: + pass + else: + if weight: + lines.append(f"# WEIGHT: {weight}\n") + try: + gap = m.gap + except AttributeError: + pass + else: + if gap: + lines.append(f"# GAP: {gap}\n") for ACGT_counts in zip( m.counts["A"], m.counts["C"], m.counts["G"], m.counts["T"] ): diff --git a/CONTRIB.rst b/CONTRIB.rst index bc92be32e..ad389a60c 100644 --- a/CONTRIB.rst +++ b/CONTRIB.rst @@ -118,6 +118,7 @@ please open an issue on GitHub or mention it on the mailing list. - Dimitris Kalafatis - Edward Haigh - Edward Liaw +- Ee Shan Shirley Liau - Emmanuel Noutahi - Eric Rasche - Eric Talevich @@ -364,6 +365,7 @@ please open an issue on GitHub or mention it on the mailing list. - Yair Benita - Yanbo Ye - Yasar L. Ahmed +- Yen-Chung Chen - Yi Hsiao - Yiming Qu - Yogesh Kulkarni