"probe2EG" <-
function(probe.mat,entrez.used,entrez.list)

# This code converts a matrix of microarray data whose rows are
# probeIDs to a matrix whose rows are entrez gene ids.  Duplicate
# entrez gene ids are averaged.

# Created by daigle 5/5/08

{
  entrez.func <- function(probe){entrez <- eval(parse(text=paste("entrez.list$\"",probe,"\"",sep=""))); ifelse(is.null(entrez),NA,entrez)}

  probe.names <- rownames(probe.mat)
  entrez.ids <- as.character(unlist(sapply(probe.names, function(probe) entrez.func(probe))))
  probe.mat <- probe.mat[!is.na(entrez.ids),]
  entrez.ids <- entrez.ids[!is.na(entrez.ids)]

  to.remove <- c()
  for (id in 1:length(entrez.ids)) {
    if (id %in% to.remove) next
    which <- which(entrez.ids == entrez.ids[id])
    if (length(which) > 1) {
        probe.mat[which[1],] <- colMeans(probe.mat[which,], na.rm = TRUE)
        to.remove <- c(to.remove, which[2:length(which)])
    }
  }
  if (length(to.remove)) {
    probe.mat <- probe.mat[-to.remove,]
    entrez.ids <- entrez.ids[-to.remove]
  }

  eg.mat <- matrix(as.numeric(NA), nrow = length(entrez.used), ncol = ncol(probe.mat))
  rownames(eg.mat) <- entrez.used
  colnames(eg.mat) <- colnames(probe.mat)
  eg.mat[entrez.ids[entrez.ids %in% entrez.used],] <- probe.mat[entrez.ids %in% entrez.used,]

  return(eg.mat)
}