last-dotplot: fix crash on long annotation names
authorMartin C. Frith
Wed Oct 14 20:45:14 2020 +0900 (19 months ago)
changeset 113304663552433d
parent 1132 2b38a62095d1
child 1134 60f728268532
last-dotplot: fix crash on long annotation names
scripts/last-dotplot
     1.1 --- a/scripts/last-dotplot	Wed Oct 14 13:49:25 2020 +0900
     1.2 +++ b/scripts/last-dotplot	Wed Oct 14 20:45:14 2020 +0900
     1.3 @@ -696,7 +696,8 @@
     1.4          else:
     1.5              yield 2000, opts.unbridged_color, seqName, beg, end, ""
     1.6  
     1.7 -def bedBoxes(beds, rangeDict, limit, isTop, bpPerPix, textSizes):
     1.8 +def bedBoxes(annots, rangeDict, limit, isTop, bpPerPix):
     1.9 +    beds, textSizes, margin = annots
    1.10      cover = [(limit, limit)]
    1.11      for layer, color, seqName, bedBeg, bedEnd, name in reversed(beds):
    1.12          textWidth, textHeight = textSizes[name]
    1.13 @@ -724,8 +725,9 @@
    1.14              nameEnd = nameBeg + textHeight
    1.15              n = ""
    1.16              if name and all(e <= nameBeg or b >= nameEnd for b, e in cover):
    1.17 -                cover.append((nameBeg, nameEnd))
    1.18 -                n = name
    1.19 +                if textWidth <= margin:
    1.20 +                    cover.append((nameBeg, nameEnd))
    1.21 +                    n = name
    1.22              yield layer, color, isTop, pixBeg, pixEnd, n, nameBeg, textWidth
    1.23  
    1.24  def drawAnnotations(im, boxes, tMargin, bMarginBeg, lMargin, rMarginBeg):
    1.25 @@ -853,7 +855,7 @@
    1.26      remainingSequences = set(i[seqIndex] for i in alignments)
    1.27      return [i for i in seqRanges if i[0] in remainingSequences]
    1.28  
    1.29 -def readAnnotations(opts, font, textDraw, sortedRanges,
    1.30 +def readAnnotations(opts, font, textDraw, sortedRanges, totalLength,
    1.31                      bedFile, repFile, geneFile, gapFile):
    1.32      rangeDict = expandedSeqDict(dict(rangesPerSeq(sortedRanges)))
    1.33      annots = sorted(itertools.chain(readBed(bedFile, rangeDict),
    1.34 @@ -862,7 +864,9 @@
    1.35                                      readGaps(opts, gapFile, rangeDict)))
    1.36      names = set(i[5] for i in annots)
    1.37      textSizes = dict(sizesPerText(names, font, textDraw))
    1.38 -    margin = max(i[0] for i in textSizes.values()) if textSizes else 0
    1.39 +    maxTextLength = totalLength // 2
    1.40 +    okLengths = [i[0] for i in textSizes.values() if i[0] <= maxTextLength]
    1.41 +    margin = max(okLengths) if okLengths else 0
    1.42      return annots, textSizes, margin
    1.43  
    1.44  def lastDotplot(opts, args):
    1.45 @@ -933,13 +937,13 @@
    1.46  
    1.47      logging.info("reading annotations...")
    1.48  
    1.49 -    a1 = readAnnotations(opts, font, textDraw, sortedRanges1,
    1.50 -                         opts.bed1, opts.rmsk1, opts.genePred1, opts.gap1)
    1.51 -    annots1, annoTextSizes1, bMargin = a1
    1.52 +    annots1 = readAnnotations(opts, font, textDraw, sortedRanges1, opts.height,
    1.53 +                              opts.bed1, opts.rmsk1, opts.genePred1, opts.gap1)
    1.54 +    bMargin = annots1[-1]
    1.55  
    1.56 -    a2 = readAnnotations(opts, font, textDraw, sortedRanges2,
    1.57 -                         opts.bed2, opts.rmsk2, opts.genePred2, opts.gap2)
    1.58 -    annots2, annoTextSizes2, rMargin = a2
    1.59 +    annots2 = readAnnotations(opts, font, textDraw, sortedRanges2, opts.width,
    1.60 +                              opts.bed2, opts.rmsk2, opts.genePred2, opts.gap2)
    1.61 +    rMargin = annots2[-1]
    1.62  
    1.63      maxPixels1 = opts.width  - lMargin - rMargin
    1.64      maxPixels2 = opts.height - tMargin - bMargin
    1.65 @@ -971,10 +975,8 @@
    1.66      rangeDict1 = expandedSeqDict(rangeDict1)
    1.67      rangeDict2 = expandedSeqDict(rangeDict2)
    1.68  
    1.69 -    boxes1 = list(bedBoxes(annots1, rangeDict1, rMarginBeg, True, bpPerPix,
    1.70 -                           annoTextSizes1))
    1.71 -    boxes2 = list(bedBoxes(annots2, rangeDict2, bMarginBeg, False, bpPerPix,
    1.72 -                           annoTextSizes2))
    1.73 +    boxes1 = list(bedBoxes(annots1, rangeDict1, rMarginBeg, True, bpPerPix))
    1.74 +    boxes2 = list(bedBoxes(annots2, rangeDict2, bMarginBeg, False, bpPerPix))
    1.75      boxes = sorted(itertools.chain(boxes1, boxes2))
    1.76  
    1.77      logging.info("drawing...")