<commit_before>#!/usr/bin/env python

# Script to get the sequence for a given region and to show polymorphic
# positions by using the nucleotide substitution IUPAC code.

from __future__ import print_function

import argparse
import unittest
import sys

try:
    from StringIO import StringIO
except ImportError:
    from io import StringIO

from gepyto.structures.region import Region
from gepyto.structures.variants import variant_list_to_dataframe, SNP
from gepyto.utils.variants import ensembl_variants_in_region


IUPAC = {
    frozenset(["A", "G"]): "R",
    frozenset(["C", "T"]): "Y",
    frozenset(["G", "T"]): "K",
    frozenset(["A", "C"]): "M",
    frozenset(["C", "G"]): "S",
    frozenset(["A", "T"]): "W",
    frozenset(["C", "G", "T"]): "B",
    frozenset(["A", "G", "T"]): "D",
    frozenset(["A", "C", "T"]): "H",
    frozenset(["A", "C", "G"]): "V",
    frozenset(["A", "C", "G", "T"]): "N",
}


def parse_args():
    parser = argparse.ArgumentParser(description="Generate a FASTA where the "
                                                 "SNPs are represented as "
                                                 "their IUPAC code.")
    parser.add_argument(
        "region",
        type=str,
        help="The region for sequence extraction (e.g. chr3:12345-12567)"
    )

    parser.add_argument(
        "--maf-over",
        type=float,
        default=0,
        help="Filter on the minor allele frequency."
    )

    args = parser.parse_args()
    return args.region, args.maf_over


def main(region, maf, out=sys.stdout):
    # Get the sequence for the region of interest.
    region = Region.from_str(region)
    sequence = region.sequence
    li_seq = list(sequence.seq)

    # Get all the variants in the region.
    variants = ensembl_variants_in_region(region)

    # Annotate all the variants to get the MAF.
    for variant in variants:
        variant.load_ensembl_annotations()
    variants = [v for v in variants if (v is None) or (v._info["maf"] > maf)]

    # Write the pandas dataframe.
    csv_filename = "variants_in_{}_{}-{}.txt".format(
        region.chrom,
        region.start,
        region.end,
    )
    df = variant_list_to_dataframe(variants)
    df.to_csv(csv_filename, sep="\t", index=False)

    # Now create and fix the fasta file.
    multi_allelics = []
    for variant in variants:
        # Ignore indels.
        if type(variant) is not SNP:
            continue<commit_msg>Python 3 compatibility and bug fixes.<commit_after>#!/usr/bin/env python

# Script to get the sequence for a given region and to show polymorphic
# positions by using the nucleotide substitution IUPAC code.

from __future__ import print_function

import argparse
import unittest
import sys

try:
    from StringIO import StringIO
except ImportError:
    from io import StringIO

from gepyto.structures.region import Region
from gepyto.structures.variants import variant_list_to_dataframe, SNP
from gepyto.utils.variants import ensembl_variants_in_region


IUPAC = {
    frozenset(["A", "G"]): "R",
    frozenset(["C", "T"]): "Y",
    frozenset(["G", "T"]): "K",
    frozenset(["A", "C"]): "M",
    frozenset(["C", "G"]): "S",
    frozenset(["A", "T"]): "W",
    frozenset(["C", "G", "T"]): "B",
    frozenset(["A", "G", "T"]): "D",
    frozenset(["A", "C", "T"]): "H",
    frozenset(["A", "C", "G"]): "V",
    frozenset(["A", "C", "G", "T"]): "N",
}


def parse_args():
    parser = argparse.ArgumentParser(description="Generate a FASTA where the "
                                                 "SNPs are represented as "
                                                 "their IUPAC code.")
    parser.add_argument(
        "region",
        type=str,
        help="The region for sequence extraction (e.g. chr3:12345-12567)"
    )

    parser.add_argument(
        "--maf-over",
        type=float,
        default=0,
        help="Filter on the minor allele frequency."
    )

    args = parser.parse_args()
    return args.region, args.maf_over


def main(region, maf, out=sys.stdout):
    # Get the sequence for the region of interest.
    region = Region.from_str(region)
    sequence = region.sequence
    li_seq = list(sequence.seq)

    # Get all the variants in the region.
    variants = ensembl_variants_in_region(region)

    # Annotate all the variants to get the MAF.
    for variant in variants:
        variant.load_ensembl_annotations()
    variants = [v for v in variants if
                (v._info["maf"] is None) or (v._info["maf"] > maf)]

    # Write the pandas dataframe.
    csv_filename = "variants_in_{}_{}-{}.txt".format(
        region.chrom,
        region.start,
        region.end,
    )
    df = variant_list_to_dataframe(variants)
    df.to_csv(csv_filename, sep="\t", index=False)

    # Now create and fix the fasta file.
    multi_allelics = []
    for variant in variants:
        # Ignore indels.
        if type(variant) is not SNP:
            continue
{'fraction_non_alphanumeric': 0.13438575187209068, 'fraction_numerical': 0.005059704513256426, 'mean_word_length': 2.6020408163265305, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '201053', 'n_tokens_mistral': 1668, 'n_tokens_neox': 1589, 'n_words': 452}