将txt文件转换为邻接矩阵

Convert a txt file to a adjacency matrix

我有一个与 posted 类似的问题

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 the file of a.csv as:

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 the file b.csv as ...

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.

在这个 post 中,有一个使用 Python 的很棒的答案。如果我想将 b.csv 的格式转换回 a.csv 怎么办?我挠了挠头,但找不到解决办法。我很想看看 Python 是如何施展魔法的!

感谢您的回答。 -小勇

这可以使用 pandas.Dataframe.groupby

来完成
import pandas as pd

df = pd.read_csv('b.csv', delimiter=' ', header=None)

data = '' 
for key, value in df.groupby([0]):
    for i,item in enumerate(value.values):
        if i == 0:
            data += item[0]
        for itm in item[1:]:
            data += ' '+str(itm)
    data += '\n'

#Saving the string
with open('a.csv', 'w') as f:
    f.write(data)

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
C F 0.3
A D 0.3

生成 a.csv 是:

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