使用ete3的Python脚本来查询NCBI的分类标准:“ sqlite3警告(一次只能执行一条语句)”

时间:2018-11-29 05:39:21

标签: python taxonomy ncbi ete3

我正在使用此脚本:

import csv
import time
import sys
from ete3 import NCBITaxa

ncbi = NCBITaxa()

def get_desired_ranks(taxid, desired_ranks):
    lineage = ncbi.get_lineage(taxid)   
    names = ncbi.get_taxid_translator(lineage)
    lineage2ranks = ncbi.get_rank(names)
    ranks2lineage = dict((rank,taxid) for (taxid, rank) in lineage2ranks.items())
    return{'{}_id'.format(rank): ranks2lineage.get(rank, '<not present>') for rank in desired_ranks}

if __name__ == '__main__':
    file = open(sys.argv[1], "r")    
    taxids = []
    contigs = []
    for line in file:
        line = line.split("\n")[0]
        taxids.append(line.split(",")[0])
        contigs.append(line.split(",")[1])

    desired_ranks = ['superkingdom', 'phylum']
    results = list()
    for taxid in taxids:
        results.append(list())
        results[-1].append(str(taxid))
        ranks = get_desired_ranks(taxid, desired_ranks)
        for key, rank in ranks.items():
            if rank != '<not present>':
                results[-1].append(list(ncbi.get_taxid_translator([rank]).values())[0])
            else:
                results[-1].append(rank)

    i = 0
    for result in results:
        print(contigs[i] + ','),
        print(','.join(result))
        i += 1

    file.close()

该脚本从文件中提取出租车,并从NCBI分类法数据库的本地副本中获取其各自的血统。奇怪的是,当我在少量的出租车上(〜70,〜100)运行该脚本时,它运行良好,但是我的大部分数据集都超过了280k出租车,这些都破坏了脚本。

我得到这个完整的错误:

Traceback (most recent call last):
  File "/data1/lstout/blast/scripts/getLineageByETE3.py", line 31, in <module>
    ranks = get_desired_ranks(taxid, desired_ranks)
  File "/data1/lstout/blast/scripts/getLineageByETE3.py", line 11, in get_desired_ranks
    lineage = ncbi.get_lineage(taxid)   
  File "/data1/lstout/.local/lib/python2.7/site-packages/ete3/ncbi_taxonomy/ncbiquery.py", line 227, in get_lineage
    result = self.db.execute('SELECT track FROM species WHERE taxid=%s' %taxid)
sqlite3.Warning: You can only execute one statement at a time.

回溯中的前两个文件只是我上面引用的脚本,第三个文件是ete3的一个。正如我所说,该脚本适用于小型数据集。

我尝试过的事情:

  • 导入时间模块并在我的第11和31行的代码行之前/之后休眠数毫秒/百分之一秒。没有效果。
  • 转到ete3的代码中的第227行...

    result = self.db.execute('SELECT track FROM species WHERE taxid=%s' %merged_conversion[taxid])
    

,并将“执行”功能更改为“执行脚本”,以便能够一次处理多个查询(这似乎是问题所在)。这产生了一个新的错误,并导致我为自己的兔子洞改变了他们脚本中的小事情,试图使它起作用。没有结果。这是完整的冒犯功能:

    def get_lineage(self, taxid):
    """Given a valid taxid number, return its corresponding lineage track as a
    hierarchically sorted list of parent taxids.
    """
    if not taxid:
        return None
    result = self.db.execute('SELECT track FROM species WHERE taxid=%s' %taxid)
    raw_track = result.fetchone()
    if not raw_track:
        #perhaps is an obsolete taxid
        _, merged_conversion = self._translate_merged([taxid])
        if taxid in merged_conversion:
            result = self.db.execute('SELECT track FROM species WHERE taxid=%s' %merged_conversion[taxid])
            raw_track = result.fetchone()
        # if not raise error
        if not raw_track:
            #raw_track = ["1"]
            raise ValueError("%s taxid not found" %taxid)
        else:
            warnings.warn("taxid %s was translated into %s" %(taxid, merged_conversion[taxid]))

    track = list(map(int, raw_track[0].split(",")))
    return list(reversed(track))

令我非常困扰的是,这对少量数据有效!我正在从学校的高性能计算机上运行这些脚本,并尝试在其头节点和交互式Moab调度程序中运行。没有任何帮助。

0 个答案:

没有答案