Convert Adjacency Matrix Into Edgelist (csv File) For Cytoscape
I have a large (200 columns/rows) adjacency matrix in a csv file. This details interactions between individuals. I would like to convert this file into an edgelist, it can be done
Solution 1:
Get data:
m <- as.matrix(read.table(text="
A B C D
A 0 0 0 1
B 0 0 1 0
C 1 0 0 1",
header=TRUE))
How about
w <- which(m==1,arr.ind=TRUE)
data.frame(r=rownames(m)[w[,"row"]],
i=1,
c=colnames(m)[w[,"col"]])
## r i c
## 1 C 1 A
## 2 B 1 C
## 3 A 1 D
## 4 C 1 D
(Do you care about the order ... ?)
PCIT
assumes symmetry anyway, so that might be a problem for you.
Solution 2:
with open('input.csv') as infile:
infile.readline()
for row in csv.reader(infile, delimiter='\t'):
src = row[0]
weights = [int(i) for i in row[1:]]
for dest, weight in zip("ABC", weights):
if not weight: continue
print(src, weight, dest)
Post a Comment for "Convert Adjacency Matrix Into Edgelist (csv File) For Cytoscape"