Parsers

This page is for users who want to squeeze the most bizarre possibilities out of PyBEL. Most users will not need this reference.

PyBEL makes extensive use of the PyParsing module. The code is organized to different modules to reflect the different faces ot the BEL language. These parsers support BEL 2.0 and have some backwards compatibility for rewriting BEL 1.0 statements as BEL 2.0. The biologist and bioinformatician using this software will likely never need to read this page, but a developer seeking to extend the language will be interested to see the inner workings of these parsers.

See: https://github.com/OpenBEL/language/blob/master/version_2.0/MIGRATE_BEL1_BEL2.md

Metadata Parser

class pybel.parser.parse_metadata.MetadataParser(manager, namespace_dict=None, annotation_dict=None, namespace_regex=None, annotations_regex=None, default_namespace=None)[source]

A parser for the document and definitions section of a BEL document.

See also

BEL 1.0 Specification for the DEFINE keyword

Parameters:
  • manager (pybel.manager.cache.CacheManager) – A cache manager
  • namespace_dict (dict) – A dictionary of pre-loaded, enumerated namespaces from {namespace keyword: set of valid values}
  • annotation_dict (dict) – A dictionary of pre-loaded, enumerated annotations from {annotation keyword: set of valid values}
  • namespace_regex (dict) – A dictionary of pre-loaded, regular expression namespaces from {namespace keyword: regex string}
  • annotations_regex (dict) – A dictionary of pre-loaded, regular expression annotations from {annotation keyword: regex string}
  • default_namespace (set of str) – A set of strings that can be used without a namespace
manager = None

This metadata parser’s internal definition cache manager

namespace_dict = None

A dictionary of cached {namespace keyword: set of values}

annotations_dict = None

A dictionary of cached {annotation keyword: set of values}

namespace_regex = None

A dictionary of {namespace keyword: regular expression string}

namespace_regex_compiled = None

A dictionary of {namespace keyword: compiled regular expression}

default_namespace = None

A set of names that can be used without a namespace

annotations_regex = None

A dictionary of {annotation keyword: regular expression string}

annotations_regex_compiled = None

A dictionary of {annotation keyword: compiled regular expression}

document_metadata = None

A dictionary containing the document metadata

namespace_url_dict = None

A dictionary from {namespace keyword: BEL namespace URL}

namespace_owl_dict = None

A dictionary from {namespace keyword: OWL namespace URL}

annotation_url_dict = None

A dictionary from {annotation keyword: BEL annotation URL}

annotations_owl_dict = None

A dictionary from {annotation keyword: OWL annotation URL}

annotation_lists = None

A set of annotation keywords that are defined ad-hoc in the BEL script

handle_document(line, position, tokens)[source]

Handles statements like SET DOCUMENT X = "Y"

handle_namespace_url(line, position, tokens)[source]

Handles statements like DEFINE NAMESPACE X AS URL "Y"

handle_namespace_owl(line, position, tokens)[source]

Handles statements like DEFINE NAMESPACE X AS OWL "Y"

handle_namespace_pattern(line, position, tokens)[source]

Handles statements like DEFINE NAMESPACE X AS PATTERN "Y"

handle_annotation_owl(line, position, tokens)[source]

Handles statements like DEFINE ANNOTATION X AS OWL "Y"

handle_annotations_url(line, position, tokens)[source]

Handles statements like DEFINE ANNOTATION X AS URL "Y"

handle_annotation_list(line, position, tokens)[source]

Handles statements like DEFINE ANNOTATION X AS LIST {"Y","Z", ...}

handle_annotation_pattern(line, position, tokens)[source]

Handles statements like DEFINE ANNOTATION X AS PATTERN "Y"

has_enumerated_annotation(annotation)[source]

Checks if this annotation is defined by an enumeration

has_regex_annotation(annotation)[source]

Checks if this annotation is defined by a regular expression

has_annotation(annotation)[source]

Checks if this annotation is defined

has_enumerated_namespace(namespace)[source]

Checks if this namespace is defined by an enumeration

has_regex_namespace(namespace)[source]

Checks if this namespace is defined by a regular expression

has_namespace(namespace)[source]

Checks if this namespace is defined

check_version(line, position, s)[source]

Checks that a version string is valid for BEL documents, meaning it’s either in the YYYYMMDD or semantic version format

Control Parser

class pybel.parser.parse_control.ControlParser(annotation_dicts=None, annotation_regex=None, citation_clearing=True)[source]

A parser for BEL control statements

See also

BEL 1.0 specification on control records

Parameters:
  • set[str]] annotation_dicts (dict[str,) – A dictionary of {annotation: set of valid values} for parsing
  • str] annotation_regex (dict[str,) – A dictionary of {annotation: regular expression string}
  • citation_clearing (bool) – Should SET Citation statements clear evidence and all annotations?
handle_annotation_key(line, position, tokens)[source]

Called on all annotation keys before parsing to validate that it’s either enumerated or as a regex

get_annotations()[source]
Returns:The currently stored BEL annotations
Return type:dict
clear_citation()[source]

Clears the citation. Additionally, if citation clearing is enabled, clears the evidence and annotations.

clear()[source]

Clears the statement_group, citation, evidence, and annotations

Identifier Parser

class pybel.parser.parse_identifier.IdentifierParser(namespace_dict=None, namespace_regex=None, default_namespace=None, allow_naked_names=False)[source]

A parser for identifiers in the form of namespace:name. Can be made more lenient when given a default namespace or enabling the use of naked names

Parameters:
  • namespace_dict (dict[str,set[str]]) – A dictionary of {namespace: set of names}
  • namespace_regex (dict[str,str]) – A dictionary of {namespace: regular expression string} to compile
  • default_namespace (set[str]) – A set of strings that can be used without a namespace
  • allow_naked_names (bool) – If true, turn off naked namespace failures
namespace_dict = None

A dictionary of cached {namespace keyword: set of values}

namespace_regex = None

A dictionary of {namespace keyword: regular expression string}

namespace_regex_compiled = None

A dictionary of {namespace keyword: compiled regular expression}

has_enumerated_namespace(namespace)[source]

Checks that the namespace has been defined by an enumeration

has_regex_namespace(namespace)[source]

Checks that the namespace has been defined by a regular expression

has_namespace(namespace)[source]

Checks that the namespace has either been defined by an enumeration or a regular expression

has_enumerated_namespace_name(namespace, name)[source]

Checks that the namespace is defined by an enumeration and that the name is a member

has_regex_namespace_name(namespace, name)[source]

Checks that the namespace is defined as a regular expression and the name matches it

BEL Parser

class pybel.parser.parse_bel.BelParser(graph, namespace_dict=None, annotation_dict=None, namespace_regex=None, annotation_regex=None, allow_naked_names=False, allow_nested=False, allow_unqualified_translocations=False, citation_clearing=True, warn_on_singleton=True, no_identifier_validation=False, autostreamline=True)[source]

Build a parser backed by a given dictionary of namespaces

Parameters:
  • graph (BELGraph) – The BEL Graph to use to store the network
  • namespace_dict (dict[str,set[str]]) – A dictionary of {namespace: set of members}. Delegated to pybel.parser.parse_identifier.IdentifierParser
  • annotation_dict (dict[str,set[str]]) – A dictionary of {annotation: set of values}. Delegated to pybel.parser.ControlParser
  • namespace_regex (dict[str,str]) – A dictionary of {namespace: regular expression strings}. Delegated to pybel.parser.parse_identifier.IdentifierParser
  • annotation_regex (dict[str,str]) – A dictionary of {annotation: regular expression strings}. Delegated to pybel.parser.ControlParser
  • allow_naked_names (bool) – If true, turn off naked namespace failures. Delegated to pybel.parser.parse_identifier.IdentifierParser
  • allow_nested (bool) – If true, turn off nested statement failures. Delegated to pybel.parser.parse_identifier.IdentifierParser
  • allow_unqualified_translocations (bool) – If true, allow translocations without TO and FROM clauses.
  • citation_clearing (bool) – Should SET Citation statements clear evidence and all annotations? Delegated to pybel.parser.ControlParser
  • warn_on_singleton (bool) – Should the parser thorugh warnings on singletons? Only disable this if you’re sure your BEL Script is syntactically and semantically valid.
  • autostreamline (bool) – Should the parser be streamlined on instantiation?
pmod = None

2.2.1 http://openbel.org/language/web/version_2.0/bel_specification_version_2.0.html#_protein_modifications

variant = None

2.2.2 http://openbel.org/language/web/version_2.0/bel_specification_version_2.0.html#_variant_var

fragment = None

2.2.3 http://openbel.org/language/web/version_2.0/bel_specification_version_2.0.html#_proteolytic_fragments

location = None

2.2.4 http://openbel.org/language/web/version_2.0/bel_specification_version_2.0.html#_cellular_location

psub = None

2.2.X DEPRECATED http://openbel.org/language/web/version_1.0/bel_specification_version_1.0.html#_amino_acid_substitutions

gsub = None

2.2.X DEPRECATED http://openbel.org/language/web/version_1.0/bel_specification_version_1.0.html#_sequence_variations

trunc = None

DEPRECATED http://openbel.org/language/web/version_1.0/bel_specification_version_1.0.html#_truncated_proteins

gmod = None

PyBEL BEL Specification variant

fusion = None

2.6.1 http://openbel.org/language/web/version_2.0/bel_specification_version_2.0.html#_fusion_fus

general_abundance = None

2.1.1 http://openbel.org/language/web/version_2.0/bel_specification_version_2.0.html#XcomplexA

gene = None

2.1.4 http://openbel.org/language/web/version_2.0/bel_specification_version_2.0.html#XgeneA

mirna = None

2.1.5 http://openbel.org/language/web/version_2.0/bel_specification_version_2.0.html#XmicroRNAA

protein = None

2.1.6 http://openbel.org/language/web/version_2.0/bel_specification_version_2.0.html#XproteinA

rna = None

2.1.7 http://openbel.org/language/web/version_2.0/bel_specification_version_2.0.html#XrnaA

complex_singleton = None

2.1.2 http://openbel.org/language/web/version_2.0/bel_specification_version_2.0.html#XcomplexA

composite_abundance = None

2.1.3 http://openbel.org/language/web/version_2.0/bel_specification_version_2.0.html#XcompositeA

molecular_activity = None

2.4.1 http://openbel.org/language/web/version_2.0/bel_specification_version_2.0.html#XmolecularA

biological_process = None

2.3.1 http://openbel.org/language/web/version_2.0/bel_specification_version_2.0.html#_biologicalprocess_bp

pathology = None

2.3.2 http://openbel.org/language/web/version_2.0/bel_specification_version_2.0.html#_pathology_path

activity = None

2.3.3 http://openbel.org/language/web/version_2.0/bel_specification_version_2.0.html#Xactivity

translocation = None

2.5.1 http://openbel.org/language/web/version_2.0/bel_specification_version_2.0.html#_translocations

degradation = None

2.5.2 http://openbel.org/language/web/version_2.0/bel_specification_version_2.0.html#_degradation_deg

reactants = None

2.5.3 http://openbel.org/language/web/version_2.0/bel_specification_version_2.0.html#_reaction_rxn

increases_tag = None

3.1.1 http://openbel.org/language/web/version_2.0/bel_specification_version_2.0.html#Xincreases

directly_increases_tag = None

3.1.2 http://openbel.org/language/web/version_2.0/bel_specification_version_2.0.html#XdIncreases

decreases_tag = None

3.1.3 http://openbel.org/language/web/version_2.0/bel_specification_version_2.0.html#Xdecreases

directly_decreases_tag = None

3.1.4 http://openbel.org/language/web/version_2.0/bel_specification_version_2.0.html#XdDecreases

analogous_tag = None

3.5.1 http://openbel.org/language/web/version_2.0/bel_specification_version_2.0.html#_analogous

causes_no_change_tag = None

3.1.6 http://openbel.org/language/web/version_2.0/bel_specification_version_2.0.html#Xcnc

regulates_tag = None

3.1.7 http://openbel.org/language/web/version_2.0/bel_specification_version_2.0.html#_regulates_reg

negative_correlation_tag = None

3.2.1 http://openbel.org/language/web/version_2.0/bel_specification_version_2.0.html#XnegCor

positive_correlation_tag = None

3.2.2 http://openbel.org/language/web/version_2.0/bel_specification_version_2.0.html#XposCor

association_tag = None

3.2.3 http://openbel.org/language/web/version_2.0/bel_specification_version_2.0.html#Xassociation

orthologous_tag = None

3.3.1 http://openbel.org/language/web/version_2.0/bel_specification_version_2.0.html#_orthologous

is_a_tag = None

3.4.5 http://openbel.org/language/web/version_2.0/bel_specification_version_2.0.html#_isa

equivalent_tag = None

PyBEL Variant

rate_limit_tag = None

3.1.5 http://openbel.org/language/web/version_2.0/bel_specification_version_2.0.html#_ratelimitingstepof

subprocess_of_tag = None

3.4.6 http://openbel.org/language/web/version_2.0/bel_specification_version_2.0.html#_subprocessof

transcribed_tag = None

3.3.2 http://openbel.org/language/web/version_2.0/bel_specification_version_2.0.html#_transcribedto

translated_tag = None

3.3.3 http://openbel.org/language/web/version_2.0/bel_specification_version_2.0.html#_translatedto

has_member_tag = None

3.4.1 http://openbel.org/language/web/version_2.0/bel_specification_version_2.0.html#_hasmember

abundance_list = None

3.4.2 http://openbel.org/language/web/version_2.0/bel_specification_version_2.0.html#_hasmembers

biomarker_tag = None

3.5.2 http://openbel.org/language/web/version_2.0/bel_specification_version_2.0.html#_biomarkerfor

prognostic_biomarker_tag = None

3.5.3 http://openbel.org/language/web/version_2.0/bel_specification_version_2.0.html#_prognosticbiomarkerfor

causal_relation_tags = None

3.1 Causal Relationships - nested. Explicitly not supported because of ambiguity

get_annotations()[source]

Get current annotations in this parser

Return type:dict
clear()[source]

Clears the graph and all control parser data (current citation, annotations, and statement group)

handle_term_singleton(line, position, tokens)[source]

This function wraps BelParser.handle_term() but is only used for top-level parsing of bel_terms. This is done solely to keep track of if a graph has any singletons

ensure_node(tokens)[source]

Turns parsed tokens into canonical node name and makes sure its in the graph

Returns:the canonical name of the node
Return type:str