将邻接矩阵转换为csv文件 [英] Convert adjacency matrix to a csv file

查看:421
本文介绍了将邻接矩阵转换为csv文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用python(或可能的R)将ARACNE的邻接矩阵输出转换为csv文件。

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

adj文件设置为显示一个基因一个正确和每一个与其他基因的相互作用。例如:

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

如上所述,A和B互相交互,交互的值为0.4。 A和C彼此交互,值为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

基本上,我想要一个所有交互节点和相应的值的列表,以便我可以将文件上传到Cytoscape并绘制网络。

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.

推荐答案

一个简单的方法使用 csv p>

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]])






示例/ Demo -


Example/Demo -

我的 a.csv -

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

代码 -

>>> 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]])

输出在 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

这篇关于将邻接矩阵转换为csv文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆