-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathNMF.r
63 lines (50 loc) · 1.79 KB
/
NMF.r
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#######START
####supress warnings
options(warn=-1)
##CHECKING AND INSTALLING NMF MODULE###
print ("Checking and Installing NMF package in R")
if (!require("devtools")) install.packages("devtools",repos="http://cran.us.r-project.org")
print ("Loading required dependencies for devtools")
library(withr)
library(httr)
library(curl)
library(devtools)
if (!require("NMF")) install.packages("NMF", repos="http://cran.us.r-project.org")
if (!require("fastICA")) install.packages("fastICA", repos="http://cran.us.r-project.org")
######LOADING NMF LIBRARY########
print ("Loading NMF library")
rm(list=ls(all=TRUE))
library(NMF)
library(fastICA)
####Taking command line options
args = commandArgs(trailingOnly=TRUE)
path <- args[1]
filenameR <- args[2]
####SETTING THE DIRECTORY######
setwd(path)
file <- filenameR
#READING USER INPUT DATA
print ("Reading input data")
data = as.matrix(read.table(file, sep="\t", header=TRUE, row.names=1,check.names=F))
print ("This is how input data looks")
head(data)
#Convert to data frame
print ("Converting input data into data frame")
data2<-as.data.frame(data)
#Add dummy values to NA
data2[is.na(data2)] <- 123456789
#Use weights to balance out NA values
w<-as.matrix(data2)
w[w==123456789] <- 0
w[w!=0]<-1
number <- as.numeric(ncol(w))
valueuse <- as.integer(number*0.9)
#Estimate value of R
print ("Performing Nonnegative Matrix Factorization using ls-nmf")
estim.r <- nmf(data2, valueuse, 'ls-nmf', nrun = 100, seed = "ica", weight=w)
print ("Generating Consensus Heatmap")
#A heatmap to see consensus of 10 runs
outfile <- paste0(file, "_NMF", "_CONSENSUSMAP", ".pdf")
pdf(outfile)
consensusmap(estim.r, color = "-RdYlBu", distfun = function(x) as.dist(1 - x), hclustfun = "complete", Rowv = TRUE, Colv = TRUE, info = FALSE,labCol=colnames(estim.r),labRow=colnames(estim.r))
dev.off()