SIGTrans.batch <- function(cue=list, nodes.state=time.bool, KO.node=KO, TF.TGs=edges, trans.flow="transcription-response", cores=4, common=common.genes, p19.genes=p19.specific.genes, f9.genes=f9.specific.genes) {

cl <- makeCluster(cores)
registerDoParallel(cl)

KO <- KO.node

time.bool <- nodes.state
time.bool <- time.bool[time.bool[,1]!=KO,]

edges <- TF.TGs
n <- nrow(edges)

ID <- c(1:n)
edges <- cbind(edges,ID)
# Layer1

#load TF-TGs as edges; gene expression boolean readouts as time.bool

#define signalling starting point at starter

if(trans.flow=="transcription-response") {
time.bool.active.t2 <- time.bool[time.bool[,2]!=0,]
time.bool.active.t6 <- time.bool[time.bool[,3]!=0,]
time.bool.active.t24 <- time.bool[time.bool[,4]!=0,]
time.bool.active.t48 <- time.bool[time.bool[,5]!=0,]
time.bool.active.t72 <- time.bool[time.bool[,6]!=0,]
nrow(time.bool.active.t2)
nrow(time.bool.active.t6)
nrow(time.bool.active.t24)
nrow(time.bool.active.t48)
nrow(time.bool.active.t72)

} else {

#time.bool.active.t2 <- time.bool[time.bool[,2]!=-2,]
#time.bool.active.t6 <- time.bool[time.bool[,3]!=-2,]
#time.bool.active.t24 <- time.bool[time.bool[,4]!=-2,]
#time.bool.active.t48 <- time.bool[time.bool[,5]!=-2,]
#time.bool.active.t72 <- time.bool[time.bool[,6]!=-2,]
}

start.list <- cue

start.list <- start.list[start.list[,1]!=KO,]

n <- nrow(start.list)

for(z in 1:n) {

starter <- start.list[z,1]

cat("Node under evaluation:\n")
print(starter)

#Layer 2

find.edges <- edges[edges[,1]==starter,]

edges.t2 <-find.edges[find.edges[,3]%in%time.bool.active.t2[,1],]

n <- nrow(edges.t2)
attribute <- rep("t2",n)
edges.t2 <-cbind(edges.t2, attribute)

#Layer 3

edges.fort6 <- foreach(i=1:n, .combine='rbind') %dopar% {

edges[edges[,1]==as.character(edges.t2[i,3]),] }

edges.fort6 <- rbind(find.edges, edges.fort6)


edges.t6 <-edges.fort6[edges.fort6[,3]%in%time.bool.active.t6[,1],]

n <- nrow(edges.t6)
attribute <- rep("t6",n)
edges.t6 <-cbind(edges.t6, attribute)

#Layer 4

edges.fort24 <- foreach(i=1:n, .combine='rbind') %dopar% {

edges[edges[,1]==as.character(edges.t6[i,3]),] }

n <- nrow(edges.t2)

edges.fort24.t2 <- foreach(i=1:n, .combine='rbind') %dopar% {

edges[edges[,1]==as.character(edges.t2[i,3]),] }

edges.fort24 <- rbind(find.edges, edges.fort24, edges.fort24.t2)

edges.t24 <-edges.fort24[edges.fort24[,3]%in%time.bool.active.t24[,1],]

n <- nrow(edges.t24)
attribute <- rep("t24",n)
edges.t24 <-cbind(edges.t24, attribute)

#Layer 5

edges.fort48 <- foreach(i=1:n, .combine='rbind') %dopar% {

edges[edges[,1]==as.character(edges.t24[i,3]),] }

n <- nrow(edges.t2)

edges.fort48.t2 <- foreach(i=1:n, .combine='rbind') %dopar% {

edges[edges[,1]==as.character(edges.t2[i,3]),] }

n <- nrow(edges.t6)

edges.fort48.t6 <- foreach(i=1:n, .combine='rbind') %dopar% {

edges[edges[,1]==as.character(edges.t6[i,3]),] }

edges.fort48 <- rbind(find.edges, edges.fort48, edges.fort48.t2, edges.fort48.t6)

edges.t48 <-edges.fort48[edges.fort48[,3]%in%time.bool.active.t48[,1],]

n <- nrow(edges.t48)
attribute <- rep("t48",n)
edges.t48 <-cbind(edges.t48, attribute)

#Layer 6

edges.fort72 <- foreach(i=1:n, .combine='rbind') %dopar% {

edges[edges[,1]==as.character(edges.t48[i,3]),] }

n <- nrow(edges.t2)

edges.fort72.t2 <- foreach(i=1:n, .combine='rbind') %dopar% {

edges[edges[,1]==as.character(edges.t2[i,3]),] }

n <- nrow(edges.t6)

edges.fort72.t6 <- foreach(i=1:n, .combine='rbind') %dopar% {

edges[edges[,1]==as.character(edges.t6[i,3]),] }

n <- nrow(edges.t24)

edges.fort72.t24 <- foreach(i=1:n, .combine='rbind') %dopar% {

edges[edges[,1]==as.character(edges.t24[i,3]),] }

edges.fort72 <- rbind(find.edges, edges.fort72, edges.fort72.t2, edges.fort72.t6, edges.fort72.t24)

edges.t72 <-edges.fort72[edges.fort72[,3]%in%time.bool.active.t72[,1],]

n <- nrow(edges.t72)
attribute <- rep("t72",n)
edges.t72 <-cbind(edges.t72, attribute)

# summary

full.signal <- rbind(edges.t2, edges.t6, edges.t24, edges.t48, edges.t72)

write.table(full.signal, file=paste(starter,"_temp_full-signal.txt",sep=""), sep="\t", quote=F, col.names=F, row.names=F)

##########IF YOU WANT TO RECOVER THE TEMPORAL NETWORKS...IT IS HERE ##########


###############################################################################

full.signal.unique <-edges[edges[,4]%in%full.signal[,4],]

fraction.network <-100*nrow(full.signal.unique)/nrow(edges)

cat("fraction of whole network:\n")
print(fraction.network)

############ unique per timepoint:#######################

edges.t2.unique <-edges[edges[,4]%in%edges.t2[,4],]
edges.t6.unique <-edges[edges[,4]%in%edges.t6[,4],]
edges.t24.unique <-edges[edges[,4]%in%edges.t24[,4],]
edges.t48.unique <-edges[edges[,4]%in%edges.t48[,4],]
edges.t72.unique <-edges[edges[,4]%in%edges.t72[,4],]


# fraction in common and specific fates

unique.TGs <- unique(full.signal.unique[,3])
id.TG <- rep("TG", length(unique.TGs))
unique.TGs.id <- cbind(as.character(unique.TGs), id.TG)

#########unique TGs per timepoint#################

unique.TGs.t2 <- unique(edges.t2.unique[,3])
id.TG.t2 <- rep("TG", length(unique.TGs.t2))
unique.TGs.t2.id <- cbind(as.character(unique.TGs.t2), id.TG.t2)

unique.TGs.t6 <- unique(edges.t6.unique[,3])
id.TG.t6 <- rep("TG", length(unique.TGs.t6))
unique.TGs.t6.id <- cbind(as.character(unique.TGs.t6), id.TG.t6)

unique.TGs.t24 <- unique(edges.t24.unique[,3])
id.TG.t24 <- rep("TG", length(unique.TGs.t24))
unique.TGs.t24.id <- cbind(as.character(unique.TGs.t24), id.TG.t24)

unique.TGs.t48 <- unique(edges.t48.unique[,3])
id.TG.t48 <- rep("TG", length(unique.TGs.t48))
unique.TGs.t48.id <- cbind(as.character(unique.TGs.t48), id.TG.t48)

unique.TGs.t72 <- unique(edges.t72.unique[,3])
id.TG.t72 <- rep("TG", length(unique.TGs.t72))
unique.TGs.t72.id <- cbind(as.character(unique.TGs.t72), id.TG.t72)


###################################################

unique.TFs <- unique(full.signal.unique[,1])
id.TF <- rep("TF", length(unique.TFs))
unique.TFs.id <- cbind(as.character(unique.TFs), id.TF)

###########unique TFs per timepoint ################

unique.TFs.t2 <- unique(edges.t2.unique[,1])
id.TF.t2 <- rep("TF", length(unique.TFs.t2))
unique.TFs.t2.id <- cbind(as.character(unique.TFs.t2), id.TF.t2)

unique.TFs.t6 <- unique(edges.t6.unique[,1])
id.TF.t6 <- rep("TF", length(unique.TFs.t6))
unique.TFs.t6.id <- cbind(as.character(unique.TFs.t6), id.TF.t6)

unique.TFs.t24 <- unique(edges.t24.unique[,1])
id.TF.t24 <- rep("TF", length(unique.TFs.t24))
unique.TFs.t24.id <- cbind(as.character(unique.TFs.t24), id.TF.t24)

unique.TFs.t48 <- unique(edges.t48.unique[,1])
id.TF.t48 <- rep("TF", length(unique.TFs.t48))
unique.TFs.t48.id <- cbind(as.character(unique.TFs.t48), id.TF.t48)

unique.TFs.t72 <- unique(edges.t72.unique[,1])
id.TF.t72 <- rep("TF", length(unique.TFs.t72))
unique.TFs.t72.id <- cbind(as.character(unique.TFs.t72), id.TF.t72)

#####################################################

unique.TF.TG <- rbind(unique.TFs.id,unique.TGs.id)
write.table(unique.TF.TG, "temp_uniqueTFTG.txt", sep="\t", quote=F, col.names=F, row.names=F)

#############unique TF and TGs per timepoint ############

unique.TF.TG.t2 <- rbind(unique.TFs.t2.id,unique.TGs.t2.id)
unique.TF.TG.t6 <- rbind(unique.TFs.t6.id,unique.TGs.t6.id)
unique.TF.TG.t24 <- rbind(unique.TFs.t24.id,unique.TGs.t24.id)
unique.TF.TG.t48 <- rbind(unique.TFs.t48.id,unique.TGs.t48.id)
unique.TF.TG.t72 <- rbind(unique.TFs.t72.id,unique.TGs.t72.id)

write.table(unique.TF.TG.t2, "temp_uniqueTFTG_T2.txt", sep="\t", quote=F, col.names=F, row.names=F)
write.table(unique.TF.TG.t6, "temp_uniqueTFTG_T6.txt", sep="\t", quote=F, col.names=F, row.names=F)
write.table(unique.TF.TG.t24, "temp_uniqueTFTG_T24.txt", sep="\t", quote=F, col.names=F, row.names=F)
write.table(unique.TF.TG.t48, "temp_uniqueTFTG_T48.txt", sep="\t", quote=F, col.names=F, row.names=F)
write.table(unique.TF.TG.t72, "temp_uniqueTFTG_T72.txt", sep="\t", quote=F, col.names=F, row.names=F)

####################################################################################################

# preparing common and specific files

n <- nrow(common)
id <-c(1:n)
common.id <- cbind(common, id)

if(nrow(unique.TF.TG)!=0){
unique.TF.TG.temp <- read.table("temp_uniqueTFTG.txt", header=F)} else {
cat("no TF-TGs found !!\n")
unique.TF.TG.temp <- faked.matrix}

########### READ UNIQUE IN TMPS ####################################
# in case there are no TFs or TGs in a given timepoint, we replace the temp matrix by a faked matrix

if(nrow(unique.TF.TG.t2)!=0){
unique.TF.TG.t2.temp <- read.table("temp_uniqueTFTG_T2.txt", header=F)} else {
cat("no TF-TGs found !!\n")
unique.TF.TG.t2.temp <- faked.matrix}

if(nrow(unique.TF.TG.t6)!=0){
unique.TF.TG.t6.temp <- read.table("temp_uniqueTFTG_T6.txt", header=F)} else {
cat("no TF-TGs found !!\n")
unique.TF.TG.t6.temp <- faked.matrix}

if(nrow(unique.TF.TG.t24)!=0){
unique.TF.TG.t24.temp <- read.table("temp_uniqueTFTG_T24.txt", header=F)} else {
cat("no TF-TGs found !!\n")
unique.TF.TG.t24.temp <- faked.matrix}

if(nrow(unique.TF.TG.t48)!=0){
unique.TF.TG.t48.temp <- read.table("temp_uniqueTFTG_T48.txt", header=F)} else {
cat("no TF-TGs found !!\n")
unique.TF.TG.t48.temp <- faked.matrix}

if(nrow(unique.TF.TG.t72)!=0){
unique.TF.TG.t72.temp <- read.table("temp_uniqueTFTG_T72.txt", header=F)} else {
cat("no TF-TGs found !!\n")
unique.TF.TG.t72.temp <- faked.matrix}


#######################################################################


full.incommon <- unique.TF.TG.temp[unique.TF.TG.temp[,1]%in%common.id[,1],]

####################### common in timepoints ##############################

full.incommon.t2 <- unique.TF.TG.t2.temp[unique.TF.TG.t2.temp[,1]%in%common.id[,1],]
full.incommon.t6 <- unique.TF.TG.t6.temp[unique.TF.TG.t6.temp[,1]%in%common.id[,1],]
full.incommon.t24 <- unique.TF.TG.t24.temp[unique.TF.TG.t24.temp[,1]%in%common.id[,1],]
full.incommon.t48 <- unique.TF.TG.t48.temp[unique.TF.TG.t48.temp[,1]%in%common.id[,1],]
full.incommon.t72 <- unique.TF.TG.t72.temp[unique.TF.TG.t72.temp[,1]%in%common.id[,1],]

#################################################################################

n <- nrow(p19.genes)
id <-c(1:n)
p19.genes.id <- cbind(p19.genes, id)

full.inp19 <- unique.TF.TG.temp[unique.TF.TG.temp[,1]%in%p19.genes.id[,1],]

################### p19-specific in timepoints ##################################

full.inp19.t2 <- unique.TF.TG.t2.temp[unique.TF.TG.t2.temp[,1]%in%p19.genes.id[,1],]
full.inp19.t6 <- unique.TF.TG.t6.temp[unique.TF.TG.t6.temp[,1]%in%p19.genes.id[,1],]
full.inp19.t24 <- unique.TF.TG.t24.temp[unique.TF.TG.t24.temp[,1]%in%p19.genes.id[,1],]
full.inp19.t48 <- unique.TF.TG.t48.temp[unique.TF.TG.t48.temp[,1]%in%p19.genes.id[,1],]
full.inp19.t72 <- unique.TF.TG.t72.temp[unique.TF.TG.t72.temp[,1]%in%p19.genes.id[,1],]

######################################################################################


n <- nrow(f9.genes)
id <-c(1:n)
f9.genes.id <- cbind(f9.genes, id)

full.inf9 <- unique.TF.TG.temp[unique.TF.TG.temp[,1]%in%f9.genes.id[,1],]

################ f9-specific in timepoints #######################################

full.inf9.t2 <- unique.TF.TG.t2.temp[unique.TF.TG.t2.temp[,1]%in%f9.genes.id[,1],]
full.inf9.t6 <- unique.TF.TG.t6.temp[unique.TF.TG.t6.temp[,1]%in%f9.genes.id[,1],]
full.inf9.t24 <- unique.TF.TG.t24.temp[unique.TF.TG.t24.temp[,1]%in%f9.genes.id[,1],]
full.inf9.t48 <- unique.TF.TG.t48.temp[unique.TF.TG.t48.temp[,1]%in%f9.genes.id[,1],]
full.inf9.t72 <- unique.TF.TG.t72.temp[unique.TF.TG.t72.temp[,1]%in%f9.genes.id[,1],]

######################################################################################

##### FRACTION OF TF/TGs IN COMMON OR SPECIFIC GROUPS ##############################

fraction.common <- 100*nrow(full.incommon)/nrow(unique.TF.TG.temp)

fraction.p19 <- 100*nrow(full.inp19)/nrow(unique.TF.TG.temp)

fraction.f9 <- 100*nrow(full.inf9)/nrow(unique.TF.TG.temp)

unique.TF.TGs <- nrow(unique.TF.TG.temp)

summary.pc <- cbind(unique.TF.TGs,fraction.common,fraction.p19, fraction.f9)

write.table(summary.pc, "summary_percent.txt", quote=F, col.names=F, row.names=F, append=TRUE, sep="\t")

rm("fraction.common")
rm("fraction.p19")
rm("fraction.f9")

####ADD PERCENT PER TIMEPOINT!!!################

fraction.common.t2 <- 100*nrow(full.incommon.t2)/nrow(unique.TF.TG.t2.temp)
fraction.common.t6 <- 100*nrow(full.incommon.t6)/nrow(unique.TF.TG.t6.temp)
fraction.common.t24 <- 100*nrow(full.incommon.t24)/nrow(unique.TF.TG.t24.temp)
fraction.common.t48 <- 100*nrow(full.incommon.t48)/nrow(unique.TF.TG.t48.temp)
fraction.common.t72 <- 100*nrow(full.incommon.t72)/nrow(unique.TF.TG.t72.temp)

fraction.p19.t2 <- 100*nrow(full.inp19.t2)/nrow(unique.TF.TG.t2.temp)
fraction.p19.t6 <- 100*nrow(full.inp19.t6)/nrow(unique.TF.TG.t6.temp)
fraction.p19.t24 <- 100*nrow(full.inp19.t24)/nrow(unique.TF.TG.t24.temp)
fraction.p19.t48 <- 100*nrow(full.inp19.t48)/nrow(unique.TF.TG.t48.temp)
fraction.p19.t72 <- 100*nrow(full.inp19.t72)/nrow(unique.TF.TG.t72.temp)

fraction.f9.t2 <- 100*nrow(full.inf9.t2)/nrow(unique.TF.TG.t2.temp)
fraction.f9.t6 <- 100*nrow(full.inf9.t6)/nrow(unique.TF.TG.t6.temp)
fraction.f9.t24 <- 100*nrow(full.inf9.t24)/nrow(unique.TF.TG.t24.temp)
fraction.f9.t48 <- 100*nrow(full.inf9.t48)/nrow(unique.TF.TG.t48.temp)
fraction.f9.t72 <- 100*nrow(full.inf9.t72)/nrow(unique.TF.TG.t72.temp)

unique.TF.TGs.t2 <- nrow(unique.TF.TG.t2.temp)
unique.TF.TGs.t6 <- nrow(unique.TF.TG.t6.temp)
unique.TF.TGs.t24 <- nrow(unique.TF.TG.t24.temp)
unique.TF.TGs.t48 <- nrow(unique.TF.TG.t48.temp)
unique.TF.TGs.t72 <- nrow(unique.TF.TG.t72.temp)

summary.t2.pc <- cbind(unique.TF.TGs.t2,fraction.common.t2,fraction.p19.t2, fraction.f9.t2)
summary.t6.pc <- cbind(unique.TF.TGs.t6,fraction.common.t6,fraction.p19.t6, fraction.f9.t6)
summary.t24.pc <- cbind(unique.TF.TGs.t24,fraction.common.t24,fraction.p19.t24, fraction.f9.t24)
summary.t48.pc <- cbind(unique.TF.TGs.t48,fraction.common.t48,fraction.p19.t48, fraction.f9.t48)
summary.t72.pc <- cbind(unique.TF.TGs.t72,fraction.common.t72,fraction.p19.t72, fraction.f9.t72)

write.table(summary.t2.pc, "summary_percent_T2.txt", quote=F, col.names=F, row.names=F, append=TRUE, sep="\t")
write.table(summary.t6.pc, "summary_percent_T6.txt", quote=F, col.names=F, row.names=F, append=TRUE, sep="\t")
write.table(summary.t24.pc, "summary_percent_T24.txt", quote=F, col.names=F, row.names=F, append=TRUE, sep="\t")
write.table(summary.t48.pc, "summary_percent_T48.txt", quote=F, col.names=F, row.names=F, append=TRUE, sep="\t")
write.table(summary.t72.pc, "summary_percent_T72.txt", quote=F, col.names=F, row.names=F, append=TRUE, sep="\t")

rm("fraction.common.t2")
rm("fraction.common.t6")
rm("fraction.common.t24")
rm("fraction.common.t48")
rm("fraction.common.t72")

rm("fraction.p19.t2")
rm("fraction.p19.t6")
rm("fraction.p19.t24")
rm("fraction.p19.t48")
rm("fraction.p19.t72")

rm("fraction.f9.t2")
rm("fraction.f9.t6")
rm("fraction.f9.t24")
rm("fraction.f9.t48")
rm("fraction.f9.t72")

##########################################################################################

########### Fraction of TF/TGs in common or specific rel to total common or specific###############

fraction.common.reltocommon <- 100*nrow(full.incommon)/nrow(common)
cat("fraction of common program reconstituted:\n")
print(fraction.common.reltocommon)

fraction.p19.reltop19 <- 100*nrow(full.inp19)/nrow(p19.genes)
cat("fraction of P19-specific program reconstituted:\n")
print(fraction.p19.reltop19)

fraction.f9.reltof9 <- 100*nrow(full.inf9)/nrow(f9.genes)
cat("fraction of F9-specific program reconstituted:\n")
print(fraction.f9.reltof9)

unique.TF.TGs <- nrow((unique.TF.TG.temp))

summary <- cbind(unique.TF.TGs,fraction.common.reltocommon,fraction.p19.reltop19, fraction.f9.reltof9)

write.table(summary, "summary_list.txt", quote=F, col.names=F, row.names=F, append=TRUE, sep="\t")

rm("fraction.common.reltocommon")
rm("fraction.p19.reltop19")
rm("fraction.f9.reltof9")

############### fractions per timepoint ###################

fraction.common.t2.reltocommon <- 100*nrow(full.incommon.t2)/nrow(common)
fraction.common.t6.reltocommon <- 100*nrow(full.incommon.t6)/nrow(common)
fraction.common.t24.reltocommon <- 100*nrow(full.incommon.t24)/nrow(common)
fraction.common.t48.reltocommon <- 100*nrow(full.incommon.t48)/nrow(common)
fraction.common.t72.reltocommon <- 100*nrow(full.incommon.t72)/nrow(common)

fraction.p19.t2.reltop19 <- 100*nrow(full.inp19.t2)/nrow(p19.genes)
fraction.p19.t6.reltop19 <- 100*nrow(full.inp19.t6)/nrow(p19.genes)
fraction.p19.t24.reltop19 <- 100*nrow(full.inp19.t24)/nrow(p19.genes)
fraction.p19.t48.reltop19 <- 100*nrow(full.inp19.t48)/nrow(p19.genes)
fraction.p19.t72.reltop19 <- 100*nrow(full.inp19.t72)/nrow(p19.genes)

fraction.f9.t2.reltof9 <- 100*nrow(full.inf9.t2)/nrow(f9.genes)
fraction.f9.t6.reltof9 <- 100*nrow(full.inf9.t6)/nrow(f9.genes)
fraction.f9.t24.reltof9 <- 100*nrow(full.inf9.t24)/nrow(f9.genes)
fraction.f9.t48.reltof9 <- 100*nrow(full.inf9.t48)/nrow(f9.genes)
fraction.f9.t72.reltof9 <- 100*nrow(full.inf9.t72)/nrow(f9.genes)

unique.TF.TGs.t2 <- nrow(unique.TF.TG.t2.temp)
unique.TF.TGs.t6 <- nrow(unique.TF.TG.t6.temp)
unique.TF.TGs.t24 <- nrow(unique.TF.TG.t24.temp)
unique.TF.TGs.t48 <- nrow(unique.TF.TG.t48.temp)
unique.TF.TGs.t72 <- nrow(unique.TF.TG.t72.temp)

summary.t2 <- cbind(unique.TF.TGs.t2,fraction.common.t2.reltocommon,fraction.p19.t2.reltop19, fraction.f9.t2.reltof9)
summary.t6 <- cbind(unique.TF.TGs.t6,fraction.common.t6.reltocommon,fraction.p19.t6.reltop19, fraction.f9.t6.reltof9)
summary.t24 <- cbind(unique.TF.TGs.t24,fraction.common.t24.reltocommon,fraction.p19.t24.reltop19, fraction.f9.t24.reltof9)
summary.t48 <- cbind(unique.TF.TGs.t48,fraction.common.t48.reltocommon,fraction.p19.t48.reltop19, fraction.f9.t48.reltof9)
summary.t72 <- cbind(unique.TF.TGs.t72,fraction.common.t72.reltocommon,fraction.p19.t72.reltop19, fraction.f9.t72.reltof9)

write.table(summary.t2, "summary_list_T2.txt", quote=F, col.names=F, row.names=F, append=TRUE, sep="\t")
write.table(summary.t6, "summary_list_T6.txt", quote=F, col.names=F, row.names=F, append=TRUE, sep="\t")
write.table(summary.t24, "summary_list_T24.txt", quote=F, col.names=F, row.names=F, append=TRUE, sep="\t")
write.table(summary.t48, "summary_list_T48.txt", quote=F, col.names=F, row.names=F, append=TRUE, sep="\t")
write.table(summary.t72, "summary_list_T72.txt", quote=F, col.names=F, row.names=F, append=TRUE, sep="\t")

rm("fraction.common.t2.reltocommon")
rm("fraction.common.t6.reltocommon")
rm("fraction.common.t24.reltocommon")
rm("fraction.common.t48.reltocommon")
rm("fraction.common.t72.reltocommon")

rm("fraction.p19.t2.reltop19")
rm("fraction.p19.t6.reltop19")
rm("fraction.p19.t24.reltop19")
rm("fraction.p19.t48.reltop19")
rm("fraction.p19.t72.reltop19")

rm("fraction.f9.t2.reltof9")
rm("fraction.f9.t6.reltof9")
rm("fraction.f9.t24.reltof9")
rm("fraction.f9.t48.reltof9")
rm("fraction.f9.t72.reltof9")

###############################################

}

summary <- read.table("summary_list.txt", header=F)
tete <- c("Gene", "ID","unique.TF.TGs", "fraction.common.Program", "fraction.P19.spec.program", "fraction.F9.spec.program")

summary.id <- cbind(start.list, summary)

summary.head <- rbind(tete, summary.id)

write.table(summary.head, "summary_list.txt", quote=F, col.names=F, row.names=F, sep="\t")

# and for summary percent:

summary <- read.table("summary_percent.txt", header=F)
tete <- c("Gene", "ID","unique.TF.TGs", "fraction.common.Program", "fraction.P19.spec.program", "fraction.F9.spec.program")

summary.id <- cbind(start.list, summary)

summary.head <- rbind(tete, summary.id)

write.table(summary.head, "summary_percent.txt", quote=F, col.names=F, row.names=F, sep="\t")


######### for timepoints summary ######################

summary <- read.table("summary_list_T2.txt", header=F)
summary.id <- cbind(start.list, summary)
summary.head <- rbind(tete, summary.id)
write.table(summary.head, "summary_list_T2.txt", quote=F, col.names=F, row.names=F, sep="\t")

summary <- read.table("summary_list_T6.txt", header=F)
summary.id <- cbind(start.list, summary)
summary.head <- rbind(tete, summary.id)
write.table(summary.head, "summary_list_T6.txt", quote=F, col.names=F, row.names=F, sep="\t")

summary <- read.table("summary_list_T24.txt", header=F)
summary.id <- cbind(start.list, summary)
summary.head <- rbind(tete, summary.id)
write.table(summary.head, "summary_list_T24.txt", quote=F, col.names=F, row.names=F, sep="\t")

summary <- read.table("summary_list_T48.txt", header=F)
summary.id <- cbind(start.list, summary)
summary.head <- rbind(tete, summary.id)
write.table(summary.head, "summary_list_T48.txt", quote=F, col.names=F, row.names=F, sep="\t")

summary <- read.table("summary_list_T72.txt", header=F)
summary.id <- cbind(start.list, summary)
summary.head <- rbind(tete, summary.id)
write.table(summary.head, "summary_list_T72.txt", quote=F, col.names=F, row.names=F, sep="\t")

# attach all timepoints

summary.t2 <- read.table("summary_list_T2.txt", header=F)
summary.t6 <- read.table("summary_list_T6.txt", header=F)
summary.t24 <- read.table("summary_list_T24.txt", header=F)
summary.t48 <- read.table("summary_list_T48.txt", header=F)
summary.t72 <- read.table("summary_list_T72.txt", header=F)

summary.tmp <- rbind(summary.t2, summary.t6, summary.t24, summary.t48, summary.t72)

write.table(summary.tmp, "summary_list_allTMPs.txt", quote=F, col.names=F, row.names=F, sep="\t")

## and for summary percent per timepoints:

summary <- read.table("summary_percent_T2.txt", header=F)
summary.id <- cbind(start.list, summary)
summary.head <- rbind(tete, summary.id)
write.table(summary.head, "summary_percent_T2.txt", quote=F, col.names=F, row.names=F, sep="\t")

summary <- read.table("summary_percent_T6.txt", header=F)
summary.id <- cbind(start.list, summary)
summary.head <- rbind(tete, summary.id)
write.table(summary.head, "summary_percent_T6.txt", quote=F, col.names=F, row.names=F, sep="\t")

summary <- read.table("summary_percent_T24.txt", header=F)
summary.id <- cbind(start.list, summary)
summary.head <- rbind(tete, summary.id)
write.table(summary.head, "summary_percent_T24.txt", quote=F, col.names=F, row.names=F, sep="\t")

summary <- read.table("summary_percent_T48.txt", header=F)
summary.id <- cbind(start.list, summary)
summary.head <- rbind(tete, summary.id)
write.table(summary.head, "summary_percent_T48.txt", quote=F, col.names=F, row.names=F, sep="\t")

summary <- read.table("summary_percent_T72.txt", header=F)
summary.id <- cbind(start.list, summary)
summary.head <- rbind(tete, summary.id)
write.table(summary.head, "summary_percent_T72.txt", quote=F, col.names=F, row.names=F, sep="\t")

# attach all timepoints

summary.t2 <- read.table("summary_percent_T2.txt", header=F)
summary.t6 <- read.table("summary_percent_T6.txt", header=F)
summary.t24 <- read.table("summary_percent_T24.txt", header=F)
summary.t48 <- read.table("summary_percent_T48.txt", header=F)
summary.t72 <- read.table("summary_percent_T72.txt", header=F)

summary.tmp <- rbind(summary.t2, summary.t6, summary.t24, summary.t48, summary.t72)

write.table(summary.tmp, "summary_percent_allTMPs.txt", quote=F, col.names=F, row.names=F, sep="\t")
 

##############################################################

stopCluster(cl)

}