<commit_before>import collections
import logging
import os
import subprocess
import tempfile


class TrackCompositor(object):
    def __init__(self, dataHub, title=None):
    # def __init__(self, width, title=None): 
        self.dataHub = dataHub
        self.sections = collections.OrderedDict()
        self.width = 1200
        self.title = title
        self.descriptions = []

        self.marginTopBottom = 20
        self.sectionLabelHeight = 32
        self.betweenSectionHeight = 30
        self.trackLabelHeight = 20

        self._fromDataHub()

    def _fromDataHub(self):
        if self.title is None:
            self.title = str(self.dataHub.variant)

        for longAlleleName in ["Alternate Allele", "Reference Allele"]:
            allele = longAlleleName[:3].lower()

            sampleNames = self.dataHub.samples.keys()
            tracks = [sample.tracks[allele] for sample in self.dataHub]

            self.addTracks(longAlleleName, sampleNames, tracks, allele)

    def getBounds(self, tracks, allele):
        """ calculate left and right bounds for the allele; this is based on the variant segments
        as well as the positions of the reads (we want to be able to see all the breakpoints as 
        well as all the reads) """

        scale = tracks[0].scale

        # bail if there are multiple sections; could do this more nicely
        if len(self.dataHub.variant.chromParts(allele).parts) > 1:
            return 0, scale.pixelWidth

        segments = list(self.dataHub.variant.chromParts(allele))[0].segments
        alleleLength = len(list(self.dataHub.variant.chromParts(allele))[0])

        if self.dataHub.args.flanks:
            begin = scale.topixels(len(segments[0])-self.dataHub.args.context)
            width = scale.topixels(sum(len(s) for s in segments[:-1])+self.dataHub.args.context) - begin
            return begin, width

        axisMin = max(0, scale.topixels(len(segments[0])-100))
        axisMax = scale.topixels(min(alleleLength-len(segments[-1])+100, alleleLength))

        for track in tracks:
            track.render()
        xmin = [track.xmin for track in tracks if track.xmin is not None]
        xmin = min(xmin) if len(xmin) > 0 else 0
        xmin = min(axisMin, xmin)

        xmax = [track.xmax for track in tracks if track.xmax is not None]
        xmax = max(xmax) if len(xmax) > 0 else 500
        xmax = max(axisMax, xmax)

        width = 500
        if xmin is not None:
            width = xmax - xmin
            xmin -= width * 0.05
            xmin = max(0, xmin)<commit_msg>--flanks option works now even if --context is not specified<commit_after>import collections
import logging
import os
import subprocess
import tempfile


class TrackCompositor(object):
    def __init__(self, dataHub, title=None):
    # def __init__(self, width, title=None): 
        self.dataHub = dataHub
        self.sections = collections.OrderedDict()
        self.width = 1200
        self.title = title
        self.descriptions = []

        self.marginTopBottom = 20
        self.sectionLabelHeight = 32
        self.betweenSectionHeight = 30
        self.trackLabelHeight = 20

        self._fromDataHub()

    def _fromDataHub(self):
        if self.title is None:
            self.title = str(self.dataHub.variant)

        for longAlleleName in ["Alternate Allele", "Reference Allele"]:
            allele = longAlleleName[:3].lower()

            sampleNames = self.dataHub.samples.keys()
            tracks = [sample.tracks[allele] for sample in self.dataHub]

            self.addTracks(longAlleleName, sampleNames, tracks, allele)

    def getBounds(self, tracks, allele):
        """ calculate left and right bounds for the allele; this is based on the variant segments
        as well as the positions of the reads (we want to be able to see all the breakpoints as 
        well as all the reads) """

        scale = tracks[0].scale

        # bail if there are multiple sections; could do this more nicely
        if len(self.dataHub.variant.chromParts(allele).parts) > 1:
            return 0, scale.pixelWidth

        segments = list(self.dataHub.variant.chromParts(allele))[0].segments
        alleleLength = len(list(self.dataHub.variant.chromParts(allele))[0])

        if self.dataHub.args.flanks and self.dataHub.args.context>0:
            begin = scale.topixels(len(segments[0])-self.dataHub.args.context)
            width = scale.topixels(sum(len(s) for s in segments[:-1])+self.dataHub.args.context) - begin
            return begin, width

        axisMin = max(0, scale.topixels(len(segments[0])-100))
        axisMax = scale.topixels(min(alleleLength-len(segments[-1])+100, alleleLength))

        for track in tracks:
            track.render()
        xmin = [track.xmin for track in tracks if track.xmin is not None]
        xmin = min(xmin) if len(xmin) > 0 else 0
        xmin = min(axisMin, xmin)

        xmax = [track.xmax for track in tracks if track.xmax is not None]
        xmax = max(xmax) if len(xmax) > 0 else 500
        xmax = max(axisMax, xmax)

        width = 500
        if xmin is not None:
            width = xmax - xmin
            xmin -= width * 0.05
            xmin = max(0, xmin)
{'fraction_non_alphanumeric': 0.09783234222136965, 'fraction_numerical': 0.016305390370228275, 'mean_word_length': 2.36604260813428, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6469434', 'n_tokens_mistral': 1636, 'n_tokens_neox': 1532, 'n_words': 442}