Exemplo n.º 1
0
def convert_to_namespace(file, output, keyword, author):
    """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=author,
        namespace_domain=NAMESPACE_DOMAIN_OTHER,
        values=resource['Values'],
        citation_name=resource['Citation']['NameString'],
        file=output,
    )
Exemplo n.º 2
0
 def write_namespace(self, file: Optional[TextIO] = None):
     """Write this ontology as a BEL namespace."""
     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=zip(self._get_values(), repeat(''.join(self.encodings))),
         file=file
     )
Exemplo n.º 3
0
def _write_namespace(path,
                     values: Mapping[str, str],
                     namespace_version: Optional[str] = None):
    with open(path, 'w') as file:
        write_namespace(
            namespace_name='Neuroimaging Feature Terminology',
            namespace_keyword='NIFT',
            namespace_domain=NAMESPACE_DOMAIN_OTHER,
            namespace_version=namespace_version,
            author_name='Sepehr Golriz',
            author_contact='*****@*****.**',
            values=values,
            author_copyright='CC0 1.0 Universal',
            case_sensitive=True,
            cacheable=True,
            file=file,
        )
Exemplo n.º 4
0
def write(name, keyword, domain, citation, author, description, species,
          version, contact, license, values, output):
    """Build 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,
        file=output,
    )
Exemplo n.º 5
0
def export_namespace(
    graph: BELGraph,
    namespace: str,
    directory: Optional[str] = None,
    cacheable: bool = False,
) -> None:
    """Export all names and missing names from the given namespace to its own BELNS file 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 namespace: The namespace to process
    :param directory: The path to the directory where to output the namespace. Defaults to the current working
     directory returned by :func:`os.getcwd`
    :param 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, f'{namespace}.belns')

    logger.info('Outputting to %s', path)
    right_names = get_names_by_namespace(graph, namespace)
    logger.info('Graph has %d correct names in %s', len(right_names), namespace)
    wrong_names = get_incorrect_names_by_namespace(graph, namespace)
    logger.info('Graph has %d incorrect names in %s', len(wrong_names), namespace)
    undefined_ns_names = get_undefined_namespace_names(graph, namespace)
    logger.info('Graph has %d names in missing namespace %s', len(undefined_ns_names), namespace)

    names = (right_names | wrong_names | undefined_ns_names)

    if 0 == len(names):
        logger.warning(f'{namespace} is empty')

    with open(path, 'w') as file:
        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,
        )
Exemplo n.º 6
0
def merge_namespaces():
    """Serve the page for merging BEL namespaces."""
    form = MergeNamespaceForm()

    if not form.validate_on_submit():
        return render_template('curation/merge_namespaces.html', form=form)

    logger.warning(form.file)

    files = request.files.getlist("file")

    names = set()

    for file in files:
        logger.warning('file: %s', file)
        resource = parse_bel_resource(codecs.iterdecode(file, 'utf-8'))
        names |= set(resource['Values'])

    file = StringIO()

    write_namespace(
        namespace_name=form.name.data,
        namespace_keyword=form.keyword.data,
        namespace_species=form.species.data,
        namespace_description=form.description.data,
        author_name=current_user.name,
        author_contact=current_user.email,
        citation_name=form.citation.data,
        citation_description=
        'This merged namespace was created by the PyBEL v{}'.format(
            get_pybel_version()),
        namespace_domain=form.domain.data,
        author_copyright=form.licenses.data,
        values=names,
        cacheable=False,
        file=file,
    )

    output = make_response(file.getvalue())
    output.headers[
        "Content-Disposition"] = "attachment; filename={}.belns".format(
            form.keyword.data)
    output.headers["Content-type"] = "text/plain"
    return output
Exemplo n.º 7
0
def _write_namespace(values):
    with open(output_file, 'w') as file:
        write_namespace(
            namespace_name='FamPlex',
            namespace_keyword='FPLX',
            namespace_domain=NAMESPACE_DOMAIN_GENE,
            author_name='John Bachman and Ben Gyori',
            citation_name='FamPlex',
            values=values,
            namespace_description=
            'FamPlex is a collection of resources for grounding biological entities from text '
            'and describing their hierarchical relationships.',
            namespace_query_url='http://identifiers.org/fplx/',
            author_copyright='CC0 1.0 Universal',
            citation_url='https://github.com/sorgerlab/famplex',
            case_sensitive=True,
            cacheable=True,
            file=file,
        )
Exemplo n.º 8
0
    def write_bel_namespace(self,
                            file: TextIO,
                            use_names: bool = False) -> None:
        """Write as a BEL namespace file."""
        if not self.is_populated():
            self.populate()

        if use_names and not self.has_names:
            raise ValueError

        values = (self._get_namespace_name_to_encoding(desc='writing names')
                  if use_names else self._get_namespace_identifier_to_encoding(
                      desc='writing identifiers'))

        write_namespace(
            namespace_name=self._get_namespace_name(),
            namespace_keyword=self._get_namespace_keyword(),
            namespace_query_url=self.identifiers_url,
            values=values,
            file=file,
        )
Exemplo n.º 9
0
def _write_namespace(path,
                     values: Mapping[str, str],
                     namespace_version: Optional[str] = None):
    with open(path, 'w') as file:
        write_namespace(
            namespace_name='Curation of Neurodegeneration Supporting Ontology',
            namespace_keyword='CONSO',
            namespace_domain=NAMESPACE_DOMAIN_OTHER,
            namespace_version=namespace_version,
            author_name='Charles Tapley Hoyt',
            citation_name='CONSO',
            values=values,
            namespace_description=
            'The Curation of Neurodegeneration Supporting Ontology (CONSO) contains terms'
            ' related to neurodegenerative disease and curation of related material. It is not '
            'disease-specific, but at least has a focus on Alzheimer\'s disease.',
            author_copyright='CC0 1.0 Universal',
            case_sensitive=True,
            cacheable=True,
            file=file,
        )
Exemplo n.º 10
0
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,
    check_keywords=True,
) -> None:
    """Merge 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 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,
            file=file,
        )