IF YOU WOULD LIKE TO GET AN ACCOUNT, please write an email to s dot adaszewski at gmail dot com. User accounts are meant only to report issues and/or generate pull requests. This is a purpose-specific Git hosting for ADARED projects. Thank you for your understanding!
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

98 lines
3.7KB

  1. #!/usr/bin/env python3
  2. from icosagon.data import Data
  3. import os
  4. import pandas as pd
  5. from bisect import bisect_left
  6. import torch
  7. import sys
  8. def index(a, x):
  9. i = bisect_left(a, x)
  10. if i != len(a) and a[i] == x:
  11. return i
  12. raise ValueError
  13. def load_data():
  14. path = '/pstore/data/data_science/ref/decagon'
  15. df_combo = pd.read_csv(os.path.join(path, 'bio-decagon-combo.csv'))
  16. df_effcat = pd.read_csv(os.path.join(path, 'bio-decagon-effectcategories.csv'))
  17. df_mono = pd.read_csv(os.path.join(path, 'bio-decagon-mono.csv'))
  18. df_ppi = pd.read_csv(os.path.join(path, 'bio-decagon-ppi.csv'))
  19. df_tgtall = pd.read_csv(os.path.join(path, 'bio-decagon-targets-all.csv'))
  20. df_tgt = pd.read_csv(os.path.join(path, 'bio-decagon-targets.csv'))
  21. lst = [ 'df_combo', 'df_effcat', 'df_mono', 'df_ppi', 'df_tgtall', 'df_tgt' ]
  22. for nam in lst:
  23. print(f'len({nam}): {len(locals()[nam])}')
  24. print(f'{nam}.columns: {locals()[nam].columns}')
  25. genes = set()
  26. genes = genes.union(df_ppi['Gene 1']).union(df_ppi['Gene 2']) \
  27. .union(df_tgtall['Gene']).union(df_tgt['Gene'])
  28. genes = sorted(genes)
  29. print('len(genes):', len(genes))
  30. drugs = set()
  31. drugs = drugs.union(df_combo['STITCH 1']).union(df_combo['STITCH 2']) \
  32. .union(df_mono['STITCH']).union(df_tgtall['STITCH']).union(df_tgt['STITCH'])
  33. drugs = sorted(drugs)
  34. print('len(drugs):', len(drugs))
  35. data = Data()
  36. data.add_node_type('Gene', len(genes))
  37. data.add_node_type('Drug', len(drugs))
  38. print('Preparing PPI...')
  39. print('Indexing rows...')
  40. rows = [index(genes, g) for g in df_ppi['Gene 1']]
  41. print('Indexing cols...')
  42. cols = [index(genes, g) for g in df_ppi['Gene 2']]
  43. indices = list(zip(rows, cols))
  44. indices = torch.tensor(indices).transpose(0, 1)
  45. values = torch.ones(len(rows))
  46. print('indices.shape:', indices.shape, 'values.shape:', values.shape)
  47. adj_mat = torch.sparse_coo_tensor(indices, values, size=(len(genes),) * 2)
  48. adj_mat = (adj_mat + adj_mat.transpose(0, 1)) / 2
  49. print('adj_mat created')
  50. fam = data.add_relation_family('PPI', 0, 0, True)
  51. rel = fam.add_relation_type('PPI', adj_mat)
  52. print('OK')
  53. print('Preparing Drug-Gene (Target) edges...')
  54. rows = [index(drugs, d) for d in df_tgtall['STITCH']]
  55. cols = [index(genes, g) for g in df_tgtall['Gene']]
  56. indices = list(zip(rows, cols))
  57. indices = torch.tensor(indices).transpose(0, 1)
  58. values = torch.ones(len(rows))
  59. adj_mat = torch.sparse_coo_tensor(indices, values, size=(len(drugs), len(genes)))
  60. fam = data.add_relation_family('Drug-Gene (Target)', 1, 0, True)
  61. rel = fam.add_relation_type('Drug-Gene (Target)', adj_mat)
  62. print('OK')
  63. print('Preparing Drug-Drug (Side Effect) edges...')
  64. fam = data.add_relation_family('Drug-Drug (Side Effect)', 1, 1, True)
  65. print('# of side effects:', len(df_combo), 'unique:', len(df_combo['Polypharmacy Side Effect'].unique()))
  66. for eff, df in df_combo.groupby('Polypharmacy Side Effect'):
  67. sys.stdout.write('.') # print(eff, '...')
  68. sys.stdout.flush()
  69. rows = [index(drugs, d) for d in df['STITCH 1']]
  70. cols = [index(drugs, d) for d in df['STITCH 2']]
  71. indices = list(zip(rows, cols))
  72. indices = torch.tensor(indices).transpose(0, 1)
  73. values = torch.ones(len(rows))
  74. adj_mat = torch.sparse_coo_tensor(indices, values, size=(len(drugs), len(drugs)))
  75. adj_mat = (adj_mat + adj_mat.transpose(0, 1)) / 2
  76. rel = fam.add_relation_type(df['Polypharmacy Side Effect'], adj_mat)
  77. print()
  78. print('OK')
  79. def main():
  80. data = load_data()
  81. if __name__ == '__main__':
  82. main()