Tweak last-dotplot's sort options
authorMartin C. Frith
Wed Nov 08 14:22:00 2017 +0900 (2017-11-08)
changeset 90787991b57ed04
parent 906 21fbf377641f
child 908 6840398323e1
Tweak last-dotplot's sort options
scripts/last-dotplot
     1.1 --- a/scripts/last-dotplot	Wed Nov 08 13:46:08 2017 +0900
     1.2 +++ b/scripts/last-dotplot	Wed Nov 08 14:22:00 2017 +0900
     1.3 @@ -168,6 +168,20 @@
     1.4      parts[1::2] = map(int, parts[1::2])
     1.5      return parts
     1.6  
     1.7 +def nameKey(oneSeqRanges):
     1.8 +    return natural_sort_key(oneSeqRanges[0][0])
     1.9 +
    1.10 +def sizeKey(oneSeqRanges):
    1.11 +    return sum(b - e for n, b, e in oneSeqRanges), nameKey(oneSeqRanges)
    1.12 +
    1.13 +def getSortedRanges(seqRanges, sortOpt):
    1.14 +    g = [list(v) for k, v in itertools.groupby(seqRanges, itemgetter(0))]
    1.15 +    if sortOpt == "1":
    1.16 +        g.sort(key=nameKey)
    1.17 +    if sortOpt == "2":
    1.18 +        g.sort(key=sizeKey)
    1.19 +    return [j for i in g for j in i]
    1.20 +
    1.21  def prettyNum(n):
    1.22      t = str(n)
    1.23      groups = []
    1.24 @@ -211,25 +225,16 @@
    1.25  
    1.26  def getSeqInfo(sortOpt, seqNames, seqLimits,
    1.27                 font, fontsize, image_mode, labelOpt, textRot):
    1.28 -    '''Return miscellaneous information about the sequences.'''
    1.29 -    if sortOpt == 1:
    1.30 -        seqNames.sort(key=natural_sort_key)
    1.31 -    rangeSizes = [seqLimits[i][1] - seqLimits[i][0] for i in seqNames]
    1.32 -    for i in seqNames:
    1.33 -        r = seqLimits[i]
    1.34 -        out = i, str(r[0]), str(r[1])
    1.35 -        warn("\t".join(out))
    1.36 +    seqRanges = [(i, seqLimits[i][0], seqLimits[i][1]) for i in seqNames]
    1.37 +    s = getSortedRanges(seqRanges, sortOpt)
    1.38 +    for i in s:
    1.39 +        warn("\t".join(map(str, i)))
    1.40      warn("")
    1.41 -    if sortOpt == 2:
    1.42 -        seqRecords = sorted(zip(rangeSizes, seqNames), reverse=True)
    1.43 -        rangeSizes = [i[0] for i in seqRecords]
    1.44 -        seqNames = [i[1] for i in seqRecords]
    1.45 -    seqRanges = [(i, seqLimits[i][0], seqLimits[i][1]) for i in seqNames]
    1.46 -    labelData = list(rangeLabels(seqRanges, labelOpt,
    1.47 -                                 font, fontsize, image_mode, textRot))
    1.48 -    margin = max(i[2] for i in labelData)
    1.49 +    rangeSizes = [e - b for n, b, e in s]
    1.50 +    labs = list(rangeLabels(s, labelOpt, font, fontsize, image_mode, textRot))
    1.51 +    margin = max(i[2] for i in labs)
    1.52      # xxx the margin may be too big, because some labels may get omitted
    1.53 -    return seqRanges, rangeSizes, labelData, margin
    1.54 +    return s, rangeSizes, labs, margin
    1.55  
    1.56  def div_ceil(x, y):
    1.57      '''Return x / y rounded up.'''
    1.58 @@ -646,10 +651,10 @@
    1.59                    help="color for forward alignments (default: %default)")
    1.60      op.add_option("-r", "--reversecolor", metavar="COLOR", default="blue",
    1.61                    help="color for reverse alignments (default: %default)")
    1.62 -    op.add_option("--sort1", type="int", default=1, metavar="N",
    1.63 +    op.add_option("--sort1", default="1", metavar="N",
    1.64                    help="genome1 sequence order: 0=input order, 1=name order, "
    1.65                    "2=length order (default=%default)")
    1.66 -    op.add_option("--sort2", type="int", default=1, metavar="N",
    1.67 +    op.add_option("--sort2", default="1", metavar="N",
    1.68                    help="genome2 sequence order: 0=input order, 1=name order, "
    1.69                    "2=length order (default=%default)")
    1.70      op.add_option("--trim1", action="store_true",