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!
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

127 lignes
4.6KB

  1. #!/usr/bin/env python3
  2. from icosagon.data import Data
  3. from icosagon.trainprep import TrainValTest, \
  4. prepare_training
  5. from icosagon.model import Model
  6. from icosagon.trainloop import TrainLoop
  7. import os
  8. import pandas as pd
  9. from bisect import bisect_left
  10. import torch
  11. import sys
  12. def index(a, x):
  13. i = bisect_left(a, x)
  14. if i != len(a) and a[i] == x:
  15. return i
  16. raise ValueError
  17. def load_data(dev):
  18. path = '/pstore/data/data_science/ref/decagon'
  19. df_combo = pd.read_csv(os.path.join(path, 'bio-decagon-combo.csv'))
  20. df_effcat = pd.read_csv(os.path.join(path, 'bio-decagon-effectcategories.csv'))
  21. df_mono = pd.read_csv(os.path.join(path, 'bio-decagon-mono.csv'))
  22. df_ppi = pd.read_csv(os.path.join(path, 'bio-decagon-ppi.csv'))
  23. df_tgtall = pd.read_csv(os.path.join(path, 'bio-decagon-targets-all.csv'))
  24. df_tgt = pd.read_csv(os.path.join(path, 'bio-decagon-targets.csv'))
  25. lst = [ 'df_combo', 'df_effcat', 'df_mono', 'df_ppi', 'df_tgtall', 'df_tgt' ]
  26. for nam in lst:
  27. print(f'len({nam}): {len(locals()[nam])}')
  28. print(f'{nam}.columns: {locals()[nam].columns}')
  29. genes = set()
  30. genes = genes.union(df_ppi['Gene 1']).union(df_ppi['Gene 2']) \
  31. .union(df_tgtall['Gene']).union(df_tgt['Gene'])
  32. genes = sorted(genes)
  33. print('len(genes):', len(genes))
  34. drugs = set()
  35. drugs = drugs.union(df_combo['STITCH 1']).union(df_combo['STITCH 2']) \
  36. .union(df_mono['STITCH']).union(df_tgtall['STITCH']).union(df_tgt['STITCH'])
  37. drugs = sorted(drugs)
  38. print('len(drugs):', len(drugs))
  39. data = Data()
  40. data.add_node_type('Gene', len(genes))
  41. data.add_node_type('Drug', len(drugs))
  42. print('Preparing PPI...')
  43. print('Indexing rows...')
  44. rows = [index(genes, g) for g in df_ppi['Gene 1']]
  45. print('Indexing cols...')
  46. cols = [index(genes, g) for g in df_ppi['Gene 2']]
  47. indices = list(zip(rows, cols))
  48. indices = torch.tensor(indices).transpose(0, 1)
  49. values = torch.ones(len(rows))
  50. print('indices.shape:', indices.shape, 'values.shape:', values.shape)
  51. adj_mat = torch.sparse_coo_tensor(indices, values, size=(len(genes),) * 2,
  52. device=dev)
  53. adj_mat = (adj_mat + adj_mat.transpose(0, 1)) / 2
  54. print('adj_mat created')
  55. fam = data.add_relation_family('PPI', 0, 0, True)
  56. rel = fam.add_relation_type('PPI', adj_mat)
  57. print('OK')
  58. print('Preparing Drug-Gene (Target) edges...')
  59. rows = [index(drugs, d) for d in df_tgtall['STITCH']]
  60. cols = [index(genes, g) for g in df_tgtall['Gene']]
  61. indices = list(zip(rows, cols))
  62. indices = torch.tensor(indices).transpose(0, 1)
  63. values = torch.ones(len(rows))
  64. adj_mat = torch.sparse_coo_tensor(indices, values, size=(len(drugs), len(genes)),
  65. device=dev)
  66. fam = data.add_relation_family('Drug-Gene (Target)', 1, 0, True)
  67. rel = fam.add_relation_type('Drug-Gene (Target)', adj_mat)
  68. print('OK')
  69. print('Preparing Drug-Drug (Side Effect) edges...')
  70. fam = data.add_relation_family('Drug-Drug (Side Effect)', 1, 1, True)
  71. print('# of side effects:', len(df_combo), 'unique:', len(df_combo['Polypharmacy Side Effect'].unique()))
  72. for eff, df in df_combo.groupby('Polypharmacy Side Effect'):
  73. sys.stdout.write('.') # print(eff, '...')
  74. sys.stdout.flush()
  75. rows = [index(drugs, d) for d in df['STITCH 1']]
  76. cols = [index(drugs, d) for d in df['STITCH 2']]
  77. indices = list(zip(rows, cols))
  78. indices = torch.tensor(indices).transpose(0, 1)
  79. values = torch.ones(len(rows))
  80. adj_mat = torch.sparse_coo_tensor(indices, values, size=(len(drugs), len(drugs)),
  81. device=dev)
  82. adj_mat = (adj_mat + adj_mat.transpose(0, 1)) / 2
  83. rel = fam.add_relation_type(df['Polypharmacy Side Effect'], adj_mat)
  84. print()
  85. print('OK')
  86. return data
  87. def _wrap(obj, method_name):
  88. orig_fn = getattr(obj, method_name)
  89. def fn(*args, **kwargs):
  90. print(f'{method_name}() :: ENTER')
  91. res = orig_fn(*args, **kwargs)
  92. print(f'{method_name}() :: EXIT')
  93. return res
  94. setattr(obj, method_name, fn)
  95. def main():
  96. dev = torch.device('cpu')
  97. data = load_data(dev)
  98. prep_d = prepare_training(data, TrainValTest(.8, .1, .1))
  99. _wrap(Model, 'build')
  100. model = Model(prep_d)
  101. model = model.to(dev)
  102. # model = torch.nn.DataParallel(model, ['cuda:0', 'cuda:1'])
  103. _wrap(TrainLoop, 'build')
  104. _wrap(TrainLoop, 'run_epoch')
  105. loop = TrainLoop(model, batch_size=512, shuffle=True)
  106. loop.run_epoch()
  107. if __name__ == '__main__':
  108. main()