Commits

Bob Harris committed 2792576

fixed problem when overlap is allowed; also simplified the caller's burden for generating genomic intervals

Comments (0)

Files changed (1)

lib/bx/intervals/random.py

                           passed the (start,stop,region) for each generated 
                           interval, where region is an entry in the regions
                           list.  If this is None, the generated intervals will
-                          be returned in a list
+                          be returned as a list of elements copied from the
+                          region with start and end modified.
     """
     # Copy regions
     regions = [( x[1]-x[0], x[0], x ) for x in regions]
         return
     else:
         intervals = []
-        save_interval_func = lambda s, e, rgn: intervals.append( ( s, e, rgn ) )
+        save_interval_func = lambda s, e, rgn: intervals.append( overwrite_start_end ( s, e, rgn ) )
         throw_random_private( lengths, regions, save_interval_func, allow_overlap )
         return intervals
 
+def overwrite_start_end(s,e,rgn):
+	rgn = list(rgn)
+	rgn[0] = s
+	rgn[1] = e
+	return tuple(rgn)
+
+
 def throw_random_private( lengths, regions, save_interval_func, allow_overlap=False, three_args=True ):
     """
     (Internal function;  we expect calls only through the interface functions
         #..    if ix <= hi_rgn: print "%2s: %5s %5s %5s" % ( ix, region[1], region[0], cc[ix] )
         #..    else:            print "%2s: %5s %5s %5s" % ( ix, region[1], region[0], "X" )
         #..print "s = %s (of %s candidates)" % ( s, candidates )
-        # .Locate region containing that candidate, by binary search
+        # Locate region containing that candidate, by binary search
         lo = 0
         hi = hi_rgn
         while hi > lo:
         s -= cc[lo]
         # If we are not allowing overlaps we will remove the placed interval
         # from the region list
-        if not allow_overlap:
+        if allow_overlap:
+            rgn_length, rgn_start, rgn_extra = regions[lo]
+        else:
             # Remove the chosen region and split
             rgn_length, rgn_start, rgn_extra = regions.pop( lo )
             rgn_end = rgn_start + rgn_length
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.