Refactor last-dotplot
authorMartin C. Frith
Thu Nov 02 22:08:14 2017 +0900 (2017-11-02)
changeset 9005ce3e786e05a
parent 899 ccf8902bf86c
child 901 2003b209d750
Refactor last-dotplot
scripts/last-dotplot
     1.1 --- a/scripts/last-dotplot	Thu Nov 02 21:56:30 2017 +0900
     1.2 +++ b/scripts/last-dotplot	Thu Nov 02 22:08:14 2017 +0900
     1.3 @@ -245,22 +245,22 @@
     1.4              return -negBpPerPix
     1.5          negBpPerPix -= 1
     1.6  
     1.7 -def get_seq_starts(seq_pix, pixTweenRanges, margin):
     1.8 -    '''Get the start pixel for each sequence.'''
     1.9 -    seq_starts = []
    1.10 +def getRangePixBegs(rangePixLens, pixTweenRanges, margin):
    1.11 +    '''Get the start pixel for each range.'''
    1.12 +    rangePixBegs = []
    1.13      pix_tot = margin - pixTweenRanges
    1.14 -    for i in seq_pix:
    1.15 +    for i in rangePixLens:
    1.16          pix_tot += pixTweenRanges
    1.17 -        seq_starts.append(pix_tot)
    1.18 +        rangePixBegs.append(pix_tot)
    1.19          pix_tot += i
    1.20 -    return seq_starts
    1.21 +    return rangePixBegs
    1.22  
    1.23  def pixelData(rangeSizes, bp_per_pix, pixTweenRanges, margin):
    1.24 -    '''Return pixel information about the sequences.'''
    1.25 -    seq_pix = [div_ceil(i, bp_per_pix) for i in rangeSizes]
    1.26 -    seq_starts = get_seq_starts(seq_pix, pixTweenRanges, margin)
    1.27 -    tot_pix = seq_starts[-1] + seq_pix[-1]
    1.28 -    return seq_pix, seq_starts, tot_pix
    1.29 +    '''Return pixel information about the ranges.'''
    1.30 +    rangePixLens = [div_ceil(i, bp_per_pix) for i in rangeSizes]
    1.31 +    rangePixBegs = getRangePixBegs(rangePixLens, pixTweenRanges, margin)
    1.32 +    tot_pix = rangePixBegs[-1] + rangePixLens[-1]
    1.33 +    return rangePixBegs, rangePixLens, tot_pix
    1.34  
    1.35  def drawLineForward(hits, width, bp_per_pix, beg1, beg2, size):
    1.36      while True:
    1.37 @@ -450,13 +450,13 @@
    1.38              out.append(i)
    1.39      return out
    1.40  
    1.41 -def axisImage(labelData, seq_starts, seq_pix, textRot,
    1.42 +def axisImage(labelData, rangePixBegs, rangePixLens, textRot,
    1.43                textAln, font, image_mode, opts):
    1.44      '''Make an image of axis labels.'''
    1.45 -    beg = seq_starts[0]
    1.46 -    end = seq_starts[-1] + seq_pix[-1]
    1.47 +    beg = rangePixBegs[0]
    1.48 +    end = rangePixBegs[-1] + rangePixLens[-1]
    1.49      margin = max(i[2] for i in labelData)
    1.50 -    labels = map(make_label, labelData, seq_starts, seq_pix)
    1.51 +    labels = map(make_label, labelData, rangePixBegs, rangePixLens)
    1.52      labels = [i for i in labels if i[1] >= beg and i[2] <= end]
    1.53      labels.sort()
    1.54      minPixTweenLabels = 0 if textRot else opts.label_space
    1.55 @@ -470,8 +470,8 @@
    1.56          draw.text(position, i[3], font=font, fill=opts.text_color)
    1.57      return im
    1.58  
    1.59 -def seqOrigins(seqNames, seq_starts, seqLimits, bp_per_pix):
    1.60 -    for i, j in zip(seqNames, seq_starts):
    1.61 +def seqOrigins(seqNames, rangePixBegs, seqLimits, bp_per_pix):
    1.62 +    for i, j in zip(seqNames, rangePixBegs):
    1.63          yield i, bp_per_pix * j - seqLimits[i][0]
    1.64  
    1.65  def lastDotplot(opts, args):
    1.66 @@ -508,15 +508,17 @@
    1.67      bpPerPix = max(bpPerPix1, bpPerPix2)
    1.68      warn("bp per pixel = " + str(bpPerPix))
    1.69  
    1.70 -    seq_pix1, seq_starts1, width  = pixelData(rangeSizes1, bpPerPix,
    1.71 -                                              opts.border_pixels, lMargin)
    1.72 -    seq_pix2, seq_starts2, height = pixelData(rangeSizes2, bpPerPix,
    1.73 -                                              opts.border_pixels, tMargin)
    1.74 +    p1 = pixelData(rangeSizes1, bpPerPix, opts.border_pixels, lMargin)
    1.75 +    rangePixBegs1, rangePixLens1, width = p1
    1.76 +
    1.77 +    p2 = pixelData(rangeSizes2, bpPerPix, opts.border_pixels, tMargin)
    1.78 +    rangePixBegs2, rangePixLens2, height = p2
    1.79 +
    1.80      warn("width:  " + str(width))
    1.81      warn("height: " + str(height))
    1.82  
    1.83 -    origins1 = dict(seqOrigins(seqNames1, seq_starts1, seqLimits1, bpPerPix))
    1.84 -    origins2 = dict(seqOrigins(seqNames2, seq_starts2, seqLimits2, bpPerPix))
    1.85 +    origins1 = dict(seqOrigins(seqNames1, rangePixBegs1, seqLimits1, bpPerPix))
    1.86 +    origins2 = dict(seqOrigins(seqNames2, rangePixBegs2, seqLimits2, bpPerPix))
    1.87  
    1.88      warn("processing alignments...")
    1.89      hits = alignmentPixels(width, height, alignments, bpPerPix,
    1.90 @@ -555,22 +557,22 @@
    1.91              elif store_value == 3: im.putpixel(xy, overlap_color)
    1.92  
    1.93      if opts.fontsize != 0:
    1.94 -        axis1 = axisImage(labelData1, seq_starts1, seq_pix1,
    1.95 +        axis1 = axisImage(labelData1, rangePixBegs1, rangePixLens1,
    1.96                            textRot1, False, font, image_mode, opts)
    1.97          if textRot1:
    1.98              axis1 = axis1.transpose(Image.ROTATE_90)
    1.99 -        axis2 = axisImage(labelData2, seq_starts2, seq_pix2,
   1.100 +        axis2 = axisImage(labelData2, rangePixBegs2, rangePixLens2,
   1.101                            textRot2, textRot2, font, image_mode, opts)
   1.102          if not textRot2:
   1.103              axis2 = axis2.transpose(Image.ROTATE_270)
   1.104          im.paste(axis1, (0, 0))
   1.105          im.paste(axis2, (0, 0))
   1.106  
   1.107 -    for i in seq_starts1[1:]:
   1.108 +    for i in rangePixBegs1[1:]:
   1.109          box = i - opts.border_pixels, tMargin, i, height
   1.110          im.paste(opts.border_color, box)
   1.111  
   1.112 -    for i in seq_starts2[1:]:
   1.113 +    for i in rangePixBegs2[1:]:
   1.114          box = lMargin, i - opts.border_pixels, width, i
   1.115          im.paste(opts.border_color, box)
   1.116