コード例 #1
0
def import_cmd(conn: sqlite3.Connection,
               feature=str,
               name=str,
               path=str,
               **kwargs):
    """Import command for wordsets only

    This following VQL command:
        `IMPORT WORDSETS "gene.txt" AS boby`
    will execute :
        `import_cmd(conn, "WORDSETS", "gene.txt")`

    Args:
        conn (sqlite3.Connection): sqlite3 connection
        feature (str): "WORDSETS" (Name of the SQL table)
        name (str): name of the set
        path (str): a filepath

    Returns:
        dict: `{success: <boolean>}`

    Raises:
        vql.VQLSyntaxError
    """
    accepted_features = ("wordsets", )

    if feature.lower() not in accepted_features:
        raise vql.VQLSyntaxError(f"option {feature} doesn't exists")

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

    affected_rows = sql.import_wordset_from_file(conn, name, path)
    return {"success": (affected_rows is not None)}
コード例 #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}
コード例 #3
0
def show_cmd(conn: sqlite3.Connection, feature: str, **kwargs):
    """Show command display information from a SHOW query

    This following VQL command:
        `SHOW variants`
    will execute :
        `show_cmd(conn, "variants")`

    Args:
        conn (sqlite3.Connection): sqlite3 connection
        feature (str): Requested feature type of items (Name of the SQL table);
            Lower case features are also accepted.
        can be: `"selections", "fields", "samples", "wordsets"`

    Yields:
        (generator[dict]): Items according to requested feature.

    Raises:
        vql.VQLSyntaxError
    """
    accepted_features = {
        "selections": sql.get_selections,
        "fields": sql.get_fields,
        "samples": sql.get_samples,
        "wordsets": sql.get_wordsets,
    }

    # Cast in lower case
    feature = feature.lower()

    if feature not in accepted_features:
        raise vql.VQLSyntaxError(f"option {feature} doesn't exists")

    for item in accepted_features[feature](conn):
        yield item
コード例 #4
0
def drop_cmd(conn: sqlite3.Connection, feature: str, name: str, **kwargs):
    """Drop selection or set from database

    This following VQL commands::

        DROP selections boby
        DROP wordsets mygene

    will execute::

        drop_cmd(conn, "selections", "boby")
        drop_cmd(conn, "wordsets", "boby")

    Args:
        conn (sqlite3.Connection): sqlite connection
        feature (str): selections or wordsets (Names of the SQL tables).
            Lower case features are also accepted.
        name (str): name of the selection or name of the wordset

    Returns:
        dict: {"success": <boolean>}; True if deletion is ok, False otherwise.

    Raises:
        vql.VQLSyntaxError
    """
    accept_features = ("selections", "wordsets")

    # Cast to lower case
    feature = feature.lower()

    if feature not in accept_features:
        raise vql.VQLSyntaxError(f"{feature} doesn't exists")

    affected_lines = sql.delete_by_name(conn, name, table_name=feature)
    return {"success": (affected_lines > 0)}
コード例 #5
0
def create_command_from_obj(conn, vql_obj: dict):
    """Create command function from vql object according to its "cmd" key

    cmd authorized:

        - select_cmd
        - count_cmd
        - drop_cmd
        - create_cmd
        - set_cmd
        - bed_cmd
        - show_cmd
        - import_cmd

    Warning:
        Use :meth:`execute` instead, that is a wrapper to this function.

    Examples:
        If you want to create a select_cmd function, pass a vql_obj like this one:

        .. code-block:: python

            {
                "cmd": "select_cmd",
                "fields": ["chr", "pos"],
                "source": "variants",
                "filters": {},
            }

    Args:
        conn (sqlite3.Connection): sqlite3.connection
        vql_obj (dict): A VQL object with requested commands at "cmd" key:
            `select_cmd, create_cmd, set_cmd, bed_cmd, show_cmd, import_cmd,
            drop_cmd, count_cmd`.
            A VQL object is dictionary returned by vql.parse.

    Returns:
        (function): Function object wrapping the given vql_object.
    """
    command = vql_obj["cmd"]
    if command in globals():
        return functools.partial(globals()[command], conn, **vql_obj)
    raise vql.VQLSyntaxError(f"cmd {command} doesn't exists")