File size: 16,361 Bytes
132149b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
"""

Module: load_data.py

Description:

    This module provides functions for loading heterogeneous networks for drug repositioning.

    It supports two datasets: 'Bdataset' and 'Kdataset'. The functions create DGL heterographs

    from CSV files containing various interactions and associations, and also assign initial node features.

"""

import dgl
import torch as th
import numpy as np
import pandas as pd
import os
from collections import defaultdict


def load(dataset):
    """

    Load the heterogeneous network for a given dataset.



    Parameters:

        dataset (str): The dataset identifier. Options are 'Bdataset' or 'Kdataset'.



    Returns:

        dgl.DGLHeteroGraph: The constructed heterogeneous graph.

    """
    if dataset == "Bdataset":
        return load_Bdataset()
    if dataset == "Kdataset":
        return load_Kdataset()
    if dataset == "KGdataset":
        return load_KGdataset()
    if dataset == 'KGdataset_tiny':
        return load_KGdataset_tiny()
    raise ValueError("Unsupported dataset. Please choose 'Bdataset', 'Kdataset', or 'KGdataset'.")


def _load_node_indices(node_path, node_files):
    node_indices = {}
    node_count = {}
    for node_type, filename in node_files.items():
        filepath = os.path.join(node_path, filename)
        if os.path.exists(filepath):
            df = pd.read_csv(filepath, usecols=['Inter_ID'], low_memory=False)
            node_indices[node_type] = pd.Index(df['Inter_ID'])
            node_count[node_type] = len(df)
            print(f"Loaded {node_type}: {len(df)} nodes")
        else:
            print(f"Warning: Node file {filepath} not found")
            node_indices[node_type] = pd.Index([])
            node_count[node_type] = 0
    return node_indices, node_count


def _build_heterograph(node_path, edge_path, node_files, edge_files, feature_dim=128):
    # Load node indices (vectorized, no Python loops over rows)
    node_indices, node_count = _load_node_indices(node_path, node_files)

    graph_data = {}

    for edge_type, filename in edge_files.items():
        filepath = os.path.join(edge_path, filename)
        if not os.path.exists(filepath):
            print(f"Warning: Edge file {filepath} not found")
            continue

        try:
            df = pd.read_csv(filepath, usecols=['x_id', 'y_id'], low_memory=False)
            # Infer src/dst types by splitting on first underscore
            # Works for e.g. disease_phenotype, protein_bioprocess, bioprocess_bioprocess, etc.
            src_type, dst_type = edge_type.split('_', 1)
            print(f"Loading {edge_type}: {len(df)} edges")
            print(f"  Source type: {src_type}, Target type: {dst_type}")

            # Vectorized mapping: get index positions or -1 when missing
            src_idx_arr = node_indices[src_type].get_indexer(df['x_id'])
            dst_idx_arr = node_indices[dst_type].get_indexer(df['y_id'])

            # Keep only rows where both src and dst are valid (not -1)
            valid_mask = (src_idx_arr != -1) & (dst_idx_arr != -1)
            if not np.any(valid_mask):
                print("  No valid edges found")
                continue

            s = th.as_tensor(src_idx_arr[valid_mask], dtype=th.long)
            d = th.as_tensor(dst_idx_arr[valid_mask], dtype=th.long)

            # Forward edges
            graph_data[(src_type, edge_type, dst_type)] = (s, d)

            # Reverse edges when not self-loop
            if src_type != dst_type:
                rev = f"{dst_type}_{src_type}"
                graph_data[(dst_type, rev, src_type)] = (d, s)

            print(f"  Added {s.numel()} edges")

        except Exception as e:
            print(f"Error loading {edge_type}: {e}")

    # Build graph
    g = dgl.heterograph(graph_data)
    print(f"Created graph with {g.num_nodes()} total nodes and {g.num_edges()} total edges")

    # Node/edge stats
    for ntype in g.ntypes:
        print(f"{ntype}: {g.num_nodes(ntype)} nodes")
    for etype in g.etypes:
        print(f"{etype}: {g.num_edges(etype)} edges")

    # Initialize node features (random as placeholder)
    for ntype in g.ntypes:
        num_nodes = g.num_nodes(ntype)
        g.nodes[ntype].data['h'] = th.randn(num_nodes, feature_dim, dtype=th.float32)

    return g


def load_KGdataset_tiny():
    """

    Load the heterogeneous network for the tiny knowledge graph dataset (drug, protein, disease).

    """
    node_path = "/vast/yg3191/AIVS/kg/node"
    edge_path = "/vast/yg3191/AIVS/kg/edge"

    node_files = {
        'drug': 'drug.csv',
        'protein': 'protein.csv',
        'disease': 'disease.csv',
    }

    edge_files = {
        'drug_drug': 'drug_drug.csv',
        'drug_protein': 'drug_protein.csv',
        'protein_protein': 'protein_protein.csv',
        'protein_disease': 'protein_disease.csv',
        'disease_disease': 'disease_disease.csv',
        'drug_disease': 'drug_disease_indication.csv',
    }

    return _build_heterograph(node_path, edge_path, node_files, edge_files, feature_dim=128)


def load_KGdataset():
    """

    Load the heterogeneous network for the full knowledge graph dataset.

    """
    node_path = "/vast/yg3191/AIVS/kg/node"
    edge_path = "/vast/yg3191/AIVS/kg/edge"

    node_files = {
        'drug': 'drug.csv',
        'disease': 'disease.csv',
        'protein': 'protein.csv',
        'bioprocess': 'bioprocess.csv',
        'cellcomp': 'cellcomp.csv',
        'molfunc': 'molfunc.csv',
        'pathway': 'pathway.csv',
        'phenotype': 'phenotype.csv',
        'exposure': 'exposure.csv',
        'effect': 'effect.csv',
    }

    edge_files = {
        'drug_drug': 'drug_drug.csv',
        'drug_effect': 'drug_effect.csv',
        'drug_protein': 'drug_protein.csv',
        'drug_disease': 'drug_disease_indication.csv',
        'protein_protein': 'protein_protein.csv',
        'protein_bioprocess': 'protein_bioprocess.csv',
        'protein_cellcomp': 'protein_cellcomp.csv',
        'protein_molfunc': 'protein_molfunc.csv',
        'protein_pathway': 'protein_pathway.csv',
        'protein_disease': 'protein_disease.csv',
        'disease_disease': 'disease_disease.csv',
        'disease_phenotype': 'disease_phenotype_positive.csv',
        'disease_exposure': 'disease_exposure.csv',
        'bioprocess_bioprocess': 'bioprocess_bioprocess.csv',
        'cellcomp_cellcomp': 'cellcomp_cellcomp.csv',
        'molfunc_molfunc': 'molfunc_molfunc.csv',
        'pathway_pathway': 'pathway_pathway.csv',
        'phenotype_phenotype': 'phenotype_phenotype.csv',
    }

    return _build_heterograph(node_path, edge_path, node_files, edge_files, feature_dim=128)


def load_Kdataset():
    """

    Load the heterogeneous network for the 'Kdataset'.



    Returns:

        dgl.DGLHeteroGraph: The constructed heterogeneous graph for Kdataset.

    """
    # Load and process drug-drug similarity data
    drug_drug = pd.read_csv("./dataset/Kdataset/drug_drug_baseline.csv", header=None).values
    drug_sim = drug_drug.copy()
    for i in range(len(drug_drug)):
        sorted_idx = np.argpartition(drug_drug[i], 15)
        drug_drug[i, sorted_idx[-15:]] = 1
    drug_drug_df = pd.DataFrame(np.array(np.where(drug_drug == 1)).T, columns=["Drug1", "Drug2"])

    # Load additional interaction data
    protein_protein = pd.read_csv("./dataset/Kdataset/interactions/protein_protein.csv")
    gene_gene = pd.read_csv("./dataset/Kdataset/interactions/gene_gene.csv")
    pathway_pathway = pd.read_csv("./dataset/Kdataset/interactions/pathway_pathway.csv")
    disease_disease = pd.read_csv("./dataset/Kdataset/disease_disease_baseline.csv", header=None).values
    disease_sim = disease_disease.copy()
    for i in range(len(disease_disease)):
        sorted_idx = np.argpartition(disease_disease[i], 15)
        disease_disease[i, sorted_idx[-15:]] = 1
    disease_disease_df = pd.DataFrame(np.array(np.where(disease_disease == 1)).T, columns=["Disease1", "Disease2"])

    drug_protein = pd.read_csv("./dataset/Kdataset/associations/drug_protein.csv")
    protein_gene = pd.read_csv("./dataset/Kdataset/associations/protein_gene.csv")
    gene_pathway = pd.read_csv("./dataset/Kdataset/associations/gene_pathway.csv")
    pathway_disease = pd.read_csv("./dataset/Kdataset/associations/pathway_disease.csv")
    drug_disease = pd.read_csv("./dataset/Kdataset/associations/Kdataset.csv")

    # Build the graph using the interaction data
    graph_data = {
        ("drug", "drug_drug", "drug"): (
            th.tensor(drug_drug_df["Drug1"].values),
            th.tensor(drug_drug_df["Drug2"].values),
        ),
        ("drug", "drug_protein", "protein"): (
            th.tensor(drug_protein["Drug"].values),
            th.tensor(drug_protein["Protein"].values),
        ),
        ("protein", "protein_drug", "drug"): (
            th.tensor(drug_protein["Protein"].values),
            th.tensor(drug_protein["Drug"].values),
        ),
        ("protein", "protein_protein", "protein"): (
            th.tensor(protein_protein["Protein1"].values),
            th.tensor(protein_protein["Protein2"].values),
        ),
        ("protein", "protein_gene", "gene"): (
            th.tensor(protein_gene["Protein"].values),
            th.tensor(protein_gene["Gene"].values),
        ),
        ("gene", "gene_protein", "protein"): (
            th.tensor(protein_gene["Gene"].values),
            th.tensor(protein_gene["Protein"].values),
        ),
        ("gene", "gene_gene", "gene"): (
            th.tensor(gene_gene["Gene1"].values),
            th.tensor(gene_gene["Gene2"].values),
        ),
        ("gene", "gene_pathway", "pathway"): (
            th.tensor(gene_pathway["Gene"].values),
            th.tensor(gene_pathway["Pathway"].values),
        ),
        ("pathway", "pathway_gene", "gene"): (
            th.tensor(gene_pathway["Pathway"].values),
            th.tensor(gene_pathway["Gene"].values),
        ),
        ("pathway", "pathway_pathway", "pathway"): (
            th.tensor(pathway_pathway["Pathway1"].values),
            th.tensor(pathway_pathway["Pathway2"].values),
        ),
        ("pathway", "pathway_disease", "disease"): (
            th.tensor(pathway_disease["Pathway"].values),
            th.tensor(pathway_disease["Disease"].values),
        ),
        ("disease", "disease_pathway", "pathway"): (
            th.tensor(pathway_disease["Disease"].values),
            th.tensor(pathway_disease["Pathway"].values),
        ),
        ("disease", "disease_disease", "disease"): (
            th.tensor(disease_disease_df["Disease1"].values),
            th.tensor(disease_disease_df["Disease2"].values),
        ),
        ("drug", "drug_disease", "disease"): (
            th.tensor(drug_disease["Drug"].values),
            th.tensor(drug_disease["Disease"].values),
        ),
        ("disease", "disease_drug", "drug"): (
            th.tensor(drug_disease["Disease"].values),
            th.tensor(drug_disease["Drug"].values),
        ),
    }
    g = dgl.heterograph(graph_data)

    # Prepare node features by concatenating similarity matrices and zero padding as needed
    drug_feature = np.hstack((drug_sim, np.zeros((g.num_nodes("drug"), g.num_nodes("disease")))))
    dis_feature = np.hstack((np.zeros((g.num_nodes("disease"), g.num_nodes("drug"))), disease_sim))
    g.nodes["drug"].data["h"] = th.from_numpy(drug_feature).to(th.float32)
    g.nodes["disease"].data["h"] = th.from_numpy(dis_feature).to(th.float32)
    g.nodes["protein"].data["h"] = th.zeros((g.num_nodes("protein"), drug_feature.shape[1])).to(th.float32)
    g.nodes["gene"].data["h"] = th.zeros((g.num_nodes("gene"), drug_feature.shape[1])).to(th.float32)
    g.nodes["pathway"].data["h"] = th.zeros((g.num_nodes("pathway"), drug_feature.shape[1])).to(th.float32)
    return g


def load_Bdataset():
    """

    Load the heterogeneous network for the 'Bdataset'.



    Returns:

        dgl.DGLHeteroGraph: The constructed heterogeneous graph for Bdataset.

    """
    # Load and process drug-drug similarity data
    drug_drug = pd.read_csv("./dataset/Bdataset/drug_drug_baseline.csv", header=None).values
    drug_sim = drug_drug.copy()
    for i in range(len(drug_drug)):
        sorted_idx = np.argpartition(drug_drug[i], 15)
        drug_drug[i, sorted_idx[-15:]] = 1
    drug_drug_df = pd.DataFrame(np.array(np.where(drug_drug == 1)).T, columns=["Drug1", "Drug2"])

    protein_protein = pd.read_csv("./dataset/Bdataset/interactions/protein_protein.csv")
    disease_disease = pd.read_csv("./dataset/Bdataset/disease_disease_baseline.csv", header=None).values
    disease_sim = disease_disease.copy()
    for i in range(len(disease_disease)):
        sorted_idx = np.argpartition(disease_disease[i], 15)
        disease_disease[i, sorted_idx[-15:]] = 1
    disease_disease_df = pd.DataFrame(np.array(np.where(disease_disease == 1)).T, columns=["Disease1", "Disease2"])
    drug_protein = pd.read_csv("./dataset/Bdataset/associations/drug_protein.csv")
    drug_disease = pd.read_csv("./dataset/Bdataset/associations/Bdataset.csv")

    # Build the graph using the interaction data
    graph_data = {
        ("drug", "drug_drug", "drug"): (
            th.tensor(drug_drug_df["Drug1"].values),
            th.tensor(drug_drug_df["Drug2"].values),
        ),
        ("drug", "drug_protein", "protein"): (
            th.tensor(drug_protein["Drug"].values),
            th.tensor(drug_protein["Protein"].values),
        ),
        ("protein", "protein_drug", "drug"): (
            th.tensor(drug_protein["Protein"].values),
            th.tensor(drug_protein["Drug"].values),
        ),
        ("protein", "protein_protein", "protein"): (
            th.tensor(protein_protein["Protein1"].values),
            th.tensor(protein_protein["Protein2"].values),
        ),
        ("disease", "disease_disease", "disease"): (
            th.tensor(disease_disease_df["Disease1"].values),
            th.tensor(disease_disease_df["Disease2"].values),
        ),
        ("drug", "drug_disease", "disease"): (
            th.tensor(drug_disease["Drug"].values),
            th.tensor(drug_disease["Disease"].values),
        ),
        ("disease", "disease_drug", "drug"): (
            th.tensor(drug_disease["Disease"].values),
            th.tensor(drug_disease["Drug"].values),
        ),
    }
    g = dgl.heterograph(graph_data)

    # Prepare node features with appropriate zero padding
    drug_feature = np.hstack((drug_sim, np.zeros((g.num_nodes("drug"), g.num_nodes("disease")))))
    dis_feature = np.hstack((np.zeros((g.num_nodes("disease"), g.num_nodes("drug"))), disease_sim))
    g.nodes["drug"].data["h"] = th.from_numpy(drug_feature).to(th.float32)
    g.nodes["disease"].data["h"] = th.from_numpy(dis_feature).to(th.float32)
    g.nodes["protein"].data["h"] = th.zeros((g.num_nodes("protein"), g.num_nodes("protein"))).to(th.float32)
    return g


def remove_graph(g, test_id):
    """

    Remove drug-disease association edges that belong to the test set from the graph.



    Parameters:

        g (dgl.DGLHeteroGraph): The heterogeneous graph.

        test_id (numpy.ndarray): Array of shape (n, 2) where each row is [drug_index, disease_index].



    Returns:

        dgl.DGLHeteroGraph: The graph with test edges removed.

    """
    test_drug_id = test_id[:, 0]
    test_dis_id = test_id[:, 1]
    # Remove edges for the ('drug', 'drug_disease', 'disease') relation
    edges_id = g.edge_ids(
        th.tensor(test_drug_id),
        th.tensor(test_dis_id),
        etype=("drug", "drug_disease", "disease"),
    )
    g = dgl.remove_edges(g, edges_id, etype=("drug", "drug_disease", "disease"))
    # Remove the reciprocal edges for the ('disease', 'disease_drug', 'drug') relation
    edges_id = g.edge_ids(
        th.tensor(test_dis_id),
        th.tensor(test_drug_id),
        etype=("disease", "disease_drug", "drug"),
    )
    g = dgl.remove_edges(g, edges_id, etype=("disease", "disease_drug", "drug"))
    return g