Beispiel #1
0
def write_hmdb_id_ns(file=None, values=None, connection=None):
    """
    Create a BEL namespace with HMDB identifiers.

    :param file: file to which the namespace should be written. If None it will be outputted in stdout
    :param values: Values that should be part of the namespace. (HMDB identifiers)
    :param str connection: Connection string to connect manager to a database. Can also directly be a manager
    """

    m = Manager.ensure(connection)

    if values is None:
        values = m.get_hmdb_accession()

    write_namespace(
        namespace_name='Human Metabolome Database',
        namespace_keyword='HMDB',
        namespace_domain=NAMESPACE_DOMAIN_CHEMICAL,
        author_name='Colin Birkenbihl, Charles Tapley Hoyt',
        author_contact='*****@*****.**',
        author_copyright='Creative Commons by 4.0',
        citation_name='HMDB',
        values=values,
        functions='A',
        file=file
    )
Beispiel #2
0
def convert_to_namespace(file, output, keyword):
    """Convert an annotation file to a namespace file"""
    resource = parse_bel_resource(file)
    write_namespace(
        namespace_keyword=(keyword
                           or resource['AnnotationDefinition']['Keyword']),
        namespace_name=resource['AnnotationDefinition']['Keyword'],
        namespace_description=resource['AnnotationDefinition']
        ['DescriptionString'],
        author_name='Charles Tapley Hoyt',
        namespace_domain=NAMESPACE_DOMAIN_OTHER,
        values=resource['Values'],
        citation_name=resource['Citation']['NameString'],
        file=output)
Beispiel #3
0
def write(name, keyword, domain, citation, author, description, species,
          version, contact, license, values, functions, output, value_prefix):
    """Builds a namespace from items"""
    write_namespace(name,
                    keyword,
                    domain,
                    author,
                    citation,
                    values,
                    namespace_description=description,
                    namespace_species=species,
                    namespace_version=version,
                    author_contact=contact,
                    author_copyright=license,
                    functions=functions,
                    file=output,
                    value_prefix=value_prefix)
def export_namespace(graph, namespace, directory=None, cacheable=False):
    """Exports all names and missing names from the given namespace to its own BEL Namespace files in the given
    directory.

    Could be useful during quick and dirty curation, where planned namespace building is not a priority.

    :param pybel.BELGraph graph: A BEL graph
    :param str namespace: The namespace to process
    :param str directory: The path to the directory where to output the namespace. Defaults to the current working
                      directory returned by :func:`os.getcwd`
    :param bool cacheable: Should the namespace be cacheable? Defaults to ``False`` because, in general, this operation
                        will probably be used for evil, and users won't want to reload their entire cache after each
                        iteration of curation.
    """
    directory = os.getcwd() if directory is None else directory
    path = os.path.join(directory, '{}.belns'.format(namespace))

    with open(path, 'w') as file:
        log.info('Outputting to %s', path)
        right_names = get_names_by_namespace(graph, namespace)
        log.info('Graph has %d correct names in %s', len(right_names),
                 namespace)
        wrong_names = get_incorrect_names_by_namespace(graph, namespace)
        log.info('Graph has %d incorrect names in %s', len(right_names),
                 namespace)
        undefined_ns_names = get_undefined_namespace_names(graph, namespace)
        log.info('Graph has %d names in missing namespace %s',
                 len(right_names), namespace)

        names = (right_names | wrong_names | undefined_ns_names)

        if 0 == len(names):
            log.warning('%s is empty', namespace)

        write_namespace(namespace_name=namespace,
                        namespace_keyword=namespace,
                        namespace_domain='Other',
                        author_name=graph.authors,
                        author_contact=graph.contact,
                        citation_name=graph.name,
                        values=names,
                        cacheable=cacheable,
                        file=file)
Beispiel #5
0
    def write_namespace(self, file=None):
        """Serializes a BEL namespace.

        :param file file: A write-enable file or file-like
        """
        values = self._get_values()

        write_namespace(namespace_name=self.title,
                        namespace_keyword=self.preferred_prefix,
                        namespace_domain=self.namespace_domain,
                        namespace_description=self.description,
                        namespace_version=self.version,
                        citation_name=self.title,
                        citation_url=self.version_iri,
                        author_name='Charles Tapley Hoyt',
                        author_contact='*****@*****.**',
                        author_copyright='Creative Commons by 4.0',
                        values=values,
                        functions=self.encodings,
                        file=file)
def merge_namespaces(input_locations,
                     output_path,
                     namespace_name,
                     namespace_keyword,
                     namespace_domain,
                     author_name,
                     citation_name,
                     namespace_description=None,
                     namespace_species=None,
                     namespace_version=None,
                     namespace_query_url=None,
                     namespace_created=None,
                     author_contact=None,
                     author_copyright=None,
                     citation_description=None,
                     citation_url=None,
                     citation_version=None,
                     citation_date=None,
                     case_sensitive=True,
                     delimiter='|',
                     cacheable=True,
                     functions=None,
                     value_prefix='',
                     sort_key=None,
                     check_keywords=True):
    """Merges namespaces from multiple locations to one.

    :param iter input_locations: An iterable of URLs or file paths pointing to BEL namespaces.
    :param str output_path: The path to the file to write the merged namespace
    :param str namespace_name: The namespace name
    :param str namespace_keyword: Preferred BEL Keyword, maximum length of 8
    :param str namespace_domain: One of: :data:`pybel.constants.NAMESPACE_DOMAIN_BIOPROCESS`,
                            :data:`pybel.constants.NAMESPACE_DOMAIN_CHEMICAL`,
                            :data:`pybel.constants.NAMESPACE_DOMAIN_GENE`, or
                            :data:`pybel.constants.NAMESPACE_DOMAIN_OTHER`
    :param str author_name: The namespace's authors
    :param str citation_name: The name of the citation
    :param str namespace_query_url: HTTP URL to query for details on namespace values (must be valid URL)
    :param str namespace_description: Namespace description
    :param str namespace_species: Comma-separated list of species taxonomy id's
    :param str namespace_version: Namespace version
    :param str namespace_created: Namespace public timestamp, ISO 8601 datetime
    :param str author_contact: Namespace author's contact info/email address
    :param str author_copyright: Namespace's copyright/license information
    :param str citation_description: Citation description
    :param str citation_url: URL to more citation information
    :param str citation_version: Citation version
    :param str citation_date: Citation publish timestamp, ISO 8601 Date
    :param bool case_sensitive: Should this config file be interpreted as case-sensitive?
    :param str delimiter: The delimiter between names and labels in this config file
    :param bool cacheable: Should this config file be cached?
    :param functions: The encoding for the elements in this namespace
    :type functions: iterable of characters
    :param str value_prefix: a prefix for each name
    :param sort_key: A function to sort the values with :func:`sorted`
    :param bool check_keywords: Should all the keywords be the same? Defaults to ``True``
    """
    results = get_merged_namespace_names(input_locations,
                                         check_keywords=check_keywords)

    with open(output_path, 'w') as file:
        write_namespace(namespace_name=namespace_name,
                        namespace_keyword=namespace_keyword,
                        namespace_domain=namespace_domain,
                        author_name=author_name,
                        citation_name=citation_name,
                        values=results,
                        namespace_species=namespace_species,
                        namespace_description=namespace_description,
                        namespace_query_url=namespace_query_url,
                        namespace_version=namespace_version,
                        namespace_created=namespace_created,
                        author_contact=author_contact,
                        author_copyright=author_copyright,
                        citation_description=citation_description,
                        citation_url=citation_url,
                        citation_version=citation_version,
                        citation_date=citation_date,
                        case_sensitive=case_sensitive,
                        delimiter=delimiter,
                        cacheable=cacheable,
                        functions=functions,
                        value_prefix=value_prefix,
                        sort_key=sort_key,
                        file=file)