Tweak dotplot labels
authorMartin C. Frith
Wed Nov 08 11:36:01 2017 +0900 (2017-11-08)
changeset 904c2f50cd4e29d
parent 903 3af6b02e3f9c
child 905 8d52cef22afa
Tweak dotplot labels
scripts/last-dotplot
     1.1 --- a/scripts/last-dotplot	Wed Nov 08 11:07:22 2017 +0900
     1.2 +++ b/scripts/last-dotplot	Wed Nov 08 11:36:01 2017 +0900
     1.3 @@ -19,6 +19,8 @@
     1.4  except ImportError: import Image, ImageDraw, ImageFont, ImageColor
     1.5  
     1.6  def myOpen(fileName):  # faster than fileinput
     1.7 +    if fileName is None:
     1.8 +        return []
     1.9      if fileName == "-":
    1.10          return sys.stdin
    1.11      if fileName.endswith(".gz"):
    1.12 @@ -171,7 +173,7 @@
    1.13      while t:
    1.14          groups.append(t[-3:])
    1.15          t = t[:-3]
    1.16 -    return " ".join(reversed(groups))
    1.17 +    return ",".join(reversed(groups))
    1.18  
    1.19  def sizeText(size):
    1.20      suffixes = "bp", "kb", "Mb", "Gb"
    1.21 @@ -187,9 +189,9 @@
    1.22      if labelOpt == 1:
    1.23          return seqName + ": " + sizeText(end - beg)
    1.24      if labelOpt == 2:
    1.25 -        return seqName + ": " + prettyNum(beg) + ": " + sizeText(end - beg)
    1.26 +        return seqName + ":" + prettyNum(beg) + ": " + sizeText(end - beg)
    1.27      if labelOpt == 3:
    1.28 -        return seqName + ": " + prettyNum(beg) + " - " + prettyNum(end)
    1.29 +        return seqName + ":" + prettyNum(beg) + "-" + prettyNum(end)
    1.30      return seqName
    1.31  
    1.32  def rangeLabels(seqRanges, labelOpt, font, fontsize, image_mode, textRot):
    1.33 @@ -235,6 +237,7 @@
    1.34  
    1.35  def get_bp_per_pix(rangeSizes, pixTweenRanges, maxPixels):
    1.36      '''Get the minimum bp-per-pixel that fits in the size limit.'''
    1.37 +    warn("choosing bp per pixel...")
    1.38      numOfRanges = len(rangeSizes)
    1.39      maxPixelsInRanges = maxPixels - pixTweenRanges * (numOfRanges - 1)
    1.40      if maxPixelsInRanges < numOfRanges:
    1.41 @@ -315,7 +318,6 @@
    1.42      return newDict
    1.43  
    1.44  def readBed(fileName, seqLimits):
    1.45 -    if not fileName: return
    1.46      for line in myOpen(fileName):
    1.47          w = line.split()
    1.48          if not w: continue
    1.49 @@ -341,7 +343,6 @@
    1.50      return map(int, text.rstrip(",").split(","))
    1.51  
    1.52  def readGenePred(opts, fileName, seqLimits):
    1.53 -    if not fileName: return
    1.54      for line in myOpen(fileName):
    1.55          fields = line.split()
    1.56          if not fields: continue
    1.57 @@ -360,7 +361,6 @@
    1.58              if b < e: yield 400, opts.cds_color, seqName, b, e
    1.59  
    1.60  def readRmsk(fileName, seqLimits):
    1.61 -    if not fileName: return
    1.62      for line in myOpen(fileName):
    1.63          fields = line.split()
    1.64          if len(fields) == 17:  # rmsk.txt
    1.65 @@ -391,7 +391,6 @@
    1.66  
    1.67  def readGaps(opts, fileName, seqLimits):
    1.68      '''Read locations of unsequenced gaps, from an agp or gap file.'''
    1.69 -    if not fileName: return
    1.70      for line in myOpen(fileName):
    1.71          w = line.split()
    1.72          if not w or w[0][0] == "#": continue
    1.73 @@ -444,13 +443,13 @@
    1.74          labelEnd = labelBeg + textWidth
    1.75          sortKey = textWidth - k
    1.76          if labelBeg < beg:
    1.77 +            sortKey += maxWidth * (beg - labelBeg)
    1.78              labelBeg = beg
    1.79              labelEnd = beg + textWidth
    1.80 -            sortKey += maxWidth
    1.81          if labelEnd > end:
    1.82 +            sortKey += maxWidth * (labelEnd - end)
    1.83              labelEnd = end
    1.84              labelBeg = end - textWidth
    1.85 -            sortKey += maxWidth
    1.86          yield sortKey, labelBeg, labelEnd, text, textHeight
    1.87  
    1.88  def nonoverlappingLabels(labels, minPixTweenLabels):
    1.89 @@ -515,8 +514,8 @@
    1.90      overlap_color = tuple([(i + j) // 2 for i, j in zipped_colors])
    1.91  
    1.92      warn("reading alignments...")
    1.93 -    alignmentInfo = readAlignments(args[0], opts)
    1.94 -    alignments, seqNames1, seqNames2, seqLimits1, seqLimits2 = alignmentInfo
    1.95 +    alnData = readAlignments(args[0], opts)
    1.96 +    alignments, seqNames1, seqNames2, seqLimits1, seqLimits2 = alnData
    1.97      warn("done")
    1.98      if not alignments: raise Exception("there are no alignments")
    1.99  
   1.100 @@ -530,7 +529,6 @@
   1.101                      font, opts.fontsize, image_mode, opts.labels2, textRot2)
   1.102      seqNames2, rangeSizes2, labelData2, lMargin = i2
   1.103  
   1.104 -    warn("choosing bp per pixel...")
   1.105      maxPixels1 = opts.width  - lMargin
   1.106      maxPixels2 = opts.height - tMargin
   1.107      bpPerPix1 = get_bp_per_pix(rangeSizes1, opts.border_pixels, maxPixels1)
   1.108 @@ -553,10 +551,8 @@
   1.109      warn("processing alignments...")
   1.110      hits = alignmentPixels(width, height, alignments, bpPerPix,
   1.111                             origins1, origins2)
   1.112 -    warn("done")
   1.113  
   1.114 -    image_size = width, height
   1.115 -    im = Image.new(image_mode, image_size, opts.background_color)
   1.116 +    warn("reading annotations...")
   1.117  
   1.118      seqLimits1 = expandedSeqDict(seqLimits1)
   1.119      seqLimits2 = expandedSeqDict(seqLimits2)
   1.120 @@ -576,6 +572,12 @@
   1.121      b2 = bedBoxes(beds2, seqLimits2, origins2, lMargin, width, False, bpPerPix)
   1.122  
   1.123      boxes = sorted(itertools.chain(b1, b2))
   1.124 +
   1.125 +    warn("drawing...")
   1.126 +
   1.127 +    image_size = width, height
   1.128 +    im = Image.new(image_mode, image_size, opts.background_color)
   1.129 +
   1.130      drawAnnotations(im, boxes)
   1.131  
   1.132      for i in range(height):