code: # -*- coding: utf-8 -*-
  """
  Created on Thu Jan  9 09:22:42 2020
  
  @author: hcji
  """
  
  
  import numpy as np
  import tensorflow.keras.backend as K
  import matplotlib.pyplot as plt
  from sklearn.model_selection import train_test_split
  from tensorflow.keras.models import Model
  from tensorflow.keras.layers import Dense, Input, Flatten, Conv1D, MaxPooling1D, concatenate
  from tensorflow.keras import optimizers
  from sklearn.metrics import mean_absolute_error, r2_score
  from smiles_to_onehot.encoding import get_dict, one_hot_coding
  
  class multi_CNN:
      def __init__(self, X, Y):
          self.X = X
          self.Y = Y
          self.X_tr, self.X_ts, self.Y_tr, self.Y_ts = train_test_split(X, Y, test_size=0.1)
          
          inp = Input(shape=(X.shape[1:3]))
          n = X.shape[1]
  
          hidv1 = Conv1D(n, kernel_size=2, activation='relu')(inp)
          # hidv1 = MaxPooling1D(pool_size=2)(hidv1)
          hidv1 = Conv1D(n, kernel_size=2, activation='relu')(hidv1)
          # hidv1 = MaxPooling1D(pool_size=2)(hidv1)
          hidv1 = Flatten()(hidv1)
          
          hidv2 = Conv1D(n, kernel_size=3, activation='relu')(inp)
          # hidv2 = MaxPooling1D(pool_size=3)(hidv2)
          hidv2 = Conv1D(n, kernel_size=3, activation='relu')(hidv2)
          # hidv2 = MaxPooling1D(pool_size=3)(hidv2)
          hidv2 = Flatten()(hidv2)
          
          hidv3 = Conv1D(n, kernel_size=4, activation='relu')(inp)
          # hidv3 = MaxPooling1D(pool_size=4)(hidv3)
          hidv3 = Conv1D(n, kernel_size=4, activation='relu')(hidv3)
          # hidv3 = MaxPooling1D(pool_size=4)(hidv3)
          hidv3 = Flatten()(hidv3)
  
          hid = concatenate([hidv1, hidv2, hidv3], axis=-1)
          hid = Dense(32, activation="relu")(hid)
          hid = Dense(32, activation="relu")(hid)
          
          prd = Dense(1, activation="linear")(hid)
          opt = optimizers.Adam(lr=0.001)
          model = Model(inp, prd)
          model.compile(optimizer=opt, loss='mse', metrics=['mae'])
          self.model = model
      
      def train(self, epochs=20):
          history = self.model.fit(self.X_tr, self.Y_tr, epochs=epochs, validation_split = 0.1)
          plt.cla()
          plt.plot(history.history['val_loss'], alpha= 0.8)
          plt.plot(history.history['val_mean_absolute_error'], alpha= 0.8)
          plt.legend(['loss', 'accuracy'], loc="lower left")
          plt.xlabel('epoch')
          return history
      
      def test(self):
          Y_test = self.Y_ts
          Y_pred = np.round(self.model.predict(self.X_ts))
          r2 = round(r2_score(Y_pred, Y_test), 4)
          mae = round(mean_absolute_error(Y_pred, Y_test), 4)
  
          plt.cla()
          plt.plot(Y_test, Y_pred, '.', color = 'blue')
          plt.plot([0,4500], [0,4500], color ='red')
          plt.ylabel('Predicted RI')
          plt.xlabel('Experimental RI')        
          plt.text(0, 4000, 'R2='+str(r2), fontsize=12)
          plt.text(0, 3600, 'MAE='+str(mae), fontsize=12)
          plt.show()
          return r2, mae
      
      def save(self, path):
          self.model.save(path)
          K.clear_session()
          
  
  if __name__ == '__main__':
      
      import json
      
      with open('DeepEI/data/split.json', 'r') as js:
          keep = np.array(json.load(js)['keep'])
          
      smiles = np.array(json.load(open('DeepEI/data/all_smiles.json')))[keep]
      rindex = np.load('DeepEI/data/retention.npy')[keep,:]
      
      words = get_dict(smiles, save_path='DeepEI/data/words.json')
      smiles = [one_hot_coding(smi, words, max_len=100).todense() for smi in smiles]
      smiles = np.array(smiles)
      
      # simipolar
      i = np.where(~ np.isnan(rindex[:,0]))[0]
      mod = multi_CNN(smiles[i], rindex[i,0])
      mod.train()
      mod.test()
      mod.save('Retention/models/SimiStdNP_CNN_multi_model.h5')
      
      # nonpolar
      i = np.where(~ np.isnan(rindex[:,1]))[0]
      mod = multi_CNN(smiles[i], rindex[i,1])
      mod.train()
      mod.test()
      mod.save('Retention/models/StdNP_CNN_multi_model.h5')
  
      # polar
      i = np.where(~ np.isnan(rindex[:,2]))[0]
      mod = multi_CNN(smiles[i], rindex[i,2])
      mod.train()
      mod.test()
      mod.save('Retention/models/StdPolar_CNN_multi_model.h5')
       # -*- coding: utf-8 -*-
  """
  Created on Thu May  7 09:04:52 2020
  
  @author: hcji
  """
  
  
  import json
  import numpy as np
  import pandas as pd
  import matplotlib.pyplot as plt
  from tqdm import tqdm
  from scipy.sparse import load_npz
  from DeepEI.utils import get_score
  
  with open('DeepEI/data/split.json', 'r') as js:
      split = json.load(js)
  keep = np.array(split['keep'])
  
  nist_smiles = np.array(json.load(open('DeepEI/data/all_smiles.json')))[keep]
  nist_masses = np.load('DeepEI/data/molwt.npy')[keep]
  nist_spec = load_npz('DeepEI/data/peakvec.npz').todense()[keep,:]
  neims_nist_spec = load_npz('DeepEI/data/neims_spec_nist.npz').todense()[keep,:]
  
  neims_msbk_smiles = np.array(json.load(open('DeepEI/data/neims_msbk_smiles.json')))
  neims_msbk_masses = np.load('DeepEI/data/neims_msbk_masses.npy')
  neims_msbk_spec = load_npz('DeepEI/data/neims_spec_msbk.npz').todense()
  
  msbk_smiles = np.array(json.load(open('DeepEI/data/msbk_smiles.json')))
  msbk_masses = np.load('DeepEI/data/msbk_masses.npy')
  msbk_spec = load_npz('DeepEI/data/msbk_spec.npz').todense()
  
  db_smiles = np.array(list(nist_smiles) + list(neims_msbk_smiles))
  db_masses = np.append(nist_masses, neims_msbk_masses)
  db_spec_a = np.append(nist_spec, neims_msbk_spec, axis=0)
  db_spec_b = np.append(neims_nist_spec, neims_msbk_spec, axis=0)
  
  i = 70
  smi = msbk_smiles[i]
  specr = msbk_spec[i]
  mass = msbk_masses[i]
  candidate = np.where(np.abs(db_masses - mass) < 5)[0]
  cand_smi = db_smiles[candidate]
  
  scores_a = get_score(specr, db_spec_a[candidate,:], m='wdp')
  scores_b = get_score(specr, db_spec_b[candidate,:], m='wdp')
  
  wh_true = np.where(cand_smi == smi)[0][0]
  true_score_a = scores_a[wh_true]
  true_score_b = scores_b[wh_true]
  rank_a = len(np.where(scores_a > true_score_a)[0]) + 1
  rank_b = len(np.where(scores_b > true_score_b)[0]) + 1
  
  true = candidate[wh_true]
  j = candidate[435]
  
  decoy_smi = db_smiles[j]
  
  plt.figure(figsize=(6, 6))
  plt.vlines(np.arange(0, 2000), np.zeros(2000), db_spec_a[j], 'red', label='NIST_decoy')
  plt.vlines(np.arange(0, 2000), np.zeros(2000), -db_spec_b[j], 'blue', label='NEIMS_decoy')
  plt.axhline(0, color='black')
  plt.xlim(0, 500)
  plt.legend()
  
  plt.figure(figsize=(6, 6))
  plt.vlines(np.arange(0, 2000), np.zeros(2000), specr, 'green', label='MassBank_true')
  plt.axhline(0, color='black')
  plt.vlines(np.arange(0, 2000), np.zeros(2000), -db_spec_b[j], 'blue', label='NEIMS_decoy')
  plt.xlim(0, 500)
  plt.legend()
  
  plt.figure(figsize=(6, 6))
  plt.vlines(np.arange(0, 2000), np.zeros(2000), specr, 'green', label='MassBank_true')
  plt.axhline(0, color='black')
  plt.vlines(np.arange(0, 2000), np.zeros(2000), -db_spec_a[j], 'red', label='NIST_decoy')
  plt.xlim(0, 500)
  plt.legend()
  
  plt.figure(figsize=(6, 6))
  plt.vlines(np.arange(0, 2000), np.zeros(2000), specr, 'green', label='MassBank_true')
  plt.axhline(0, color='black')
  plt.vlines(np.arange(0, 2000), np.zeros(2000), -db_spec_a[true], 'purple', label='NEIMS_true')
  plt.xlim(0, 500)
  plt.legend()
  
apis: tensorflow.keras.optimizers.Adam tensorflow.keras.layers.Flatten matplotlib.pyplot.cla tensorflow.keras.models.Model matplotlib.pyplot.ylabel matplotlib.pyplot.plot tensorflow.keras.layers.Conv1D tensorflow.keras.layers.Dense numpy.isnan numpy.load sklearn.metrics.mean_absolute_error tensorflow.keras.layers.concatenate sklearn.metrics.r2_score sklearn.model_selection.train_test_split matplotlib.pyplot.legend tensorflow.keras.backend.clear_session matplotlib.pyplot.show numpy.array matplotlib.pyplot.xlabel tensorflow.keras.layers.Input
apis: numpy.load matplotlib.pyplot.legend numpy.append numpy.zeros matplotlib.pyplot.figure matplotlib.pyplot.axhline numpy.abs scipy.sparse.load_npz matplotlib.pyplot.xlim numpy.arange numpy.array numpy.where