#source("Regulatory effects scoring script.R")
#type: the type of identifier used in the expression and prediction data. 
#Options are "refseq" and "entrez". Defaults to "refseq".
RE_score_analysis <- function(
		predictions, 
		expression, 
		largegroupindexes, 
		smallgroupindexes, 
		type="refseq") {

	startdate <- date()
	#TODO
	#Add a check to see if hte mRNAs in prediction and expression
	#data are present in the annotation that will be used for EASE.
	
	print("Checking data...")
	
	if(!is.data.frame(expression)) {
		stop("Expression data must be in a dataframe.")
	}
	
	if(	is.null(dimnames(data.matrix(expression))[[1]]) ||
		is.null(dimnames(data.matrix(expression))[[2]])) {
		stop("expression matrix must have column and row names")
	}
	if(length(names(expression)) < length(row.names(expression))) {
		warning("More samples than genes... Is this right?")
	}
	
	if(length(predictions$miRNA) == 0 || length(predictions$mRNA) == 0) {
		stop("predictions mapping columns must be labeled miRNA and mRNA.")
	}
	#Check that predictions is two columns, and labeled $mRNA and $miRNA
	if(!is.data.frame(predictions) || length(predictions) != 2) {
		stop("predictions must be a data matrix with have two columns.")
	}

	
	#check if group assignments are useable
	if(!is.integer(largegroupindexes) || 
		length(largegroupindexes) < 1 ||
		!is.integer(smallgroupindexes) ||
		length(smallgroupindexes) < 1) {
		stop("largegroupindexes and smallgroup indexes must be vectors of integer values.")
	}

	# check that the indexes are within range of expression matrix size
	if(length(which(expression[largegroupindexes,] < length(largegroupindexes)))) {

	}

	# and non-overlapping
	if(length(intersect(smallgroupindexes, largegroupindexes)) > 0) {
		stop("two index groups must not overlap.")
	}
	

	
	print("Data looks good. Loading libraries.")
	#Load support function libraries
	library(org.Hs.eg.db)
	library(GOstats)
	# if GOstats install fails, try: 
		#source("http://bioconductor.org/biocLite.R")
		#biocLite("org.Hs.eg.db")
		#biocLite("GOstats")
	
	print("Done loading libraries")
	
	results <- new.env()
	
	nummRNAs <- length(unique(predictions$mRNA))
	nummiRNAs <- length(unique(predictions$miRNA))
	results$NumbermRNAs = nummRNAs
	results$NumbermiRNAs = nummiRNAs

	#need a test that will determine if the RE score calculation will return all NAs
	#make sure that miRNAs in miRNA list are predicted to target some mRNAs in mRNA list

	if(length(intersect(names(expression), predictions$mRNA)) < 1) {
		stop("No mRNAs in prediction dataset match mRNAs in expression dataset.")
	}
	#Calculate RE scores and filter out miRNAs with no predictions
	mRNAexpression <- as.matrix(expression)
	


	reScores <- calcREScores(mRNAexpression, miRNAnames=unique(predictions$miRNA), predictions=predictions)
	filteredREScores <- reScores[which(!is.na(reScores[1,]))]
	names(filteredREScores) <- names(reScores[which(!is.na(reScores[1,]))])
	
	#Create a boxplot of the RE scores for all miRNAs, each group of samples
	largegroupREScores <- filteredREScores[largegroupindexes,]
	smallgroupREScores <- filteredREScores[smallgroupindexes,]
	#boxplot(c(largegroupREScores, recursive=TRUE), c(smallgroupREScores, recursive=TRUE))

	
	results$filteredREScores <- filteredREScores
	results$smallgroupindexes <- smallgroupindexes
	results$largegroupindexes <- largegroupindexes
	
	#Is there a difference in overall RE scores between the two groups?
	overalldifference <- t.test(c(largegroupREScores, recursive=TRUE), c(smallgroupREScores, recursive=TRUE), alternate="two.sided")

	results$OverallDifference = overalldifference
	
	#Calculate p-values, test statistic and RE score difference for each miRNA
	#TODO remove data.frame conversion
	reCalc <- data.frame(t(apply(filteredREScores, 2, permutedRowwiseTtest, group1=smallgroupindexes, group2=largegroupindexes, permutationCount=1000)))
	names(reCalc) <- c("pvalue", "statistic", "reDiff", "fdr") 
	reCalc[which(reCalc$pvalue < .05),]
	row.names(reCalc) <- names(filteredREScores)
	
#	results$REScoreCalculation = reCalc
	
	#Print out significant miRNAs
	sigmiRNAs <- reCalc[intersect(which(reCalc$pvalue < .05), which(reCalc$fdr < .1)),] 

	results$reCalc <- reCalc
	
	higherREinSmallGroup <- row.names(sigmiRNAs[which(sigmiRNAs$reDiff > 0),])
	lowerREinSmallGroup <- row.names(sigmiRNAs[which(sigmiRNAs$reDiff < 0),])

	print("miRNAs more active in small sample group:")
	print(higherREinSmallGroup)
	print("miRNAs less active in small sample group:")
	print(lowerREinSmallGroup)

	#Run EASE on significant miRNAs
	xx <- as.list(org.Hs.egACCNUM2EG)
	hgCutoff <- 1
	library(org.Hs.eg.db)
	if(type != "entrez") {
		wholeGenome <- unlist(unique(xx[which(names(xx) %in% predictions$mRNA)]))
	} else {
		wholeGenome <- unlist(intersect(xx, predictions$mRNA))
	}
	higherREinSmallGroupTargets <- predictions$mRNA[which(predictions$miRNA %in% higherREinSmallGroup)]
	if(length(higherREinSmallGroupTargets) > 0) {
	
		if(type != "entrez") {
			#Get Entrez ids corresponding to the refeseq ids in the target list.
			higherREinSmallGroupTargetsEntrez <- xx[which(names(xx) %in% higherREinSmallGroupTargets)]
			higherREinSmallGroupTargetsEntrez <- unlist(higherREinSmallGroupTargetsEntrez)
			uniqueh <- unique(higherREinSmallGroupTargetsEntrez)
		} else {
			uniqueh <- unique(higherREinSmallGroupTargets)
		}
		uniqueh <- as.character(uniqueh)
		#Remove any null values
		if("NULL" %in% uniqueh) {
			uniqueh <- uniqueh[-1*(which(uniqueh == "NULL"))]
		}
		
		print(paste("Running positive Biological Process EASE", date()))
		posEASEBP <- runEASE(
					uniqueh, 
					annotation="org.Hs.eg.db", 
					wholeGenome=wholeGenome, 
					hgCutoff=hgCutoff, 
					ontology="BP"
		) 

		print(paste("Running positive Molecular Function EASE", date()))
		posEASEMF <- runEASE(
					uniqueh, 
					annotation="org.Hs.eg.db", 
					wholeGenome=wholeGenome, 
					hgCutoff=hgCutoff, 
					ontology="MF"
		) 
		print(paste("Running positive Cellular Component EASE", date()))
		posEASECC <- runEASE(
					uniqueh, 
					annotation="org.Hs.eg.db", 
					wholeGenome=wholeGenome, 
					hgCutoff=hgCutoff, 
					ontology="CC"
		) 
		
		higherREinSmallGroup <- as.list(higherREinSmallGroup)
		names(higherREinSmallGroup) <- "higherREinSmallGroup"
		#higherREinSmallGroupTargetsEntrez <- as.list(higherREinSmallGroupTargetsEntrez)
		#names(higherREinSmallGroupTargetsEntrez) <- "higherREinSmallGroupTargetsEntrez"
		names(uniqueh) <- "higherREinSmallGroupTargetsEntrez"
		
		#Add result data to result object
		results$miRNAsHigherREinSmallGroup = higherREinSmallGroup
		#results$HigherREinSmallGroupTargets = higherREinSmallGroupTargetsEntrez
		results$HigherREinSmallGroupTargets = uniqueh
		results$PosEASEBP = posEASEBP
		results$PosEASEMF = posEASEMF
		results$PosEASECC = posEASECC

	} else {
		print("no miRNAs had higher RE scores in the small target group")
	}

	lowerREinSmallGroupTargets <-predictions$mRNA[which(predictions$miRNA %in% lowerREinSmallGroup)]
	if(length(lowerREinSmallGroupTargets) > 0) {
		if(type != "entrez") {
			#Get Entrez ids corresponding to the refeseq ids in the target list.
			lowerREinSmallGroupTargetsEntrez <- xx[which(names(xx) %in% lowerREinSmallGroupTargets)]
			lowerREinSmallGroupTargetsEntrez <- unlist(lowerREinSmallGroupTargetsEntrez)
			uniqueh <- unique(lowerREinSmallGroupTargetsEntrez)
		} else {
			uniqueh <- unique(lowerREinSmallGroupTargets)
		}
		#Remove any null values
		if("NULL" %in% uniqueh) {
			uniqueh <- uniqueh[-1*(which(uniqueh == "NULL"))]
		}
		uniqueh <- as.character(uniqueh)
		print(paste("Running negative Biological Process EASE", date()))
		negEASEBP <- runEASE(
					uniqueh, 
					annotation="org.Hs.eg.db", 
					wholeGenome=wholeGenome, 
					hgCutoff=hgCutoff, 
					ontology="BP"
		) 
		print(paste("Running negative Molecular Function EASE", date()))
		negEASEMF <- runEASE(
					uniqueh, 
					annotation="org.Hs.eg.db", 
					wholeGenome=wholeGenome, 
					hgCutoff=hgCutoff, 
					ontology="MF"
		) 
		print(paste("Running negative Cellular Component EASE", date()))
		negEASECC <- runEASE(
					uniqueh, 
					annotation="org.Hs.eg.db", 
					wholeGenome=wholeGenome, 
					hgCutoff=hgCutoff, 
					ontology="CC"
		)
		lowerREinSmallGroup <- as.list(lowerREinSmallGroup)
		names(lowerREinSmallGroup) <- "lowerREinSmallGroup"
		#lowerREinSmallGroupTargetsEntrez <- as.list(lowerREinSmallGroupTargetsEntrez)
		#names(lowerREinSmallGroupTargetsEntrez) <- "lowerREinSmallGroupTargetsEntrez"
		names(uniqueh) <- "lowerREinSmallGroupTargetsEntrez"
		
		#Add result data to result object
		results$miRNAsLowerREinSmallGroup = lowerREinSmallGroup
		#results$LowerREinSmallGroupTargets = lowerREinSmallGroupTargetsEntrez
		results$LowerREinSmallGroupTargets = uniqueh
		results$NegEASEBP = negEASEBP
		results$NegEASEMF = negEASEMF
		results$NegEASECC = negEASECC

	} else {
		print("no miRNAs had lower RE scores in the small target group")
	}
	print(paste("Run started at", startdate))
	results
} #End RE_score_analysis function



permutedRowwiseTtest <- function(scores, group1, group2, permutationCount) {
	temp <- t.test(scores[group1], scores[group2], alternative="two.sided")
	sampleNumber <- length(group1) + length(group2)
	fdr <- 0
	for(j in 1:permutationCount) {
		#permute sample membership
		permutegroup1 <- as.integer(runif(n=length(group1), min=1, max=sampleNumber))
		permutegroup2 <- setdiff(seq(1:sampleNumber), permutegroup1)
		#Run a t-test on each miRNA, with the two groups as the two sides. 
		permutedTestResult <- t.test(scores[permutegroup1], scores[permutegroup2], alternative="two.sided")
		if(permutedTestResult$p.value < temp$p.value) {
			#print(paste("teststat", permutedTestResult$p.value, "larger than", temp$p.value))
			fdr <- fdr + 1
		}
	}
	fdr <- fdr / permutationCount
	c(temp$p.value, temp$statistic, (mean(scores[group1]) - mean(scores[group2])), fdr)
}


runEASE <- function(x, annotation, wholeGenome, hgCutoff, ontology) {
	#Biological Process

	params.bp <- new("GOHyperGParams", 
					geneIds = x, 
					universeGeneIds = unique(wholeGenome), 
					annotation = annotation, 
					ontology = ontology, 
					pvalueCutoff = hgCutoff, 
					testDirection = "over", 
					conditional = FALSE
	)
	hgout.bp <- hyperGTest(params.bp)
	table.bp <- summary(hgout.bp)
	pvals.bp.bh <- p.adjust(table.bp[,2], "BH")    
	x.bp.bh.sig <- data.frame(
						table.bp[pvals.bp.bh <= .05,1:2], 
						pvals.bp.bh[pvals.bp.bh <= .05], 
						table.bp[pvals.bp.bh <= .05,3:7]
					)
	colnames(x.bp.bh.sig)[3] <- "AdjPvalue" 
	x.bp.bh.sig
}




calcREScores <- function(mRNAexpression, miRNAnames, predictions) {
	if(length(predictions$miRNA) < 1 || 
		length(predictions$mRNA) < 1 
	) {
		fail("No predictions found or predictions mapped incorrectly.")
	}
	scores <- matrix(nrow=132, ncol=0)
	dimnames(scores) <- list(list(), list())
	debug=FALSE
		
	print(paste("starting lapply at", date()))
	scores <- sapply(miRNAnames, getscores, 
					mRNAexpression=mRNAexpression, 
					predictions=predictions, 
					debug=debug)
	scores <- data.frame(scores, check.names=FALSE)
	names(scores) <- miRNAnames

	print(paste("ending lapply at", date()))
	scores
}

getscores <- function(thismiRNA, mRNAexpression, predictions, debug) {
#	if(thismiRNA == "hsa-miR-186") {
#		debug=TRUE
#	} else {
#		debug=FALSE
#	}
	print(paste("running miRNA", thismiRNA, "at", date()))
	targets <- predictions$mRNA[which(predictions$miRNA == thismiRNA)]	
	targetmask <- dimnames(mRNAexpression)[[2]] %in% targets

	#this line takes three minutes to execute.
	thisCol <- apply(mRNAexpression, 1, rescore, targets=targetmask, debug=debug)

	if(length(thisCol) > 0) {
		thisCol	
	} else {
		print(paste("RE score for", thismiRNA, "couldn't be calculated"))
	}
}

#miRNAname is the name of the miRNA in question. Probably don't need it
#targets is the list of mRNAs that are predicted to be targeted by the miRNA
#targets must be logical, and the same length as expression
#expression is the expression level of all of the mRNAs for one sample
#As described in Chen et al 2009. 10.1186/gb-2009-10-9-r90
rescore <- function(expression, targets, debug) {

	if(debug) {
		print(paste("  in rescore function", date()))
	}
	
	if(debug) {
		print(paste("  got expression", date()))
	}
	#predicted <- as.logical(targets)
	if(debug) {
		print(paste("  got expression booleans", date()))
	}
	if(length(targets) != length(expression)) {
		print(paste("Failure: length of expression vector != length of mask. Expression vector length: ", length(expression), " Mask length: ", length(predicted)))
		return
	}
	rankings <- rank(expression, ties.method="average")
	if(debug) {
		print(paste("Ranked expression data", date()))
	}
	predictaverage <- mean(rankings[which(targets)])

	nonpredictaverage <- mean(rankings[which(!targets)])


	if(debug) {
		print(paste("  two means", date()))
	}
	REScore <- nonpredictaverage - predictaverage
	REScore
}

#Unit tests for this analysis
runAllTests <- function() {
	outputtestfilename <- "unit_test_output.Rdata"
	unlink(outputtestfilename, recursive=FALSE)
	
	errorCount <- 0
	
	testRuns <- list()
	
	print(paste("begin test at ", date()))
	#correct data if the run is done correctly from input test files
	#mRNA_sample_rand.txt and predictions_sample.txt.
	correctNummRNAs <- as.numeric(4765)
	correctNummiRNAs <- as.numeric(4)
	correctHigherREmiRNA <- "hsa-miR-202"
	correctLowerREmiRNA <- "hsa-miR-500"
	testValue <- 0.000302344989288372
	statisticTestValue <- -1.3312664
	rediffTestValue <- -345.077342
	fdrTestValue <- 0.750
	#need a special tolerance value for the FDR calculation because of the random permutations.
	fdrToleranceValue <- .1
	
	#numeric tolerance for comparing floating-point numbers
	tol <- .0001
	
	colclasses <- c("numeric")
	length(colclasses) <- 1000
	colclasses[1] <- "character"
	colclasses[2:1000] <- "numeric"
	mRNA <- read.delim("mRNA_sample_rand.txt", header=TRUE, colClasses=colclasses, check.names=FALSE, row.names=1)

	predictions <- read.table(
							file="predictions_sample.txt", 
							sep="\t", header=FALSE, as.is=TRUE
						)
	names(predictions) <- c("mRNA", "miRNA")

	largegroupindexes <-as.integer(c(1,2,3))
	smallgroupindexes <- as.integer(c(4,5,6))

	
	testRun <- RE_score_analysis(predictions, mRNA, largegroupindexes, smallgroupindexes, outputtestfilename)
	
	if(testRun$miRNAsLowerREinSmallGroup[1] != correctLowerREmiRNA) {
		print(paste("lower miRNAs don't match: ", testRun$miRNAsLowerREinSmallGroup,
		"!=", correctLowerREmiRNA))
		errorCount <- errorCount + 1
	}
	if(testRun$miRNAsHigherREinSmallGroup[1] != correctHigherREmiRNA) {
		print(paste("higher miRNAs don't match: ", testRun$miRNAsHigherREinSmallGroup,
		"!=", correctHigherREmiRNA))
		errorCount <- errorCount + 1
	}
	if(testRun$NumbermRNAs != correctNummRNAs) {
		print(paste("Number of mRNAs is not correct:", testRun$NumbermRNAs, "!=", correctNummRNAs))
		errorCount <- errorCount + 1
	}
	if(testRun$NumbermiRNAs != correctNummiRNAs) {
		print(paste("Number of miRNAs is not correct:", 
		testRun$NumbermiRNAs, "!=", correctNummiRNAs))
		errorCount <- errorCount + 1
	}
	if(!isTRUE(all.equal(testRun$reCalc$pvalue[1], testValue, tolerance=tol))) {
		print(paste("reCalc contains incorrect values: ", 
		testRun$reCalc$pvalue[1], 
		"is not within", tol, 
		"of", testValue))
		errorCount <- errorCount + 1
	}
	if(!isTRUE(all.equal(testRun$reCalc$statistic[2], statisticTestValue, tolerance=tol))) {
		print(paste("reCalc statistic 2 contains incorrect values: ", 
			testRun$reCalc$statistic[2], 
			"is not within", tol, 
			"of", statisticTestValue))
		errorCount <- errorCount + 1
	}
	if(!isTRUE(all.equal(testRun$reCalc$reDiff[3], rediffTestValue, tolerance=tol))) {
		print(paste("reCalc reDiff 3 contains incorrect values: ", 
			testRun$reCalc$reDiff[3], 
			"is not within", tol, 
			"of", rediffTestValue))
		errorCount <- errorCount + 1
	}
	if(!isTRUE(all.equal(testRun$reCalc$fdr[4], fdrTestValue, tolerance=fdrToleranceValue))) {
		print(paste("reCalc fdr 4 contains incorrect values: ", testRun$reCalc$fdr[4], 
		"is not within", fdrToleranceValue, 
		"of", fdrTestValue))
		errorCount <- errorCount + 1
	}
	print(paste("end test at ", date()))
	print("****")
	print("*")
	if(errorCount == 0) {
		print("* All tests completed successfully")
	} else {
		print(paste("* ", errorCount, "errors were found."))
	}
	print("*")
	print("****")
	
	testRuns$refseqRun <- testRun
	testRuns$entrezRun <- runEntrezTests()
	testRuns
}

#Unit tests for this analysis
runEntrezTests <- function() {
	outputtestfilename <- "unit_test_output.Rdata"
	unlink(outputtestfilename, recursive=FALSE)
	
	errorCount <- 0
	
	print(paste("begin test at ", date()))
	#correct data if the run is done correctly from input test files
	#mRNA_sample_rand.txt and predictions_sample.txt.
	correctNummRNAs <- as.numeric(3558)
	correctNummiRNAs <- as.numeric(4)
	correctHigherREmiRNA <- "hsa-miR-202"
	correctLowerREmiRNA <- "hsa-miR-500"
	testValue <- 1.67581083372778e-05
	statisticTestValue <- -1.3312664
	rediffTestValue <- -299.517207655806
	fdrTestValue <- 0.750
	#need a special tolerance value for the FDR calculation because of the random permutations.
	fdrToleranceValue <- .1
	
	#numeric tolerance for comparing floating-point numbers
	tol <- .0001
	
	colclasses <- c("numeric")
	length(colclasses) <- 1000
	colclasses[1] <- "character"
	colclasses[2:1000] <- "numeric"
	mRNA <- read.delim("mRNA_sample_entrez.txt", header=TRUE, colClasses=colclasses, check.names=FALSE, row.names=1)

	predictions <- read.table(
							file="predictions_sample_entrez.txt", 
							sep="\t", header=FALSE, as.is=TRUE
						)
	names(predictions) <- c("refseq", "miRNA", "mRNA")

	largegroupindexes <-as.integer(c(1,2,3))
	smallgroupindexes <- as.integer(c(4,5,6))

	
	testRun <- RE_score_analysis(predictions[,c(2,3)], 
									mRNA, 
									largegroupindexes, 
									smallgroupindexes, 
									outputtestfilename,
									type="entrez")
	
	if(testRun$miRNAsLowerREinSmallGroup[1] != correctLowerREmiRNA) {
		print(paste("lower miRNAs don't match: ", testRun$miRNAsLowerREinSmallGroup,
		"!=", correctLowerREmiRNA))
		errorCount <- errorCount + 1
	}
	if(testRun$miRNAsHigherREinSmallGroup[1] != correctHigherREmiRNA) {
		print(paste("higher miRNAs don't match: ", testRun$miRNAsHigherREinSmallGroup,
		"!=", correctHigherREmiRNA))
		errorCount <- errorCount + 1
	}
	if(testRun$NumbermRNAs != correctNummRNAs) {
		print(paste("Number of mRNAs is not correct:", testRun$NumbermRNAs, "!=", correctNummRNAs))
		errorCount <- errorCount + 1
	}
	if(testRun$NumbermiRNAs != correctNummiRNAs) {
		print(paste("Number of miRNAs is not correct:", 
		testRun$NumbermiRNAs, "!=", correctNummiRNAs))
		errorCount <- errorCount + 1
	}
	if(!isTRUE(all.equal(testRun$reCalc$pvalue[1], testValue, tolerance=tol))) {
		print(paste("reCalc contains incorrect values: ", 
		testRun$reCalc$pvalue[1], 
		"is not within", tol, 
		"of", testValue))
		errorCount <- errorCount + 1
	}
	if(!isTRUE(all.equal(testRun$reCalc$statistic[2], statisticTestValue, tolerance=tol))) {
		print(paste("reCalc statistic 2 contains incorrect values: ", 
			testRun$reCalc$statistic[2], 
			"is not within", tol, 
			"of", statisticTestValue))
		errorCount <- errorCount + 1
	}
	if(!isTRUE(all.equal(testRun$reCalc$reDiff[3], rediffTestValue, tolerance=tol))) {
		print(paste("reCalc reDiff 3 contains incorrect values: ", 
			testRun$reCalc$reDiff[3], 
			"is not within", tol, 
			"of", rediffTestValue))
		errorCount <- errorCount + 1
	}
	if(!isTRUE(all.equal(testRun$reCalc$fdr[4], fdrTestValue, tolerance=fdrToleranceValue))) {
		print(paste("reCalc fdr 4 contains incorrect values: ", testRun$reCalc$fdr[4], 
		"is not within", fdrToleranceValue, 
		"of", fdrTestValue))
		errorCount <- errorCount + 1
	}
	print(paste("end test at ", date()))
	print("****")
	print("*")
	if(errorCount == 0) {
		print("* All tests completed successfully")
	} else {
		print(paste("* ", errorCount, "errors were found."))
	}
	print("*")
	print("****")
	
	testRun
}

tcgaDataREscoreCalculation <- function() {
	#done for the first time 7/22/11, desktop at DFCI
	setwd("C:/Users/eleanora/workspace/oxford/vignettes/REScore_calculation")
	source("RE_score_analysis.R")
	load(file="pitaPreds.Rdata")
	load("../../source-data/tcga-ovarian-2011/mRNA_expression/tcga.Rdata", envir=tcgamRNA)
		
	library("hgu133a.db")
	ls("package:hgu133a.db")

	xx <- as.list(hgu133aENTREZID)
	#map tcga affy ids to entrez
	mRNAexpression <- filteredtcgamRNA

	dimnames(mRNAexpression)[[2]] <- sapply(
										dimnames(mRNAexpression)[[2]],
										function(x) {xx[[which(names(xx) == x)]]}
									)


	predictions <- data.frame(mRNA=pitaPreds$entrez, miRNA=pitaPreds$miRNA, stringsAsFactors=FALSE)

	tcgareScores <- calcREScores(mRNAexpression, miRNAnames=unique(predictions$miRNA), predictions=predictions)

	setwd("C:/Users/eleanora/workspace/oxford/vignettes/miRNA_expression_vs_RE_lars")
	save(tcgareScores, file="tcgareScores.Rdata")
}