[med-svn] [python-pymummer] 01/05: Imported Upstream version 0.7.0

Afif Elghraoui afif-guest at moszumanska.debian.org
Fri Feb 12 09:31:33 UTC 2016


This is an automated email from the git hooks/post-receive script.

afif-guest pushed a commit to branch master
in repository python-pymummer.

commit 6772d7727b96fe493176f7aa466402d885346c2a
Author: Afif Elghraoui <afif at ghraoui.name>
Date:   Fri Feb 12 01:05:59 2016 -0800

    Imported Upstream version 0.7.0
---
 pymummer/alignment.py            |  35 ++++++++++++
 pymummer/tests/alignment_test.py | 114 ++++++++++++++++++++++++++++++++++++++-
 setup.py                         |   2 +-
 3 files changed, 149 insertions(+), 2 deletions(-)

diff --git a/pymummer/alignment.py b/pymummer/alignment.py
index 1e73250..ac066bb 100644
--- a/pymummer/alignment.py
+++ b/pymummer/alignment.py
@@ -1,4 +1,5 @@
 import pyfastaq
+from pymummer import variant
 
 class Error (Exception): pass
 
@@ -134,3 +135,37 @@ class Alignment:
                 self.ref_name
         ])
 
+
+    def qry_coords_from_ref_coord(self, ref_coord, variant_list):
+        '''Given a reference position and a list of variants ([variant.Variant]),
+           works out the position in the query sequence, accounting for indels.
+           Returns a tuple: (position, True|False), where second element is whether
+           or not the ref_coord lies in an indel. If it is, then
+           returns the corresponding start position
+           of the indel in the query'''
+        if self.ref_coords().distance_to_point(ref_coord) > 0:
+            raise Error('Cannot get query coord in qry_coords_from_ref_coord because given ref_coord ' + str(ref_coord) + ' does not lie in nucmer alignment:\n' + str(self))
+
+        indel_variant_indexes = []
+
+        for i in range(len(variant_list)):
+            if variant_list[i].var_type not in {variant.INS, variant.DEL}:
+                continue
+            if variant_list[i].ref_start <= ref_coord <= variant_list[i].ref_end:
+                return variant_list[i].qry_start, True
+            elif variant_list[i].ref_start < ref_coord:
+                indel_variant_indexes.append(i)
+
+        distance = ref_coord - min(self.ref_start, self.ref_end)
+
+        for i in indel_variant_indexes:
+            if variant_list[i].var_type == variant.INS:
+                distance += len(variant_list[i].qry_base)
+            else:
+                assert variant_list[i].var_type == variant.DEL
+                distance -= len(variant_list[i].ref_base)
+
+        if self.on_same_strand():
+            return min(self.qry_start, self.qry_end) + distance, False
+        else:
+            return max(self.qry_start, self.qry_end) - distance, False
diff --git a/pymummer/tests/alignment_test.py b/pymummer/tests/alignment_test.py
index 0574f22..0db2701 100644
--- a/pymummer/tests/alignment_test.py
+++ b/pymummer/tests/alignment_test.py
@@ -1,7 +1,7 @@
 import unittest
 import os
 import pyfastaq
-from pymummer import alignment
+from pymummer import alignment, snp, variant
 
 modules_dir = os.path.dirname(os.path.abspath(alignment.__file__))
 data_dir = os.path.join(modules_dir, 'tests', 'data')
@@ -129,3 +129,115 @@ class TestNucmer(unittest.TestCase):
         expected = '8 80.00 1 10 qry 100 110 ref'
         self.assertEqual(expected, a.to_msp_crunch())
 
+
+    def test_qry_coords_from_ref_coord_test_bad_ref_coord(self):
+        '''Test qry_coords_from_ref_coord with bad ref coords'''
+        aln = alignment.Alignment('\t'.join(['100', '200', '1', '100', '100', '100', '100.00', '300', '300', '1', '1', 'ref', 'qry']))
+        with self.assertRaises(alignment.Error):
+            got = aln.qry_coords_from_ref_coord(98, [])
+
+        with self.assertRaises(alignment.Error):
+            got = aln.qry_coords_from_ref_coord(200, [])
+
+
+    def test_qry_coords_from_ref_coord_test_same_strand(self):
+        '''Test qry_coords_from_ref_coord on same strand'''
+        aln = alignment.Alignment('\t'.join(['100', '200', '1', '101', '100', '100', '100.00', '300', '300', '1', '1', 'ref', 'qry']))
+        snp0 = snp.Snp('\t'.join(['140', 'A', 'T', '40', 'x', 'x', '300', '300', 'x', 'x', 'ref', 'qry'])) # snp
+        snp0 = variant.Variant(snp0)
+        snp1 = snp.Snp('\t'.join(['140', 'A', '.', '40', 'x', 'x', '300', '300', 'x', 'x', 'ref', 'qry'])) # del from qry
+        snp2 = snp.Snp('\t'.join(['141', 'C', '.', '40', 'x', 'x', '300', '300', 'x', 'x', 'ref', 'qry'])) # del from qry
+        del1 = variant.Variant(snp1)
+        del2 = variant.Variant(snp1)
+        self.assertTrue(del2.update_indel(snp2))
+        snp3 = snp.Snp('\t'.join(['150', '.', 'A', '50', 'x', 'x', '300', '300', 'x', 'x', 'ref', 'qry'])) # del from ref
+        snp4 = snp.Snp('\t'.join(['150', '.', 'C', '51', 'x', 'x', '300', '300', 'x', 'x', 'ref', 'qry'])) # del from ref
+        snp5 = snp.Snp('\t'.join(['150', '.', 'G', '52', 'x', 'x', '300', '300', 'x', 'x', 'ref', 'qry'])) # del from ref
+        ins1 = variant.Variant(snp3)
+        ins2 = variant.Variant(snp3)
+        self.assertTrue(ins2.update_indel(snp4))
+        self.assertTrue(ins2.update_indel(snp5))
+
+        tests = [
+            (99, [], (0, False)),
+            (100, [], (1, False)),
+            (199, [], (100, False)),
+            (119, [del1], (20, False)),
+            (149, [], (50, False)),
+            (149, [del1], (49, False)),
+            (149, [del2], (48, False)),
+            (159, [], (60, False)),
+            (159, [ins1], (61, False)),
+            (159, [ins2], (63, False)),
+            (159, [del1, ins1], (60, False)),
+            (159, [del1, ins2], (62, False)),
+            (159, [del2, ins1], (59, False)),
+            (159, [del2, ins2], (61, False)),
+            (139, [del1], (39, True)),
+            (139, [snp0], (40, False)),
+            (149, [ins1], (49, True)),
+        ]
+
+        for ref_coord, variant_list, expected in tests:
+            got = aln.qry_coords_from_ref_coord(ref_coord, variant_list)
+            self.assertEqual(expected, got)
+            # if we reverse the direction of hit in query and reference, should get the same answer
+            aln.qry_start, aln.qry_end = aln.qry_end, aln.qry_start
+            aln.ref_start, aln.ref_end = aln.ref_end, aln.ref_start
+            got = aln.qry_coords_from_ref_coord(ref_coord, variant_list)
+            self.assertEqual(expected, got)
+            aln.qry_start, aln.qry_end = aln.qry_end, aln.qry_start
+            aln.ref_start, aln.ref_end = aln.ref_end, aln.ref_start
+
+
+    def test_qry_coords_from_ref_coord_test_different_strand(self):
+        '''Test qry_coords_from_ref_coord on different strand'''
+        aln = alignment.Alignment('\t'.join(['100', '200', '101', '1', '100', '100', '100.00', '300', '300', '1', '1', 'ref', 'qry']))
+        snp0 = snp.Snp('\t'.join(['140', 'A', 'T', '40', 'x', 'x', '300', '300', 'x', 'x', 'ref', 'qry'])) # snp
+        snp0 = variant.Variant(snp0)
+        snp1 = snp.Snp('\t'.join(['140', 'A', '.', '40', 'x', 'x', '300', '300', 'x', 'x', 'ref', 'qry'])) # del from qry
+        snp2 = snp.Snp('\t'.join(['141', 'C', '.', '40', 'x', 'x', '300', '300', 'x', 'x', 'ref', 'qry'])) # del from qry
+        del1 = variant.Variant(snp1)
+        del2 = variant.Variant(snp1)
+        self.assertTrue(del2.update_indel(snp2))
+        snp3 = snp.Snp('\t'.join(['150', '.', 'A', '50', 'x', 'x', '300', '300', 'x', 'x', 'ref', 'qry'])) # del from ref
+        snp4 = snp.Snp('\t'.join(['150', '.', 'C', '51', 'x', 'x', '300', '300', 'x', 'x', 'ref', 'qry'])) # del from ref
+        snp5 = snp.Snp('\t'.join(['150', '.', 'G', '52', 'x', 'x', '300', '300', 'x', 'x', 'ref', 'qry'])) # del from ref
+        ins1 = variant.Variant(snp3)
+        ins2 = variant.Variant(snp3)
+        self.assertTrue(ins2.update_indel(snp4))
+        self.assertTrue(ins2.update_indel(snp5))
+
+        tests = [
+            (99, [], (100, False)),
+            (100, [], (99, False)),
+            (199, [], (0, False)),
+            (119, [], (80, False)),
+            (119, [del1], (80, False)),
+            (149, [], (50, False)),
+            (149, [del1], (51, False)),
+            (149, [del2], (52, False)),
+            (159, [], (40, False)),
+            (159, [ins1], (39, False)),
+            (159, [ins2], (37, False)),
+            (159, [del1, ins1], (40, False)),
+            (159, [del1, ins2], (38, False)),
+            (159, [del2, ins1], (41, False)),
+            (159, [del2, ins2], (39, False)),
+            (139, [del1], (39, True)),
+            (139, [snp0], (60, False)),
+            (149, [ins1], (49, True)),
+        ]
+
+        for ref_coord, variant_list, expected in tests:
+            got = aln.qry_coords_from_ref_coord(ref_coord, variant_list)
+            self.assertEqual(expected, got)
+            # if we reverse the direction of hit in query and reference, should get the same answer
+            aln.qry_start, aln.qry_end = aln.qry_end, aln.qry_start
+            aln.ref_start, aln.ref_end = aln.ref_end, aln.ref_start
+            got = aln.qry_coords_from_ref_coord(ref_coord, variant_list)
+            self.assertEqual(expected, got)
+            aln.qry_start, aln.qry_end = aln.qry_end, aln.qry_start
+            aln.ref_start, aln.ref_end = aln.ref_end, aln.ref_start
+
+
diff --git a/setup.py b/setup.py
index 36dcac8..150d9c4 100644
--- a/setup.py
+++ b/setup.py
@@ -26,7 +26,7 @@ if not found_all_progs:
 
 setup(
     name='pymummer',
-    version='0.6.1',
+    version='0.7.0',
     description='Wrapper for MUMmer',
     packages = find_packages(),
     author='Martin Hunt, Nishadi De Silva',

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-med/python-pymummer.git



More information about the debian-med-commit mailing list