Skip to content

Commit fbb2008

Browse files
Add files via upload
1 parent 2a55003 commit fbb2008

File tree

2 files changed

+436
-0
lines changed

2 files changed

+436
-0
lines changed

CSmiRsyn.R

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
## Function for discovering cell-specific miRNA-miRNA positive correlation
2+
# miR1 and miR2: Gene expression values of two miRNAs in single cells
3+
# cell_index: Index of single cells
4+
# boxsize: Size of neighborhood (0.1 in default)
5+
# interp_betw_point: The number of interpolation points between each cell (5 in default), interp_betw_point = 0 is used to compute
6+
# the normalized statistic of edge gx-gy in large number of cells (more than 100)
7+
# bootstrap_num: The number of bootstrapping for interpolating pseudo-cells
8+
# Output: res is a list of cell-specific miRNA-miRNA positive correlation
9+
CSmiRsyn_edge_bootstrap <- function(miR1, miR2, cell_index, boxsize = 0.1, bootstrap_betw_point = 5, bootstrap_num = 100) {
10+
11+
cell_num <- length(miR1)
12+
set.seed(123)
13+
bootstrap_sample <- lapply(seq(bootstrap_num), function(i) sample(seq(cell_num), bootstrap_betw_point * (cell_num - 1), replace = TRUE))
14+
miR1_bootstrap <- lapply(seq(bootstrap_num), function(i) c(miR1, miR1[bootstrap_sample[[i]]]))
15+
miR2_bootstrap <- lapply(seq(bootstrap_num), function(i) c(miR2, miR2[bootstrap_sample[[i]]]))
16+
res <- do.call(pmedian, lapply(seq(bootstrap_num),
17+
function(k) csn_edge(miR1_bootstrap[[k]],
18+
miR2_bootstrap[[k]],
19+
boxsize = boxsize)[seq(cell_num)]))
20+
21+
return(res[cell_index])
22+
}
23+
24+
## Identifying cell-specific miRNA synergistic network
25+
CSmiRsyn_net <- function(miRTarget, cell_index, miRExp, mRExp, minSharedmR = 1, p.value.cutoff = 0.05) {
26+
27+
miRTarget <- as.matrix(miRTarget)
28+
miRExpNames <- as.matrix(colnames(miRExp))
29+
30+
miR <- miRTarget[, 1]
31+
mR <- miRTarget[, 2]
32+
33+
miRSym <- unique(miR)
34+
mRSym <- unique(mR)
35+
36+
m2 <- length(miRSym)
37+
38+
# Initialize variables
39+
miRInt <- matrix(NA, m2 * (m2 - 1)/2, 2)
40+
C <- matrix(NA, m2 * (m2 - 1)/2, 3)
41+
42+
for (i in seq_len(m2 - 1)) {
43+
for (j in seq(i + 1, m2)) {
44+
45+
Interin1 <- miRTarget[which(miRTarget[, 1] %in% miRSym[i]), 2]
46+
Interin2 <- miRTarget[which(miRTarget[, 1] %in% miRSym[j]), 2]
47+
48+
M1 <- length(Interin1)
49+
M2 <- length(Interin2)
50+
M3 <- length(intersect(Interin1, Interin2))
51+
M4 <- length(mRSym)
52+
M5 <- 1 - phyper(M3 - 1, M2, M4 - M2, M1)
53+
54+
if (M3 >= minSharedmR & M5 < p.value.cutoff) {
55+
56+
miRInt[(i - 1) * m2 + j - sum(seq_len(i)), 1] <- miRSym[i]
57+
miRInt[(i - 1) * m2 + j - sum(seq_len(i)), 2] <- miRSym[j]
58+
59+
miRExpIdx1 <- which(miRExpNames %in% miRSym[i])
60+
miRExpIdx2 <- which(miRExpNames %in% miRSym[j])
61+
62+
# Calculate cell-specific correlation of each miRNA-miRNA pair
63+
M6 <- CSmiRsyn_edge_bootstrap(miRExp[, miRExpIdx1], miRExp[, miRExpIdx2], cell_index)
64+
M7 <- pnorm(-M6)
65+
66+
C[(i - 1) * m2 + j - sum(seq_len(i)), 1] <- M5
67+
C[(i - 1) * m2 + j - sum(seq_len(i)), 2] <- M6
68+
C[(i - 1) * m2 + j - sum(seq_len(i)), 3] <- M7
69+
70+
}
71+
}
72+
}
73+
74+
# Extract miRNA-miRNA pairs
75+
miRInt <- miRInt[which((C[, 1] < p.value.cutoff & C[, 3] < p.value.cutoff) == "TRUE"), ]
76+
77+
C <- C[which((C[, 1] < p.value.cutoff & C[, 3] < p.value.cutoff) == "TRUE"), ]
78+
79+
if (is.vector(C)) {
80+
res_miRInt <- c(miRInt, C)
81+
names(res_miRInt) <- c("miRNA_1", "miRNA_2", "p_value of shared mRNAs", "correlation", "p_value of correlation")
82+
83+
} else {
84+
res_miRInt <- cbind(miRInt, C)
85+
colnames(res_miRInt) <- c("miRNA_1", "miRNA_2", "p_value of shared mRNAs", "correlation", "p_value of correlation")
86+
}
87+
88+
return(res_miRInt)
89+
}
90+
91+
## Function for calculating similarity matrix between two list of module groups
92+
# Module.group1: List object, the first list of module group
93+
# Module.group2: List object, the second list of module group
94+
# Output: Sim is a similarity matrix between two list of module groups
95+
Sim.module.group <- function(Module.group1, Module.group2){
96+
97+
if(class(Module.group1)!="list" | class(Module.group2)!="list") {
98+
stop("Please check your input module group! The input module group should be list object! \n")
99+
}
100+
101+
m <- length(Module.group1)
102+
n <- length(Module.group2)
103+
Sim <- matrix(NA, m, n)
104+
105+
for (i in seq(m)){
106+
for (j in seq(n)){
107+
overlap_interin <- length(intersect(Module.group1[[i]], Module.group2[[j]]))
108+
Sim[i, j] <- overlap_interin/min(length(Module.group1[[i]]), length(Module.group2[[j]]))
109+
}
110+
}
111+
112+
if (m < n) {
113+
GS <- mean(unlist(lapply(seq(m), function(i) Sim[i, max.col(Sim)[i]])))*m/n
114+
} else if (m == n) {
115+
GS <- mean(c(unlist(lapply(seq(m), function(i) Sim[i, max.col(Sim)[i]])),
116+
unlist(lapply(seq(n), function(i) Sim[max.col(t(Sim))[i], i]))))
117+
} else if (m > n) {
118+
GS <- mean(unlist(lapply(seq(n), function(i) Sim[max.col(t(Sim))[i], i])))*n/m
119+
}
120+
121+
return(GS)
122+
}
123+
124+
## Function for calculating cluster coefficients in random networks
125+
# nodes.num: The number of nodes
126+
# edges.num: The number of edges
127+
# perm: The number of permutation
128+
# directed: Logical value, false or true
129+
# Output: Mean and std of cluster coefficients in random networks
130+
Random_net_clusterCoeff <- function(nodes.num, edges.num, perm = 10000, directed = FALSE) {
131+
set.seed(123)
132+
res <- c()
133+
for (i in seq(perm)) {
134+
g <- sample_pa(n = nodes.num, m = edges.num, directed = directed)
135+
g <- delete_edges(g, sample(1:gsize(g), size = gsize(g) - edges.num))
136+
res[i] <- transitivity(g, type="average")
137+
}
138+
139+
return(list(mean(res), sd(res)))
140+
}
141+

0 commit comments

Comments
 (0)