0

我有 2 个(相当大,约 15k 行)csv 表,格式如下:

Disease/Trait                Mapped_gene    p-Value 
Wegener's granulomatosis    HLA-DPB1        2.00E-50    
Wegener's granulomatosis    TENM3 - DCTD    2.00E-06    
Brugada syndrome            SCN5A           1.00E-14    
Brugada syndrome            SCN10A          1.00E-68    
Brugada syndrome            HEY2 - NCOA7    5.00E-17    
Major depressive disorder   IRF8 - FENDRR   3.00E-07    


Identifier  Homologues  Symbol
CG11621     5286    HEY2
CG11621     5287    IRF8
CG11621     5287    PIK3C2B
CG11621     5288    PIK3C2G
CG11621     5288    PIK3C2G
CG11949     2035    DCTD
CG11949     2035    EPB41
CG11949     2036    EPB41L1
CG11949     2037    EPB41L2

我想使用 Python 来比较表,这样如果表 2 中的任何“符号”列与表 1 中的“Mapped_gene”匹配,则每个表中的匹配行可以合并在一起并放入输出文件中。

我试过使用 Pandas 插件,但无法正常工作。有没有人有更好的想法?

谢谢。

4

1 回答 1

0

这应该可以按您的意愿工作:

import csv

diseases = {}

# Load the disease file in memory
with csv.reader(open('table1.csv', 'rb')) as dfile:
    # Skip the header
    dfile.next()
    for disease, gene, pvalue in dfile:
        diseases[gene] = (disease, pvalue)

with csv.reader(open('table2.csv', 'rb')) as idfile, csv.writer(open('output.csv', 'wb')) as output:
    # Skip the header
    idfile.next()
    for ident, homologue, symbol in idfile:
        if symbol in diseases:
            output.writerow((ident, homologue, symbol) + diseases[symbol])

但是,它假定下面的每个基因名称Mapped_gene都是唯一的。它可以很容易地扩展以应对重复,否则。

于 2014-02-11T13:09:55.577 回答