Repository: pybel/pybel

Issue:
Add OBO-style name/identifier parsing
As a proof of concept, try parsing the identifier and name delimited by a `!` as in `p(HGNC:6893!MAPT)`. Quotes should be allowed on both the identifier and the name.

Should whitespace should be allowed?

`p(HGNC:6893 ! MAPT)`



Comments:

Patch:
diff --git a/src/pybel/parser/parse_concept.py b/src/pybel/parser/parse_concept.py
index 0ea18dda..c8e94cad 100644
--- a/src/pybel/parser/parse_concept.py
+++ b/src/pybel/parser/parse_concept.py
@@ -13,7 +13,7 @@ from .exc import (
     UndefinedNamespaceWarning,
 )
 from .utils import quote, word
-from ..constants import DIRTY, NAME, NAMESPACE
+from ..constants import DIRTY, IDENTIFIER, NAME, NAMESPACE
 
 __all__ = [
     'ConceptParser',
@@ -47,9 +47,18 @@ class ConceptParser(BaseParser):
         self.default_namespace = set(default_namespace) if default_namespace is not None else None
         self.allow_naked_names = allow_naked_names
 
+        self.identifier_fqualified = (
+            word(NAMESPACE) +
+            Suppress(':') +
+            (word | quote)(IDENTIFIER) +
+            Suppress('!') +
+            (word | quote)(NAME)
+        )
+
         self.identifier_qualified = word(NAMESPACE) + Suppress(':') + (word | quote)(NAME)
 
         if self.namespace_to_terms:
+            self.identifier_fqualified.setParseAction(self.handle_identifier_qualified)
             self.identifier_qualified.setParseAction(self.handle_identifier_qualified)
 
         self.identifier_bare = (word | quote)(NAME)
@@ -60,7 +69,9 @@ class ConceptParser(BaseParser):
             self.handle_namespace_invalid
         )
 
-        super(ConceptParser, self).__init__(self.identifier_qualified | self.identifier_bare)
+        super().__init__(
+            self.identifier_fqualified | self.identifier_qualified | self.identifier_bare
+        )
 
     def has_enumerated_namespace(self, namespace: str) -> bool:
         """Check that the namespace has been defined by an enumeration."""


Test patch:
diff --git a/tests/test_parse/test_parse_bel.py b/tests/test_parse/test_parse_bel.py
index 4a599f98..e76cc71d 100644
--- a/tests/test_parse/test_parse_bel.py
+++ b/tests/test_parse/test_parse_bel.py
@@ -9,7 +9,7 @@ from pybel import BELGraph
 from pybel.constants import (
     ABUNDANCE, ACTIVITY, BEL_DEFAULT_NAMESPACE, BIOPROCESS, COMPLEX, COMPOSITE, CONCEPT, DEGRADATION,
     DIRECTLY_INCREASES, DIRTY, EFFECT, FRAGMENT, FROM_LOC, FUNCTION, FUSION, FUSION_MISSING, FUSION_REFERENCE,
-    FUSION_START, FUSION_STOP, GENE, HAS_COMPONENT, HAS_VARIANT, HGVS, KIND, LOCATION, MEMBERS, MIRNA,
+    FUSION_START, FUSION_STOP, GENE, HAS_COMPONENT, HAS_VARIANT, HGVS, IDENTIFIER, KIND, LOCATION, MEMBERS, MIRNA,
     MODIFIER, NAME, NAMESPACE, OBJECT, PARTNER_3P, PARTNER_5P, PATHOLOGY, PRODUCTS, PROTEIN, RANGE_3P, RANGE_5P,
     REACTANTS, REACTION, RELATION, RNA, SUBJECT, TARGET, TO_LOC, TRANSLOCATION, VARIANTS,
 )
@@ -89,6 +89,71 @@ class TestAbundance(TestTokenParserBase):
         self._test_abundance_with_location_helper('abundance(CHEBI:"oxygen atom", location(GO:intracellular))')
 
 
+class TestAbundanceLabeled(TestTokenParserBase):
+    """2.1.1"""
+
+    def setUp(self):
+        self.parser.clear()
+        self.parser.general_abundance.setParseAction(self.parser.handle_term)
+
+        self.expected_node_data = abundance(namespace='chebi', name='oxygen atom', identifier='CHEBI:25805')
+        self.expected_canonical_bel = 'a(chebi:"oxygen atom")'
+
+    def _test_abundance_helper(self, statement):
+        result = self.parser.general_abundance.parseString(statement)
+        self.assertEqual(dict(self.expected_node_data), result.asDict())
+
+        self.assertIn(self.expected_node_data, self.graph)
+        self.assertEqual(self.expected_canonical_bel, self.graph.node_to_bel(self.expected_node_data))
+
+        self.assertEqual({}, modifier_po_to_dict(result), msg='The modifier dictionary should be empty')
+
+    def test_abundance(self):
+        """Test short/long abundance name."""
+        self._test_abundance_helper('a(chebi:"CHEBI:25805"!"oxygen atom")')
+        self._test_abundance_helper('abundance(chebi:"CHEBI:25805"!"oxygen atom")')
+        self._test_abundance_helper('abundance(chebi:"CHEBI:25805" ! "oxygen atom")')
+
+    def _test_abundance_with_location_helper(self, statement):
+        result = self.parser.general_abundance.parseString(statement)
+
+        expected_result = {
+            FUNCTION: ABUNDANCE,
+            CONCEPT: {
+                NAMESPACE: 'chebi',
+                NAME: 'oxygen atom',
+                IDENTIFIER: 'CHEBI:25805',
+            },
+            LOCATION: {
+                NAMESPACE: 'GO',
+                NAME: 'intracellular',
+            }
+        }
+
+        self.assertEqual(expected_result, result.asDict())
+
+        self.assertIn(self.expected_node_data, self.graph)
+        self.assertEqual(self.expected_canonical_bel, self.graph.node_to_bel(self.expected_node_data))
+
+        modifier = modifier_po_to_dict(result)
+        expected_modifier = {
+            LOCATION: {
+                NAMESPACE: 'GO',
+                NAME: 'intracellular',
+            }
+        }
+        self.assertEqual(expected_modifier, modifier)
+
+    def test_abundance_with_location(self):
+        """Test short/long abundance name and short/long location name."""
+        self._test_abundance_with_location_helper('a(chebi:"CHEBI:25805"!"oxygen atom", loc(GO:intracellular))')
+        self._test_abundance_with_location_helper('abundance(chebi:"CHEBI:25805"!"oxygen atom", loc(GO:intracellular))')
+        self._test_abundance_with_location_helper('a(chebi:"CHEBI:25805"!"oxygen atom", location(GO:intracellular))')
+        self._test_abundance_with_location_helper(
+            'abundance(chebi:"CHEBI:25805"!"oxygen atom", location(GO:intracellular))'
+        )
+
+
 class TestGene(TestTokenParserBase):
     """2.1.4 http://openbel.org/language/web/version_2.0/bel_specification_version_2.0.html#XgeneA"""