From 843a09f8fa3fbb86f9b4b0ae698af258c1e6b5ff Mon Sep 17 00:00:00 2001
From: Chris Maffeo <cmaffeo2@illinois.edu>
Date: Fri, 21 Sep 2018 20:09:26 -0500
Subject: [PATCH] Improved readability of code for connection beads

---
 mrdna/segmentmodel.py | 45 +++++++++++++++++++------------------------
 1 file changed, 20 insertions(+), 25 deletions(-)

diff --git a/mrdna/segmentmodel.py b/mrdna/segmentmodel.py
index 4101d4b..af50091 100644
--- a/mrdna/segmentmodel.py
+++ b/mrdna/segmentmodel.py
@@ -1891,31 +1891,26 @@ class SegmentModel(ArbdModel):
             ## TODO: offload the work here to s1/s2 (?)
             a1,a2 = [l.address   for l in (A,B)]
 
-            if A.particle is None:
-                b = s1.get_nearest_bead(a1)
-                try:
-                    bead_is_far = s1.contour_to_nt_pos(np.abs(b.contour_position-a1)) >= min(max_basepairs_per_bead, max_nucleotides_per_bead)*0.5
-                    if bead_is_far or "is_intrahelical" in b.__dict__:
-                        raise Exception("not close")
-                    ## combine beads
-                    b.update_position( 0.5*(b.contour_position + a1) ) # avg position
-                except:
-                    b = s1._generate_one_bead(a1,0)
-                A.particle = b
-                b.locations.append(A)
-
-            if B.particle is None:
-                b = s2.get_nearest_bead(a2)
-                try:
-                    bead_is_far = s2.contour_to_nt_pos(np.abs(b.contour_position-a2)) >= min(max_basepairs_per_bead, max_nucleotides_per_bead)*0.5
-                    if bead_is_far or "is_intrahelical" in b.__dict__:
-                        raise Exception("not close")
-                    ## combine beads
-                    b.update_position( 0.5*(b.contour_position + a2) ) # avg position
-                except:
-                    b = s2._generate_one_bead(a2,0)
-                B.particle = b
-                b.locations.append(B)
+            def maybe_add_bead(location, seg, address, ):
+                if location.particle is None:
+                    b = seg.get_nearest_bead(address)
+                    try:
+                        distance = seg.contour_to_nt_pos(np.abs(b.contour_position-address))
+                        max_distance =  min(max_basepairs_per_bead, max_nucleotides_per_bead)*0.5
+                        if "is_intrahelical" in b.__dict__:
+                            max_distance = 0.5
+                        if distance >= max_distance:
+                            raise Exception("except")
+
+                        ## combine beads
+                        b.update_position( 0.5*(b.contour_position + address) ) # avg position
+                    except:
+                        b = seg._generate_one_bead(address,0)
+                    location.particle = b
+                    b.locations.append(location)
+
+            maybe_add_bead(A,s1,a1)
+            maybe_add_bead(B,s2,a2)
 
         """ Some tests """
         for c,A,B in self.get_connections("intrahelical"):
-- 
GitLab