data.norm <-
function(datalist, std.outliers = 3, zero.missing = TRUE, min.corr = .75, max.rt = 3, mode = "", mz.tol = 1e-3, adducts = c(), fplot = TRUE){

##########
## INIT ##
##########
datalist$names <- colnames(datalist$data[3:dim(datalist$data)[2]])
mz        <- as.numeric(as.matrix(datalist$data[,1]))
rt        <- as.numeric(as.matrix(datalist$data[,2]))
Nfeature  <- length(rt)
Nsamples  <- ncol(datalist$data)-2
data      <- as.numeric(as.matrix(datalist$data[,3:ncol(datalist$data)]))
nans      <- which(is.nan(data))
data[nans]<- 0
data      <- matrix(data,Nfeature,Nsamples)
for(i in 1:Nfeature) {
   for(j in 1:Nsamples) data[i,j] <- as.numeric(data[i,j])
}
index     <- order(mz)
mz        <- mz[index]
rt        <- rt[index]
data      <- data[index,]
cat("DATASET:\n", sep = "")
cat("","- #Features: ",Nfeature,"\n", sep = "\t")
cat("","- #Samples:  ",Nsamples,"\n", sep = "\t")
cat("","- RT limits: ",paste("[",round(min(rt),2),",",round(max(rt),2),"]"),"\n", sep = "\t")
cat("","- M/Z limits:",paste("[",round(min(mz),2),",",round(max(mz),2),"]"),"\n", sep = "\t")
##############################################
## COUNTS NORMALIZATION & OUTLIER DETECTION ##
##############################################
out    <- norm.counts(datalist$names, data, mz, rt, std.outliers, zero.missing, min.corr, max.rt, mode = mode, mz.tol = mz.tol, adducts = adducts, fplot)
data   <- data[,out$inp]
colnames(out$corrM) <- c("Index feature 1","Index feature 2","Correlation","%samples","RTdiff","M/Zdiff")
colnames(data)      <- datalist$names[out$inp]
cat("FEATURE PAIRS SELECTION RESULTS:\n", sep = "")
cat("","- MAX RT DIFFERENCE ALLOWED:",max.rt,"\n", sep = "\t")
cat("","- MIN CORRELATION ALLOWED:",min.corr,"\n", sep = "\t")
cat("","- FEATURE PAIRS SELECTED:",dim(out$corrM)[1],"\n", sep = "\t")
if (mode=="") cat("","- M/Z-DIFF PRE-FILTERING: OFF\n", sep = "\t")
if (mode!="") cat("","- M/Z-DIFF PRE-FILTERING: ON\n", sep = "\t")

return(list(data=data ,class=datalist$class, mz=mz, rt=rt, feature.set=out$corrM, outliers=out$out, scores=out$scores))
}
