From f11157eab21f82a9c74186b9f5b826e690fa5cc6 Mon Sep 17 00:00:00 2001
From: AndiMajore <andi.majore@googlemail.com>
Date: Fri, 8 Jul 2022 17:00:14 +0200
Subject: [PATCH] small fixes and cleanup

---
 drugstone/management/commands/cleanuptasks.py |  1 -
 drugstone/util/nedrex.py                      |  8 --------
 drugstone/views.py                            | 18 ++++++++----------
 tasks/util/read_graph_tool_graph.py           |  2 ++
 4 files changed, 10 insertions(+), 19 deletions(-)
 delete mode 100644 drugstone/util/nedrex.py

diff --git a/drugstone/management/commands/cleanuptasks.py b/drugstone/management/commands/cleanuptasks.py
index 470611a..ee6fe53 100755
--- a/drugstone/management/commands/cleanuptasks.py
+++ b/drugstone/management/commands/cleanuptasks.py
@@ -11,7 +11,6 @@ class Command(BaseCommand):
 
     def handle(self, *args, **kwargs):
         print('Task cleanup...')
-        print()
 
         print(f'Closing {Task.objects.filter(started_at__isnull=True).count()} queued tasks')
         Task.objects.filter(started_at__isnull=True).update(started_at=timezone.now(), finished_at=timezone.now(),
diff --git a/drugstone/util/nedrex.py b/drugstone/util/nedrex.py
deleted file mode 100644
index d3f85eb..0000000
--- a/drugstone/util/nedrex.py
+++ /dev/null
@@ -1,8 +0,0 @@
-def fetch_nedrex_data():
-    print('fetching')
-    return
-
-
-def integrate_nedrex_data():
-    print('integrating')
-    return
diff --git a/drugstone/views.py b/drugstone/views.py
index c081718..fa5ea27 100755
--- a/drugstone/views.py
+++ b/drugstone/views.py
@@ -337,16 +337,8 @@ def result_view(request) -> Response:
     for node_id in nodes:
         is_seed[node_id] = node_id in seeds
         node_type = node_types.get(node_id).lower()
-        # if node_type == 'node':
-        #     # TODO remove this if after next make_graphs
-        #     node_type = 'protein'
         pvd_entity = None
         details_s = None
-        # if not node_type:
-        #     # TODO can this be removed? should never be reached
-        #     print('we should not see this 2')
-        #     node_type, pvd_entity = infer_node_type_and_details(int(node_id[1:]))
-        # else:
         if node_type == 'protein':
             pvd_entity = Protein.objects.get(id=int(node_id[1:]))
         elif node_type == 'drug':
@@ -409,14 +401,20 @@ def result_view(request) -> Response:
     nodes_mapped, id_key = query_proteins_by_identifier(edge_endpoint_ids, identifier)
     # change data structure to dict in order to be quicker when merging
     nodes_mapped_dict = {node[id_key]: node for node in nodes_mapped}
-
     for edge in edges:
         # change edge endpoints if they were matched with a protein in the database
         edge['from'] = nodes_mapped_dict[edge['from']][node_name_attribute] if edge['from'] in nodes_mapped_dict else \
             edge['from']
         edge['to'] = nodes_mapped_dict[edge['to']][node_name_attribute] if edge['to'] in nodes_mapped_dict else edge[
             'to']
-
+    if 'autofill_edges' in parameters['config'] and parameters['config']['autofill_edges']:
+        proteins = set(map(lambda n:n[node_name_attribute][1:],filter(lambda n: node_name_attribute in n,parameters['input_network']['nodes'])))
+        dataset = 'STRING' if 'interaction_protein_protein' not in parameters['config'] else parameters['config']['interaction_protein_protein']
+        dataset_object = models.PPIDataset.objects.filter(name__iexact=dataset).last()
+        interaction_objects = models.ProteinProteinInteraction.objects.filter(
+            Q(ppi_dataset=dataset_object) & Q(from_protein__in=proteins) & Q(to_protein__in=proteins))
+        auto_edges = list(map(lambda n: {"from": f'p{n.from_protein_id}', "to":f'p{n.to_protein_id}'} ,interaction_objects))
+        edges.extend(auto_edges)
     result['network']['edges'].extend(edges)
 
     if not view:
diff --git a/tasks/util/read_graph_tool_graph.py b/tasks/util/read_graph_tool_graph.py
index 94db075..1e9caf2 100755
--- a/tasks/util/read_graph_tool_graph.py
+++ b/tasks/util/read_graph_tool_graph.py
@@ -106,6 +106,8 @@ def read_graph_tool_graph(file_path, seeds, max_deg, include_indirect_drugs=Fals
                         deleted_edges.append(edge)
                     if indir_drug and int(edge.source()) in drug_ids:
                         drug_ids.remove(int(edge.source()))
+            else:
+                deleted_edges.append(edge)
 
     g.set_fast_edge_removal(fast=True)
     for edge in deleted_edges:
-- 
GitLab