#prepare target file
#load library
library(xlsx)

#load data
#setwd("D:/Janosch Hildebrand D/Arrays Q10 Studie/Single-experiment raw data")
#t1 <- read.xlsx("Alt_vs_Jung.xlsx", sheetName = "Blatt5")
#t2 <- t1[order(t1$Array_Nr),]

#f1 <- list.files(path = "W:/users/Andreas/MATLAB/Skin Ageing/131213_Report_Winnefeld_Soehle_A4758/Single-experiment raw data/Single-experiment raw data") 
#f2.Array <- substr(f1,1,12) 
#f2.Position <- substr(f1,31,34) 
#f2.Array_Nr <- paste(f2.Array, f2.Position, sep = "")

#t2$FileName <- f1
#t2$Array_Nr_new <- f2.Array_Nr

#write.table(t2, file = "targets_test.txt", sep ="\t")

#target.file <- t2[,c(5,8,4,3)]
#colnames(target.file) <- c("SlideNumber","FileName","Condition","Age")

#setwd("~/Documents/Arrays/Array age 2/Single-experiment raw data/Single-experiment raw data")
#write.table(target.file, file = "targets_Array_Age_2.txt", sep ="\t")

#load library
library(limma)

#load array data sets
setwd("W:/users/Andreas/MATLAB/Skin Ageing/GeneExpression/Single-experiment raw data")
targets <- readTargets("targets_Array_Age_2.txt")
da <- read.maimages(files = targets, source = "agilent", green.only = TRUE, 
                    other.columns = list(pas = "gIsPosAndSignif", ab = "gIsWellAboveBG", sat = "gIsSaturated", con="ControlType"))

#copy of loaded original dataset da
da2 <- da

#set filters on copied dataset da2
da2$other$isSignif <- round(rowMeans(da2$other$gIsPosAndSignif)+1,  digits = 1)-1
da2$other$isAboveBG <- round(rowMeans(da2$other$gIsWellAboveBG)+1,  digits = 1)-1
da2$other$isSaturated <- round(rowMeans(da2$other$gIsSaturated)+1,  digits = 1)-1
da2$other$isControl <- rowSums(da2$other$ControlType)
filter <- da2$other$isSignif>0.5&da2$other$isAboveBG>0.5&da2$other$isSaturated<0.5&da2$other$isControl==0

#copy of loaded original dataset da2
da3 <- da2

#background correction and normalization
da3 <- backgroundCorrect(da3, method = "normexp", offset = 16)
da4 <- normalizeBetweenArrays(da3, method = "quantile")
Normplot <- plotDensities(list(R = da3$E, G = da4$E))

#filter genes (other muss NULL sein, sonst kann nicht gefiltert werden)
da5 <- da4
da5$other <- NULL
da6 <- da5[filter,]

#average of probes spotted
da7 <- avereps(da6,ID = da6$genes$ProbeName)

# #log2 transform
# da8 <- da7
# da8$E <- log2(da7$E)

#Create design matrix and fit linear models
Condition <- factor(targets$Condition)
design <- model.matrix (~0+Condition)
da7fit <- lmFit(da7, design)
cont.matrix <- makeContrasts(AvsJ = Conditionalt-Conditionjung, levels = design)
da7cfit <- contrasts.fit(da7fit, cont.matrix)
da7cfit <- eBayes(da7cfit)

#Output
output2.unsorted <- topTable(da7cfit, adjust = "BH", genelist = da7$genes$ProbeName, number = Inf, sort.by = "none")
output2.unsorted.nam <- topTable(da7cfit, adjust = "BH", genelist = da7$genes$GeneName, number = Inf, sort.by = "none")

#Names included
GeneName <- as.data.frame(output2.unsorted.nam$ID)
output2.unsorted.names <- cbind(output2.unsorted,GeneName)

#Output ordered
output.fin <- output2.unsorted.nam
s[order(output2.unsorted.names$P.Value),]
colnames(output.fin) <- c(names(output.fin[-8]),"GeneName")

#write tables
write.table(output.fin, file = "SBEQ10_AvsJ_sorted_AK.txt",sep ="\t")

#Generation of MegaTable, including intensities
output3.unsorted.names <- output2.unsorted.names
colnames(output3.unsorted.names) <- c(names(output3.unsorted.names[-8]),"GeneName")
intensities <- as.data.frame(da7$E)
output.incl.int <- cbind(output3.unsorted.names,intensities)
output.incl.int <- output.incl.int[order(output.incl.int$P.Value),]

#write tables
write.table(output.incl.int, file = "SBEQ10_AvsJ_sorted_incl_intensities_AK.txt",sep ="\t")

