Repository: EdinburghGenomics/EGCG-Core

Issue:
Canis lupus familiaris NCBI query returns None
Because _C. l. familiaris_ has `subspecies` in its efetch, the regex system in `fetch_from_eutils` breaks down. We should query the XML more robustly.

Discussion:

Patch:
diff --git a/egcg_core/ncbi.py b/egcg_core/ncbi.py
index 13f74d6..69cc44c 100644
--- a/egcg_core/ncbi.py
+++ b/egcg_core/ncbi.py
@@ -76,7 +76,7 @@ def _fetch_from_eutils(species):
         rank = None
         if match:
             rank = match.group(1)
-        if rank == 'species':
+        if rank in ['species', 'subspecies']:
             scientific_name = common_name = None
             match = re.search('<ScientificName>(.+?)</ScientificName>', r.text, re.MULTILINE)
             if match:


Test patch:
diff --git a/tests/test_ncbi.py b/tests/test_ncbi.py
index ef931bb..af10556 100644
--- a/tests/test_ncbi.py
+++ b/tests/test_ncbi.py
@@ -24,6 +24,11 @@ def test_fetch_from_eutils():
         <OtherNames><CommonName>a common name</CommonName></OtherNames>
         <Rank>species</Rank>
     '''
+    ncbi_fetch_data_sub_spe = '''
+        <ScientificName>Genus species</ScientificName>
+        <OtherNames><CommonName>a common name</CommonName></OtherNames>
+        <Rank>subspecies</Rank>
+    '''
 
     patched_get = patch(
         'egcg_core.ncbi.requests.get',
@@ -34,7 +39,15 @@ def test_fetch_from_eutils():
             FakeRestResponse(content=ncbi_fetch_data)
         )
     )
-
+    patched_get2 = patch(
+        'egcg_core.ncbi.requests.get',
+        side_effect=(
+            FakeRestResponse(content=ncbi_search_data),
+            FakeRestResponse(content=ncbi_fetch_data_sub_spe),
+            FakeRestResponse(content=ncbi_fetch_data_sub_spe),
+            FakeRestResponse(content=ncbi_fetch_data_sub_spe)
+        )
+    )
     with patched_get as mocked_get:
         obs = fetch_from_eutils('a_species')
         assert obs == ('1337', 'Genus species', 'a common name')
@@ -46,6 +59,10 @@ def test_fetch_from_eutils():
             'http://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi',
             params={'db': 'Taxonomy', 'id': '1337'}
         )
+    with patched_get2:
+        obs = fetch_from_eutils('a_species')
+        assert obs == ('1337', 'Genus species', 'a common name')
+
 
 
 def test_cache():