
# make sure that THERE IS ONLY ONE .csv FILE IN DIRECTORY!
files <- list.files(path = getwd(), pattern = ".csv", all.files = FALSE,
                     full.names = FALSE, recursive = FALSE, ignore.case = TRUE,
                     include.dirs = FALSE)
## import dataset
quad1 <- read.table(files[1], sep=",", header=TRUE)
quad2 <- data.frame(lapply(quad1, as.character), stringsAsFactors=FALSE, header=FALSE)
# keep only values with non-zero expression
quad <- quad2[with(quad2, agg.exp.out != 0), ]

# calculate correlation coefficients for bins of size 0.25-5% of maximal value for that bin
x.vec <- c(21)
y.vec <- seq(from=34, to=255, by=1)
pseudocounts <- seq(from=0.25, to=5, by=0.25)
corr.list <- list()
for(j in pseudocounts){
  p.out <- NULL
  for(i in 1:length(y.vec)){
    ds1 <- quad[with(quad, is.na(quad[,x.vec]) == FALSE & is.na(quad[,y.vec[i]]) == FALSE), ] 
    # X is log2 transformation of TSS-specific transcription
    X <- log2(as.numeric(ds1$agg.exp.out))
    max.val <- max(as.numeric(ds1[,y.vec[i]]))
    # Y is the variable to test for correlation
    Y <- log2(as.numeric(ds1[,y.vec[i]])+j*max.val/100)
    p <- cor.test(X, Y,
                  alternative = c("two.sided", "less", "greater"),
                  method = c("pearson", "kendall", "spearman"),
                  exact = NULL, conf.level = 0.95, continuity = FALSE)
    p.out <- c(p.out, as.vector(p$estimate))
    print(i)
  }
  corr.list[[j*4]] <- p.out
}
plot(y.vec, corr.list[[1]])
# find maximal values for each series in each list element, make sure these are correct for each dataset!
starts <- c(3,23,43,63,83,103,123,143,163,183,203)
# repeat for each of the 20 pseudocount values
for(i in 1:20){
  print("enter pseudocount loop")
  # repeat for each of the histone modifications
  max.out <- NULL
  max.loc.out <- NULL
  for(j in starts){
    print("enter histone modification loop")
    max <- 0
    max.loc <- NULL
    # loop over a single dataset to find maximum value
    for(k in 0:19){
      print("enter find max loop")
      point <- abs(corr.list[[i]][j+k])
      if(point > max(max)){
        max <- point
        max.loc <- j+k
      }
    }
    max.out <- c(max.out, max)
    max.loc.out <- c(max.loc.out, max.loc)
  }
  if(i == 1){
    max.out.df <- as.data.frame(max.out)
    max.loc.out.df <- as.data.frame(max.loc.out)
  }
  if(i > 1){
    max.out.df <- cbind(max.out.df, as.data.frame(max.out))
    max.loc.out.df <- cbind(max.loc.out.df, as.data.frame(max.loc.out))
  }
  print(i)
}
# max.out.df- across rows are corr values for increasing pseudocount sizes
# down the columns are histone modifications
# next step is to find the column number for the maximal value for each feature (%max of the added pseudocount)
# this column and row location in max.loc.out.df contains the coordinate of this feature within the 

# repeat across each of the 20 columns representing different sizes of pseudocount and across the 11 rows
max.loc.out <- NULL
max.out <- NULL
for(i in 1:11){
  max <- 0
  max.loc <- NULL
  for(j in 1:20){
    point <- max.out.df[i,j]
    if(point > max){
      max <- point
      max.loc <- j
    }
  }
  max.out <- c(max.out, max)
  max.loc.out <- c(max.loc.out, max.loc)
}
pseudocounts2 <- pseudocounts[max.loc.out]

# bestbin coordinated in "quad" dataset
extract.list <- NULL
for(i in 1:11){
  print(max.loc.out.df[i,max.loc.out[i]]+33)
  extract.list <- c(extract.list, max.loc.out.df[i,max.loc.out[i]]+33)
}

####################################################################################
# now use the bestbins and pseudocounts calculated up top
adj.pseudocounts <- NULL
# calculate absolute pseudocounts to add
for(i in 1:11){
  ds2.1 <- quad[with(quad, is.na(quad[,extract.list[i]]) == FALSE), ] 
  ds2 <- as.numeric(ds2.1[,extract.list[i]])
  adj.pseudocounts <- c(adj.pseudocounts, pseudocounts2[i]*as.numeric(max(ds2))/100)
  print(i)
}
# extract "quad" subset with no "NAs" listed in columns specified by extract.list
extract1 <- quad[with(quad, is.na(quad[,extract.list[1]]) == FALSE & is.na(quad[,extract.list[2]]) == FALSE &
                        is.na(quad[,extract.list[3]]) == FALSE & is.na(quad[,extract.list[4]]) == FALSE &
                        is.na(quad[,extract.list[5]]) == FALSE & is.na(quad[,extract.list[6]]) == FALSE &
                        is.na(quad[,extract.list[7]]) == FALSE & is.na(quad[,extract.list[8]]) == FALSE &
                        is.na(quad[,extract.list[9]]) == FALSE & is.na(quad[,extract.list[10]]) == FALSE &
                        is.na(quad[,extract.list[11]]) == FALSE), ]
# create log2 transformation of expression data
exp <- as.data.frame(log2(as.numeric(extract1$agg.exp.out)))
colnames(exp) <- "log.exp"
# create log2 transformation of genomic tracks (after adding appropriate pseudocount)
for(i in 1:11){
  col <- as.numeric(extract1[,extract.list[i]])
  col.plus.pseudo <- col+adj.pseudocounts[i]
  log.col <- log2(col.plus.pseudo)
  if(i == 1){
    log.track.df <- as.data.frame(log.col)
  }
  if(i > 1){
    log.track.df <- cbind(log.track.df, as.data.frame(log.col))
  }
}
colnames(log.track.df) <- c("DNAse","H2az","H3k79me2","H3k4me3","H3k27ac","H4k20me1","H3k27me3",
                            "H3k36me3","H3k4me2","H3k9ac","H3k4me1")
# plot correlation for data adjusted for log2 and pseudocount
Y <- as.numeric(as.vector(exp[,1]))
for(i in 1:11){
  X <- log.track.df[,i]
  p <- cor.test(X, Y,
                alternative = c("two.sided", "less", "greater"),
                method = c("pearson", "kendall", "spearman"),
                exact = NULL, conf.level = 0.95, continuity = FALSE)
  print(i)
  print(p$estimate)
  print(p$p.value)
  plot(X,Y, main=i)
}
# extract log2 expression and log2 and pseudocount track info into new df
extract2 <- extract1[,seq(from=1, to=34, by=1)]
ds <- cbind(extract2, exp, log.track.df)


write.table(ds, file="cell_name_expression_modeling_dataset.csv", sep=",", col.names=colnames(ds), row.names=FALSE)

