Convert adjacency matrix to a csv file

こ雲淡風輕ζ 提交于 2020-01-15 07:36:21

问题


I want to convert the adjacency matrix output from ARACNE into a csv file using python (or possibly R).

The adj file is set up to show one gene one the right and each of its interactions with other genes. For example:

A B 0.4 C 0.3
B C 0.1 E 0.4
C D 0.2 E 0.3

So above, A and B interact with each other and the value of that interaction is 0.4. A and C interact with each other and the value is 0.3 and so on.

I want to change the layout so I get...

A B 0.4
A C 0.3
B C 0.1
B E 0.4
C D 0.2
C E 0.3

Basically I want a list of all interacting nodes and the corresponding values so that I can upload the file to Cytoscape and plot a network.


回答1:


A simple way to do this using csv module -

import csv
with open('<inputfile>','r') as f , open('<outputfile>','w') as of:
    reader = csv.reader(f, delimiter=' ')
    writer = csv.writer(of, delimiter=' ')
    for lines in reader:
            key = lines.pop(0)
            for i in range(0,len(lines),2):
                    writer.writerow([key, lines[i], lines[i+1]])

Example/Demo -

My a.csv -

A B 0.4 C 0.3
B C 0.1 E 0.4
C D 0.2 E 0.3

Code -

>>> import csv
>>> with open('a.csv','r') as f , open('b.csv','w') as of:
...     reader = csv.reader(f, delimiter=' ')
...     writer = csv.writer(of, delimiter=' ')
...     for lines in reader:
...             key = lines.pop(0)
...             for i in range(0,len(lines),2):
...                     writer.writerow([key, lines[i], lines[i+1]])

Output in b.csv -

A B 0.4
A C 0.3
B C 0.1
B E 0.4
C D 0.2
C E 0.3


来源:https://stackoverflow.com/questions/31837035/convert-adjacency-matrix-to-a-csv-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!