Exemple #1
0
def read(fname, format, f_id="conkit", **kwargs):
    """Parse a file handle to read into structure

    Parameters
    ----------
    fname : filehandle, filename
       A file path or open file handle
    format : str
       File format of handle
    f_id : str
       Identifier for the returned file

    Returns
    -------
    hierarchy
       The hierarchy instance of the requested file

    Examples
    --------
    1) Read a Multiple Sequence Alignment file into a ConKit hierarchy:

    >>> from conkit import io
    >>> with open('example.a3m', 'r') as f_in:
    ...     hierarchy = io.read(f_in, 'a3m')

    2) Read a contact prediction file into a conkit hierarchy:

    >>> from conkit import io
    >>> with open('example.mat', 'r') as f_in:
    ...     hierarchy = io.read(f_in, 'ccmpred')

    """
    if format in PARSER_CACHE:
        parser_in = PARSER_CACHE.import_class(format)()
    else:
        raise ValueError("Unrecognised format: {}".format(format))

    kwargs.update({"f_id": f_id})
    if format == "a3m-inserts":
        kwargs["remove_inserts"] = False

    if format in BINARY_FILE_FORMATS:
        mode = "rb"
    else:
        mode = "r"

    with open_f_handle(fname, mode) as f_in:
        hierarchy = parser_in.read(f_in, **kwargs)

    return hierarchy
Exemple #2
0
def write(fname, format, hierarchy, **kwargs):
    """Parse a file handle to read into structure

    Parameters
    ----------
    fname : filehandle, filename
       A file path or open file handle
    format : str
       File format of handle
    hierarchy
       ConKit hierarchy to write

    Examples
    --------
    1) Write a ConKit hierarchy into a Multiple Sequence Alignment file:

    >>> from conkit import io
    >>> with open('example.fas', 'r') as f_in, open('example.a3m', 'w') as f_out:
    ...     hierarchy = io.read(f_in, 'fasta')
    ...     io.write(f_out, 'a3m', hierarchy)

    2) Write a ConKit hierarchy into a contact prediction file:

    >>> from conkit import io
    >>> with open('example.txt', 'r') as f_in, open('example.rr', 'w') as f_out:
    ...     hierarchy = io.read(f_in, 'psicov')
    ...     io.write(f_out, 'casprr', hierarchy)

    """
    if format in PARSER_CACHE:
        parser_out = PARSER_CACHE.import_class(format)()
    else:
        raise ValueError("Unrecognised format: {}".format(format))

    if format in ["flib", "pconsc", "pconsc2", "saint2"]:
        kwargs["write_header_footer"] = False

    with open_f_handle(fname, "w") as f_out:
        parser_out.write(f_out, hierarchy, **kwargs)