使用inpu中的多个文件标识多个列表的交集

2024-10-02 02:32:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我想写一个需要元数据.txt作为一个输入,然后它识别不同输入文件中的共同基因,这些文件的名称是从元数据.txt文件。你知道吗

示例元数据.txt你知道吗

SIG1    SIG2
File1   File3
File2   File4
File3   File5
File4

我的目录中的文件是File1.xls,File2.xls,File3.xls…File6.xls。为简单起见,我对文件1和文件3以及文件2和4有相同的输入。你知道吗

文件1.xls或文件3.xls

TargetID    FoldChange  p-value Adjusted-p
A   0.543528215 0.000518847 0.000518847
B   0.638469898 0.00204759  0.00204759
C   1.936595724 0.00250229  0.00250229
D   0.657322154 0.012840013 0.012840013
E   1.728842021 0.00251105  0.00251105
F   2.024842641 0.000719261 0.000719261
G   4.049059413 2.25E-05    2.25E-05
H   0.478660942 0.000352179 0.000352179
I   0.449304016 0.000489521 0.000489521

文件2.xls或文件4.xls

TargetID    FoldChange  p-value Adjusted-p
JJ  0.453537892 4.22E-06    4.22E-06
A   0.558325503 0.001697851 0.001697851
B   0.637336564 7.64E-05    7.64E-05
D   1.804853034 0.000492439 0.000492439
E   0.378445825 1.72E-05    1.72E-05
JJJJ    1.601997491 0.019618883 0.019618883

文件5.xls

TargetID    FoldChange  p-value Adjusted-p
A   3.140223972 0.013347275 0.013347275
B   1.5205222   0.032318774 0.032318774
C   1.532760451 0.043763101 0.043763101
D   1.522865896 0.001791471 0.001791471

目标是输出两个文件“SIG1.txt”和“SIG2.txt”,这两个文件分别具有File1/File2和File3/File4/File5之间的共同基因。因此元数据提供了一个平台来迭代事物。 到目前为止,我得到的是:

md_input = pd.read_table("Metadata.txt", sep="\t")  #opens the metadata file

for c in range(0, len(md_input.columns)):
        first_file=md_input.ix[0,c]+".xls"
        print first_file #this will print "File1.xls" for column1 and File3.xls for column#2
        first_sig=pd.read_table(first_file, sep="\t", usecols=["TargetID", 'FoldChange']) #opens the first file
        list1=list(first_file.iloc[:,0]) #takes column of first file and converts to list
        #Then, I aim to iterate over the remaining files in each column of the metadata and find the intersection/common with each other. I tried the following:
        for i in range(1, md_input.count()[c]):
            list2=[]
            df=pd.read_table("{}.xls".format(md_input.ix[i,c]), sep="\t", usecols=["TargetID", 'FoldChange'])
            list2=list(df.iloc[:,0]) #assign the LIST
            common=list(set(list_up_0).intersection(set(list2))) #find intersection

print common

当我打印“common”时,我只得到最后一个文件的common。考虑到我是如何编写循环/代码的,这是应该的。我找不到一种方法来遍历列中的所有文件,使其保持打开状态,然后标识一个交集。你知道吗

请告知我是否需要进一步澄清上述内容。我知道这听起来很复杂,但不应该。我没有试图简化它,我希望它能奏效


Tags: 文件the数据txtinputcommonxlsmd
1条回答
网友
1楼 · 发布于 2024-10-02 02:32:32

我终于可以让它工作了。我不确定这是否是最简单的方法,但它是有效的。我认为下面脚本中令人困惑的部分是globals键的使用,它允许打开多个文件并根据for循环中的#分配文件名。不管怎样,这个脚本是有效的,它还考虑了折叠的变化。我希望这对其他人有用。你知道吗

md_input = pd.read_table('Metadata.txt', sep="\t")
list_cols=list(md_input.columns)
df=pd.DataFrame(columns=list_cols)
for c in range(0, len(md_input.columns)):
    sets_up=[]
    sets_down=[]
    for i in range(0, md_input.count()[c]):
        globals()["file_"+str(i)]=md_input.ix[i,c]+".xls"
        globals()["sig_"+str(i)]=pd.read_table(globals()["file_"+str(i)], sep="\t", usecols=["TargetID", 'FoldChange'])
        globals()["List_up"+str(i)]=[]
        globals()["List_down"+str(i)]=[]
        for z in range(0, len(globals()["sig_"+str(i)].index)):
            if globals()["sig_"+str(i)].ix[z,'FoldChange']>=1.5:
                globals()["List_up"+str(i)].append(globals()["sig_"+str(i)].iloc[z,0])
            elif globals()["sig_"+str(i)].ix[z,'FoldChange']<=1.5:
                globals()["List_down"+str(i)].append(globals()["sig_"+str(i)].iloc[z,0])
        sets_up.append(set(globals()["List_up"+str(i)]))
        sets_down.append(set(globals()["List_down"+str(i)]))

    common_up=list(set.intersection(*sets_up))
    common_down=list(set.intersection(*sets_down))
    common=common_up + common_down

    for x in range(0, len(common)):
        df.loc[x,md_input.columns[c]]=common[x]

df.to_csv("Output.xls",sep="\t", index=False)

相关问题 更多 >

    热门问题