PageRenderTime 57ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/ReverseTrainSecondLayer.py

https://gitlab.com/hyeonjik420/deep_learning_transcriptomics
Python | 86 lines | 78 code | 5 blank | 3 comment | 3 complexity | a6386d9742fb3a78ced1d0f596811c18 MD5 | raw file
  1. from keras.layers import Input, Dense, Activation
  2. import numpy as np
  3. from keras.models import Model, load_model
  4. from keras import optimizers, activations
  5. import math
  6. from numpy import *
  7. import pandas as pd
  8. from keras.callbacks import ModelCheckpoint
  9. from keras.callbacks import CSVLogger
  10. from keras import optimizers
  11. import tensorflow as tf
  12. from keras.backend import tensorflow_backend as K
  13. import glob
  14. def sigmo(x):
  15. return (1 / (1 + np.exp(-x)))
  16. yykt = glob.glob("Path to the folder having all the files of phenotipic associated gene expression profile/*.txt") # the files are needed with .txt extension
  17. yykt = sorted(yykt)
  18. count=0
  19. b = np.identity(len(yykt), dtype = float)
  20. for fl in yykt:
  21. if count==0:
  22. UXU = np.loadtxt(fl)
  23. yead1e = UXU.shape
  24. Trg0t = np.repeat(b[:,count], repeats = yead1e[1], axis=0).reshape(len(yykt),yead1e[1])
  25. else:
  26. VVV = np.loadtxt(fl)
  27. yead1e = VVV.shape
  28. Trgft = np.repeat(b[:,count], repeats = yead1e[1], axis=0).reshape(len(yykt),yead1e[1])
  29. Trg0t = np.append(Trg0t,Trgft, axis=1)
  30. UXU = np.append(UXU,VVV, axis=1)
  31. count = count+1
  32. qfwqwcv = np.random.permutation(UXU.shape[1])
  33. Trg0t = Trg0t[:,qfwqwcv]
  34. UXU = UXU[:,qfwqwcv]
  35. n_genes = 20848 # We can replace this number according to the number of genes in the expression profile
  36. # The trained autoencoder is needed, e.g. an output of Autoencoder3layer.py
  37. autoencoder = load_model('MicroarrayDeep512_512_512_AE20K.h5')
  38. autoencoder.layers[2].activation = activations.linear
  39. yrts = np.repeat([1],n_genes).reshape(1,n_genes)
  40. input_SigF=Input(shape=(n_genes,))
  41. laye1r = autoencoder.get_layer("dense_1")
  42. laye1r.name = "D1"
  43. L1 = laye1r(input_SigF)
  44. laye2r = autoencoder.get_layer("dense_2")
  45. laye2r.name = "D2"
  46. L2 = laye2r(L1)
  47. autoencodeL2r = Model(input_SigF, L2)
  48. yytrk = autoencodeL2r.predict(np.transpose(UXU))
  49. Etr = np.transpose(yytrk)
  50. Dtr = np.transpose(Trg0t)
  51. ttxrmi = Etr.mean(axis=0)
  52. Etr = (Etr-ttxrmi[None,:])
  53. Etr = np.transpose(Etr)
  54. # In case of loading the autoencoder MicroarrayDeep1024_1024_1024_AE20K.h5 and MicroarrayDeep256_256_256_AE20K.h5, we replaced com_dim = 512 to com_dim = 1024 and com_dim = 256 respectively.
  55. com_dim = 512
  56. input_SigS = Input(shape=(len(yykt),))
  57. LL1 = Dense(com_dim, activation='sigmoid',name='X1')(input_SigS)
  58. LLf = Dense(com_dim, activation='linear',name='X2')(LL1)
  59. DeasMSDeepNN = Model(input_SigS, LLf)
  60. admO = optimizers.Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-8, decay=1e-6)
  61. DeasMSDeepNN.compile(optimizer=admO, loss = 'mean_absolute_error')
  62. new_filename1 = "L2ConvS" + str(com_dim) + ".csv"
  63. csv_logger = CSVLogger(new_filename1, append=False, separator='\t')
  64. DeasMSDeepNN.fit(Dtr, Etr, epochs=5000, batch_size=32, shuffle=True, callbacks=[csv_logger])
  65. zoz = DeasMSDeepNN.get_weights()
  66. yy = autoencoder.get_weights()
  67. yy[4] = (np.var(zoz[2])/np.var(yy[4]))*yy[4]
  68. yy[5] = (np.var(zoz[3])/np.var(yy[5]))*yy[5]
  69. yfecfa = DeasMSDeepNN.predict(b)
  70. reswf = np.matmul(yfecfa, yy[4])
  71. yfuj = reswf.shape
  72. zzosk = np.transpose(np.tile(yy[5],yfuj[0]).reshape(yfuj[1],yfuj[0]))
  73. L3inS = (reswf+zzosk)
  74. L3otS = sigmo(L3inS)
  75. reswMidf = np.transpose(np.matmul(L3otS, yy[6]))
  76. # The reference file for the genes in which the autoencoder is trained, RefEntrezMicroarray.txt and RefEntrezRNAseq.txt correspond to micro-array and RNA-seq data respectively.
  77. ytre = np.genfromtxt('RefEntrezMicroarray.txt',dtype='str')
  78. yytrv = np.argsort(-np.absolute(reswMidf), axis=0)
  79. entNe = ytre[yytrv]
  80. np.savetxt("OrderDisease.txt", yykt,delimiter='\t',fmt='%s')
  81. np.savetxt("DeepAE_DiseaseGene.txt", entNe,delimiter='\t',fmt='%s')