Skip to content

Commit

Permalink
Fixed relative imports for 'read_files' method.
Browse files Browse the repository at this point in the history
  • Loading branch information
smelandr committed Oct 23, 2017
1 parent f5b21a3 commit 4c98b0e
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 190 deletions.
10 changes: 9 additions & 1 deletion nmrstarlib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@
:class:`~nmrstarlib.translator.StarFileToPeakList` for conversion of NMR-STAR
formatted files into peak list files using chemical shift values and assignment
information.
``fileio``
This module provides the :func:`~nmrstarlib.fileio.read_files` generator
to open files from different sources (single file/multiple files on a local
machine, directory/archive of files, URL address of a file).
"""

__version__ = "2.0.3"
__version__ = "2.0.4"


from .fileio import read_files
8 changes: 5 additions & 3 deletions nmrstarlib/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@
nmrstarlib -h | --help
nmrstarlib --version
nmrstarlib convert (<from_path> <to_path>) [--from_format=<format>] [--to_format=<format>] [--bmrb_url=<url>] [--nmrstar_version=<version>] [--verbose]
nmrstarlib csview <starfile_path> [--amino_acids=<aa>] [--atoms=<at>] [--csview_outfile=<path>] [--csview_format=<format>] [--bmrb_url=<url>] [--nmrstar_version=<version>] [--verbose]
nmrstarlib csview <starfile_path> [--amino_acids=<aa>] [--atoms=<at>] [--csview_outfile=<path>] [--csview_format=<format>] [--bmrb_url=<url>] [--nmrstar_version=<version>] [--verbose] [--show]
nmrstarlib plsimulate (<from_path> <to_path> <spectrum>) [--from_format=<format>] [--to_format=<format>] [--plsplit=<%>] [--distribution=<func>] [--H=<value>] [--C=<value>] [--N=<value>] [--bmrb_url=<url>] [--nmrstar_version=<version>] [--spectrum_descriptions=<path>] [--verbose]
Options:
-h, --help Show this screen.
--version Show version.
--verbose Print what files are processing.
--show Display chemical shifts image generated by 'csview' command by default image viewer.
--from_format=<format> Input file format, available formats: nmrstar, json [default: nmrstar].
--to_format=<format> Output file format, available formats: nmrstar, json [default: json].
--nmrstar_version=<version> Version of NMR-STAR format to use, available: 2, 3 [default: 3].
Expand Down Expand Up @@ -64,8 +65,9 @@ def cli(cmdargs):
amino_acids=aminoacids,
atoms=atoms,
filename=cmdargs["--csview_outfile"],
csview_format=cmdargs["--csview_format"])
chemshift_viewer.csview(view=True)
csview_format=cmdargs["--csview_format"],
nmrstar_version=cmdargs["--nmrstar_version"])
chemshift_viewer.csview(view=cmdargs["--show"])

elif cmdargs["plsimulate"]:
if cmdargs["--spectrum_descriptions"]:
Expand Down
5 changes: 2 additions & 3 deletions nmrstarlib/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,7 @@
import bz2
import gzip

from . import nmrstarlib

from . import fileio

class Converter(object):
"""Converter class to convert BMRB NMR-STAR files from NMR-STAR to JSON or from JSON to NMR-STAR format."""
Expand All @@ -135,7 +134,7 @@ def convert(self):

if os.path.isdir(self.file_generator.from_path):
self._many_to_many()
elif os.path.isfile(self.file_generator.from_path) or nmrstarlib.GenericFilePath.is_url(self.file_generator.from_path):
elif os.path.isfile(self.file_generator.from_path) or fileio.GenericFilePath.is_url(self.file_generator.from_path):
if self.file_generator.from_path_compression in ("zip", "tar", "tar.gz", "tar.bz2"):
self._many_to_many()
elif self.file_generator.from_path_compression in ("gz", "bz2"):
Expand Down
5 changes: 2 additions & 3 deletions nmrstarlib/csviewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
"""

from graphviz import Source
from . import nmrstarlib

from . import fileio

class CSViewer(object):
"""Chemical Shifts Viewer uses :meth:`~nmrstarlib.nmrstarlib.StarFile.chem_shifts_by_residue`
Expand Down Expand Up @@ -74,7 +73,7 @@ def csview(self, view=False):
:return: None
:rtype: :py:obj:`None`
"""
for starfile in nmrstarlib.read_files(self.from_path):
for starfile in fileio.read_files(self.from_path):
chains = starfile.chem_shifts_by_residue(amino_acids=self.amino_acids,
atoms=self.atoms,
nmrstar_version=self.nmrstar_version)
Expand Down
179 changes: 3 additions & 176 deletions nmrstarlib/nmrstarlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@
import sys
import os
import io
import zipfile
import bz2
import gzip
import tarfile
import pprint


Expand All @@ -47,13 +43,6 @@
else:
import json

if sys.version_info.major == 3:
from urllib.request import urlopen
from urllib.parse import urlparse
else:
from urllib2 import urlopen
from urlparse import urlparse

try:
from .cbmrblex import bmrblex
except ImportError:
Expand Down Expand Up @@ -238,6 +227,7 @@ def _build_starfile(self, nmrstar_str):
print("Error: Invalid token {}".format(token), file=sys.stderr)
print("In _build_starfile except block", file=sys.stderr)
raise

finally:
token = next(lexer)
return self
Expand Down Expand Up @@ -317,7 +307,7 @@ def _build_loop(self, lexer):
values = [OrderedDict(zip(fields, values[i:i + len(fields)])) for i in range(0, len(values), len(fields))]
return fields, values

def print_starfile(self, f=sys.stdout, file_format="nmrstar", tw=3):
def print_file(self, f=sys.stdout, file_format="nmrstar", tw=3):
"""Print :class:`~nmrstarlib.nmrstarlib.StarFile` into a file or stdout.
:param io.StringIO f: writable file-like stream.
Expand Down Expand Up @@ -420,7 +410,7 @@ def _to_nmrstar(self):
:rtype: :py:class:`str`
"""
nmrstar_str = io.StringIO()
self.print_starfile(nmrstar_str)
self.print_file(nmrstar_str)
return nmrstar_str.getvalue()

def _skip_saveframe(self, lexer):
Expand Down Expand Up @@ -540,169 +530,6 @@ def chem_shifts_by_residue(self, amino_acids=None, atoms=None, amino_acids_and_a
return chains


def _generate_filenames(sources):
"""Generate filenames.
:param tuple sources: Sequence of strings representing path to file(s).
:return: Path to file(s).
:rtype: :py:class:`str`
"""
for source in sources:
if os.path.isdir(source):
for path, dirlist, filelist in os.walk(source):
for fname in filelist:
print("Processing...", fname)
if GenericFilePath.is_compressed(fname):
if VERBOSE:
print("Skipping compressed file: {}".format(os.path.abspath(fname)))
continue
else:
yield os.path.join(path, fname)
elif os.path.isfile(source):
yield source
elif source.isdigit():
yield BMRB_REST + source
elif GenericFilePath.is_url(source):
yield source
else:
raise TypeError("Unknown file source.")


def _generate_handles(filenames):
"""Open a sequence of filenames one at time producing file objects.
The file is closed immediately when proceeding to the next iteration.
:param generator filenames: Generator object that yields the path to each file, one at a time.
:return: Filehandle to be processed into a :class:`~nmrstarlib.nmrstarlib.StarFile` instance.
"""
for fname in filenames:
if VERBOSE:
print("Processing file: {}".format(os.path.abspath(fname)))
path = GenericFilePath(fname)
for filehandle, source in path.open():
yield filehandle, source
filehandle.close()


def read_files(*sources):
"""Construct a generator that yields :class:`~nmrstarlib.nmrstarlib.StarFile` instances.
:param sources: One or more strings representing path to file(s).
:return: :class:`~nmrstarlib.nmrstarlib.StarFile` instance(s).
:rtype: :class:`~nmrstarlib.nmrstarlib.StarFile`
"""
filenames = _generate_filenames(sources)
filehandles = _generate_handles(filenames)
for fh, source in filehandles:
starfile = StarFile(source)
starfile.read(fh)
yield starfile


class GenericFilePath(object):
"""`GenericFilePath` class knows how to open local files or files over URL."""

def __init__(self, path):
"""Initialize path.
:param str path: String representing a path to local file(s) or valid URL address of file(s).
"""
self.path = path

def open(self):
"""Generator that opens and yields filehandles using appropriate facilities:
test if path represents a local file or file over URL, if file is compressed
or not.
:return: Filehandle to be processed into a :class:`~nmrstarlib.nmrstarlib.StarFile` instance.
"""
is_url = self.is_url(self.path)
compressiontype = self.is_compressed(self.path)

if not compressiontype:
if is_url:
filehandle = urlopen(self.path)
else:
filehandle = open(self.path, "r")
source = self.path
yield filehandle, source
filehandle.close()

elif compressiontype:
if is_url:
response = urlopen(self.path)
path = response.read()
response.close()
else:
path = self.path

if compressiontype == "zip":
ziparchive = zipfile.ZipFile(io.BytesIO(path), "r") if is_url else zipfile.ZipFile(path)
for name in ziparchive.infolist():
if not name.filename.endswith("/"):
filehandle = ziparchive.open(name)
source = self.path + "/" + name.filename
yield filehandle, source
filehandle.close()

elif compressiontype in ("tar", "tar.bz2", "tar.gz"):
tararchive = tarfile.open(fileobj=io.BytesIO(path)) if is_url else tarfile.open(path)
for name in tararchive:
if name.isfile():
filehandle = tararchive.extractfile(name)
source = self.path + "/" + name.name
yield filehandle, source
filehandle.close()

elif compressiontype == "bz2":
filehandle = bz2.BZ2File(io.BytesIO(path)) if is_url else bz2.BZ2File(path)
source = self.path
yield filehandle, source
filehandle.close()

elif compressiontype == "gz":
filehandle = gzip.open(io.BytesIO(path)) if is_url else gzip.open(path)
source = self.path
yield filehandle, source
filehandle.close()

@staticmethod
def is_compressed(path):
"""Test if path represents compressed file(s).
:param str path: Path to file(s).
:return: String specifying compression type if compressed, "" otherwise.
:rtype: :py:class:`str`
"""
if path.endswith(".zip"):
return "zip"
elif path.endswith(".tar.gz"):
return "tar.gz"
elif path.endswith(".tar.bz2"):
return "tar.bz2"
elif path.endswith(".gz"):
return "gz"
elif path.endswith(".bz2"):
return "bz2"
elif path.endswith(".tar"):
return "tar"
return ""

@staticmethod
def is_url(path):
"""Test if path represents a valid URL.
:param str path: Path to file.
:return: True if path is valid url string, False otherwise.
:rtype: :py:obj:`True` or :py:obj:`False`
"""
try:
parse_result = urlparse(path)
return all((parse_result.scheme, parse_result.netloc, parse_result.path))
except ValueError:
return False


class InvalidToken(Exception):
def __init__(self, value):
self.parameter = value
Expand Down
9 changes: 5 additions & 4 deletions nmrstarlib/translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import itertools

from . import nmrstarlib
from . import fileio
from . import plsimulator


Expand All @@ -32,8 +33,8 @@ def __init__(self, from_path, to_path, from_format=None, to_format=None):
self.to_path = to_path
self.from_format = from_format
self.to_format = to_format
self.from_path_compression = nmrstarlib.GenericFilePath.is_compressed(from_path)
self.to_path_compression = nmrstarlib.GenericFilePath.is_compressed(to_path)
self.from_path_compression = fileio.GenericFilePath.is_compressed(from_path)
self.to_path_compression = fileio.GenericFilePath.is_compressed(to_path)

def __iter__(self):
"""Abstract iterator must be implemented in a subclass."""
Expand Down Expand Up @@ -62,7 +63,7 @@ def __iter__(self):
:return: instance of :class:`~nmrstarlib.nmrstarlib.StarFile` object instance.
:rtype: :class:`~nmrstarlib.nmrstarlib.StarFile`
"""
for starfile in nmrstarlib.read_files(self.from_path):
for starfile in fileio.read_files(self.from_path):
yield starfile


Expand Down Expand Up @@ -248,7 +249,7 @@ def __iter__(self):
:return: instance of :class:`~nmrstarlib.plsimulator.PeakList` object instance.
:rtype: :class:`~nmrstarlib.plsimulator.PeakList`
"""
for starfile in nmrstarlib.read_files(self.from_path):
for starfile in fileio.read_files(self.from_path):
chains = starfile.chem_shifts_by_residue(amino_acids_and_atoms=self.spectrum.amino_acids_and_atoms,
nmrstar_version=self.nmrstar_version)

Expand Down

0 comments on commit 4c98b0e

Please sign in to comment.