This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Conversion from pairwise matrix to Cytoscape edge table is too slow

My code is similar to this. Given a matrix like this:

  a  b  c  d
a 1  NA 3  4
b NA 2  NA 4
c NA NA NA NA
d NA NA NA 4

It converts it to this:

a  a  1
a  c  3
a  d  4
b  b  2
b  d  4
d  d  4

The relevant code is as below:

  2 pears <- read.delim("pears.txt", header = TRUE, sep = "\t", dec = ".")
  3 edges <- NULL
  4 for (i in 1:nrow(pears)) {
  5         for (j in 1:ncol(pears)) {
  6                 if (!is.na(pears[i,j]))) {
  7                         edges <- rbind(edges, c(rownames(pears)[i], colnames(pears)[j], pears[i,j]))
  8                 }
  9         }
 10         print(i)
 11 }
 12 colnames(edges) <- c("gene1", "gene2", "PCC")
 13 write.table(edges, "edges.txt", row.names = FALSE, quote = FALSE, sep = "\t")

When I run the code from a remote server in the background using screen -S on a 17804x17804 sparse (99% NA) matrix, it initially runs 5 print statements every 13 seconds. However, it has now slowed down to 7 print statements every minute. Why is the algorithm getting slower and slower as it progresses? Is there another way I can convert my matrix into a Cytoscape's format quicker?

r cytoscape sparse-matrix

1 answer

OP answering their own question, but if anyone is looking back at this look here

Log in to answer this question.