web.report <-
  function(ftlist, results, datafile, dataset.name = "", output.dir = "web_report"){
    
# FORMAT FOLDER
pathdir <- system.file(package="AStream")
dir.create(output.dir,showWarnings=FALSE)
dir.create(paste(output.dir,"/figures",sep=""),showWarnings=FALSE)
figpath <- paste(output.dir,"/figures/",sep="")
file.copy(paste(pathdir,"html",sep="/"),output.dir,recursive=T)
# LOAD DATA
f    <- file(datafile,"r")
head <- strsplit(readLines(f,1),"\t")[[1]]
r    <- readLines(f,1)
res  <- c()
while(length(r)>0){
  data <- strsplit(r,"\t")[[1]]
  res <- rbind(res,data)
  r    <- readLines(f,1)
}
close(f)
## FORMAT DATA TABLE
for (i in 1:nrow(res)){
  id <- as.numeric(res[i,1])
  res[i,1] <- paste("<td>",res[i,1],"</td>",sep="")
  plott <- 0
  for (j in 2:3){
    temp <- as.character(round(as.numeric(res[i,j]),3))
    res[i,j] <- paste('<td align="left">',temp,'</td>',sep="")
  }
  temp <- as.character(round(as.numeric(res[i,4]),1))
  res[i,4] <- paste('<td align="left">',temp,'</td>',sep="")
  for (j in 5:8){
    res[i,j] <- paste('<td align="left">',res[i,j],"</td>",sep="")
  }
  res[i,9] <- paste('<td align="left"><a href="',res[i,9],'" target="_blank">link</a>','</td>',sep="")
  if (res[i,10]!="0"){
    plott <- 1
    res[i,1] <- gsub("<td>",paste('<td><a href="figures/peak_',id,'.jpg" class="lytebox lytetip" data-tip="" data-title="PEAK NUMER" data-description="" data-lyte-options="showPrint:true tipStyle:classic">',sep=""),res[i,1])
    res[i,1] <- gsub("</td>","</a></td>",res[i,1])
    jpeg(filename=paste(figpath,"peak_",id,".jpg",sep=""),width=800,height=1000)
    g <- plotcompound(id,ftlist,results)
    dev.off()
    res[i,10] <- paste('<td align="left" bgcolor="#00FF00" class=" ">',res[i,10],'</td>',sep="")
  } else{
    res[i,10] <- paste('<td align="left" bgcolor="#FF0000" class=" ">',res[i,10],'</td>',sep="")
  }
  temp <- ""
  for (j in 11:13){
    if (res[i,j]!="NA"){
      if (j==11){
        for (kk in c("id=","rtdiff=","mzdev=","C=")) res[i,j] <- gsub(kk,"",res[i,j])
        temp=paste(temp," [13C1]",res[i,j],sep="")
      }
      if (j==12){
        for (kk in c("id=","rtdiff=","mzdev=","C=")) res[i,j] <- gsub(kk,"",res[i,j])
        temp=paste(temp," [13C2]",res[i,j],sep="")
      }
      if (j==13){
        for (kk in c("id=","rtdiff=","mzdev=","C=")) res[i,j] <- gsub(kk,"",res[i,j])
        temp <- paste(temp," ",res[i,j],sep="")
      }
      
    }
  }
  if (temp==""){
    temp="NA"
    res[i,11]<-paste('<td align="left" bgcolor="#FF0000" class=" ">',temp,"</td>",sep="")
  }else{
    temp <- gsub(" ","<br>",substring(temp,2,nchar(temp)))
    res[i,11]<-paste('<td align="left" bgcolor="#00FF00" class=" ">',temp,"</td>",sep="")
  }
  if (res[i,14]!="0"){
    res[i,14] <- paste('<td align="left" bgcolor="#00FF00" class=" ">',res[i,14],'</td>',sep="")
  } else{
    res[i,14] <- paste('<td align="left" bgcolor="#FF0000" class=" ">',res[i,14],'</td>',sep="")
  }
  if (res[i,15]!=""){
    for (kk in c("id=","rtdiff=","mzdev=","C=")) res[i,15] <- gsub(kk,"",res[i,15])
    res[i,15] <- paste('<td align="left" bgcolor="#00FF00" class=" ">',res[i,15],'</td>',sep="")
  } else{
    res[i,15] <- paste('<td align="left" bgcolor="#FF0000" class=" ">',res[i,15],'</td>',sep="")
  }
  if (res[i,16]!="0"){
    res[i,16] <- paste('<td align="left" bgcolor="#00FF00" class=" ">',res[i,16],'</td>',sep="")
  } else{
    res[i,16] <- paste('<td align="left" bgcolor="#FF0000" class=" ">',res[i,16],'</td>',sep="")
  }
  if (res[i,17]!="NA"){
    temp <- strsplit(res[i,17]," HMDB")[[1]]
    for (k in 1:length(temp)){
      if (k>1){
        temp[k] <- paste("HMDB",temp[k],sep="")
      }
      if (nchar(temp[k])>37){
        temp[k] <- substring(temp[k],1,36)
      }
      j <- strsplit(temp[k],":")[[1]]
      temp[k] <- paste('<a href="http://www.hmdb.ca/metabolites/',j[1],'" target="_blank">',j[1],'</a>:',j[2],sep="")
    }
    temp <- paste(temp,collapse="<br>")
    res[i,17] <- paste('<td align="left" bgcolor="#00FF00" class=" ">',temp,'</td>',sep="")
  } else{
    res[i,17] <- paste('<td align="left" bgcolor="#FF0000" class=" ">',"NA",'</td>',sep="")
  }
  
}

if (file.copy(paste(pathdir,"supp/header.txt",sep="/"),paste(output.dir,"/dataset.html",sep=""))==FALSE){
  file.remove(paste(output.dir,"/dataset.html",sep=""))
  file.copy(paste(pathdir,"supp/header.txt",sep="/"),paste(output.dir,"/dataset.html",sep=""))
}
f    <- file(paste(output.dir,"/dataset.html",sep=""),"a")
cat('<body>',"\n",sep="",file=f)
cat('<div id="header"><img src="./html/css/images/header.png"></div>',"\n",sep="",file=f)
cat('<div id="box1"><div id="content">',"\n",sep="",file=f)
cat("<h2>STUDY NAME: ",dataset.name," / ",strftime(Sys.time(),"%Y-%m-%d %H:%M:%S"),'</h2>',"\n",sep="",file=f)
cat("<center>","\n",sep="",file=f)
cat('<table border="1" class="display dataTable" id="focus_peak_table" style="width:80%">',"\n",sep="",file=f)


header = "<thead><tr>"
for (i in 1+c(0,1,2,3,4,5,6,7,9,10,13,14,15,16)){
  header <- paste(header,'<td>',head[i],'</td>',sep="")
  if (i==4){
    header <- paste(header,'<td bgcolor="#000000"></td>',sep="")
  }
  if (i==8){
    header <- paste(header,'<td bgcolor="#000000"></td>',sep="")
  }
  if (i==11){
    header <- paste(header,'<td bgcolor="#000000"></td>',sep="")
  }
  if (i==15){
    header <- paste(header,'<td bgcolor="#000000"></td>',sep="")
  }
}
header <- paste(gsub("NOTREF.LABELS","NOTREF.LABELS (id/rtD/mzD/C)",gsub("REF.13C#1-isotope","REF.LABELS (id/rtD/mzD/C)",header)),'</tr></thead>',sep="")
cat(header,"\n",sep="",file=f)
cat('<tbody role="alert" aria-live="polite" aria-relevant="all">',"\n",sep="",file=f)
for (i in 1:nrow(res)){
  if ((i%%2)==0){
    n <- '<tr class="even">'
  }else{
    n <- '<tr class="odd">'
  }
  for (j in 1+c(0,1,2,3,4,5,6,7,9,10,13,14,15,16)){
    n <- paste(n,res[i,j],sep="")
    if (j==4){
      n <- paste(n,'<td bgcolor="#000000"></td>',sep="")
    }
    if (j==8){
      n <- paste(n,'<td bgcolor="#000000"></td>',sep="")
    }
    if (j==11){
      n <- paste(n,'<td bgcolor="#000000"></td>',sep="")
    }
    if (j==15){
      n <- paste(n,'<td bgcolor="#000000"></td>',sep="")
    }
  }
  n <- paste(n,'</tr>',sep="")
  cat(n,"\n",sep="",file=f)
}
cat("</tbody></table></center></div></div></body></html>",sep="",file=f)
close(f)
}