scripts/last-dotplot
changeset 896 2238466f36bd
parent 895 8eb06508b3ed
child 897 f7bc7d099e2b
     1.1 --- a/scripts/last-dotplot	Thu Nov 02 19:24:59 2017 +0900
     1.2 +++ b/scripts/last-dotplot	Thu Nov 02 19:44:23 2017 +0900
     1.3 @@ -10,7 +10,8 @@
     1.4  # result is too faint.  How can this be done better?
     1.5  
     1.6  import gzip
     1.7 -import fnmatch, itertools, optparse, os, re, sys
     1.8 +from fnmatch import fnmatchcase
     1.9 +import itertools, optparse, os, re, sys
    1.10  
    1.11  # Try to make PIL/PILLOW work:
    1.12  try: from PIL import Image, ImageDraw, ImageFont, ImageColor
    1.13 @@ -114,7 +115,7 @@
    1.14      if not seqRanges: return 0, seqLen
    1.15      base = name.split(".")[-1]  # allow for names like hg19.chr7
    1.16      for pat, beg, end in seqRanges:
    1.17 -        if fnmatch.fnmatchcase(name, pat) or fnmatch.fnmatchcase(base, pat):
    1.18 +        if fnmatchcase(name, pat) or fnmatchcase(base, pat):
    1.19              return max(beg, 0), min(end, seqLen)
    1.20      return None
    1.21  
    1.22 @@ -216,33 +217,33 @@
    1.23      q, r = divmod(x, y)
    1.24      return q + (r != 0)
    1.25  
    1.26 -def get_bp_per_pix(seq_sizes, pix_tween_seqs, pix_limit):
    1.27 +def get_bp_per_pix(rangeSizes, pixTweenRanges, maxPixels):
    1.28      '''Get the minimum bp-per-pixel that fits in the size limit.'''
    1.29 -    seq_num = len(seq_sizes)
    1.30 -    seq_pix_limit = pix_limit - pix_tween_seqs * (seq_num - 1)
    1.31 -    if seq_pix_limit < seq_num:
    1.32 +    numOfRanges = len(rangeSizes)
    1.33 +    maxPixelsInRanges = maxPixels - pixTweenRanges * (numOfRanges - 1)
    1.34 +    if maxPixelsInRanges < numOfRanges:
    1.35          raise Exception("can't fit the image: too many sequences?")
    1.36 -    negLimit = -seq_pix_limit
    1.37 -    negBpPerPix = sum(seq_sizes) // negLimit
    1.38 +    negLimit = -maxPixelsInRanges
    1.39 +    negBpPerPix = sum(rangeSizes) // negLimit
    1.40      while True:
    1.41 -        if sum(i // negBpPerPix for i in seq_sizes) >= negLimit:
    1.42 +        if sum(i // negBpPerPix for i in rangeSizes) >= negLimit:
    1.43              return -negBpPerPix
    1.44          negBpPerPix -= 1
    1.45  
    1.46 -def get_seq_starts(seq_pix, pix_tween_seqs, margin):
    1.47 +def get_seq_starts(seq_pix, pixTweenRanges, margin):
    1.48      '''Get the start pixel for each sequence.'''
    1.49      seq_starts = []
    1.50 -    pix_tot = margin - pix_tween_seqs
    1.51 +    pix_tot = margin - pixTweenRanges
    1.52      for i in seq_pix:
    1.53 -        pix_tot += pix_tween_seqs
    1.54 +        pix_tot += pixTweenRanges
    1.55          seq_starts.append(pix_tot)
    1.56          pix_tot += i
    1.57      return seq_starts
    1.58  
    1.59 -def get_pix_info(seq_sizes, bp_per_pix, pix_tween_seqs, margin):
    1.60 +def pixelData(rangeSizes, bp_per_pix, pixTweenRanges, margin):
    1.61      '''Return pixel information about the sequences.'''
    1.62 -    seq_pix = [div_ceil(i, bp_per_pix) for i in seq_sizes]
    1.63 -    seq_starts = get_seq_starts(seq_pix, pix_tween_seqs, margin)
    1.64 +    seq_pix = [div_ceil(i, bp_per_pix) for i in rangeSizes]
    1.65 +    seq_starts = get_seq_starts(seq_pix, pixTweenRanges, margin)
    1.66      tot_pix = seq_starts[-1] + seq_pix[-1]
    1.67      return seq_pix, seq_starts, tot_pix
    1.68  
    1.69 @@ -484,17 +485,17 @@
    1.70      seqNames2, seqSizes2, seqLabels2, labelSizes2, lMargin = i2
    1.71  
    1.72      warn("choosing bp per pixel...")
    1.73 -    pix_limit1 = opts.width  - lMargin
    1.74 -    pix_limit2 = opts.height - tMargin
    1.75 -    bpPerPix1 = get_bp_per_pix(seqSizes1, opts.border_pixels, pix_limit1)
    1.76 -    bpPerPix2 = get_bp_per_pix(seqSizes2, opts.border_pixels, pix_limit2)
    1.77 +    maxPixels1 = opts.width  - lMargin
    1.78 +    maxPixels2 = opts.height - tMargin
    1.79 +    bpPerPix1 = get_bp_per_pix(seqSizes1, opts.border_pixels, maxPixels1)
    1.80 +    bpPerPix2 = get_bp_per_pix(seqSizes2, opts.border_pixels, maxPixels2)
    1.81      bpPerPix = max(bpPerPix1, bpPerPix2)
    1.82      warn("bp per pixel = " + str(bpPerPix))
    1.83  
    1.84 -    seq_pix1, seq_starts1, width  = get_pix_info(seqSizes1, bpPerPix,
    1.85 -                                                 opts.border_pixels, lMargin)
    1.86 -    seq_pix2, seq_starts2, height = get_pix_info(seqSizes2, bpPerPix,
    1.87 -                                                 opts.border_pixels, tMargin)
    1.88 +    seq_pix1, seq_starts1, width  = pixelData(seqSizes1, bpPerPix,
    1.89 +                                              opts.border_pixels, lMargin)
    1.90 +    seq_pix2, seq_starts2, height = pixelData(seqSizes2, bpPerPix,
    1.91 +                                              opts.border_pixels, tMargin)
    1.92      warn("width:  " + str(width))
    1.93      warn("height: " + str(height))
    1.94