Change last-dotplot's label options
authorMartin C. Frith
Thu Nov 02 21:08:55 2017 +0900 (2017-11-02)
changeset 898f6a9c15287ea
parent 897 f7bc7d099e2b
child 899 ccf8902bf86c
Change last-dotplot's label options
doc/last-dotplot.txt
scripts/last-dotplot
     1.1 --- a/doc/last-dotplot.txt	Thu Nov 02 20:04:51 2017 +0900
     1.2 +++ b/doc/last-dotplot.txt	Thu Nov 02 21:08:55 2017 +0900
     1.3 @@ -92,6 +92,8 @@
     1.4        Number of pixels between sequences.
     1.5    --border-color=COLOR
     1.6        Color for pixels between sequences.
     1.7 +  --margin-color=COLOR
     1.8 +      Color for the margins.
     1.9  
    1.10  Text options
    1.11  ~~~~~~~~~~~~
    1.12 @@ -100,14 +102,18 @@
    1.13        TrueType or OpenType font file.
    1.14    -s SIZE, --fontsize=SIZE
    1.15        TrueType or OpenType font size.
    1.16 +  --labels1=N
    1.17 +      Label the displayed regions of the 1st genome with their:
    1.18 +      sequence name (0), name:length (1), name:start:length (2),
    1.19 +      name:start-end (3).
    1.20 +  --labels2=N
    1.21 +      Label the displayed regions of the 2nd genome with their:
    1.22 +      sequence name (0), name:length (1), name:start:length (2),
    1.23 +      name:start-end (3).
    1.24    --rot1=ROT
    1.25        Text rotation for the 1st genome: h(orizontal) or v(ertical).
    1.26    --rot2=ROT
    1.27        Text rotation for the 2nd genome: h(orizontal) or v(ertical).
    1.28 -  --lengths1
    1.29 -      Show sequence lengths for the 1st (horizontal) genome.
    1.30 -  --lengths2
    1.31 -      Show sequence lengths for the 2nd (vertical) genome.
    1.32  
    1.33  Annotation options
    1.34  ~~~~~~~~~~~~~~~~~~
     2.1 --- a/scripts/last-dotplot	Thu Nov 02 20:04:51 2017 +0900
     2.2 +++ b/scripts/last-dotplot	Thu Nov 02 21:08:55 2017 +0900
     2.3 @@ -176,6 +176,14 @@
     2.4      draw = ImageDraw.Draw(im)
     2.5      return [textDimensions(draw, font, textRot, i) for i in my_strings]
     2.6  
     2.7 +def prettyNum(n):
     2.8 +    t = str(n)
     2.9 +    groups = []
    2.10 +    while t:
    2.11 +        groups.append(t[-3:])
    2.12 +        t = t[:-3]
    2.13 +    return " ".join(reversed(groups))
    2.14 +
    2.15  def sizeText(size):
    2.16      suffixes = "bp", "kb", "Mb", "Gb"
    2.17      for i, x in enumerate(suffixes):
    2.18 @@ -185,11 +193,18 @@
    2.19          if size < j * 1000 or i == len(suffixes) - 1:
    2.20              return "%.0f" % (1.0 * size / j) + x
    2.21  
    2.22 -def seqNameAndSizeText(seqName, seqSize):
    2.23 -    return seqName + ": " + sizeText(seqSize)
    2.24 +def labelText(seqRange, labelOpt):
    2.25 +    seqName, beg, end = seqRange
    2.26 +    if labelOpt == 1:
    2.27 +        return seqName + ": " + sizeText(end - beg)
    2.28 +    if labelOpt == 2:
    2.29 +        return seqName + ": " + prettyNum(beg) + ": " + sizeText(end - beg)
    2.30 +    if labelOpt == 3:
    2.31 +        return seqName + ": " + prettyNum(beg) + " - " + prettyNum(end)
    2.32 +    return seqName
    2.33  
    2.34  def getSeqInfo(sortOpt, seqNames, seqLimits,
    2.35 -               font, fontsize, image_mode, isShowSize, textRot):
    2.36 +               font, fontsize, image_mode, labelOpt, textRot):
    2.37      '''Return miscellaneous information about the sequences.'''
    2.38      if sortOpt == 1:
    2.39          seqNames.sort(key=natural_sort_key)
    2.40 @@ -203,10 +218,8 @@
    2.41          seqRecords = sorted(zip(seqSizes, seqNames), reverse=True)
    2.42          seqSizes = [i[0] for i in seqRecords]
    2.43          seqNames = [i[1] for i in seqRecords]
    2.44 -    if isShowSize:
    2.45 -        seqLabels = map(seqNameAndSizeText, seqNames, seqSizes)
    2.46 -    else:
    2.47 -        seqLabels = seqNames
    2.48 +    seqRanges = [(i, seqLimits[i][0], seqLimits[i][1]) for i in seqNames]
    2.49 +    seqLabels = [labelText(i, labelOpt) for i in seqRanges]
    2.50      labelSizes = get_text_sizes(seqLabels, font, fontsize, image_mode, textRot)
    2.51      margin = max(i[1] for i in labelSizes)
    2.52      # xxx the margin may be too big, because some labels may get omitted
    2.53 @@ -477,12 +490,12 @@
    2.54  
    2.55      textRot1 = "vertical".startswith(opts.rot1)
    2.56      i1 = getSeqInfo(opts.sort1, seqNames1, seqLimits1,
    2.57 -                    font, opts.fontsize, image_mode, opts.lengths1, textRot1)
    2.58 +                    font, opts.fontsize, image_mode, opts.labels1, textRot1)
    2.59      seqNames1, rangeSizes1, seqLabels1, labelSizes1, tMargin = i1
    2.60  
    2.61      textRot2 = "horizontal".startswith(opts.rot2)
    2.62      i2 = getSeqInfo(opts.sort2, seqNames2, seqLimits2,
    2.63 -                    font, opts.fontsize, image_mode, opts.lengths2, textRot2)
    2.64 +                    font, opts.fontsize, image_mode, opts.labels2, textRot2)
    2.65      seqNames2, rangeSizes2, seqLabels2, labelSizes2, lMargin = i2
    2.66  
    2.67      warn("choosing bp per pixel...")
    2.68 @@ -607,14 +620,16 @@
    2.69                    help="TrueType or OpenType font file")
    2.70      og.add_option("-s", "--fontsize", metavar="SIZE", type="int", default=11,
    2.71                    help="TrueType or OpenType font size (default: %default)")
    2.72 +    og.add_option("--labels1", type="int", default=0, metavar="N", help=
    2.73 +                  "genome1 labels: 0=name, 1=name:length, "
    2.74 +                  "2=name:start:length, 3=name:start-end (default=%default)")
    2.75 +    og.add_option("--labels2", type="int", default=0, metavar="N", help=
    2.76 +                  "genome2 labels: 0=name, 1=name:length, "
    2.77 +                  "2=name:start:length, 3=name:start-end (default=%default)")
    2.78      og.add_option("--rot1", metavar="ROT", default="h",
    2.79                    help="text rotation for the 1st genome (default=%default)")
    2.80      og.add_option("--rot2", metavar="ROT", default="v",
    2.81                    help="text rotation for the 2nd genome (default=%default)")
    2.82 -    og.add_option("--lengths1", action="store_true",
    2.83 -                  help="show sequence lengths for the 1st (horizontal) genome")
    2.84 -    og.add_option("--lengths2", action="store_true",
    2.85 -                  help="show sequence lengths for the 2nd (vertical) genome")
    2.86      op.add_option_group(og)
    2.87  
    2.88      og = optparse.OptionGroup(op, "Annotation options")