R/Molecular_graph_V01.R

Defines functions Vdistance_distance_matrix Extended_Vadj_matrix_Veln Extended_Vadj_matrix_Vdegrees Sparse_csi_V_matrix Laplacian_Vdistance_full Eadj_matrix_full Vadj_matrix_full Vadj_highpower_matrix Vadj_kneigh_matrix Laplacian_Edistance Laplacian_Vdistance Distance_path_Edistance Distance_path_Vdistance Rec_Complementary_Edistance Rec_Complementary_Vdistance Complementary_Edistance Complementary_Vdistance Rec_ECdistance Rec_VCdistance E_Harary_matrix V_Harary_matrix ECdistance_matrix Edistance_matrix Eadj_matrix VCdistance_matrix Vdistance_matrix Vadj_matrix graphical_matrix

Documented in Complementary_Edistance Complementary_Vdistance Distance_path_Edistance Distance_path_Vdistance Eadj_matrix Eadj_matrix_full ECdistance_matrix Edistance_matrix E_Harary_matrix Extended_Vadj_matrix_Vdegrees Extended_Vadj_matrix_Veln graphical_matrix Laplacian_Edistance Laplacian_Vdistance Laplacian_Vdistance_full Rec_Complementary_Edistance Rec_Complementary_Vdistance Rec_ECdistance Rec_VCdistance Sparse_csi_V_matrix Vadj_highpower_matrix Vadj_kneigh_matrix Vadj_matrix Vadj_matrix_full VCdistance_matrix Vdistance_distance_matrix Vdistance_matrix V_Harary_matrix

## This source file contain all the necessary functions to compute several
# graphical matrix necessary for the further computation of twoD descriptors (TOPOLOGICAL)
# The main function graphical_matrix() Calls every grpahical function and warn the user
# if any of the computation failed. Each matrix is then stored in the Mol_mat environment
# for future use.

#' Moleculors graphical matrices calculator
#'
#' This function take the cartesian coordinates of a molecule and return the graphical matrices
#' of its structure. To do this first of all remove all the hydrogens leaving only the carbons
#' and heteroatoms. Then it compute the relative distance for each atom from each other and
#' round it up. In the end we will have a matrix n*n where n is the number of atoms
#' and each cell contain an integer pointing to the number of bonds intercurring
#' between the diagonal element and the others. The adjacency matrix is then computed by checking if
#' the distance is 1 or not.
#' For the edge matrix, the edge coordinate is computed from the input coordinates as the mid point
#' between two adjacent atoms and then the distance and adjacency is computed using the previuos
#' algorithm
#'
#'
#' @return Graphical matrices for edges and verteces stored inside the Mol_mat environment.
#'
#' @examples
#' graphical_matrix()
#'
#' @export


graphical_matrix = function(){

  if(is.data.frame(Mol_mat$input)){

    hydrogen_vector = c()

    for (i in 1:nrow(Mol_mat$input)) {

      if (as.character(Mol_mat$input$Atom[i] == "H")) {

        hydrogen_vector = append(hydrogen_vector, i)

      }
      if (i == nrow(Mol_mat$input) & length(hydrogen_vector) > 0) {

        Mol_mat$input_H_suppressed = Mol_mat$input[-hydrogen_vector,]

      } else {
        Mol_mat$input_H_suppressed <- Mol_mat$input
      }
    }


    Vadj_matrix(Mol_mat$input_H_suppressed)

    Vadj_matrix_full(Mol_mat$input)

    Vdistance_matrix()

    VCdistance_matrix()

    Eadj_matrix(Mol_mat$input_H_suppressed)

    Eadj_matrix_full(Mol_mat$input)

    Edistance_matrix()

    ECdistance_matrix()

    V_Harary_matrix()

    E_Harary_matrix()

    Rec_VCdistance()

    Rec_ECdistance()

    Complementary_Vdistance()

    Complementary_Edistance()

    Rec_Complementary_Vdistance()

    Rec_Complementary_Edistance()

    Distance_path_Vdistance()

    Distance_path_Edistance()

    Laplacian_Vdistance()

    Laplacian_Vdistance_full()

    Laplacian_Edistance()

    Vadj_kneigh_matrix(2)

    Vadj_highpower_matrix(2)

    Sparse_csi_V_matrix()

    Extended_Vadj_matrix_Vdegrees()

    Extended_Vadj_matrix_Veln()

    Vdistance_distance_matrix()



  } else {

    message("No Input file detected")

    return("No graphical matrices were computed")

  }

  return(message("Computing graphical Matrices... OK"))

}


#' Moleculors vertex adjacency matrix
#'
#' This function calculate the V adjacency matrix using as Input the Hydrogen suppressed
#' cartesian matrix. In order to compute the distances it starts by calculating the
#' magnitude of the vector from each atom to one another. Then after rounding 'too many digits
#' may lead to bad calculation in the following steps' it choose the smallest value 'different
#' from 0' has the 1 distance value. A loop is used to assure that every value of lenght 1 has
#' the same value for the normalization step following later.
#' a normalization is the computed and  every other value different from 1 is set to 0
#'
#'
#' @param Cart_Input_Hsupp Cartesian coordinates of the molecule without the hydrogen atoms
#'
#' @return Vertex adjacency matrix for the loaded molecule. Matrix is stored in Mol_mat environment.
#'
#' @examples
#' Vadj_matrix(input_H_suppressed)
#'
#' @export
#'

Vadj_matrix = function(Cart_Input_Hsupp){

  graph_Vadj_matrix = matrix(nrow = nrow(Cart_Input_Hsupp), ncol = nrow(Cart_Input_Hsupp))

  for (i in 1:nrow(graph_Vadj_matrix)) {

    for (j in 1:ncol(graph_Vadj_matrix)) {

      graph_Vadj_matrix[i,j] = sqrt((Cart_Input_Hsupp$X[j] - Cart_Input_Hsupp$X[i])^2 +
                                      (Cart_Input_Hsupp$Y[j] - Cart_Input_Hsupp$Y[i])^2 +
                                      (Cart_Input_Hsupp$Z[j] - Cart_Input_Hsupp$Z[i])^2)

    }
  }

  graph_Vadj_matrix = apply(graph_Vadj_matrix, 2, round, 2)


  for (i in 1:nrow(graph_Vadj_matrix)) {
    for (j in 1:nrow(graph_Vadj_matrix)) {
      if ((graph_Vadj_matrix[i,j] - min(graph_Vadj_matrix[i,-i])) <= 0.58 & i != j & j > i) {
        graph_Vadj_matrix[i,j] = min(graph_Vadj_matrix[i,-i])
      }
    }
  }

  for (i in 1:nrow(graph_Vadj_matrix)) {
    divider = min(graph_Vadj_matrix[i,-i])
    for (j in 1:nrow(graph_Vadj_matrix)) {
      if (i != j & j > i) {
        graph_Vadj_matrix[i,j] = graph_Vadj_matrix[i,j]/divider
      }
    }
  }


  for (i in 1:nrow(graph_Vadj_matrix)) {
    for (j in 1:nrow(graph_Vadj_matrix)) {
      if (graph_Vadj_matrix[i,j] != 1) {
        graph_Vadj_matrix[i,j] = 0
      }
      graph_Vadj_matrix[j,i] = graph_Vadj_matrix[i,j]
    }
  }


  Mol_mat$graph_Vadj_matrix = graph_Vadj_matrix

  message("Vertex adjacency matrix ... OK")
}

#' Moleculors vertex distance matrix
#'
#' This function take the adjacency matrix as input and generate a connection list where the index
#' is related to the atom and the value in the index list are the atoms to which that atom is connected
#' For each element with d != 1 is then calculated the distance by taking the value in the connection list
#' copying those into the connection vector and if the interested atom j is not in the connection vector
#' a new vector is created with all the connection of the atom to which the first atoms was connected
#' this is looped increasing distance at each failed loop to detect the interested element.
#'
#'
#' @return Vertex distance matrix for the loaded molecule. Matrix is stored in Mol_mat environment.
#'
#' @examples
#' Vdistance_matrix()
#'
#' @export
#'



Vdistance_matrix = function(){

  if (is.matrix(Mol_mat$graph_Vadj_matrix)) {
    graph_Vdistance_matrix = matrix(nrow = nrow(Mol_mat$graph_Vadj_matrix), ncol = nrow(Mol_mat$graph_Vadj_matrix))

    connections = list()
    index_counter = c()

    for (i in 1:nrow(Mol_mat$graph_Vadj_matrix)) {
      connections = append(connections, list(which(Mol_mat$graph_Vadj_matrix[i,] == 1)))
    }


    for (i in 1:nrow(graph_Vdistance_matrix)) {
      for (j in 1:nrow(graph_Vdistance_matrix)) {
        d = 1
        if (Mol_mat$graph_Vadj_matrix[i,j] == 1) {
          graph_Vdistance_matrix[i,j] = 1
        } else if (i == j) {
          graph_Vdistance_matrix[i,j] = 0
        } else if (i != j & Mol_mat$graph_Vadj_matrix[i,j] == 0) {
          connection_vector = connections[[i]]
          while (!(j %in% unique(connection_vector))) {
            connections_holder = c()
            for (k in 1:length(unique(connection_vector))) {
              connections_holder = append(connections_holder, connections[[unique(connection_vector)[k]]])
            }
            d = d + 1
            connection_vector = connections_holder
          }
          graph_Vdistance_matrix[i,j] = d
        }
      }
    }

    Mol_mat$graph_Vdistance_matrix = graph_Vdistance_matrix

    message("Vertex distance matrix ... OK")
  } else {
    message("Vertex distance matrix ... FAIL")
  }
}


#' Moleculors vertex distance matrix
#'
#' This function requires the Vdistance_matrix in order to compute the complement
#'  of the distance matrix. Each element is set has nrowVdistance - Vdistanceij
#'
#'
#' @return Vertex complement distance matrix for the loaded molecule. Matrix is stored in Mol_mat environment.
#'
#' @examples
#' VCdistance_matrix()
#'
#' @export
#'



VCdistance_matrix = function(){

  if (is.matrix(Mol_mat$graph_Vdistance_matrix)) {
    graph_VCdistance_matrix = matrix(nrow = nrow(Mol_mat$graph_Vdistance_matrix), ncol = nrow(Mol_mat$graph_Vdistance_matrix))

    for (i in 1:nrow(graph_VCdistance_matrix)) {
      for (j in 1:nrow(graph_VCdistance_matrix)) {
        if (i != j) {
          graph_VCdistance_matrix[i,j] = nrow(Mol_mat$graph_Vdistance_matrix) - Mol_mat$graph_Vdistance_matrix[i,j]
        } else {
          graph_VCdistance_matrix[i,j] = 0
        }
      }
    }
    Mol_mat$graph_VCdistance_matrix = graph_VCdistance_matrix

    message("Vertex complement distance matrix ... OK")
  } else {
    message("Vertex complement distance matrix ... FAIL")
  }
}

#' Moleculors edge adjacency matrix
#'
#' This function require the H suppressed cartesian matrix
#' and the adjagency matrix for the vertexes in order to compute
#' the edge cartesian matrix and the adjacency edges matrices.
#'
#'
#' @param Cart_Input_Hsupp Cartesian coordinates of the molecule without the hydrogen atoms
#'
#' @return Edge adjacency matrix for the loaded molecule. Matrix is stored in Mol_mat environment.
#'
#' @examples
#' Eadj_matrix(input_H_suppressed)
#'
#' @export
#'


Eadj_matrix = function(Cart_Input_Hsupp){

  if (is.matrix(Mol_mat$graph_Vadj_matrix)) {
    edge_matrix = matrix(nrow = nrow(Cart_Input_Hsupp)*3,
                         ncol = (ncol(Cart_Input_Hsupp)-1))
    h = 1
    counter = 1

    for (i in 1:nrow(Mol_mat$graph_Vadj_matrix)) {
      for (j in h:nrow(Mol_mat$graph_Vadj_matrix)) {
        if (Mol_mat$graph_Vadj_matrix[i,j] == 1) {
          edge_matrix[counter, 1] = (Cart_Input_Hsupp$X[i] + Cart_Input_Hsupp$X[j])/2
          edge_matrix[counter, 2] = (Cart_Input_Hsupp$Y[i] + Cart_Input_Hsupp$Y[j])/2
          edge_matrix[counter, 3] = (Cart_Input_Hsupp$Z[i] + Cart_Input_Hsupp$Z[j])/2
          counter = counter + 1
        }
      }
      h = h + 1
    }

    graph_Eadj_matrix = matrix(nrow = nrow(edge_matrix), ncol = nrow(edge_matrix))


    for (i in 1:nrow(graph_Eadj_matrix)) {
      for (j in 1:ncol(graph_Eadj_matrix)) {
        graph_Eadj_matrix[i,j] = sqrt((edge_matrix[j,1] - edge_matrix[i,1])^2 +
                                        (edge_matrix[j,2] - edge_matrix[i,2])^2 +
                                        (edge_matrix[j,3] - edge_matrix[i,3])^2)
      }
    }

    NA_vector = c()

    for (j in 1:ncol(graph_Eadj_matrix)) {
      if (is.na(graph_Eadj_matrix[1,j]) & !(j %in% NA_vector)) {
        NA_vector = append(NA_vector, j)
      }
    }

    if (!(is.null(NA_vector))) {
      graph_Eadj_matrix = graph_Eadj_matrix[-NA_vector, -NA_vector]
    }


    graph_Eadj_matrix = apply(graph_Eadj_matrix, 2, round, 2)

    for (i in 1:nrow(graph_Eadj_matrix)) {
      for (j in 1:nrow(graph_Eadj_matrix)) {
        if ((graph_Eadj_matrix[i,j] - min(graph_Eadj_matrix[i,-i])) <= 0.58 & i != j & j > i) {
          graph_Eadj_matrix[i,j] = min(graph_Eadj_matrix[i,-i])
        }
      }
    }

    for (i in 1:nrow(graph_Eadj_matrix)) {
      divider = min(graph_Eadj_matrix[i,-i])
      for (j in 1:nrow(graph_Eadj_matrix)) {
        if (i != j & j > i) {
          graph_Eadj_matrix[i,j] = graph_Eadj_matrix[i,j]/divider
        }
      }
    }


    for (i in 1:nrow(graph_Eadj_matrix)) {
      for (j in 1:nrow(graph_Eadj_matrix)) {
        if (graph_Eadj_matrix[i,j] != 1) {
          graph_Eadj_matrix[i,j] = 0
        }
        graph_Eadj_matrix[j,i] = graph_Eadj_matrix[i,j]
      }
    }
    Mol_mat$graph_Eadj_matrix = graph_Eadj_matrix

    message("Edge adjacency matrix ... OK")
  } else {
    message("Edge adjacency matrix ... FAIL")
  }

}

#' Moleculors edge distance matrix
#'
#' This function takes as input the Eadjancency matrix and compute, using
#' the same algorithm used for the vertexes, the distance matrix for the edges.
#' This is done by computing a connection vector that states with which edge each edge is connected
#' than it loop through each connection to find the wanted connection 'e.g. elemij It loop through
#' the connection of 1 until it find the element j, increasing the distance at each loop'
#'
#'
#' @return Edge distance matrix for the loaded molecule. Matrix is stored in Mol_mat environment.
#'
#' @examples
#' Edistance_matrix()
#'
#' @export
#'


Edistance_matrix = function(){

  if (is.matrix(Mol_mat$graph_Eadj_matrix)) {
    graph_Edistance_matrix = matrix(nrow = nrow(Mol_mat$graph_Eadj_matrix), ncol = nrow(Mol_mat$graph_Eadj_matrix))

    connections = list()
    index_counter = c()

    for (i in 1:nrow(Mol_mat$graph_Eadj_matrix)) {
      connections = append(connections, list(which(Mol_mat$graph_Eadj_matrix[i,] == 1)))
    }


    for (i in 1:nrow(graph_Edistance_matrix)) {
      for (j in 1:nrow(graph_Edistance_matrix)) {
        d = 1
        if (Mol_mat$graph_Eadj_matrix[i,j] == 1) {
          graph_Edistance_matrix[i,j] = 1
        } else if (i == j) {
          graph_Edistance_matrix[i,j] = 0
        } else if (i != j & Mol_mat$graph_Eadj_matrix[i,j] == 0) {
          connection_vector = connections[[i]]
          while (!(j %in% unique(connection_vector))) {
            connections_holder = c()
            for (k in 1:length(unique(connection_vector))) {
              connections_holder = append(connections_holder, connections[[unique(connection_vector)[k]]])
            }
            d = d + 1
            if (d > nrow(Mol_mat$graph_Eadj_matrix)) {
              return(message("Edge distance matrix ... FAIL"))
            }
            connection_vector = connections_holder
          }
          graph_Edistance_matrix[i,j] = d
        }
      }
    }

    Mol_mat$graph_Edistance_matrix = graph_Edistance_matrix

    message("Edge distance matrix ... OK")
  } else {
    message("Edge distance matrix ... FAIL")
  }
}

#' Moleculors edge complement distance matrix
#'
#' This function takes as input the Edistance matrix and compute for each element
#' the complement as nrowEdistance_matrix - Edistance_matrixi,j
#'
#'
#' @return Edge complement distance matrix for the loaded molecule. Matrix is stored in Mol_mat environment.
#'
#' @examples
#' ECdistance_matrix()
#'
#' @export
#'


ECdistance_matrix = function(){

  if (is.matrix(Mol_mat$graph_Edistance_matrix)) {
    graph_ECdistance_matrix = matrix(nrow = nrow(Mol_mat$graph_Edistance_matrix), ncol = nrow(Mol_mat$graph_Edistance_matrix))

    for (i in 1:nrow(graph_ECdistance_matrix)) {
      for (j in 1:nrow(graph_ECdistance_matrix)) {
        if (i != j) {
          graph_ECdistance_matrix[i,j] = nrow(Mol_mat$graph_Edistance_matrix) - Mol_mat$graph_Edistance_matrix[i,j]
        } else {
          graph_ECdistance_matrix[i,j] = 0
        }
      }
    }
    Mol_mat$graph_ECdistance_matrix = graph_ECdistance_matrix

    message("Edge distance complement matrix ... OK")
  } else {
    message("Edge distance complement matrix ... FAIL")
  }
}


#' Moleculors vertex harary matrix
#'
#' This function compute the vertex version of the so called
#' Harary matrix. It take has input the Vdistance_matrix and for each
#' element of the matrix if i != j it returns the reciprocal value of
#' the distance matrix
#'
#'
#' @return Vertex Harary matrix for the loaded molecule. Matrix is stored in Mol_mat environment.
#'
#' @examples
#' V_Harary_matrix()
#'
#' @export
#'


V_Harary_matrix = function(){

  if (is.matrix(Mol_mat$graph_Vdistance_matrix)) {
    graph_VHarary_matrix = matrix(nrow = nrow(Mol_mat$graph_Vdistance_matrix), ncol = nrow(Mol_mat$graph_Vdistance_matrix))
    for (i in 1:nrow(Mol_mat$graph_Vdistance_matrix)) {
      for (j in 1:nrow(Mol_mat$graph_Vdistance_matrix)) {
        if (i != j) {
          graph_VHarary_matrix[i,j] = 1 /Mol_mat$graph_Vdistance_matrix[i,j]
        } else {
          graph_VHarary_matrix[i,j] = 0
        }
      }
    }

    Mol_mat$graph_VHarary_matrix = graph_VHarary_matrix

    message("Vertex Harary matrix ... OK")
  } else {
    message("Vertex Harary matrix ... FAIL")
  }
}


#' Moleculors edge harary matrix
#'
#' This function compute the edge version of the so called
#' Harary matrix. It take has input the Edistance_matrix and for each
#' element of the matrix if i != j it returns the reciprocal value of
#' the distance matrix
#'
#'
#' @return Edge Harary matrix for the loaded molecule. Matrix is stored in Mol_mat environment.
#'
#' @examples
#' E_Harary_matrix()
#'
#' @export
#'


E_Harary_matrix = function(){

  if (is.matrix(Mol_mat$graph_Edistance_matrix)) {
    graph_EHarary_matrix = matrix(nrow = nrow(Mol_mat$graph_Edistance_matrix), ncol = nrow(Mol_mat$graph_Edistance_matrix))
    for (i in 1:nrow(Mol_mat$graph_Edistance_matrix)) {
      for (j in 1:nrow(Mol_mat$graph_Edistance_matrix)) {
        if (i != j) {
          graph_EHarary_matrix[i,j] = 1 /Mol_mat$graph_Edistance_matrix[i,j]
        } else {
          graph_EHarary_matrix[i,j] = 0
        }
      }
    }

    Mol_mat$graph_EHarary_matrix = graph_EHarary_matrix

    message("Edge Harary matrix ... OK")
  } else {
    message("Edge Harary matrix ... FAIL")
  }
}


#' Moleculors vertex reciprocal complement matrix
#'
#' This function compute the vertex version of the reciprocal
#' complement vertex matrix . It take has input the VCdistance_matrix and for each
#' element of the matrix if i != j it returns the reciprocal value of
#' the distance matrix
#'
#'
#' @return Vertex reciprocal complement matrix for the loaded molecule. Matrix is stored in Mol_mat environment.
#'
#' @examples
#' Rec_VCdistance()
#'
#' @export
#'


Rec_VCdistance = function(){

  if (is.matrix(Mol_mat$graph_VCdistance_matrix)) {
    graph_RecVCdistance_matrix = matrix(nrow = nrow(Mol_mat$graph_VCdistance_matrix), ncol = nrow(Mol_mat$graph_VCdistance_matrix))
    for (i in 1:nrow(Mol_mat$graph_VCdistance_matrix)) {
      for (j in 1:nrow(Mol_mat$graph_VCdistance_matrix)) {
        if (i != j) {
          graph_RecVCdistance_matrix[i,j] = 1 /Mol_mat$graph_VCdistance_matrix[i,j]
        } else {
          graph_RecVCdistance_matrix[i,j] = 0
        }
      }
    }

    Mol_mat$graph_RecVCdistance_matrix = graph_RecVCdistance_matrix

    message("Reciprocal vertex complement matrix ... OK")
  } else {
    message("Reciprocal vertex complement matrix ... FAIL")
  }
}


#' Moleculors edge reciprocal complement matrix
#'
#' This function compute the vertex version of the reciprocal
#' complement vertex matrix . It take has input the VCdistance_matrix and for each
#' element of the matrix if i != j it returns the reciprocal value of
#' the distance matrix
#'
#'
#' @return Edge reciprocal complement matrix for the loaded molecule. Matrix is stored in Mol_mat environment.
#'
#' @examples
#' Rec_ECdistance()
#'
#' @export
#'


Rec_ECdistance = function(){

  if (is.matrix(Mol_mat$graph_ECdistance_matrix)) {
    graph_RecECdistance_matrix = matrix(nrow = nrow(Mol_mat$graph_ECdistance_matrix), ncol = nrow(Mol_mat$graph_ECdistance_matrix))
    for (i in 1:nrow(Mol_mat$graph_ECdistance_matrix)) {
      for (j in 1:nrow(Mol_mat$graph_ECdistance_matrix)) {
        if (i != j) {
          graph_RecECdistance_matrix[i,j] = 1 /Mol_mat$graph_ECdistance_matrix[i,j]
        } else {
          graph_RecECdistance_matrix[i,j] = 0
        }
      }
    }

    Mol_mat$graph_RecECdistance_matrix = graph_RecECdistance_matrix

    message("Reciprocal edge complement matrix ... OK")
  } else {
    message("Reciprocal edge complement matrix ... FAIL")
  }
}


#' Moleculors vertex complementary matrix
#'
#' This function compute the vertex version of the complementary
#' vertex matrix . It take has input the Vdistance_matrix and for each
#' element of the matrix if i != j it returns the  value of dmin + dmax - elemij
#' were dmin is the minimal lenght in the molecular graph (1 for simple molecules)
#' dmax is the maximum lenght in the system and elemij is the value of the element i j
#' of the vertex distance matrix
#'
#'
#' @return Vertex reciprocal matrix for the loaded molecule. Matrix is stored in Mol_mat environment.
#'
#' @examples
#' Complementary_Vdistance()
#'
#' @export
#'


Complementary_Vdistance = function(){

  if (is.matrix(Mol_mat$graph_Vdistance_matrix)) {
    graph_VCOMPdistance_matrix = matrix(nrow = nrow(Mol_mat$graph_Vdistance_matrix), ncol = nrow(Mol_mat$graph_Vdistance_matrix))
    for (i in 1:nrow(Mol_mat$graph_Vdistance_matrix)) {
      for (j in 1:nrow(Mol_mat$graph_Vdistance_matrix)) {
        if (i != j) {
          graph_VCOMPdistance_matrix[i,j] = 1 + max(Mol_mat$graph_Vdistance_matrix) - Mol_mat$graph_Vdistance_matrix[i,j]
        } else {
          graph_VCOMPdistance_matrix[i,j] = 0
        }
      }
    }

    Mol_mat$graph_VCOMPdistance_matrix = graph_VCOMPdistance_matrix

    message("Complementary vertex matrix ... OK")
  } else {
    message("Complementary vertex matrix ... FAIL")
  }
}

#' Moleculors edge complementary matrix
#'
#' This function compute the edge version of the complementary
#' edge matrix . It take has input the Edistance_matrix and for each
#' element of the matrix if i != j it returns the  value of dmin + dmax - elemij
#' were dmin is the minimal lenght in the molecular graph (1 for simple molecules)
#' dmax is the maximum lenght in the system and elemij is the value of the element i j
#' of the edge distance matrix
#'
#'
#'
#' @return Edge complementary matrix for the loaded molecule. Matrix is stored in Mol_mat environment.
#'
#' @examples
#' Complementary_Edistance()
#'
#' @export
#'


Complementary_Edistance = function(){

  if (is.matrix(Mol_mat$graph_Edistance_matrix)) {
    graph_ECOMPdistance_matrix = matrix(nrow = nrow(Mol_mat$graph_Edistance_matrix), ncol = nrow(Mol_mat$graph_Edistance_matrix))
    for (i in 1:nrow(Mol_mat$graph_Edistance_matrix)) {
      for (j in 1:nrow(Mol_mat$graph_Edistance_matrix)) {
        if (i != j) {
          graph_ECOMPdistance_matrix[i,j] = 1 + max(Mol_mat$graph_Edistance_matrix) - Mol_mat$graph_Edistance_matrix[i,j]
        } else {
          graph_ECOMPdistance_matrix[i,j] = 0
        }
      }
    }

    Mol_mat$graph_ECOMPdistance_matrix = graph_ECOMPdistance_matrix
    message("Complementary edge matrix ... OK")
  } else {
    message("Complementary edge matrix ... FAIL")
  }
}

#' Moleculors vertex reciprocal complementary matrix
#'
#' This function compute the vertex version of the reciprocal
#' complementary vertex matrix . It take has input the Complementary_Vdistance_matrix and for each
#' element of the matrix if i != j it returns the reciprocal value of
#' the distance matrix
#'
#'
#' @return Vertex reciprocal complementary matrix for the loaded molecule. Matrix is stored in Mol_mat environment.
#'
#' @examples
#' Rec_Complementary_Vdistance()
#'
#' @export
#'


Rec_Complementary_Vdistance = function(){

  if (is.matrix(Mol_mat$graph_VCOMPdistance_matrix)) {
    graph_VRecCOMPdistance_matrix = matrix(nrow = nrow(Mol_mat$graph_VCOMPdistance_matrix), ncol = nrow(Mol_mat$graph_VCOMPdistance_matrix))
    for (i in 1:nrow(Mol_mat$graph_VCOMPdistance_matrix)) {
      for (j in 1:nrow(Mol_mat$graph_VCOMPdistance_matrix)) {
        if (i != j) {
          graph_VRecCOMPdistance_matrix[i,j] = 1 /Mol_mat$graph_VCOMPdistance_matrix[i,j]
        } else {
          graph_VRecCOMPdistance_matrix[i,j] = 0
        }
      }
    }

    Mol_mat$graph_VRecCOMPdistance_matrix = graph_VRecCOMPdistance_matrix
    message("Reciprocal complementary vertex matrix ... OK")
  } else {
    message("Reciprocal complementary vertex matrix ... FAIL")
  }
}

#' Moleculors edge reciprocal complementary matrix
#'
#' This function compute the edge version of the reciprocal
#' complementary edge matrix . It take has input the Complementary_Edistance_matrix and for each
#' element of the matrix if i != j it returns the reciprocal value of
#' the distance matrix
#'
#'
#' @return Edge reciprocal complementary matrix for the loaded molecule. Matrix is stored in Mol_mat environment.
#'
#' @examples
#' Rec_Complementary_Edistance()
#'
#' @export
#'


Rec_Complementary_Edistance = function(){

  if (is.matrix(Mol_mat$graph_ECOMPdistance_matrix)) {
    graph_ERecCOMPdistance_matrix = matrix(nrow = nrow(Mol_mat$graph_ECOMPdistance_matrix), ncol = nrow(Mol_mat$graph_ECOMPdistance_matrix))
    for (i in 1:nrow(Mol_mat$graph_ECOMPdistance_matrix)) {
      for (j in 1:nrow(Mol_mat$graph_ECOMPdistance_matrix)) {
        if (i != j) {
          graph_ERecCOMPdistance_matrix[i,j] = 1 /Mol_mat$graph_ECOMPdistance_matrix[i,j]
        } else {
          graph_ERecCOMPdistance_matrix[i,j] = 0
        }
      }
    }

    Mol_mat$graph_ERecCOMPdistance_matrix = graph_ERecCOMPdistance_matrix
    message("Reciprocal complementary edge matrix ... OK")
  } else {
    message("Reciprocal complementary edge matrix ... FAIL")
  }
}

#' Moleculors vertex distance path matrix
#'
#' This function compute the vertex version of the distance-path-matrix
#' It take has input the Vdistance_matrix and for each
#' element of the matrix if i != j it returns elemij*'elemij + 1'/2 value of
#' the distance matrix
#'
#'
#'
#' @return Vertex distance path matrix for the loaded molecule. Matrix is stored in Mol_mat environment.
#'
#' @examples
#' Distance_path_Vdistance()
#'
#' @export
#'


Distance_path_Vdistance = function(){

  if (is.matrix(Mol_mat$graph_Vdistance_matrix)) {
    graph_Vpathdistance_matrix = matrix(nrow = nrow(Mol_mat$graph_Vdistance_matrix), ncol = nrow(Mol_mat$graph_Vdistance_matrix))
    for (i in 1:nrow(Mol_mat$graph_Vdistance_matrix)) {
      for (j in 1:nrow(Mol_mat$graph_Vdistance_matrix)) {
        if (i != j) {
          graph_Vpathdistance_matrix[i,j] = Mol_mat$graph_Vdistance_matrix[i,j]*((Mol_mat$graph_Vdistance_matrix[i,j] + 1)/2)
        } else {
          graph_Vpathdistance_matrix[i,j] = 0
        }
      }
    }

    Mol_mat$graph_Vpathdistance_matrix = graph_Vpathdistance_matrix
    message("Distance path vertex matrix ... OK")
  } else {
    message("Distance path vertex matrix ... FAIL")
  }
}

#' Moleculors edge distance path matrix
#'
#' This function compute the edge version of the distance-path-matrix
#' It take has input the Edistance_matrix and for each
#' element of the matrix if i != j it returns elemij*'elemij + 1'/2 value of
#' the distance matrix
#'
#'
#' @return Edge distance path matrix for the loaded molecule. Matrix is stored in Mol_mat environment.
#'
#' @examples
#' Distance_path_Edistance()
#'
#' @export
#'


Distance_path_Edistance = function(){

  if (is.matrix(Mol_mat$graph_Edistance_matrix)) {
    graph_Epathdistance_matrix = matrix(nrow = nrow(Mol_mat$graph_Edistance_matrix), ncol = nrow(Mol_mat$graph_Edistance_matrix))
    for (i in 1:nrow(Mol_mat$graph_Edistance_matrix)) {
      for (j in 1:nrow(Mol_mat$graph_Edistance_matrix)) {
        if (i != j) {
          graph_Epathdistance_matrix[i,j] = Mol_mat$graph_Edistance_matrix[i,j]*((Mol_mat$graph_Edistance_matrix[i,j] + 1)/2)
        } else {
          graph_Epathdistance_matrix[i,j] = 0
        }
      }
    }

    Mol_mat$graph_Epathdistance_matrix = graph_Epathdistance_matrix
    message("Distance path vertex matrix ... OK")
  } else {
    message("Distance path vertex matrix ... FAIL")
  }
}

#' Moleculors vertex Laplacian matrix
#'
#' This function return the laplacian matrix for the vertex distance matrix
#' it take as input the Vdistance_matrix and for each element return
#' -1 if elemij = 1, sum'elemi, == 1' if i = j 'which is the angle of the vertex'
#' and 0 for every other element
#'
#'
#' @return Laplacian vertex matrix for the loaded molecule. Matrix is stored in Mol_mat environment.
#'
#' @examples
#' Laplacian_Vdistance()
#'
#' @export
#'


Laplacian_Vdistance = function(){

  if (is.matrix(Mol_mat$graph_Vdistance_matrix)) {
    graph_Vlaplacian_matrix = matrix(nrow = nrow(Mol_mat$graph_Vdistance_matrix), ncol = nrow(Mol_mat$graph_Vdistance_matrix))
    for (i in 1:nrow(Mol_mat$graph_Vdistance_matrix)) {
      for (j in 1:nrow(Mol_mat$graph_Vdistance_matrix)) {
        if (Mol_mat$graph_Vdistance_matrix[i,j] == 1) {
          graph_Vlaplacian_matrix[i,j] = - 1
        } else if (i == j){
          graph_Vlaplacian_matrix[i,j] = sum(Mol_mat$graph_Vdistance_matrix[i,] == 1)
        } else {
          graph_Vlaplacian_matrix[i,j] = 0
        }
      }
    }

    Mol_mat$graph_Vlaplacian_matrix = graph_Vlaplacian_matrix
    message("Laplacian vertex matrix ... OK")
  } else {
    message("Laplacian vertex matrix ... FAIL")
  }
}

#' Moleculors edge Laplacian matrix
#'
#' This function return the laplacian matrix for the edge distance matrix
#' it take as input the Edistance_matrix and for each element return
#' -1 if elemij = 1, sum'elemi, == 1' if i = j 'which is the angle of the edge'
#' and 0 for every other element
#'
#'
#' @return Laplacian edge matrix for the loaded molecule. Matrix is stored in Mol_mat environment.
#'
#' @examples
#' Laplacian_Edistance()
#'
#' @export
#'


Laplacian_Edistance = function(){

  if (is.matrix(Mol_mat$graph_Edistance_matrix)) {
    graph_Elaplacian_matrix = matrix(nrow = nrow(Mol_mat$graph_Edistance_matrix), ncol = nrow(Mol_mat$graph_Edistance_matrix))
    for (i in 1:nrow(Mol_mat$graph_Edistance_matrix)) {
      for (j in 1:nrow(Mol_mat$graph_Edistance_matrix)) {
        if (Mol_mat$graph_Edistance_matrix[i,j] == 1) {
          graph_Elaplacian_matrix[i,j] = - 1
        } else if (i == j){
          graph_Elaplacian_matrix[i,j] = sum(Mol_mat$graph_Edistance_matrix[i,] == 1)
        } else {
          graph_Elaplacian_matrix[i,j] = 0
        }
      }
    }

    Mol_mat$graph_Elaplacian_matrix = graph_Elaplacian_matrix
    message("Laplacian edge matrix ... OK")
  } else {
    message("Laplacian edge matrix ... FAIL")
  }
}

#' Moleculors vertex kneighbour matrix
#'
#' This function take as input the Vdistance matrix and an interger < then
#' nrow'vdistance' and return the adjmatrix for the kn neighbour
#'
#'
#' @param n integer for kn neighbour
#'
#' @return K neighbour vertex matrix for the loaded molecule. Matrix is stored in Mol_mat environment.
#'
#' @examples
#' Vadj_kneigh_matrix()
#'
#' @export
#'


Vadj_kneigh_matrix = function(n) {

  if (is.matrix(Mol_mat$graph_Vdistance_matrix) & n < nrow(Mol_mat$graph_Vdistance_matrix) & n%%1 == 0){
    graph_Vadj_kneigh_matrix = matrix(nrow = nrow(Mol_mat$graph_Vdistance_matrix), ncol = nrow(Mol_mat$graph_Vdistance_matrix))


    for (i in 1:nrow(Mol_mat$graph_Vdistance_matrix)) {
      for (j in 1:nrow(Mol_mat$graph_Vdistance_matrix)) {
        if (i != j & Mol_mat$graph_Vdistance_matrix[i,j] == n){
          graph_Vadj_kneigh_matrix[i,j] = 1
        } else {
          graph_Vadj_kneigh_matrix[i,j] = 0
        }
      }
    }

    Mol_mat$graph_Vadj_kneigh_matrix = graph_Vadj_kneigh_matrix

    message(paste('Vadj_kneigh_matrix computed for n = ', n, sep = ''))

  } else {
    message("no adjacency K neighbour matrix was computed")
  }
}

#' Moleculors vertex highpower matrix
#'
#' This function takes as input the adjacency matrix and an integer n and
#' return the n power of the adjacency matrix.
#'
#'
#' @param n integer for matrix power
#'
#' @return nth power vertex matrix for the loaded molecule. Matrix is stored in Mol_mat environment.
#'
#' @examples
#' Vadj_highpower_matrix()
#'
#' @export
#'



Vadj_highpower_matrix = function(n){

  if (is.matrix(Mol_mat$graph_Vadj_matrix) & n%%1 == 0){

    graph_Vadj_highpower_matrix = as.matrix(Mol_mat$graph_Vdistance_matrix)

    for (i in 2:n) {

      graph_Vadj_highpower_matrix = graph_Vadj_highpower_matrix %*% as.matrix(Mol_mat$graph_Vdistance_matrix)
    }

    Mol_mat$graph_vadj_highpower_matrix = graph_Vadj_highpower_matrix

    message(paste('Vadj_highpower_matrix computed for n = ', n, sep =''))

  } else {
    message("no adj highpower matrix was computed")
  }
}



#' Moleculors vertex adjacency matrix for the whole input
#'
#' This function calculate the V adjacency matrix using as Input
#' cartesian matrix. In order to compute the distances it starts by calculating the
#' magnitude of the vector from each atom to one another. Then after rounding 'too many digits
#' may lead to bad calculation in the following steps' it choose the smallest value 'different
#' from 0' has the 1 distance value. A loop is used to assure that every value of lenght 1 has
#' the same value for the normalization step following later.
#' a normalization is the computed and  every other value different from 1 is set to 0
#'
#'
#' @param full_input Cartesian coordinates of the molecule
#'
#' @return Vertex adjacency matrix for the loaded molecule. Matrix is stored in Mol_mat environment.
#'
#' @examples
#' Vadj_matrix(Mol_mat$Input)
#'
#' @export
#'

Vadj_matrix_full = function(full_input){

  graph_Vadj_matrix_full = matrix(nrow = nrow(full_input), ncol = nrow(full_input))

  for (i in 1:nrow(graph_Vadj_matrix_full)) {

    for (j in 1:ncol(graph_Vadj_matrix_full)) {

      graph_Vadj_matrix_full[i,j] = sqrt((full_input$X[j] - full_input$X[i])^2 +
                                      (full_input$Y[j] - full_input$Y[i])^2 +
                                      (full_input$Z[j] - full_input$Z[i])^2)

    }
  }

  graph_Vadj_matrix_full = apply(graph_Vadj_matrix_full, 2, round, 2)


  for (i in 1:nrow(graph_Vadj_matrix_full)) {
    for (j in 1:nrow(graph_Vadj_matrix_full)) {
      if ((graph_Vadj_matrix_full[i,j] - min(graph_Vadj_matrix_full[i,-i])) <= 0.58 & i != j & j > i) {
        graph_Vadj_matrix_full[i,j] = min(graph_Vadj_matrix_full[i,-i])
      }
    }
  }

  for (i in 1:nrow(graph_Vadj_matrix_full)) {
    divider = min(graph_Vadj_matrix_full[i,-i])
    for (j in 1:nrow(graph_Vadj_matrix_full)) {
      if (i != j & j > i) {
        graph_Vadj_matrix_full[i,j] = graph_Vadj_matrix_full[i,j]/divider
      }
    }
  }


  for (i in 1:nrow(graph_Vadj_matrix_full)) {
    for (j in 1:nrow(graph_Vadj_matrix_full)) {
      if (graph_Vadj_matrix_full[i,j] != 1) {
        graph_Vadj_matrix_full[i,j] = 0
      }
      graph_Vadj_matrix_full[j,i] = graph_Vadj_matrix_full[i,j]
    }
  }

  Mol_mat$graph_Vadj_matrix_full = graph_Vadj_matrix_full

  message("Vertex full adjacency matrix ... OK")
}


#' Moleculors edge adjacency matrix full
#'
#' This function require the cartesian matrix
#' and the adjacency matrix for the vertexes in order to compute
#' the edge cartesian matrix and the adjacency edges matrices.
#'
#'
#' @param Cart_Input Cartesian coordinates of the molecule without the hydrogen atoms
#'
#' @return Edge adjacency matrix for the loaded molecule. Matrix is stored in Mol_mat environment.
#'
#' @examples
#' Eadj_matrix_full(input)
#'
#' @export
#'


Eadj_matrix_full = function(Cart_Input){

  if (is.matrix(Mol_mat$graph_Vadj_matrix_full)) {
    edge_matrix = matrix(nrow = nrow(Cart_Input)*3,
                         ncol = (ncol(Cart_Input)-1))
    h = 1
    counter = 1

    for (i in 1:nrow(Mol_mat$graph_Vadj_matrix_full)) {
      for (j in h:nrow(Mol_mat$graph_Vadj_matrix_full)) {
        if (Mol_mat$graph_Vadj_matrix_full[i,j] == 1) {
          edge_matrix[counter, 1] = (Cart_Input$X[i] + Cart_Input$X[j])/2
          edge_matrix[counter, 2] = (Cart_Input$Y[i] + Cart_Input$Y[j])/2
          edge_matrix[counter, 3] = (Cart_Input$Z[i] + Cart_Input$Z[j])/2
          counter = counter + 1
        }
      }
      h = h + 1
    }

    graph_Eadj_matrix = matrix(nrow = nrow(edge_matrix), ncol = nrow(edge_matrix))


    for (i in 1:nrow(graph_Eadj_matrix)) {
      for (j in 1:ncol(graph_Eadj_matrix)) {
        graph_Eadj_matrix[i,j] = sqrt((edge_matrix[j,1] - edge_matrix[i,1])^2 +
                                        (edge_matrix[j,2] - edge_matrix[i,2])^2 +
                                        (edge_matrix[j,3] - edge_matrix[i,3])^2)
      }
    }

    NA_vector = c()

    for (j in 1:ncol(graph_Eadj_matrix)) {
      if (is.na(graph_Eadj_matrix[1,j]) & !(j %in% NA_vector)) {
        NA_vector = append(NA_vector, j)
      }
    }

    if (!(is.null(NA_vector))) {
      graph_Eadj_matrix = graph_Eadj_matrix[-NA_vector, -NA_vector]
    }


    graph_Eadj_matrix = apply(graph_Eadj_matrix, 2, round, 2)

    for (i in 1:nrow(graph_Eadj_matrix)) {
      for (j in 1:nrow(graph_Eadj_matrix)) {
        if ((graph_Eadj_matrix[i,j] - min(graph_Eadj_matrix[i,-i])) <= 0.58 & i != j & j > i) {
          graph_Eadj_matrix[i,j] = min(graph_Eadj_matrix[i,-i])
        }
      }
    }

    for (i in 1:nrow(graph_Eadj_matrix)) {
      divider = min(graph_Eadj_matrix[i,-i])
      for (j in 1:nrow(graph_Eadj_matrix)) {
        if (i != j & j > i) {
          graph_Eadj_matrix[i,j] = graph_Eadj_matrix[i,j]/divider
        }
      }
    }


    for (i in 1:nrow(graph_Eadj_matrix)) {
      for (j in 1:nrow(graph_Eadj_matrix)) {
        if (graph_Eadj_matrix[i,j] != 1) {
          graph_Eadj_matrix[i,j] = 0
        }
        graph_Eadj_matrix[j,i] = graph_Eadj_matrix[i,j]
      }
    }

    Mol_mat$graph_Eadj_matrix_full = graph_Eadj_matrix

    message("Full Edge adjacency matrix ... OK")
  } else {
    message("Full Edge adjacency matrix ... FAIL")
  }

}

#' Moleculors full vertex Laplacian matrix
#'
#' This function return the laplacian matrix for the vertex distance matrix
#' it take as input the Vadj_full_matrix and for each element return
#' -1 if elemij = 1, sum'elemi, == 1' if i = j 'which is the angle of the vertex'
#' and 0 for every other element
#'
#'
#' @return Laplacian vertex full matrix for the loaded molecule. Matrix is stored in Mol_mat environment.
#'
#' @examples
#' Laplacian_Vdistance_full()
#'
#' @export
#'


Laplacian_Vdistance_full = function(){

  if (is.matrix(Mol_mat$graph_Vadj_matrix_full)) {
    graph_Vlaplacian_matrix = matrix(nrow = nrow(Mol_mat$graph_Vadj_matrix_full), ncol = nrow(Mol_mat$graph_Vadj_matrix_full))
    for (i in 1:nrow(Mol_mat$graph_Vadj_matrix_full)) {
      for (j in 1:nrow(Mol_mat$graph_Vadj_matrix_full)) {
        if (Mol_mat$graph_Vadj_matrix_full[i,j] == 1) {
          graph_Vlaplacian_matrix[i,j] = - 1
        } else if (i == j){
          graph_Vlaplacian_matrix[i,j] = sum(Mol_mat$graph_Vadj_matrix_full[i,] == 1)
        } else {
          graph_Vlaplacian_matrix[i,j] = 0
        }
      }
    }

    Mol_mat$graph_Vlaplacian_full_matrix = graph_Vlaplacian_matrix
    message("Laplacian vertex full matrix ... OK")
  } else {
    message("Laplacian vertex full matrix ... FAIL")
  }
}


#' Moleculors sparse csi vertex matrix
#'
#' This function return the sparse csi vertex matrix for the selected molecule
#' it take as input the Vadj_matrix, the laplacian_Vertex matrix and the Vdistance matrix
#'  and for each element return wij = "di x dj"^-1/2 where di is the vertex degree
#'  of the element i. By definition wij with i=j will be 0
#'
#'
#' @return sparse Csi vertex matrix for the loaded molecule. Matrix is stored in Mol_mat environment.
#'
#' @examples
#' Sparse_csi_V_matrix()
#'
#' @export
#'


Sparse_csi_V_matrix <- function(){

  if (is.matrix(Mol_mat$graph_Vadj_matrix) & is.matrix(Mol_mat$graph_Vlaplacian_matrix) & is.matrix(Mol_mat$graph_Vdistance_matrix)) {
    graph_Vsparsecsi_matrix = matrix(nrow = nrow(Mol_mat$graph_Vadj_matrix), ncol = nrow(Mol_mat$graph_Vadj_matrix))


    for (i in 1:nrow(Mol_mat$graph_Vadj_matrix)) {
      for (j in 1:nrow(Mol_mat$graph_Vadj_matrix)) {
        if (i == j) {
          graph_Vsparsecsi_matrix[i,j] = 0
        } else {

          graph_Vsparsecsi_matrix[i,j] = 1/sqrt(Mol_mat$graph_Vlaplacian_matrix[i,i]*Mol_mat$graph_Vlaplacian_matrix[j,j])
        }
      }
    }

    Mol_mat$graph_Vsparsecsi_matrix = graph_Vsparsecsi_matrix
    message("sparse Csi vertex matrix ... OK")
  } else {
    message("sparse Csi vertex matrix ... FAIL")
  }
}


#' Moleculors extended adjacency matrix from vertex degrees
#'
#' This function return the extended adjacency matrix taking into account the
#' effect of vertexes degrees.
#' it take as input the Vadj_matrix, the laplacian_Vertex matrix
#' and for each element return a'ij = ai * 'di/dj + dj/di'/2 where di is the vertex degree
#' of the element i. By definition a'ij with i=j will be 0
#'
#'
#' @return extended adjacency matrix from vertex degree for the loaded molecule. Matrix is stored in Mol_mat environment.
#'
#' @examples
#' Extended_Vadj_matrix_Vdegrees()
#'
#' @export
#'

Extended_Vadj_matrix_Vdegrees <- function(){
  if (is.matrix(Mol_mat$graph_Vadj_matrix) & is.matrix(Mol_mat$graph_Vlaplacian_matrix)){

    Extended_Vadj_degree <- Mol_mat$graph_Vadj_matrix

    for (i in 1:nrow(Extended_Vadj_degree)) {
      for (j in 1:nrow(Extended_Vadj_degree)) {
        Extended_Vadj_degree[i,j] = Mol_mat$graph_Vadj_matrix[i,j]*((Mol_mat$graph_Vlaplacian_matrix[i,i]/Mol_mat$graph_Vlaplacian_matrix[j,j]) +
                                                                      (Mol_mat$graph_Vlaplacian_matrix[j,j]/Mol_mat$graph_Vlaplacian_matrix[i,i]))/2
      }
    }

    Mol_mat$graph_Extended_Vadj_degree_matrix = Extended_Vadj_degree
    message("Extended Vadj vertex degree matrix ... OK")
  } else {
    message("Extended Vadj vertex degree matrix ... FAIL")
  }
}



#' Moleculors extended adjacency matrix from vertex electronegativity
#'
#' This function return the extended adjacency matrix taking into account the
#' effect of vertexes electronegativity.
#' it take as input the Vadj_matrix, the laplacian_Vertex matrix
#' and for each element return a'ij = ai * 'di''/dj'' + dj''/di''/2 where di'' is the vertex degree
#' of the element i multiplied by its electronegativity elni. By definition a'ij with i=j will be elni
#'
#'
#' @return extended adjacency matrix from vertex electronegativity for the loaded molecule. Matrix is stored in Mol_mat environment.
#'
#' @examples
#' Extended_Vadj_matrix_Veln()
#'
#' @export
#'

Extended_Vadj_matrix_Veln <- function(){
  if (is.matrix(Mol_mat$graph_Vadj_matrix) & is.matrix(Mol_mat$graph_Vlaplacian_matrix) & is.data.frame(Mol_mat$input_H_suppressed)){

    Extended_Vadj_eln <- Mol_mat$graph_Vadj_matrix
    eln_table <- read.csv("tables/electronegativity_table.csv", sep = ";")

    for (i in 1:nrow(Extended_Vadj_eln)) {
      for (j in 1:nrow(Extended_Vadj_eln)) {
        if (i == j) {
          Extended_Vadj_eln[i,j] = eln_table$Eln[which(eln_table$Atom == as.character(Mol_mat$input_H_suppressed$Atom[[i]]))]
        } else {
          Extended_Vadj_eln[i,j] = Mol_mat$graph_Vadj_matrix[i,j]*(((Mol_mat$graph_Vlaplacian_matrix[i,i]*eln_table$Eln[which(eln_table$Atom == as.character(Mol_mat$input_H_suppressed$Atom[[i]]))])/(Mol_mat$graph_Vlaplacian_matrix[j,j]*eln_table$Eln[which(eln_table$Atom == as.character(Mol_mat$input_H_suppressed$Atom[[j]]))])) +
                                                                        ((Mol_mat$graph_Vlaplacian_matrix[j,j]*eln_table$Eln[which(eln_table$Atom == as.character(Mol_mat$input_H_suppressed$Atom[[j]]))])/(Mol_mat$graph_Vlaplacian_matrix[i,i]*eln_table$Eln[which(eln_table$Atom == as.character(Mol_mat$input_H_suppressed$Atom[[i]]))])))/2
        }
      }
    }

    Mol_mat$graph_Extended_Vadj_eln_matrix = Extended_Vadj_eln
    message("Extended Vadj vertex electronegativity matrix ... OK")
  } else {
    message("Extended Vadj vertex electronegativity matrix ... FAIL")
  }
}


#' Moleculors Vdistance/distance matrix
#'
#' This function return the Vdistance/distance matrix for the selected molecule
#' it takes as input the Vdistance matrix, compute the geometrical distance matrix for each element ij
#' and for each element return ddij = geomij/distanceij
#'
#'
#' @return distance/distance matrix for the loaded molecule. Matrix is stored in Mol_mat environment.
#'
#' @examples
#' Vdistance_distance_matrix()
#'
#' @export
#'

Vdistance_distance_matrix <- function(){
  if (is.matrix(Mol_mat$graph_Vdistance_matrix) & is.data.frame(Mol_mat$input_H_suppressed)){

    Vdistance_distance = Mol_mat$graph_Vdistance_matrix

    geom_matrix = matrix(nrow = nrow(Mol_mat$input_H_suppressed), ncol = nrow(Mol_mat$input_H_suppressed))

    for (i in 1:nrow(geom_matrix)) {

      for (j in 1:ncol(geom_matrix)) {

        geom_matrix[i,j] = sqrt((Mol_mat$input_H_suppressed$X[j] - Mol_mat$input_H_suppressed$X[i])^2 +
                                  (Mol_mat$input_H_suppressed$Y[j] - Mol_mat$input_H_suppressed$Y[i])^2 +
                                  (Mol_mat$input_H_suppressed$Z[j] - Mol_mat$input_H_suppressed$Z[i])^2)

      }
    }


    for (i in 1:nrow(Vdistance_distance)) {
      for (j in 1:nrow(Vdistance_distance)) {
        if (i != j) {
          Vdistance_distance[i,j] = geom_matrix[i,j]/Mol_mat$graph_Vdistance_matrix[i,j]
        }
      }
    }

    Mol_mat$graph_Vdistance_distance_matrix = Vdistance_distance
    message("Vertex distance/distance matrix ... OK")
  } else {
    message("Vertex distance/distance matrix matrix ... FAIL")
  }
}
FedericoViv/Moleculors documentation built on Jan. 17, 2022, 12:23 a.m.