Ejemplo n.º 1
0
    def test_all(self):

        expected = '>ACACA--STAC2'

        fasta_path = str(
            (pathlib.Path(__file__).parent / 'data/finspector.fa').resolve())
        data = fasta.get_data(fasta_path)

        self.assertTrue(data.startswith(expected))
Ejemplo n.º 2
0
    def test_region(self):

        region = 'ACACA--STAC2:61-70'
        expected = 'ACAAATATTA'

        data = fasta.get_data('data/finspector.fa', region)

        self.assertTrue(data)
        self.assertEqual(expected, data)
Ejemplo n.º 3
0
    def test_remote(self):

        data = fasta.get_data(
            "https://s3.amazonaws.com/igv.broadinstitute.org/genomes/seq/hg38/hg38.fa",
            {
                "chr": "chr1",
                "start": 100,
                "end": 200
            })
        self.assertTrue(data)
Ejemplo n.º 4
0
def create_report(args):

    variants_file = args.sites

    if variants_file.endswith(".vcf") or variants_file.endswith(".vcf.gz"):
        table = VariantTable(variants_file, args.info_columns,
                             args.info_columns_prefixes, args.sample_columns)

    elif variants_file.endswith(".bed") or variants_file.endswith(".bed.gz"):
        if args.type is not None and args.type == "junction":
            table = JunctionBedTable(variants_file, args.info_columns)
        else:
            table = BedTable(variants_file)

    table_json = table.to_JSON()

    session_dict = {}

    # Create file readers for tracks.  This is done outside the loop so initialization happens onc
    trackreaders = []
    if args.tracks is not None:
        for track in args.tracks:
            reader = utils.getreader(track)
            trackreaders.append({"track": track, "reader": reader})

    trackconfigs = []
    if args.track_config is not None:
        for trackobj in args.track_config:
            with open(trackobj) as f:
                data = json.load(f)
                for config in data:
                    reader = utils.getreader(config["url"])
                    trackconfigs.append({"config": config, "reader": reader})

    # loop through variants creating an igv.js session for each one
    flanking = 0
    if args.flanking is not None:
        flanking = float(args.flanking)

    for tuple in table.features:

        feature = tuple[0]
        unique_id = tuple[1]
        if hasattr(feature, "session_id"):
            session_id = feature.session_id
        else:
            session_id = str(unique_id)

        if session_id not in session_dict:

            # Define a genomic region around the variant
            if hasattr(feature, "viewport"):
                region = parse_region(feature.viewport)
                chr = region["chr"]
                start = region["start"]
                end = region["end"]
            else:
                chr = feature.chr
                start = int(math.floor(feature.start - flanking / 2))
                end = int(math.ceil(feature.end + flanking / 2))
                region = {"chr": chr, "start": start, "end": end}

            # Fasta
            data = fasta.get_data(args.fasta, region)
            fa = '>' + chr + ':' + str(start) + '-' + str(end) + '\n' + data
            fasta_uri = datauri.get_data_uri(fa)
            fastaJson = {
                "fastaURL": fasta_uri,
            }

            # Ideogram
            if (args.ideogram):
                ideo_string = ideogram.get_data(args.ideogram, region)
                ideo_uri = datauri.get_data_uri(ideo_string)
                fastaJson["cytobandURL"] = ideo_uri

            # Initial locus, +/- 20 bases
            if (hasattr(feature, "viewport")):
                initial_locus = feature.viewport
            else:
                position = int(math.floor(
                    (feature.start + feature.end) /
                    2)) + 1  # center of region in 1-based coordinates
                initial_locus = chr + ":" + str(position -
                                                20) + "-" + str(position + 20)
            session_json = {
                "locus": initial_locus,
                "reference": fastaJson,
                "tracks": []
            }

            for tr in trackreaders:

                track = tr["track"]
                reader = tr["reader"]
                trackobj = tracks.get_track_json_dict(track)
                data = reader.slice(region)
                trackobj["url"] = datauri.get_data_uri(data)
                if (trackobj["type"] == "alignment"):
                    trackobj["height"] = 500

                    # Sort TODO -- do this only for SNV
                    # if (trackObj["type"]) == "alignment":
                    #     trackObj["sort"] = {
                    #         "option": "NUCLEOTIDE",
                    #         "locus": chr + ":" + str(variant.pos - 1)
                    #     }

                session_json["tracks"].append(trackobj)

            for tc in trackconfigs:
                trackobj = tc["config"]
                if "name" not in trackobj:
                    trackobj["name"] = trackobj["url"]
                reader = tc["reader"]
                data = reader.slice(region)
                trackobj["url"] = datauri.get_data_uri(data)
                session_json["tracks"].append(trackobj)

            # Build the session data URI

            session_string = json.dumps(session_json)

            session_uri = datauri.get_data_uri(session_string)

            session_dict[session_id] = session_uri

    session_dict = json.dumps(session_dict)

    template_file = args.template
    if None == template_file:
        if 'junction' == args.type:
            template_file = os.path.dirname(
                sys.modules['igv_reports'].__file__
            ) + '/templates/junction_template.html'
        else:
            template_file = os.path.dirname(
                sys.modules['igv_reports'].__file__
            ) + '/templates/variant_template.html'

    output_file = args.output

    standalone = args.standalone
    with open(template_file, "r") as f:
        data = f.readlines()

        with open(output_file, "w") as o:

            for i, line in enumerate(data):

                if args.title is not None and line.startswith("<!--title-->"):
                    o.write("<h1>" + args.title + "</h1>")

                if standalone:
                    if line.strip().startswith("<script") and ".js\"" in line:
                        inline_script(line, o, "js")
                        continue
                    elif line.strip().startswith(
                            "<link") and line.strip().endswith("css\">"):
                        inline_script(line, o, "css")
                        continue
                j = line.find('"@TABLE_JSON@"')
                if j >= 0:
                    line = line.replace('"@TABLE_JSON@"', table_json)

                j = line.find('"@SESSION_DICTIONARY@"')
                if j >= 0:
                    line = line.replace('"@SESSION_DICTIONARY@"', session_dict)

                o.write(line)
Ejemplo n.º 5
0
def create_report(args):

    variants_file = args.sites

    if variants_file.endswith(".vcf") or variants_file.endswith(".vcf.gz"):
        table = VariantTable(variants_file, args.species, args.info_columns,
                             args.info_columns_prefixes, args.sample_columns)

    elif variants_file.endswith(".bed") or variants_file.endswith(".bed.gz"):
        table = BedTable(variants_file)

    table_json = table.to_JSON()

    session_dict = {}

    # Create file readers for tracks.  This is done outside the loop so initialization happens onc
    trackreaders = []
    if args.tracks is not None:
        for track in args.tracks:
            reader = utils.getreader(track)
            trackreaders.append({"track": track, "reader": reader})

    # loop through variants creating an igv.js session for each one
    for tuple in table.features:

        feature = tuple[0]
        unique_id = tuple[1]

        # Define a genomic region around the variant
        chr = feature.chr
        position = int(math.floor(
            (feature.start + feature.end) /
            2)) + 1  # center of region in 1-based coordinates
        start = int(math.floor(feature.start - int(args.flanking) / 2))
        end = int(math.ceil(feature.end + int(args.flanking) / 2))
        region = {"chr": chr, "start": start, "end": end}

        # Fasta
        data = fasta.get_data(args.fasta, region)
        fa = '>' + chr + ':' + str(start) + '-' + str(end) + '\n' + data
        fasta_uri = datauri.get_data_uri(fa)
        fastaJson = {
            "fastaURL": fasta_uri,
        }

        # Ideogram
        if (args.ideogram):
            ideo_string = ideogram.get_data(args.ideogram, region)
            ideo_uri = datauri.get_data_uri(ideo_string)
            fastaJson["cytobandURL"] = ideo_uri

        # Initial locus, +/- 20 bases
        initial_locus = chr + ":" + str(position - 20) + "-" + str(position +
                                                                   20)
        session_json = {
            "locus": initial_locus,
            "reference": fastaJson,
            "tracks": []
        }

        for tr in trackreaders:

            track = tr["track"]
            reader = tr["reader"]
            trackObj = tracks.get_track_json_dict(track)
            data = reader.slice(region)
            trackObj["url"] = datauri.get_data_uri(data)
            if (trackObj["type"] == "alignment"):
                trackObj["height"] = 500

                # Sort TODO -- do this only for SNV
                # if (trackObj["type"]) == "alignment":
                #     trackObj["sort"] = {
                #         "option": "NUCLEOTIDE",
                #         "locus": chr + ":" + str(variant.pos - 1)
                #     }

            session_json["tracks"].append(trackObj)

        # Build the session data URI

        session_string = json.dumps(session_json)

        session_uri = datauri.get_data_uri(session_string)

        session_dict[str(unique_id)] = session_uri

    session_dict = json.dumps(session_dict)

    template_file = args.template
    if None == template_file:
        template_file = os.path.dirname(sys.modules['igv_reports'].__file__
                                        ) + '/templates/variant_template.html'

    output_file = args.output

    standalone = args.standalone
    with open(template_file, "r") as f:
        data = f.readlines()

        with open(output_file, "w") as o:

            for i, line in enumerate(data):

                if standalone and "<script" in line and line.find(".js") > 0:
                    inline_script(line, o)

                else:
                    j = line.find('"@TABLE_JSON@"')
                    if j >= 0:
                        line = line.replace('"@TABLE_JSON@"', table_json)

                    j = line.find('"@SESSION_DICTIONARY@"')
                    if j >= 0:
                        line = line.replace('"@SESSION_DICTIONARY@"',
                                            session_dict)

                    o.write(line)