简体   繁体   English

TypeError:“ str”对象不可调用-填充数据库时

[英]TypeError: 'str' object is not callable - When filling database

I have to make a script what automatic does a BLAST for me and than fill the database automatic. 我必须编写一个脚本,该脚本自动对我执行BLAST,然后自动填充数据库。 Everything goes wel until the script is trying to fill the database. 一切顺利,直到脚本尝试填充数据库为止。 Then I get the following error: 然后我得到以下错误:

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

I know something with filling the database is going wrong, but I don't know what and how I can solve it. 我知道填充数据库时出现了问题,但是我不知道该如何解决。 Can you please help me? 你能帮我么? This is my code: 这是我的代码:

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()

In your cursor.execute lines you need a comma between the query string and the arguments list. cursor.execute行中,查询字符串和参数列表之间需要逗号。 As it stands you have something like string(args) which looks like a function, thus you get the error that you can't call a string. 就目前而言,您拥有类似于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

Just do that for all your cursor.execute statements 只需对所有cursor.execute语句执行此操作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM