Exemple #1
0
def test_selection_from_bedfile_and_subselection(conn):
    """Test the creation of a selection based on BED data

    .. note:: Please note that the bedreader **is not** tested here!
    """
    larger_string = """
        chr1 1    10   feature1  0 +
        chr1 50   60   feature2  0 -
        chr1 51 59 another_feature 0 +
    """
    # According to VARIANTS global variable with 3 variants (pos 10, 50 and 45)
    # 1: chr1, pos 1 to 10 => 1 variant concerned (pos 10)
    # 2: chr1, pos 50 to 60 => 1 variant concerned (pos 50)
    # 3: chr1, pos 51 to 59 => 0 variants
    bedtool = BedReader(larger_string)

    # Create now a sub selection => 2 variants (pos 10, 45)
    query = "SELECT variants.id,chr,pos,ref,alt FROM variants WHERE ref='G'"
    set_A_id = sql.create_selection_from_sql(conn, query, "setA", count=None)
    # 2nd selection (1st is the default "variants")
    assert set_A_id == 2
    assert "setA" in list(s["name"] for s in sql.get_selections(conn))

    # 1: chr1, pos 1 to 10 => 1 remaining variant
    # 2: chr1, pos 50 to 60 => 0 variant
    # 3: chr1, pos 51 to 59 => 0 variant
    ret = sql.create_selection_from_bed(conn, "setA", "sub_bedname", bedtool)
    # id of selection
    assert ret == 3

    data = conn.execute(
        "SELECT * FROM selection_has_variant WHERE selection_id = ?", (ret, ))
    expected = ((1, ret), )
    record = tuple([tuple(i) for i in data])
    assert record == expected
Exemple #2
0
def bed_cmd(conn: sqlite3.Connection, path: str, target: str, source: str,
            **kwargs):
    """Create a new selection from a bed file

    This following VQL command:
        `CREATE boby FROM variants INTERSECT "path/to/file.bed"`
    will execute :
        `bed_cmd(conn, "path/to/file.bed", "boby", "source")`

    Args:
        conn (sqlite3.Connection): sqlite3 connection
        path (str): path to bedfile ( a 3 columns files with chr, start, end )
        target (str): target selection table
        source (str): source selection table

    Returns:
        dict: {"id": id of last line inserted} if lines have been inserted,
            or empty dict in case of error

    Raises:
        vql.VQLSyntaxError
    """
    if not os.path.isfile(path):
        raise vql.VQLSyntaxError(f"{path} doesn't exists")

    # bed_intervals: chrom, start, end, name, etc. keys in each interval
    # see also cutevariant/core/reader/bedreader.py
    selection_id = sql.create_selection_from_bed(conn, source, target,
                                                 BedReader(path))
    return dict() if selection_id is None else {"id": selection_id}
Exemple #3
0
    def create_selection_from_bed(self):
        """Ask user for a bed file and create a new selection from it """
        # Reload last directory used
        app_settings = QSettings()
        last_directory = app_settings.value("last_directory", QDir.homePath())

        filepath, _ = QFileDialog.getOpenFileName(
            self, self.tr("Open BED file"), last_directory,
            self.tr("BED - Browser Extensible Data (*.bed)"))
        if not filepath:
            return

        selection_name = self.ask_and_check_selection_name()
        if not selection_name:
            return

        current_index = self.view.selectionModel().currentIndex()
        current_selection = self.model.record(current_index)
        source = current_selection["name"]

        # Open bed intervals & create selection
        intervals = BedReader(filepath)
        sql.create_selection_from_bed(self.model.conn, source, selection_name,
                                      intervals)
        # Refresh UI
        self.model.load()
Exemple #4
0
def test_bedreader_from_empty_string():
    """Test bed string with no data at all or just no data after the header"""

    large_string = """
        browser position chr7:127471196-127495720
    """

    bedtool = BedReader(large_string)
    intervals = tuple(bedtool)

    assert intervals == tuple()
    assert bedtool.count == 0

    large_string = ""

    bedtool = BedReader(large_string)
    intervals = tuple(bedtool)

    assert intervals == tuple()
    assert bedtool.count == 0
Exemple #5
0
def test_bedreader_from_string():
    """Test bed string"""

    large_string = """
        chr1 1    10   feature1  0 +
        chr1 50   60   feature2  0 -
        chr1 51 59 another_feature 0 +
    """
    intervals = tuple(BedReader(large_string))
    expected = (
        OrderedDict([
            ("chrom", "chr1"),
            ("start", "1"),
            ("end", "10"),
            ("name", "feature1"),
            ("score", "0"),
            ("strand", "+"),
            ("thickStart", None),
            ("thickEnd", None),
            ("itemRgb", None),
            ("blockCount", None),
            ("blockSizes", None),
            ("blockStarts", None),
        ]),
        OrderedDict([
            ("chrom", "chr1"),
            ("start", "50"),
            ("end", "60"),
            ("name", "feature2"),
            ("score", "0"),
            ("strand", "-"),
            ("thickStart", None),
            ("thickEnd", None),
            ("itemRgb", None),
            ("blockCount", None),
            ("blockSizes", None),
            ("blockStarts", None),
        ]),
        OrderedDict([
            ("chrom", "chr1"),
            ("start", "51"),
            ("end", "59"),
            ("name", "another_feature"),
            ("score", "0"),
            ("strand", "+"),
            ("thickStart", None),
            ("thickEnd", None),
            ("itemRgb", None),
            ("blockCount", None),
            ("blockSizes", None),
            ("blockStarts", None),
        ]),
    )
    assert intervals == expected
Exemple #6
0
def test_selection_from_bedfile(conn):
    """Test the creation of a selection based on BED data

    .. note:: Please note that the bedreader **is not** tested here!
    """

    larger_string = """
        chr1 1    10   feature1  0 +
        chr1 50   60   feature2  0 -
        chr1 51 59 another_feature 0 +
    """
    # According to VARIANTS global variable with 3 variants (pos 10, 50 and 45)
    # 1: chr1, pos 1 to 10 => 1 variant concerned (pos 10)
    # 2: chr1, pos 50 to 60 => 1 variant concerned (pos 50)
    # 3: chr1, pos 51 to 59 => 0 variants

    bedtool = BedReader(larger_string)

    # Create a new selection (a second one, since there is a default one during DB creation)
    selection_name = "bedname"
    ret = sql.create_selection_from_bed(conn, "variants", selection_name,
                                        bedtool)

    # Test last id of the selection
    assert ret == 2

    # Query the association table (variant_id, selection_id)
    data = conn.execute(
        "SELECT * FROM selection_has_variant WHERE selection_id = ?", (ret, ))
    # 2 variants (see above)
    # format: [(id variant, id selection),]
    expected = ((1, ret), (2, ret))
    record = tuple([tuple(i) for i in data])

    # Is the association table 'selection_has_variant' ok ?
    print("record:", record)
    assert record == expected

    bed_selection = [
        s for s in sql.get_selections(conn) if s["name"] == selection_name
    ][0]
    print("selection content", bed_selection)
    assert bed_selection["name"] == selection_name
    assert bed_selection["count"] == 2  # 2 variants retrieved
Exemple #7
0
def test_bedreader_from_file():
    """Test bed data in gz and uncompressed files"""

    bedtool = BedReader("examples/test.bed.gz")
    intervals = tuple(bedtool)
    expected = (
        OrderedDict([
            ("chrom", "chr11"),
            ("start", "10000"),
            ("end", "15000"),
            ("name", None),
            ("score", None),
            ("strand", None),
            ("thickStart", None),
            ("thickEnd", None),
            ("itemRgb", None),
            ("blockCount", None),
            ("blockSizes", None),
            ("blockStarts", None),
        ]),
        OrderedDict([
            ("chrom", "chr11"),
            ("start", "119000"),
            ("end", "123000"),
            ("name", None),
            ("score", None),
            ("strand", None),
            ("thickStart", None),
            ("thickEnd", None),
            ("itemRgb", None),
            ("blockCount", None),
            ("blockSizes", None),
            ("blockStarts", None),
        ]),
        OrderedDict([
            ("chrom", "chr11"),
            ("start", "123002"),
            ("end", "123999"),
            ("name", None),
            ("score", None),
            ("strand", None),
            ("thickStart", None),
            ("thickEnd", None),
            ("itemRgb", None),
            ("blockCount", None),
            ("blockSizes", None),
            ("blockStarts", None),
        ]),
        OrderedDict([
            ("chrom", "chr1"),
            ("start", "0"),
            ("end", "999999"),
            ("name", None),
            ("score", None),
            ("strand", None),
            ("thickStart", None),
            ("thickEnd", None),
            ("itemRgb", None),
            ("blockCount", None),
            ("blockSizes", None),
            ("blockStarts", None),
        ]),
    )

    assert intervals == expected
    assert bedtool.count == 4

    bedtool = BedReader("examples/test_with_headers.bed")
    intervals = tuple(bedtool)

    assert intervals == expected
    assert bedtool.count == 4