From 22eda98afa7eb02b5d5bcc4546f067ebdf9623d5 Mon Sep 17 00:00:00 2001
From: Chris Maffeo <cmaffeo2@illinois.edu>
Date: Wed, 18 Jul 2018 18:58:35 -0500
Subject: [PATCH] Made intrahelical connections remove any existing
 connections; important for intrahelical "crossovers"

---
 segmentmodel.py | 41 +++++++++++++++++++++++++++++++++++------
 1 file changed, 35 insertions(+), 6 deletions(-)

diff --git a/segmentmodel.py b/segmentmodel.py
index 76c73d9..b00a29f 100644
--- a/segmentmodel.py
+++ b/segmentmodel.py
@@ -104,6 +104,12 @@ class Connection():
         else:
             raise Exception("OutOfBoundsError")
 
+    def delete(self):
+        self.A.container.connections.remove(self)
+        self.B.container.connections.remove(self)
+        self.A.connection = None
+        self.B.connection = None
+
     def __repr__(self):
         return "<Connection {}--{}--{}]>".format( self.A, self.type_, self.B )
         
@@ -858,6 +864,15 @@ class DoubleStrandedSegment(Segment):
             assert( isinstance(end, Location) )
             assert( end.type_ in ("end3","end5") )
         assert( end1.type_ != end2.type_ )
+
+        ## Remove other connections involving these points
+        if end1.connection is not None:
+            print("WARNING: reconnecting {}".format(end1))
+            end1.connection.delete()
+        if end2.connection is not None:
+            print("WARNING: reconnecting {}".format(end2))
+            end2.connection.delete()
+
         ## Create and add connection
         if end2.type_ == "end5":
             end1.container._connect( end2.container, Connection( end1, end2, type_=type_ ), in_3prime_direction=True )
@@ -923,19 +938,33 @@ class SingleStrandedSegment(Segment):
     def _connect_end(self, other, _5_to_3, force_connection):
         assert( isinstance(other, Location) )
         if _5_to_3 == True:
-            my_end = self.end3
+            seg1 = self
+            seg2 = other.container
+            end1 = self.end3
+            end2 = other
             # assert( other.type_ == "end5" )
             if (other.type_ is not "end5"):
                 print("Warning: code does not prevent connecting 3prime to 3prime, etc")
-            conn = Connection( my_end, other, type_="intrahelical" )
-            self._connect( other.container, conn, in_3prime_direction=True )
         else:
-            my_end = self.end5
+            seg1 = other.container
+            seg2 = self
+            end1 = other
+            end2 = self.end5
             # assert( other.type_ == "end3" )
             if (other.type_ is not "end3"):
                 print("Warning: code does not prevent connecting 3prime to 3prime, etc")
-            conn = Connection( other, my_end, type_="intrahelical" )
-            other.container._connect( self, conn, in_3prime_direction=True )
+
+        ## Remove other connections involving these points
+        if end1.connection is not None:
+            print("WARNING: reconnecting {}".format(end1))
+            end1.connection.delete()
+        if end2.connection is not None:
+            print("WARNING: reconnecting {}".format(end2))
+            end2.connection.delete()
+
+        conn = Connection( end1, end2, type_="intrahelical" )
+        seg1._connect( seg2, conn, in_3prime_direction=True )
+
 
     def add_crossover(self, nt, other, other_nt, strands_fwd=(True,False), nt_on_5prime=True):
         """ Add a crossover between two helices """
-- 
GitLab