Repository: iossifovlab/gpf

Issue:
[BUG] Genomic scores DB initialisation in GPF instance is broken
**What happened**:
When a GPFInstance has an annotation configuration the initialization of genomic scores DB throws an exception.

**What you expected to happen**:
Genomic scores DB should be initialized with scores configured in the annotation configuration.

**Minimal Complete Verifiable Example**:
1. Add `annotation.yaml` to `data-hg19-startup` with following content:
   ```yaml
   - position_score: hg19/scores/phyloP46_placentals
   ```
2. Import `iossifov_2014` into `data-hg19-startup`
3. Start the `wdae`
4. Go to Genotype Browser. The backend will throw `KeyError: 'phyloP46_placentals'`

**Environment:**

Development version of gpf/gpfjs from the master.

Discussion:

Patch:
diff --git a/dae/dae/gpf_instance/gpf_instance.py b/dae/dae/gpf_instance/gpf_instance.py
index 2b4724a3f..5e57e4b0f 100644
--- a/dae/dae/gpf_instance/gpf_instance.py
+++ b/dae/dae/gpf_instance/gpf_instance.py
@@ -170,11 +170,9 @@ class GPFInstance:
                     continue
 
                 for field_name in schema.public_fields:
-                    field = schema[field_name]
-                    score_name = field.source.attribute_config["source"]
-                    scores.append((resource_id, score_name))
+                    scores.append((resource_id, field_name))
 
-        if self.dae_config.genomic_scores_db is not None:
+        elif self.dae_config.genomic_scores_db is not None:
             for score_def in self.dae_config.genomic_scores_db:
                 scores.append((score_def["resource"], score_def["score"]))
 
@@ -484,7 +482,6 @@ class GPFInstance:
     def get_annotation_pipeline(self):
         """Return the annotation pipeline configured in the GPF instance."""
         if self._annotation_pipeline is None:
-
             if self.dae_config.annotation is None:
                 self._annotation_pipeline = build_annotation_pipeline(
                     [], grr_repository=self.grr)


Test patch:
diff --git a/dae/dae/gene/tests/test_genomic_scores.py b/dae/dae/gene/tests/test_genomic_scores.py
index 6fbbc7551..98f64ba78 100644
--- a/dae/dae/gene/tests/test_genomic_scores.py
+++ b/dae/dae/gene/tests/test_genomic_scores.py
@@ -1,8 +1,12 @@
 # pylint: disable=W0621,C0114,C0116,W0212,W0613
 
 import textwrap
+
 import pytest
 
+from dae.testing import setup_directories, setup_genome, \
+    setup_empty_gene_models, setup_gpf_instance
+
 from dae.gene.scores import GenomicScoresDb
 from dae.genomic_resources.testing import build_testing_repository
 from dae.genomic_resources.repository import GR_CONF_FILE_NAME
@@ -78,3 +82,52 @@ def test_genomic_scores_db(scores_repo):
     assert len(score.hist.bins) == 11
     assert score.hist.x_scale == "linear"
     assert score.hist.y_scale == "linear"
+
+
+@pytest.fixture
+def annotation_gpf(scores_repo, tmp_path_factory):
+    root_path = tmp_path_factory.mktemp("genomic_scores_db_gpf")
+
+    setup_directories(root_path / "gpf_instance", {
+        "gpf_instance.yaml": textwrap.dedent("""
+        annotation:
+            conf_file: annotation.yaml
+        """),
+        "annotation.yaml": textwrap.dedent("""
+        - position_score: phastCons
+        """)
+    })
+    genome = setup_genome(
+        root_path / "alla_gpf" / "genome" / "allChr.fa",
+        f"""
+        >chrA
+        {100 * "A"}
+        """
+    )
+    empty_gene_models = setup_empty_gene_models(
+        root_path / "alla_gpf" / "empty_gene_models" / "empty_genes.txt")
+    gpf_instance = setup_gpf_instance(
+        root_path / "gpf_instance",
+        reference_genome=genome,
+        gene_models=empty_gene_models,
+        grr=scores_repo
+    )
+    return gpf_instance
+
+
+def test_genomic_scores_db_with_annotation(annotation_gpf):
+    assert annotation_gpf is not None
+    annotaiton_pipeline = annotation_gpf.get_annotation_pipeline()
+    assert annotaiton_pipeline is not None
+
+    db = annotation_gpf.genomic_scores_db
+    assert db is not None
+    assert len(db.get_scores()) == 1
+    assert "phastcons100" in db
+    assert db["phastcons100"] is not None
+
+    score = db["phastcons100"]
+    assert len(score.hist.bars) == 11
+    assert len(score.hist.bins) == 11
+    assert score.hist.x_scale == "linear"
+    assert score.hist.y_scale == "linear"
diff --git a/dae/dae/testing/setup_helpers.py b/dae/dae/testing/setup_helpers.py
index 49764470e..65773dd75 100644
--- a/dae/dae/testing/setup_helpers.py
+++ b/dae/dae/testing/setup_helpers.py
@@ -132,7 +132,8 @@ def setup_gpf_instance(
         reference_genome=None, gene_models=None,
         grr=None):
     """Set up a GPF instance using prebuild genome, gene models, etc."""
-    setup_directories(out_path, {"gpf_instance.yaml": ""})
+    if not (out_path / "gpf_instance.yaml").exists():
+        setup_directories(out_path, {"gpf_instance.yaml": ""})
     # pylint: disable=import-outside-toplevel
     from dae.gpf_instance import GPFInstance
     return GPFInstance(