scripts/last-dotplot
changeset 898 f6a9c15287ea
parent 897 f7bc7d099e2b
child 899 ccf8902bf86c
     1.1 --- a/scripts/last-dotplot	Thu Nov 02 20:04:51 2017 +0900
     1.2 +++ b/scripts/last-dotplot	Thu Nov 02 21:08:55 2017 +0900
     1.3 @@ -176,6 +176,14 @@
     1.4      draw = ImageDraw.Draw(im)
     1.5      return [textDimensions(draw, font, textRot, i) for i in my_strings]
     1.6  
     1.7 +def prettyNum(n):
     1.8 +    t = str(n)
     1.9 +    groups = []
    1.10 +    while t:
    1.11 +        groups.append(t[-3:])
    1.12 +        t = t[:-3]
    1.13 +    return " ".join(reversed(groups))
    1.14 +
    1.15  def sizeText(size):
    1.16      suffixes = "bp", "kb", "Mb", "Gb"
    1.17      for i, x in enumerate(suffixes):
    1.18 @@ -185,11 +193,18 @@
    1.19          if size < j * 1000 or i == len(suffixes) - 1:
    1.20              return "%.0f" % (1.0 * size / j) + x
    1.21  
    1.22 -def seqNameAndSizeText(seqName, seqSize):
    1.23 -    return seqName + ": " + sizeText(seqSize)
    1.24 +def labelText(seqRange, labelOpt):
    1.25 +    seqName, beg, end = seqRange
    1.26 +    if labelOpt == 1:
    1.27 +        return seqName + ": " + sizeText(end - beg)
    1.28 +    if labelOpt == 2:
    1.29 +        return seqName + ": " + prettyNum(beg) + ": " + sizeText(end - beg)
    1.30 +    if labelOpt == 3:
    1.31 +        return seqName + ": " + prettyNum(beg) + " - " + prettyNum(end)
    1.32 +    return seqName
    1.33  
    1.34  def getSeqInfo(sortOpt, seqNames, seqLimits,
    1.35 -               font, fontsize, image_mode, isShowSize, textRot):
    1.36 +               font, fontsize, image_mode, labelOpt, textRot):
    1.37      '''Return miscellaneous information about the sequences.'''
    1.38      if sortOpt == 1:
    1.39          seqNames.sort(key=natural_sort_key)
    1.40 @@ -203,10 +218,8 @@
    1.41          seqRecords = sorted(zip(seqSizes, seqNames), reverse=True)
    1.42          seqSizes = [i[0] for i in seqRecords]
    1.43          seqNames = [i[1] for i in seqRecords]
    1.44 -    if isShowSize:
    1.45 -        seqLabels = map(seqNameAndSizeText, seqNames, seqSizes)
    1.46 -    else:
    1.47 -        seqLabels = seqNames
    1.48 +    seqRanges = [(i, seqLimits[i][0], seqLimits[i][1]) for i in seqNames]
    1.49 +    seqLabels = [labelText(i, labelOpt) for i in seqRanges]
    1.50      labelSizes = get_text_sizes(seqLabels, font, fontsize, image_mode, textRot)
    1.51      margin = max(i[1] for i in labelSizes)
    1.52      # xxx the margin may be too big, because some labels may get omitted
    1.53 @@ -477,12 +490,12 @@
    1.54  
    1.55      textRot1 = "vertical".startswith(opts.rot1)
    1.56      i1 = getSeqInfo(opts.sort1, seqNames1, seqLimits1,
    1.57 -                    font, opts.fontsize, image_mode, opts.lengths1, textRot1)
    1.58 +                    font, opts.fontsize, image_mode, opts.labels1, textRot1)
    1.59      seqNames1, rangeSizes1, seqLabels1, labelSizes1, tMargin = i1
    1.60  
    1.61      textRot2 = "horizontal".startswith(opts.rot2)
    1.62      i2 = getSeqInfo(opts.sort2, seqNames2, seqLimits2,
    1.63 -                    font, opts.fontsize, image_mode, opts.lengths2, textRot2)
    1.64 +                    font, opts.fontsize, image_mode, opts.labels2, textRot2)
    1.65      seqNames2, rangeSizes2, seqLabels2, labelSizes2, lMargin = i2
    1.66  
    1.67      warn("choosing bp per pixel...")
    1.68 @@ -607,14 +620,16 @@
    1.69                    help="TrueType or OpenType font file")
    1.70      og.add_option("-s", "--fontsize", metavar="SIZE", type="int", default=11,
    1.71                    help="TrueType or OpenType font size (default: %default)")
    1.72 +    og.add_option("--labels1", type="int", default=0, metavar="N", help=
    1.73 +                  "genome1 labels: 0=name, 1=name:length, "
    1.74 +                  "2=name:start:length, 3=name:start-end (default=%default)")
    1.75 +    og.add_option("--labels2", type="int", default=0, metavar="N", help=
    1.76 +                  "genome2 labels: 0=name, 1=name:length, "
    1.77 +                  "2=name:start:length, 3=name:start-end (default=%default)")
    1.78      og.add_option("--rot1", metavar="ROT", default="h",
    1.79                    help="text rotation for the 1st genome (default=%default)")
    1.80      og.add_option("--rot2", metavar="ROT", default="v",
    1.81                    help="text rotation for the 2nd genome (default=%default)")
    1.82 -    og.add_option("--lengths1", action="store_true",
    1.83 -                  help="show sequence lengths for the 1st (horizontal) genome")
    1.84 -    og.add_option("--lengths2", action="store_true",
    1.85 -                  help="show sequence lengths for the 2nd (vertical) genome")
    1.86      op.add_option_group(og)
    1.87  
    1.88      og = optparse.OptionGroup(op, "Annotation options")