0

我在 excel 中有一个 csv 文件,其中包含 BLAST 搜索的输出,格式如下:

# BLASTN 2.2.29+                                            
# Query: Cryptocephalus androgyne                                           
# Database: SANdouble                                           
# Fields: query id   subject id  % identity  alignment length    mismatches  gap opens   q. start    q. end  s. start    s. end  evalue  bit score
# 1 hits found                                          
Cryptocephalus  ctg7180000094003    79.59   637 110 9   38  655 1300    1935    1.00E-125   444
# BLASTN 2.2.29+                                            
# Query: Cryptocephalus aureolus                                            
# Database: SANdouble                                           
# Fields: query id   subject id  % identity  alignment length    mismatches  gap opens   q. start    q. end  s. start    s. end  evalue  bit score
# 4 hits found                                          
Cryptocephalus  ctg7180000093816    95.5    667 12  8   7   655 1269    1935    0   1051
Cryptocephalus  ctg7180000094021    88.01   667 62  8   7   655 1269    1935    0   780
Cryptocephalus  ctg7180000094015    81.26   667 105 13  7   654 1269    1934    2.00E-152   532
Cryptocephalus  ctg7180000093818    78.64   515 106 4   8   519 1270    1783    2.00E-94    340

我已将其作为 csv 导入 python 使用

with open('BLASToutput.csv', 'rU') as csvfile:
    contents = csv.reader(csvfile, delimiter=' ', quotechar='|')
    for row in contents:
        table = ', '.join(row)

我现在想要做的是将数据列提取为列表。我的总体目标是计算所有具有超过 98% 身份的匹配项(第三列)。

问题是,由于这不是典型的 csv 格式,因此顶部没有标题,因此我无法根据其标题提取列。我在想是否可以将第三列提取为列表,然后我可以在 python 中使用普通列表工具来提取我想要的数字,但我从未使用过 pythons csv 模块,我正在努力寻找合适的命令。SO上的其他问题类似,但不涉及我没有标题和空单元格的具体情况。如果您能帮助我,我将不胜感激!

4

2 回答 2

1

数据文件与 CSV 格式不同。它有注释,它的分隔符不是单个字符,而是格式化的空格。

由于您的总体目标是

计算所有具有超过 98% 身份的匹配项(第三列)。

并且数据文件内容格式正确,可以使用正常的文件解析方式:

import re

with open('BLASToutput.csv') as f:
    # read the file line by line
    for line in f:
        # skip comments (or maybe leave as it is)
        if line.startswith('#'):
            # print line
            continue
        # split fields
        fields = re.split(r' +', line)
        # check if the 3rd field is greater than 98%
        if float(fields[2]) > 98:
            # output the matched line
            print line
于 2014-05-01T09:37:51.133 回答
0

我设法基于以下方法找到了一种方法:

Python:使用多个拆分分隔符拆分文件

import csv

csvfile = open("SANDoubleSuperMatrix.csv", "rU")
dialect = csv.Sniffer().sniff(csvfile.read(1024))
csvfile.seek(0)
reader = csv.reader(csvfile, dialect)


identity = []

for line in reader:
    identity.append(line[2])

print identity
于 2014-05-01T09:39:16.793 回答