-
Notifications
You must be signed in to change notification settings - Fork 3
/
9.1_ADJs2sq_matrix.R
64 lines (52 loc) · 1.76 KB
/
9.1_ADJs2sq_matrix.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
64
################################################################################
# ADJs to Square matrix
# Based on wonderful: Diana Drago https://github.com/dianadrago
# Author: Miguel Angel Garcia-Campos - https://github.com/AngelCampos
################################################################################
# Install/load packages
if (!require("plyr")) {
install.packages("plyr", dependencies = TRUE)
library(plyr)
}
if (!require("minet")) {
biocLite("minet", ask =FALSE)
library(minet)
}
if (!require("igraph")) {
install.packages("igraph", dependencies = TRUE)
library(igraph)
}
# Getting file names
adjs <- sort(list.files(pattern="*.adj"))
# Number of ADJ must be equal to number ofgenes
print(paste("Number of ADJ to be coerced =", length(adjs)))
# Reads the last line from file take values as characters
# converts to matrix and assign first col as rownames
adjtosqmtx <- function(g){
a <- scan(file = g, skip = 17, what = "")
m <- matrix(unlist(a[-(1)]), ncol = 2, byrow = T)
rownames(m) <- c(m[,1])
m <- t(m[,2])
m
}
# Merges ADJS in square matrix and fills empty spaces with NA
M <- lapply(adjs, adjtosqmtx)
x <- t(rbind.fill.matrix(M))
# Replace all NA with 0
x[is.na(x)] <- 0
# Turn character matrix to numeric
class(x) <- "numeric"
# Set colnames as the gene name from ADJs name
names <- gsub('.{6}$','', adjs)
colnames(x) <- names
# Select rownames as colnames and orders them alphabetically
x <- x[sort(rownames(x)), sort(colnames(x)) ]
# Control: class must be 'matrix'
# Matrix must be symmetric
print(paste("Is object matrix? =", class(x))
print(paste("Is matrix square? =", isSymmetric(x))
# save squared matrix
if isSymmetric(x) {
write.table(x, file = "SQnetwork.txt", sep = "\t", col.names= NA, row.names= T,
quote = F )
}