Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Raise an exception in CDAOIO.py if librdf is not available; catch thi…

…s exception in Bio.Phylo._io and disable CDAO as a supported format.
  • Loading branch information...
commit dd8a300716d025a616a8e76295887700636851a0 1 parent 45fec14
@bendmorris bendmorris authored etal committed
Showing with 12 additions and 27 deletions.
  1. +6 −24 Bio/Phylo/CDAOIO.py
  2. +6 −3 Bio/Phylo/_io.py
View
30 Bio/Phylo/CDAOIO.py
@@ -26,6 +26,11 @@
import os
import urlparse
+try:
+ import RDF
+ import Redland
+except ImportError:
+ raise CDAOError('Support for CDAO tree format requires the librdf Python bindings.')
RDF_NAMESPACES = {
'owl': 'http://www.w3.org/2002/07/owl#',
@@ -35,8 +40,6 @@
def node_uri(graph, uri):
'''Returns the full URI of a node by appending the node URI to the graph URI.'''
- RDF = import_rdf()
-
if graph.endswith('/'):
return RDF.Uri(urlparse.urljoin(graph, uri))
else:
@@ -47,20 +50,9 @@ class CDAOError(Exception):
pass
-def import_rdf():
- '''Attempt to import librdf in this function, and raise a CDAOError if
- import fails. This avoids an explicit dependence on librdf.'''
-
- try: import RDF
- except ImportError: raise CDAOError('Redland Python bindings are required for CDAO support.')
- #RDF.debug(1)
- return RDF
-
-
def new_storage():
'''Create a new in-memory Redland store for storing the RDF model.'''
- RDF = import_rdf()
-
+
storage = RDF.Storage(storage_name="hashes",
name="test",
options_string="new='yes',hash-type='memory',dir='.'")
@@ -116,7 +108,6 @@ def parse(self, **kwargs):
def parse_handle_to_model(self, rooted=False, storage=None,
mime_type='text/turtle', context=None, **kwargs):
'''Parse self.handle into RDF model self.model.'''
- RDF = import_rdf()
if storage is None:
# store RDF model in memory for now
@@ -146,7 +137,6 @@ def parse_handle_to_model(self, rooted=False, storage=None,
def parse_model(self, model=None, context=None):
'''Generator that yields CDAO.Tree instances from an RDF model.'''
- RDF = import_rdf()
if model is None:
model = self.model
@@ -164,7 +154,6 @@ def parse_model(self, model=None, context=None):
def new_clade(self, node):
'''Returns a CDAO.Clade object for a given named node.'''
- RDF = import_rdf()
result = self.node_info[node]
@@ -180,7 +169,6 @@ def new_clade(self, node):
def get_node_info(self, model, context=None):
'''Creates a dictionary containing information about all nodes in the tree.'''
- RDF = import_rdf()
Uri = RDF.Uri
@@ -287,7 +275,6 @@ def write(self, handle, mime_type='text/turtle', tree_uri='tree', context=None,
mime_type: used to determine the serialization format.
default is 'text/turtle'
"""
- RDF = import_rdf()
self.record_complete_ancestry = record_complete_ancestry
@@ -297,8 +284,6 @@ def write(self, handle, mime_type='text/turtle', tree_uri='tree', context=None,
def add_trees_to_model(self, trees=None, storage=None, tree_uri='tree', context=None):
"""Add triples describing a set of trees to an RDF model."""
- RDF = import_rdf()
- import Redland
if context: context = RDF.Node(RDF.Uri(context))
self.tree_uri = tree_uri
@@ -340,7 +325,6 @@ def add_trees_to_model(self, trees=None, storage=None, tree_uri='tree', context=
def serialize_model(self, handle, mime_type='text/turtle'):
"""Serialize RDF model to file handle"""
- RDF = import_rdf()
# serialize RDF model to output file
serializer = RDF.Serializer(mime_type=mime_type)
@@ -354,7 +338,6 @@ def serialize_model(self, handle, mime_type='text/turtle'):
def process_clade(self, clade, parent=None, root=False):
'''recursively generate statements describing a tree of clades'''
- RDF = import_rdf()
self.node_counter += 1
clade.uri = 'node%s' % str(self.node_counter).zfill(7)
@@ -447,6 +430,5 @@ def process_clade(self, clade, parent=None, root=False):
def qUri(s):
'''returns the full URI from a namespaced URI string (i.e. rdf:type)'''
- RDF = import_rdf()
return RDF.Uri(resolve_uri(s, namespaces=RDF_NAMESPACES))
View
9 Bio/Phylo/_io.py
@@ -19,18 +19,21 @@
NexusIO,
PhyloXMLIO,
NeXMLIO,
- CDAOIO,
)
-
supported_formats = {
'newick': NewickIO,
'nexus': NexusIO,
'phyloxml': PhyloXMLIO,
'nexml': NeXMLIO,
- 'cdao': CDAOIO,
}
+try:
+ from Bio.Phylo import CDAOIO
+ supported_formats.update('cdao', CDAOIO)
+except:
+ pass
+
def parse(file, format, **kwargs):
"""Iteratively parse a file and return each of the trees it contains.
Please sign in to comment.
Something went wrong with that request. Please try again.