From 48a0b15165ecb9c2e1c2c656c6cc726a3b3a580f Mon Sep 17 00:00:00 2001
From: Martijn Vermaat <martijn@vermaat.name>
Date: Mon, 14 Jan 2013 14:10:08 +0000
Subject: [PATCH] Allow selectors on transcript references in position
 converter

git-svn-id: https://humgenprojects.lumc.nl/svn/mutalyzer/trunk@664 eb6bd6ab-9ccd-42b9-aceb-e2899b4a52f1
---
 mutalyzer/Db.py       | 19 +++++++++++---
 mutalyzer/mapping.py  | 11 +++++++-
 tests/test_mapping.py | 60 +++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 86 insertions(+), 4 deletions(-)

diff --git a/mutalyzer/Db.py b/mutalyzer/Db.py
index 7e5e740f..41867362 100644
--- a/mutalyzer/Db.py
+++ b/mutalyzer/Db.py
@@ -287,20 +287,33 @@ class Mapping(Db) :
             args.append(mrnaAcc)
             args.append(version)
 
+        # The fallback to NULL selector (and selector_version) is necessary
+        # to accept transcript selection in NM references. To be safe, we also
+        # prefer entries with a match over entries with NULL.
+        # Example: NM_017780.2(CHD7_v001):c.109A>T
         if selector is not None:
-            where.append('selector = %s')
+            where.append('(selector = %s or (selector IS NULL and selector_version IS NULL and gene = %s and %s = 1))')
             args.append(selector)
+            args.append(selector)
+            args.append(selector_version)
+            order.append('(selector is null) asc')
 
         if selector_version is not None:
-            where.append('selector_version = %s')
+            where.append('(selector_version = %s or (selector_version IS NULL and selector_version IS NULL and gene = %s and %s = 1))')
             args.append(selector_version)
+            args.append(selector)
+            args.append(selector_version)
+            order.append('(selector_version is null) asc')
 
         q += """
              where """ + ' AND '.join(where) + """
              order by """ + ', '.join(order) + ';'
 
         statement = q, tuple(args)
-        return self.query(statement)[0]
+        try:
+            return self.query(statement)[0]
+        except IndexError:
+            return None
 
     def get_Transcripts(self, chrom, p1, p2, overlap) :
         """
diff --git a/mutalyzer/mapping.py b/mutalyzer/mapping.py
index e005a895..810d7d19 100644
--- a/mutalyzer/mapping.py
+++ b/mutalyzer/mapping.py
@@ -212,6 +212,13 @@ class Converter(object) :
         else :
             if version in versions :
                 Values = self.__database.getAllFields(acc, version, selector, selector_version)
+                if not Values:
+                    self.__output.addMessage(__file__, 4, "EACCNOTINDB",
+                                             "The accession number %s version %s "
+                                             "with transcript %s version %s could not be found "
+                                             "in our database." %
+                                             (acc, version, selector, selector_version))
+                    return None
                 return self._FieldsFromValues(Values)
             #if
             if not version :
@@ -510,7 +517,7 @@ class Converter(object) :
             version = self.parseTree.Version
             if self.parseTree.Gene:
                 selector = self.parseTree.Gene.GeneSymbol
-                selector_version = int(self.parseTree.Gene.TransVar)
+                selector_version = int(self.parseTree.Gene.TransVar or 1)
             else:
                 selector = selector_version = None
             self._FieldsFromDb(acc, version, selector, selector_version)
@@ -592,6 +599,8 @@ class Converter(object) :
             return None
 
         values = self.__database.getAllFields(reference, version)
+        if not values:
+            return None
         self._FieldsFromValues(values)
 
         mapper = self.makeCrossmap()
diff --git a/tests/test_mapping.py b/tests/test_mapping.py
index 8a9390bf..1732d9af 100644
--- a/tests/test_mapping.py
+++ b/tests/test_mapping.py
@@ -236,3 +236,63 @@ class TestConverter():
         converter = self._converter('hg19')
         genomic = converter.c2chrom('NC_012920.1(ND4_v001):c.1271del')
         assert_equal(genomic, 'NC_012920.1:m.12030del')
+
+    def test_nm_without_selector_chrom2c(self):
+        """
+        NM reference without transcript selection c. to g.
+        """
+        converter = self._converter('hg19')
+        genomic = converter.c2chrom('NM_017780.2:c.109A>T')
+        assert_equal(genomic, 'NC_000008.10:g.61654100A>T')
+
+    def test_nm_with_selector_chrom2c(self):
+        """
+        NM reference with transcript selection c. to g.
+        """
+        converter = self._converter('hg19')
+        genomic = converter.c2chrom('NM_017780.2(CHD7_v001):c.109A>T')
+        assert_equal(genomic, 'NC_000008.10:g.61654100A>T')
+
+    def test_nm_c2chrom_no_selector(self):
+        """
+        To NM reference should never result in transcript selection.
+        """
+        converter = self._converter('hg19')
+        variant = converter.correctChrVariant('NC_000008.10:g.61654100A>T')
+        coding = converter.chrom2c(variant, 'list')
+        assert 'NM_017780.2:c.109A>T' in coding
+
+    def test_incorrect_selector_c2chrom(self):
+        """
+        Incorrect selector.
+        """
+        converter = self._converter('hg19')
+        genomic = converter.c2chrom('NM_017780.2(CHD8):c.109A>T')
+        erange = self.output.getMessagesWithErrorCode('EACCNOTINDB')
+        assert_equal(len(erange), 1)
+
+    def test_incorrect_selector_version_c2chrom(self):
+        """
+        Incorrect selector version.
+        """
+        converter = self._converter('hg19')
+        genomic = converter.c2chrom('NM_017780.2(CHD7_v002):c.109A>T')
+        erange = self.output.getMessagesWithErrorCode('EACCNOTINDB')
+        assert_equal(len(erange), 1)
+
+    def test_no_selector_version_c2chrom(self):
+        """
+        Selector but no selector version.
+        """
+        converter = self._converter('hg19')
+        genomic = converter.c2chrom('NM_017780.2(CHD7):c.109A>T')
+        assert_equal(genomic, 'NC_000008.10:g.61654100A>T')
+
+    def test_incorrect_selector_no_selector_version_c2chrom(self):
+        """
+        Incorrect selector, no selector version.
+        """
+        converter = self._converter('hg19')
+        genomic = converter.c2chrom('NM_017780.2(CHD8):c.109A>T')
+        erange = self.output.getMessagesWithErrorCode('EACCNOTINDB')
+        assert_equal(len(erange), 1)
-- 
GitLab