Exemple #1
0
 def __call__(self, X, Y):
     sentinel = (sys.maxint, sys.maxint)
     X = common.sentinelize(X, sentinel)
     Y = common.sentinelize(Y, sentinel)
     x = X.next()
     y = Y.next()
     if x == sentinel or y == sentinel: continue_loop = False
     else:                              continue_loop = True
     while continue_loop:
         open_window  = y[0]
         close_window = y[1]
         # Extend the y window as long as possible #
         while True:
             if y == sentinel: break
             y = Y.next()
             if y[0] > close_window: break
             if y[1] > close_window: close_window = y[1]
         # Read features from X until overshooting the y window #
         while True:
             if x[0] >= close_window: break
             if x[1] >  open_window:  yield x
             x = X.next()
             if x == sentinel:
                 continue_loop = False
                 break
Exemple #2
0
 def __call__(self, X, Y):
     # Sentinel #
     sentinel = (sys.maxint, sys.maxint, 0.0)
     X = common.sentinelize(X, sentinel)
     # Growing and shrinking list of features in X #
     F = [(-sys.maxint, -sys.maxint, 0.0)]
     # --- Core loop --- #
     for y in Y:
         # Check that we have all the scores necessary #
         xnext = F[-1]
         while xnext[0] < y[1]: 
             xnext = X.next()
             if xnext[1] > y[0]: F.append(xnext)
         # Throw away the scores that are not needed anymore #
         n = 0
         while F[n][1] <= y[0]: 
             n+=1
         F = F[n:]
         # Compute the average #
         score = 0.0
         for f in F:
             if y[1] <= f[0]: continue
             if f[0] <  y[0]: start = y[0]
             else:            start = f[0]
             if y[1] <  f[1]: end   = y[1]
             else:            end   = f[1]
             score += (end-start) * f[2]
         # Emit a feature #
         yield y[0:3]+(score/(y[1]-y[0]),)+y[4:]
Exemple #3
0
 def __call__(self, X, Y):
     def make_name(a, b):
         if a and b: return a + ' + ' + b
         elif a: return a
         return b
     def make_feature(a, b):
         return (max(a[0],b[0]),
                 min(a[1],b[1]),
                 make_name(a[2], b[2]),
                 (a[3]+b[3])/2.0,
                 a[4]==b[4] and b[4] or 0)
     # Preparation #
     sentinel = (sys.maxint, sys.maxint)
     X = common.sentinelize(X, sentinel)
     Y = common.sentinelize(Y, sentinel)
     x = X.next()
     y = Y.next()
     Wx = []
     Wy = []
     # Core loop stops when both x and y are at the sentinel
     while x[0] != sentinel or y[0] != sentinel:
         # Take the leftmost current feature and scan it against the other window
         if x[0] < y[0]:
             # Remove features from the y window that are left of x
             Wy = [f for f in Wy if f[1] > x[0]]
             # Yield new features with all overlaps of x in Wy
             for f in [f for f in Wy if f[1] > x[0] and x[1] > f[0]]: yield make_feature(x, f)
             # Put x in the window only if it is not left of y
             if x[1] >= y[0]: Wx.append(x)
             # Advance current x feature
             x = X.next()
         else:
             # Remove features from the x window that are left of y
             Wx = [f for f in Wx if f[1] > y[0]]
             # Yield new features with all overlaps of y in Wx
             for f in [f for f in Wx if f[1] > y[0] and y[1] > f[0]]: yield make_feature(y, f)
             # Put y in the window only if it is not left of x
             if y[1] >= x[0]: Wy.append(y)
             # Advance current y feature
             y = Y.next()
Exemple #4
0
 def __call__(self, X, L, stop_val):
     # Sentinel #
     sentinel = (sys.maxint, sys.maxint, 0.0)
     X = common.sentinelize(X, sentinel)
     # Growing and shrinking list of features around our moving window #
     F = []
     # Current position counting on nucleotides (first nucleotide is zero) #
     p = -L-2
     # Position since which the mean hasn't changed #
     same_since = -L-3
     # The current mean and the next mean #
     curt_mean = 0
     next_mean = 0
     # Multipication factor instead of division #
     f = 1.0 / (2*L+1)
     # First feature if it exists #
     F.append(X.next())
     if F == [sentinel]: return
     # --- Core loop --- #
     while True:
         # Advance one #
         p += 1
         # Window start and stop #
         s = p-L
         e = p+L+1
         # Scores entering window #
         if F[-1][1] < e: F.append(X.next())
         if F[-1][0] < e: next_mean += F[-1][2] * f
         # Scores exiting window #
         if F[ 0][1] < s: F.pop(0)
         if F[ 0][0] < s: next_mean -= F[ 0][2] * f
         # Border condition on the left #
         if p < 0:
             curt_mean = 0
             same_since = p
             continue
         # Border condition on the right #
         if p == stop_val:
             if curt_mean != 0: yield (same_since, p, curt_mean)
             break
         # Maybe emit a feature #
         if next_mean != curt_mean:
             if curt_mean != 0: yield (same_since, p, curt_mean)
             curt_mean  = next_mean
             same_since = p
Exemple #5
0
 def __call__(self, list_of_tracks, geometric=False):
     # Get all iterators #
     sentinel = (sys.maxint, sys.maxint, 0.0)
     tracks = [common.sentinelize(x, sentinel) for x in list_of_tracks]
     elements = [x.next() for x in tracks]
     # Declare meaning functions #
     def arithmetic_mean(scores): return sum(scores)*tracks_denom
     def geometric_mean(scores):  return sum(scores)**tracks_denom
     tracks_denom = 1.0/len(tracks)
     # Choose meaning function #
     if not geometric: mean_fn = arithmetic_mean
     else:             mean_fn = geometric_mean
     # Check empty #
     for i in xrange(len(tracks)-1, -1, -1):
         if elements[i] == sentinel:
             tracks.pop(i)
             elements.pop(i)
     # Core loop #
     while tracks:
         # Find the next boundaries #
         start = min([x[0] for x in elements])
         end = min([x[0] for x in elements if x[0] > start] + [x[1] for x in elements])
         # Scores between boundaries #
         scores = [x[2] for x in elements if x[1] > start and x[0] < end]
         if scores: yield (start, end, mean_fn(scores))
         # Iterate over elements #
         for i in xrange(len(tracks)-1, -1, -1):
             # Change all starts #
             if elements[i][0] < end:
                 elements[i] = (end, elements[i][1], elements[i][2])
             # Advance the elements that need to be advanced #
             if elements[i][1] <= end:
                 elements[i] = tracks[i].next()
             # Pop sentinels #
             if elements[i] == sentinel:
                 tracks.pop(i)
                 elements.pop(i)