# Description:
# ------------
# This code creates an expression set based on  pre-processed drugmatrix gene expression data.
# It then reads in  hierarchical clusters, PPI modules, and SVM gene signatures as input seed gene sets for ISA.
# Input gene sets were expanded by adding random genes.
# Finally, it runs ISA to produce modules

# Unzip the script and input file given in supporting information Script S1 in the working directory

# Input files:
# ----------------
#  1_pheno_data.txt (Natsoulis et al., supplemental histopathology data with +1, 0, and -1)
#  2_my_conditions.txt (list of 640 conditions)
#  3_liver_expression_matrix_7826_640.txt (~7K vs 640)
#  4_global_ppi_modules.txt
#  5_hcluster.txt
#  6_Nat_sig.txt

# --------------
# Load libraries
# --------------
library("cluster")
library("BSDA")
library("eisa")
library("rat2302.db")
library("biomaRt")
# ---------------------
# Read input parameters
# ---------------------
expand<-2 #For production run, change to: 17
feat<-3.5
samp<-1.8
type<-"combo"
#------------------------
# Phenotype names
nolevels<-c("Compound","Level","SAC","AC","SV0562011R5RU", "SV0562014R5RU", "SV0562020R5RU", "SV0562026R5RU", "SV0562050R5RU", "SV0562116R5RU", "SV0567082R5RU"
            ,"SV0567088R5RU", "SV0567098R5RU", "SV0567149R5RU", "SV0567153R5RU", "SV0567174R5RU", "SV0571010R5RU", "SV0571053R5RU",
            "SV0575020R5RU", "SV0599196R5RU", "SV0599291R5RU", "SV0599539R5RU" ,"SV0614084R5RU", "SV0614125R5RU", "SV0614137R5RU",
            "SV0614148R5RU", "SV0614202R5RU", "SV0614270R5RU", "SV0635003R5RU", "SV0642001R5RU", "SV0643002R5RU", "SV0643003R5RU",
            "SV0650033R5RU", "SV0650093R5RU", "SV0650106R5RU", "SV0650121R5RU", "SV0650143R5RU", "SV0651106R5RU")

# Read in Natsoulis severity table
pheno_data<-read.table("1_pheno_data.txt", header = TRUE, sep = "\t", as.is =  nolevels)

# Read in condition definitions
my_conditions<-read.table("2_my_conditions.txt", header = TRUE, sep = "\t", as.is = c("Compound", "Vehicle"))
colnames(my_conditions)[[1]]<-"Condition_number"

# Combine condition definitions with severity data
nconditions<-length(my_conditions[,1])
npheno<-length(pheno_data[,1])

my_conditions2<-data.frame(my_conditions)
vector<-array(0,dim = c(nconditions))
for (i in 1:38)
{
    my_conditions2<-cbind(my_conditions2,vector)
    
}

for (j in 1:5)
{
    for (i in 1:nconditions )
    {
        my_conditions2[i,j]<-my_conditions[i,j]
    }
}

count = 0
zero_out = 1
tail=0

for (i in 1:nconditions)
{
    for (j in 1:npheno)
    {
        temp<-sub("_"," ",my_conditions[i,2])
        if (temp == pheno_data[j,2] && my_conditions[i,3] == pheno_data[j,3] && my_conditions[i,4] == pheno_data[j,4])
        {
            count = count + 1
            my_conditions2[i,6] <-pheno_data[j,1]     
            my_conditions2[i,7:43]<-pheno_data[j,5:41]
            zero_out = 0
        }
        
    }
    
    if (zero_out == 1)
    {
        tail=tail+1
        temp<-pheno_data[j,8:41]
        temp[1:34]<-2
        my_conditions2[i,10:43]<-temp
    }
    
    zero_out = 1
}
colnames(my_conditions2)[6]<-"rep_id"
colnames(my_conditions2)[7]<-"level"
colnames(my_conditions2)[8]<-"sac"
colnames(my_conditions2)[9]<-"ac"
colnames(my_conditions2)[10:43]<-colnames(pheno_data)[8:41]

write.table(my_conditions2, file = "test", sep = "\t", row.names = FALSE)

# ------------------------
# Import expression matrix
# ------------------------
exprs<-as.matrix(read.table("3_liver_expression_matrix_7826_640.txt", header = TRUE, sep = "\t", row.names = 1, as.is=TRUE))
index<-seq(1,640,1)
colnames(exprs)<-index
exprs_pruned <- exprs

# ----------------------
# Create Pruned Z matrix
# ----------------------
count = 0
flat_pruned<-array(0, c(dim(exprs_pruned)[[1]])*dim(exprs_pruned)[[2]])

for ( i in 1:dim(exprs_pruned)[[1]])
{
    for ( j in 1:dim(exprs_pruned)[[2]])
    {
        count = count+1
        flat_pruned[count]<-exprs_pruned[i,j] # Bug fix, before LHS was exprs.
    }
}

zavg<-mean(flat_pruned)
zstd<-sd(flat_pruned)

zexprs<-array(0, dim = c(dim(exprs_pruned)[[1]],dim(exprs_pruned)[[2]]))

for (i in 1:dim(exprs_pruned)[[1]])
{
    for (j in 1:dim(exprs_pruned)[[2]])
    {
        zexprs[i,j] = (exprs_pruned[i,j]-zavg)/zstd
    }
}

rownames(zexprs)<-rownames(exprs_pruned)
colnames(zexprs)<-colnames(exprs_pruned)
# --------------------------------------------------
# Create an expression set object for input into ISA
# --------------------------------------------------
temp<-colnames(my_conditions2)
metadata<-data.frame(labelDescription = temp, stringsAsFactors = FALSE)
phenodata<-new("AnnotatedDataFrame",data=my_conditions2, varMetadata=metadata)
exampleSet<-new("ExpressionSet", exprs=zexprs, phenoData = phenodata, annotation = "rat2302")

#Import gene sets:
#------------------
# Import PPI modules
#------------------
global_modules<-as.matrix(read.table("4_global_ppi_modules.txt",header = TRUE, sep = "\t",row.names = 1, as.is = TRUE))

#-------------------------------
#Import hierarchical cluster data
#--------------------------------
hcluster<-as.matrix(read.table("5_hcluster.txt",header = TRUE, sep = "\t", row.names = 1, as.is=TRUE))

index<-match(rownames(exprs_pruned),rownames(hcluster))
cluster_seed<-hcluster[index,]

#-------------------------
#Import SVM gene signatures
#-------------------------
nat<-as.matrix(read.table("6_Nat_sig.txt",header = TRUE, sep = "\t", row.names = 1, as.is = TRUE))
nat_rows<-as.matrix(rownames(nat))

rat=useMart(biomart="ensembl", dataset="rnorvegicus_gene_ensembl")
att<-as.matrix(listAttributes(rat))

# Convert GenBank IDs to rat2302 probe IDs
x <- rat2302ACCNUM
mapped_probes <- mappedkeys(x)
xx <- as.list(x[mapped_probes])

nat_match<-match(nat_rows,xx)

yy<-data.frame(xx)
zz<-colnames(yy)
zz<-sub("X","",zz)

assign<-zz[nat_match]

rownames(nat)<-assign
colnames(nat)<-seq(1,34,1)

temp<-exprs(exampleSet)
temp2<-rownames(temp)

help<-match(rownames(nat),temp2)
rownames(nat)<-help

#  remove<-"<NA>"
b<-rownames(nat)
c<-na.omit(b)
d<-match(c,b)
nat_final<-nat[d,]

# Convert non-zero entries to 1
nat_final[nat_final != 0] <- 1

# Rename rownames to rat probe IDs
index_final<-as.numeric(rownames(nat_final))
rownames(nat_final)<-zz[index_final]

a<-rownames(nat_final)
b<-rownames(zexprs)
c<-match(a,b)
d<-na.omit(c)

nat_final2<-array(0,dim = c(dim(zexprs)[1],34))
nat_final2[as.numeric(d),1:34]<-nat_final[1:length(d),1:34]
rownames(nat_final2)<-b
colnames(nat_final2)<-seq(1,34,1)

# -------------------------------
# Random seeds size 100, 100 sets.
# -------------------------------
final_rows<-dim(exprs_pruned)[1]

# Expand each seed class

#Expand PPI
nppi<-dim(global_modules)[2]
ppi_augment<-global_modules
for ( i in 1:expand)
{
    set.seed(i)
    random_seed<-generate.seeds(final_rows,count=nppi, sparsity = 100)
    ppi_seed2<-random_seed + global_modules
    ppi_seed2[ppi_seed2>1]<-1
    ppi_seed3<-cbind(ppi_seed2,ppi_augment)
    ppi_augment<-ppi_seed3
}

# Expand Cluster
ncluster<-dim(cluster_seed)[2]
cluster_augment<-cluster_seed
for ( i in 1:expand)
{
    set.seed(i)
    random_seed<-generate.seeds(final_rows,count=ncluster, sparsity = 100)
    cluster_seed2<-random_seed + cluster_seed
    cluster_seed2[cluster_seed2>1]<-1
    cluster_seed3<-cbind(cluster_seed2,cluster_augment)
    cluster_augment<-cluster_seed3
}

# Expand SVM
nsvm<-dim(nat_final2)[2]
svm_augment<-nat_final2
for ( i in 1:expand)
{
    set.seed(i)
    random_seed<-generate.seeds(final_rows,count=nsvm, sparsity = 100)
    nat_final2a<-random_seed + nat_final2
    nat_final2a[nat_final2a>1]<-1
    nat_final3<-cbind(nat_final2a,svm_augment)
    svm_augment<-nat_final3
}

cluster_ppi_svm_augment<-cbind(cluster_augment,ppi_augment,svm_augment)

# Relevant parameters
# global_modules
# cluster_seed
# ppi_augment
# cluster_augment
# cluster_ppi_svm_augment
# exampleSet
# zexprs

if (type == "cluster")
{
    modules<-ISAIterate(exampleSet,feature.seeds=cluster_augment,thr.feat=feat,thr.samp=samp)
}

if (type == "ppi")
{
    modules<-ISAIterate(exampleSet,feature.seeds=ppi_augment,thr.feat=feat,thr.samp=samp)
}  

if (type == "svm")
{
    modules<-ISAIterate(exampleSet,feature.seeds=svm_augment,thr.feat=feat,thr.samp=samp)
}
if (type == "combo")
{
    modules<-ISAIterate(exampleSet,feature.seeds=cluster_ppi_svm_augment,thr.feat=feat,thr.samp=samp)
}

modules.unique<-ISAUnique(exampleSet, modules)
modules.robust <- ISAFilterRobust(exampleSet, modules.unique)
isa_run<-modules.robust

# Clean up
rm(modules, modules.unique, modules.robust, exprs)
rm(exprs_pruned)
rm(ppi_seed2,ppi_seed3, cluster_seed2, cluster_seed3)
gc()
###############################
# Get geneid, module membership,and count
###############################
#testgeneset<-as.matrix(getFeatureNames(isa_run)[[2]])
GENESET_MODID<-data.frame(NULL)
for (i in 1:length(isa_run)) {
    testGENESET<-data.frame(NULL)
    testGENESET<-as.data.frame(getFeatureNames(isa_run)[[i]])
    testGENESET$MODID <- i
    testGENESET$COUNT <- nrow(testGENESET)
    names(testGENESET) <- c("GENEID","MODID", "COUNT")
    GENESET_MODID <- rbind(GENESET_MODID, testGENESET)
}
write.table(GENESET_MODID, file="Gene_Modules_out.txt",sep="\t")
###############################

# Important output files
# global_modules       PPI modules
# cluster_seed         Hierarchical clusters
# nat_final2           SVM gene signatures
# Gene_Modules_out.txt  Modules-gene list
# isa_run              ISA modules (class = eisa)
#####################################################################################
