last-dotplot: implementing annotation names...
authorMartin C. Frith
Fri Jul 31 14:48:36 2020 +0900 (11 days ago)
changeset 107570b0e512d696
parent 1074 170ab6c64728
child 1076 3b8d229f8359
last-dotplot: implementing annotation names...
scripts/last-dotplot
     1.1 --- a/scripts/last-dotplot	Fri Jul 31 14:36:00 2020 +0900
     1.2 +++ b/scripts/last-dotplot	Fri Jul 31 14:48:36 2020 +0900
     1.3 @@ -393,6 +393,13 @@
     1.4      t2 = mySortedRanges(seqRangesB2, oB2, 1, alignmentsB, s1)
     1.5      return s1 + t1, s2 + t2
     1.6  
     1.7 +def sizesPerText(texts, font, textDraw):
     1.8 +    sizes = 0, 0
     1.9 +    for t in texts:
    1.10 +        if textDraw is not None:
    1.11 +            sizes = textDraw.textsize(t, font=font)
    1.12 +        yield t, sizes
    1.13 +
    1.14  def prettyNum(n):
    1.15      t = str(n)
    1.16      groups = []
    1.17 @@ -826,13 +833,16 @@
    1.18      remainingSequences = set(i[seqIndex] for i in alignments)
    1.19      return [i for i in seqRanges if i[0] in remainingSequences]
    1.20  
    1.21 -def readAnnotations(opts, sortedRanges, bedFile, repFile, geneFile, gapFile):
    1.22 +def readAnnotations(opts, font, textDraw, sortedRanges,
    1.23 +                    bedFile, repFile, geneFile, gapFile):
    1.24      rangeDict = expandedSeqDict(dict(rangesPerSeq(sortedRanges)))
    1.25 -    annots = itertools.chain(readBed(bedFile, rangeDict),
    1.26 -                             readRmsk(repFile, rangeDict),
    1.27 -                             readGenePred(opts, geneFile, rangeDict),
    1.28 -                             readGaps(opts, gapFile, rangeDict))
    1.29 -    return annots
    1.30 +    annots = sorted(itertools.chain(readBed(bedFile, rangeDict),
    1.31 +                                    readRmsk(repFile, rangeDict),
    1.32 +                                    readGenePred(opts, geneFile, rangeDict),
    1.33 +                                    readGaps(opts, gapFile, rangeDict)))
    1.34 +    names = set(i[5] for i in annots)
    1.35 +    textSizes = dict(sizesPerText(names, font, textDraw))
    1.36 +    return annots, textSizes
    1.37  
    1.38  def lastDotplot(opts, args):
    1.39      logLevel = logging.INFO if opts.verbose else logging.WARNING
    1.40 @@ -900,6 +910,16 @@
    1.41      i2 = dataFromRanges(sortedRanges2, font, textDraw, opts.labels2, textRot2)
    1.42      rangeSizes2, labelData2, lMargin = i2
    1.43  
    1.44 +    logging.info("reading annotations...")
    1.45 +
    1.46 +    a1 = readAnnotations(opts, font, textDraw, sortedRanges1,
    1.47 +                         opts.bed1, opts.rmsk1, opts.genePred1, opts.gap1)
    1.48 +    annots1, annoTextSizes1 = a1
    1.49 +
    1.50 +    a2 = readAnnotations(opts, font, textDraw, sortedRanges2,
    1.51 +                         opts.bed2, opts.rmsk2, opts.genePred2, opts.gap2)
    1.52 +    annots2, annoTextSizes2 = a2
    1.53 +
    1.54      bMargin = rMargin = 0  # xxx
    1.55  
    1.56      maxPixels1 = opts.width  - lMargin - rMargin
    1.57 @@ -929,18 +949,11 @@
    1.58      hits = alignmentPixels(width, height, allAlignments, bpPerPix,
    1.59                             rangeDict1, rangeDict2)
    1.60  
    1.61 -    logging.info("reading annotations...")
    1.62 +    rangeDict1 = expandedSeqDict(rangeDict1)
    1.63 +    rangeDict2 = expandedSeqDict(rangeDict2)
    1.64  
    1.65 -    rangeDict1 = expandedSeqDict(rangeDict1)
    1.66 -    annots1 = readAnnotations(opts, sortedRanges1,
    1.67 -                              opts.bed1, opts.rmsk1, opts.genePred1, opts.gap1)
    1.68      boxes1 = bedBoxes(annots1, rangeDict1, tMargin, bMarginBeg, 1, bpPerPix)
    1.69 -
    1.70 -    rangeDict2 = expandedSeqDict(rangeDict2)
    1.71 -    annots2 = readAnnotations(opts, sortedRanges2,
    1.72 -                              opts.bed2, opts.rmsk2, opts.genePred2, opts.gap2)
    1.73      boxes2 = bedBoxes(annots2, rangeDict2, lMargin, rMarginBeg, 0, bpPerPix)
    1.74 -
    1.75      boxes = sorted(itertools.chain(boxes1, boxes2))
    1.76  
    1.77      logging.info("drawing...")