TypeError:'str'对象不可调用 - 填充数据库

时间:2015-06-09 12:51:54

标签: python mysql database biopython blast

我必须创建一个脚本,自动为我执行BLAST并自动填充数据库。 一切顺利,直到脚本试图填充数据库。然后我收到以下错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/rubenoldenkamp/anaconda/lib/python3.4/site-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 580, in runfile
    execfile(filename, namespace)
  File "/Users/rubenoldenkamp/anaconda/lib/python3.4/site-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 48, in execfile
    exec(compile(open(filename, 'rb').read(), filename, 'exec'), namespace)
  File "/Users/rubenoldenkamp/Documents/Python/BLASTscript.py", line 74, in <module>
    ReadFile()
  File "/Users/rubenoldenkamp/Documents/Python/BLASTscript.py", line 44, in ReadFile
    BLASTfor(SeqFor)
  File "/Users/rubenoldenkamp/Documents/Python/BLASTscript.py", line 72, in BLASTfor
    fillblast(titel_lijst, score_lijst, e_lijst, iden_lijst, pos_lijst, gaps_lijst)
  File "/Users/rubenoldenkamp/Documents/Python/BLASTscript.py", line 25, in fillblast
    cursor.execute("INSERT INTO `pg2`.`blast` (`blast_sequentie_id`,`blast_titel`, `blast_score`, `blast_evalue`, `blast_gaps`, `blast_positives`, `blast_identity`) VALUES (%s, %s, %s,%s, %s,%s, %s);"(i,titel_lijst[i], score_lijst[i], e_lijst[i], iden_lijst[i], pos_lijst[i], gaps_lijst[i]))
TypeError: 'str' object is not callable

我知道填充数据库的事情出错了,但我不知道我能解决什么问题。你能帮我么?这是我的代码:

import mysql.connector
import xlrd
from Bio.Blast import NCBIWWW
from Bio.Blast import NCBIXML

#Deze lijsten vullen met de sequentie header en sequentie uit het xlsx bestand!

def fillseq(SeqFor2, SeqFor3): 
    titels_lijst = SeqFor2
    sequenties_lijst = SeqFor3

    conn = mysql.connector.connect(host = "ithurtswhenip.nl", user = "pg2", password = "pg2", database= "pg2", port= "3307") 
    cursor = conn.cursor() 
    for i in range(0,len(titels_lijst)):    
        cursor.execute("INSERT INTO `pg2`.`sequenties` (`sequenties_id`,`sequenties_titel`, `sequenties_seq`) VALUES (%s,%s, %s);"(i,titels_lijst[i], sequenties_lijst[i]))
        print("1 record toegevoegd")
    cursor.commit()
    cursor.close() 
    conn.close()

def fillblast(titel_lijst, score_lijst, e_lijst, iden_lijst, pos_lijst, gaps_lijst): 
    conn = mysql.connector.connect(host = "ithurtswhenip.nl", user = "pg2", password = "pg2", database= "pg2", port= "3307") 
    cursor = conn.cursor() 
    for i in range(0,len(titel_lijst)):    
        cursor.execute("INSERT INTO `pg2`.`blast` (`blast_sequentie_id`,`blast_titel`, `blast_score`, `blast_evalue`, `blast_gaps`, `blast_positives`, `blast_identity`) VALUES (%s, %s, %s,%s, %s,%s, %s);"(i,titel_lijst[i], score_lijst[i], e_lijst[i], iden_lijst[i], pos_lijst[i], gaps_lijst[i]))
        print("1 record toegevoegd")
    cursor.commit()
    cursor.close() 
    conn.close()

def ReadFile():

    sh = xlrd.open_workbook('TestBLAST.xlsx').sheet_by_index(0)

    SeqFor = list()
    SeqFor2 = list()
    SeqFor3 = list() 

    for rownum in range(sh.nrows):
        SeqFor.append(sh.cell(rownum, 1).value)
        SeqFor2.append(sh.cell(rownum, 0).value)
        SeqFor3.append(sh.cell(rownum, 1).value)

    BLASTfor(SeqFor)
    fillseq(SeqFor2, SeqFor3)

def BLASTfor(SeqFor):

    sequence = SeqFor

    for ForwardSeq in sequence:
        results_handle = NCBIWWW.qblast("blastx","nr", ForwardSeq, hitlist_size = 1)
        bestand= open ("blast_report.xml", "w")
        bestand.writelines (results_handle.readlines())
        bestand.close()

        result = open("blast_report.xml", "r")
        blast_records = NCBIXML.parse(result)
        blast_record = next(blast_records)
        titel_lijst, score_lijst, e_lijst, iden_lijst, pos_lijst, gaps_lijst = [], [], [], [], [] , []
        E_VALUE_THRESH = 1
        for alignment in blast_record.alignments:
            for hsp in alignment.hsps:
                if hsp.expect < E_VALUE_THRESH:
                    titel_lijst.append(alignment.title)
                    score_lijst.append(hsp.score)
                    e_lijst.append(hsp.expect)
                    iden_lijst.append(hsp.identities)
                    pos_lijst.append(hsp.positives)
                    gaps_lijst.append(hsp.gaps)

        fillblast(titel_lijst, score_lijst, e_lijst, iden_lijst, pos_lijst, gaps_lijst)

ReadFile()
#BLASTfor()
#fillseq()
#z,x,c,v,b,n = fillblast()

1 个答案:

答案 0 :(得分:0)

cursor.execute行中,您需要在查询字符串和参数列表之间使用逗号。就目前而言,你有类似string(args)的东西,看起来像一个函数,因此你得到的错误是你不能称之为字符串。

cursor.execute("INSERT INTO `pg2`.`sequenties` (`sequenties_id`,`sequenties_titel`, `sequenties_seq`) VALUES (%s,%s, %s);", (i,titels_lijst[i], sequenties_lijst[i]))
                                                                                                                          ^ added comma right here

只需对所有cursor.execute语句执行此操作