Refactoring.
authorMartin C. Frith
Tue Feb 28 18:21:21 2017 +0900 (2017-02-28)
changeset 8361b4db1600728
parent 835 cdb1e40c28e4
child 837 2c3511fa1734
Refactoring.
scripts/last-dotplot
     1.1 --- a/scripts/last-dotplot	Tue Feb 28 17:33:22 2017 +0900
     1.2 +++ b/scripts/last-dotplot	Tue Feb 28 18:21:21 2017 +0900
     1.3 @@ -184,12 +184,11 @@
     1.4          beg2 -= next_pix
     1.5          size -= next_pix
     1.6  
     1.7 -def alignmentPixels(width, height, alignments, bp_per_pix,
     1.8 -                    seq_start_dic1, seq_start_dic2):
     1.9 +def alignmentPixels(width, height, alignments, bp_per_pix, origins1, origins2):
    1.10      hits = [0] * (width * height)  # the image data
    1.11      for seq1, seq2, blocks in alignments:
    1.12 -        ori1 = seq_start_dic1[seq1] * bp_per_pix
    1.13 -        ori2 = seq_start_dic2[seq2] * bp_per_pix
    1.14 +        ori1 = origins1[seq1]
    1.15 +        ori2 = origins2[seq2]
    1.16          for beg1, beg2, size in blocks:
    1.17              if beg1 < 0:
    1.18                  beg1 = -(beg1 + size)
    1.19 @@ -228,18 +227,18 @@
    1.20          bridgedText = w[7]
    1.21          yield seqName, beg, end, bridgedText
    1.22  
    1.23 -def drawUnsequencedGaps(im, gaps, start_dic, margin, limit, isTop, bridgedText,
    1.24 +def drawUnsequencedGaps(im, gaps, origins, margin, limit, isTop, bridgedText,
    1.25                          bp_per_pix, color):
    1.26      '''Draw rectangles representing unsequenced gaps.'''
    1.27      for seqName, beg, end, b in gaps:
    1.28          if b != bridgedText: continue
    1.29 -        if seqName not in start_dic: continue
    1.30 -        origin = start_dic[seqName]
    1.31 -        b = div_ceil(beg, bp_per_pix)  # use fully-covered pixels only
    1.32 -        e = end // bp_per_pix
    1.33 +        if seqName not in origins: continue
    1.34 +        ori = origins[seqName]
    1.35 +        b = div_ceil(ori + beg, bp_per_pix)  # use fully-covered pixels only
    1.36 +        e = (ori + end) // bp_per_pix
    1.37          if e <= b: continue
    1.38 -        if isTop: box = origin + b, margin, origin + e, limit
    1.39 -        else:     box = margin, origin + b, limit, origin + e
    1.40 +        if isTop: box = b, margin, e, limit
    1.41 +        else:     box = margin, b, limit, e
    1.42          im.paste(color, box)
    1.43  
    1.44  def make_label(text, text_size, range_start, range_size):
    1.45 @@ -278,6 +277,10 @@
    1.46          draw.text(position, i[3], font=font, fill=opts.text_color)
    1.47      return im
    1.48  
    1.49 +def seqOrigins(seq_names, seq_starts, bp_per_pix):
    1.50 +    for i, j in zip(seq_names, seq_starts):
    1.51 +        yield i, bp_per_pix * j
    1.52 +
    1.53  def lastDotplot(opts, args):
    1.54      if opts.fontfile:  font = ImageFont.truetype(opts.fontfile, opts.fontsize)
    1.55      else:              font = ImageFont.load_default()
    1.56 @@ -311,29 +314,29 @@
    1.57                                                   opts.pix_tween_seqs, margin1)
    1.58      seq_pix2, seq_starts2, height = get_pix_info(seq_sizes2, bp_per_pix,
    1.59                                                   opts.pix_tween_seqs, margin2)
    1.60 -    seq_start_dic1 = dict(zip(seq_names1, seq_starts1))
    1.61 -    seq_start_dic2 = dict(zip(seq_names2, seq_starts2))
    1.62 +    origins1 = dict(seqOrigins(seq_names1, seq_starts1, bp_per_pix))
    1.63 +    origins2 = dict(seqOrigins(seq_names2, seq_starts2, bp_per_pix))
    1.64  
    1.65      warn("processing alignments...")
    1.66      hits = alignmentPixels(width, height, alignments, bp_per_pix,
    1.67 -                           seq_start_dic1, seq_start_dic2)
    1.68 +                           origins1, origins2)
    1.69      warn("done")
    1.70  
    1.71      image_size = width, height
    1.72      im = Image.new(image_mode, image_size, opts.background_color)
    1.73  
    1.74 -    start_dic1 = expandedSeqDict(seq_start_dic1)
    1.75 -    start_dic2 = expandedSeqDict(seq_start_dic2)
    1.76 +    origins1 = expandedSeqDict(origins1)
    1.77 +    origins2 = expandedSeqDict(origins2)
    1.78      gaps1 = list(readGaps(opts.gap1))
    1.79      gaps2 = list(readGaps(opts.gap2))
    1.80      # draw bridged gaps first, then unbridged gaps on top:
    1.81 -    drawUnsequencedGaps(im, gaps1, start_dic1, margin2, height, True, "yes",
    1.82 +    drawUnsequencedGaps(im, gaps1, origins1, margin2, height, True, "yes",
    1.83                          bp_per_pix, opts.bridged_color)
    1.84 -    drawUnsequencedGaps(im, gaps2, start_dic2, margin1, width, False, "yes",
    1.85 +    drawUnsequencedGaps(im, gaps2, origins2, margin1, width, False, "yes",
    1.86                          bp_per_pix, opts.bridged_color)
    1.87 -    drawUnsequencedGaps(im, gaps1, start_dic1, margin2, height, True, "no",
    1.88 +    drawUnsequencedGaps(im, gaps1, origins1, margin2, height, True, "no",
    1.89                          bp_per_pix, opts.unbridged_color)
    1.90 -    drawUnsequencedGaps(im, gaps2, start_dic2, margin1, width, False, "no",
    1.91 +    drawUnsequencedGaps(im, gaps2, origins2, margin1, width, False, "no",
    1.92                          bp_per_pix, opts.unbridged_color)
    1.93  
    1.94      for i in range(height):