コード例 #1
0
 def test_open_f_handle_4(self):
     fname = self.tempfile()
     with _iotools.open_f_handle(fname, "w") as fhandle:
         self.assertEqual("w", fhandle.mode)
         fhandle.write("hello world!")
     with _iotools.open_f_handle(fname, "r") as fhandle:
         self.assertEqual("r", fhandle.mode)
         self.assertEqual("hello world!", fhandle.read().strip())
コード例 #2
0
 def test_open_f_handle_3(self):
     fname = self.tempfile()
     with _iotools.open_f_handle(fname, "w") as fhandle:
         self.assertEqual("w", fhandle.mode)
     f_in_handle = _iotools.open_f_handle(fname, "w")
     with _iotools.open_f_handle(f_in_handle, "w") as fhandle:
         self.assertEqual("w", fhandle.mode)
     f_in_handle.close()
コード例 #3
0
 def test_open_f_handle_4(self):
     fname = _iotools.create_tmp_f()
     with _iotools.open_f_handle(fname, 'write') as fhandle:
         self.assertEqual('w', fhandle.mode)
         fhandle.write("hello world!")
     with _iotools.open_f_handle(fname, 'read') as fhandle:
         self.assertEqual('r', fhandle.mode)
         self.assertEqual("hello world!", fhandle.read().strip())
     os.unlink(fname)
コード例 #4
0
 def test_open_f_handle_3(self):
     fname = _iotools.create_tmp_f()
     with _iotools.open_f_handle(fname, 'write') as fhandle:
         self.assertEqual('w', fhandle.mode)
     f_in_handle = _iotools.open_f_handle(fname, 'write')
     with _iotools.open_f_handle(f_in_handle, 'write') as fhandle:
         self.assertEqual('w', fhandle.mode)
     f_in_handle.close()
     os.unlink(fname)
コード例 #5
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
コード例 #6
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)
コード例 #7
0
 def test_open_f_handle_6(self):
     fname = _iotools.create_tmp_f()
     with self.assertRaises(ValueError):
         _iotools.open_f_handle(fname, 'foo')
     with self.assertRaises(ValueError):
         _iotools.open_f_handle(fname, 'bar')
コード例 #8
0
 def test_open_f_handle_5(self):
     with self.assertRaises(TypeError):
         _iotools.open_f_handle(1, 'read')
     with self.assertRaises(TypeError):
         _iotools.open_f_handle(1.0, 'write')
コード例 #9
0
 def test_open_f_handle_6(self):
     fname = self.tempfile()
     with self.assertRaises(ValueError):
         _iotools.open_f_handle(fname, "foo")
     with self.assertRaises(ValueError):
         _iotools.open_f_handle(fname, "bar")
コード例 #10
0
 def test_open_f_handle_5(self):
     with self.assertRaises(TypeError):
         _iotools.open_f_handle(1, "r")
     with self.assertRaises(TypeError):
         _iotools.open_f_handle(1.0, "w")