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!
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

132 satır
4.8KB

  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. df_combo_effcat = df_combo.merge(df_effcat, left_on='Polypharmacy Side Effect', right_on='Side Effect')
  70. disease_classes = []
  71. print('Preparing Drug-Drug (Side Effect) edges...')
  72. fam = data.add_relation_family('Drug-Drug (Side Effect)', 1, 1, True)
  73. print('# of side effects:', len(df_combo), 'unique:', len(df_combo['Polypharmacy Side Effect'].unique()))
  74. for discls, df in df_combo_effcat.groupby('Disease Class'):
  75. disease_classes.append(discls)
  76. sys.stdout.write('.') # print(eff, '...')
  77. sys.stdout.flush()
  78. rows = [index(drugs, d) for d in df['STITCH 1']]
  79. cols = [index(drugs, d) for d in df['STITCH 2']]
  80. indices = list(zip(rows, cols))
  81. indices = torch.tensor(indices).transpose(0, 1)
  82. values = torch.ones(len(rows))
  83. adj_mat = torch.sparse_coo_tensor(indices, values, size=(len(drugs), len(drugs)),
  84. device=dev)
  85. adj_mat = (adj_mat + adj_mat.transpose(0, 1)) / 2
  86. rel = fam.add_relation_type(df['Polypharmacy Side Effect'], adj_mat)
  87. print()
  88. print('len(disease_classes):', len(disease_classes))
  89. print('OK')
  90. return data
  91. def _wrap(obj, method_name):
  92. orig_fn = getattr(obj, method_name)
  93. def fn(*args, **kwargs):
  94. print(f'{method_name}() :: ENTER')
  95. res = orig_fn(*args, **kwargs)
  96. print(f'{method_name}() :: EXIT')
  97. return res
  98. setattr(obj, method_name, fn)
  99. def main():
  100. dev = torch.device('cuda:0')
  101. data = load_data(dev)
  102. prep_d = prepare_training(data, TrainValTest(.8, .1, .1))
  103. _wrap(Model, 'build')
  104. model = Model(prep_d)
  105. model = model.to(dev)
  106. # model = torch.nn.DataParallel(model, ['cuda:0', 'cuda:1'])
  107. _wrap(TrainLoop, 'build')
  108. _wrap(TrainLoop, 'run_epoch')
  109. loop = TrainLoop(model, batch_size=512, shuffle=True)
  110. loop.run_epoch()
  111. if __name__ == '__main__':
  112. main()