From 4db71666f8d86dd7b6a8db83561369f2b539d58a Mon Sep 17 00:00:00 2001
From: Martijn Vermaat <martijn@vermaat.name>
Date: Fri, 18 Dec 2015 18:04:52 +0100
Subject: [PATCH] Create legend only after gene model enrichment

This fixes a bug where transcripts created from CDS by construction did
not show up in the legend because the legend was created before that
construction.
---
 mutalyzer/variantchecker.py  | 36 +++++++++++++++++++-----------------
 tests/test_variantchecker.py | 12 ++++++++++++
 2 files changed, 31 insertions(+), 17 deletions(-)

diff --git a/mutalyzer/variantchecker.py b/mutalyzer/variantchecker.py
index 9da54db8..a47d80e9 100644
--- a/mutalyzer/variantchecker.py
+++ b/mutalyzer/variantchecker.py
@@ -1773,23 +1773,6 @@ def check_variant(description, output):
     record = GenRecord.GenRecord(output)
     record.record = retrieved_record
 
-    # Create the legend.
-    for gene in record.record.geneList:
-        for transcript in sorted(gene.transcriptList, key=attrgetter('name')):
-            if not transcript.name:
-                continue
-            output.addOutput('legends',
-                             ['%s_v%s' % (gene.name, transcript.name),
-                              transcript.transcriptID, transcript.locusTag,
-                              transcript.transcriptProduct,
-                              transcript.linkMethod])
-            if transcript.translate:
-                output.addOutput('legends',
-                                 ['%s_i%s' % (gene.name, transcript.name),
-                                  transcript.proteinID, transcript.locusTag,
-                                  transcript.proteinProduct,
-                                  transcript.linkMethod])
-
     # Note: The GenRecord instance is carrying the sequence in .record.seq.
     #       So is the Mutator instance in .mutator.orig.
 
@@ -1802,6 +1785,25 @@ def check_variant(description, output):
         process_variant(mutator, parsed_description, record, output)
     except _VariantError:
         return
+    finally:
+        # The legend needs to be created after processing the variant (which
+        # enriches the gene model by calling record.checkRecord), but we can
+        # create it regardless of success or failure.
+        for gene in record.record.geneList:
+            for transcript in sorted(gene.transcriptList, key=attrgetter('name')):
+                if not transcript.name:
+                    continue
+                output.addOutput('legends',
+                                 ['%s_v%s' % (gene.name, transcript.name),
+                                  transcript.transcriptID, transcript.locusTag,
+                                  transcript.transcriptProduct,
+                                  transcript.linkMethod])
+                if transcript.translate:
+                    output.addOutput('legends',
+                                     ['%s_i%s' % (gene.name, transcript.name),
+                                      transcript.proteinID, transcript.locusTag,
+                                      transcript.proteinProduct,
+                                      transcript.linkMethod])
 
     output.addOutput('original', unicode(mutator.orig))
     output.addOutput('mutated', unicode(mutator.mutated))
diff --git a/tests/test_variantchecker.py b/tests/test_variantchecker.py
index 1f9cd1c2..897a3e08 100644
--- a/tests/test_variantchecker.py
+++ b/tests/test_variantchecker.py
@@ -1578,3 +1578,15 @@ def test_start_codon_alt_start_yield_start(output, checker):
     assert output.getOutput('oldProtein')[0].startswith('M')
     assert 'ATG' in output.getOutput('altStart')
     assert output.getOutput('altProtein')[0].startswith('M')
+
+
+@with_references('AB026906.1')
+def test_legend_mrna_by_construction(output, checker):
+    """
+    Transcript created from CDS by construction should be in the legend.
+    """
+    checker('AB026906.1:g.7872G>T')
+    assert output.getOutput('legends') == [
+        ['SDHD_v001', None, None, None, 'construction'],
+        ['SDHD_i001', 'BAA81889.1', None, 'small subunit of cytochrome b of succinate dehydrogenase', 'construction']
+    ]
-- 
GitLab