返回外部功能

时间:2011-05-19 09:40:07

标签: python biopython

Hii我在Biopython中遇到以下错误:'return'在函数外部(文件名..第26行) 下面是myfile的代码 请帮助

# File Name RandonProteinSequences.py
# standard library
import os
import random

# biopython
from Bio.Seq import Seq
from Bio.Alphabet import IUPAC
from Bio.SeqRecord import SeqRecord
import Bio.writers.SeqRecord.fasta
from Bio import SeqIO
from sys import *

residueList1 = ["C","D","E","F","G","H","I"]
residueList2 = ["A","K","L","M","N","S"]
residueList3 = ["P","Q","R","T","V","W","Y"]
residueList4 = ["C","A","G","U"]
def getProteinSeqRecord(residue, seqcount):
    strSeq = ""
for i in range(0,100,1):
    index = random.randint(0, len(residue)-1)
    strSeq += residue[index]

sequence = Seq(strSeq, IUPAC.IUPACProtein)
seqRec = SeqRecord(sequence, id = 'randSeq' + str(seqcount), description= 'A random sequence using Amino acid residues.')
return seqRec

def getProteinSequence(residue):
    strSeq = ""
for i in range(0,100,1):
    index = random.randint(0, len(residue)-1)
strSeq += residue[index]

sequence = Seq(strSeq, IUPAC.IUPACProtein)
return sequence

def randomProteinSeqRecord(index):
    if(index%2)==0:
        return getProteinSeqRecord(residueList1, index)
    elif(index%3)==0:
        return getProteinSeqRecord(residueList2, index)
    else:
        return getProteinSeqRecord(residueList3, index)

#information
print '--- This is python based program to generate random sequences ---'
print '--- Provide number of random sequences to generate. Default 10 ---'
print '--- Inorder to save to a file provide file path or filename ---'
print '--- If none or invalid filepath is provided then results will be displayed to console ---'
print '--- The file will be created in fasta format ---'
print

filepathProvided = False
#raw_input received the user input as string
try:
    filepath = raw_input('Enter filepath to save sequences ... ')
    filepath = filepath + '.fasta'
    handle = open(filepath, "w")
    handle.close()

    filepathProvided = True
except IOError:
    print 'Invalid or No File provided will print results to console'
print
ranSeqCount = 10
try:
    ranSeqCount = int(raw_input('Enter number of random sequences to generate ... '))
except ValueError:
    ranSeqCount = 10
pass

if(filepathProvided):
    handle = open(filepath, "w")

if(filepathProvided):
    fasta_writer = Bio.writers.SeqRecord.fasta.WriteFasta(handle)
else:
    fasta_writer = Bio.writers.SeqRecord.fasta.WriteFasta(stdout)
print 'Sequence Count : '
print ranSeqCount

for i in range(0,ranSeqCount,1):
    fasta_writer.write(randomProteinSeqRecord(i+1))
if(filepathProvided):
    handle.close()
print 'File created at : ' + filepath

print
raw_input('Press any key to exit ...')
print

3 个答案:

答案 0 :(得分:8)

Python对缩进很敏感。如果您的代码严重缩进,则无法正常工作。

我惊人的谷歌搜索能力告诉我你已经从this page获取了你的代码,遗憾的是代码也没有正确格式化。

但是在这里,我付出了努力。如果这会失败,我不负责任,因为我没有运行它,甚至没有精神上。

# File Name RandonProteinSequences.py
# standard library
import os
import random

# biopython
from Bio.Seq import Seq
from Bio.Alphabet import IUPAC
from Bio.SeqRecord import SeqRecord
import Bio.writers.SeqRecord.fasta
from Bio import SeqIO
from sys import *

residueList1 = ["C","D","E","F","G","H","I"]
residueList2 = ["A","K","L","M","N","S"]
residueList3 = ["P","Q","R","T","V","W","Y"]
residueList4 = ["C","A","G","U"]
def getProteinSeqRecord(residue, seqcount):
    strSeq = ""
    for i in range(0,100,1):
        index = random.randint(0, len(residue)-1)
        strSeq += residue[index]

    sequence = Seq(strSeq, IUPAC.IUPACProtein)
    seqRec = SeqRecord(sequence, id = 'randSeq' + str(seqcount), description= 'A random sequence using Amino acid residues.')
    return seqRec

def getProteinSequence(residue):
    strSeq = ""
    for i in range(0,100,1):
        index = random.randint(0, len(residue)-1)
        strSeq += residue[index]

    sequence = Seq(strSeq, IUPAC.IUPACProtein)
    return sequence

def randomProteinSeqRecord(index):
    if(index%2)==0:
        return getProteinSeqRecord(residueList1, index)
    elif(index%3)==0:
        return getProteinSeqRecord(residueList2, index)
    else:
        return getProteinSeqRecord(residueList3, index)

#information
print '--- This is python based program to generate random sequences ---'
print '--- Provide number of random sequences to generate. Default 10 ---'
print '--- Inorder to save to a file provide file path or filename ---'
print '--- If none or invalid filepath is provided then results will be displayed to console ---'
print '--- The file will be created in fasta format ---'
print

filepathProvided = False
#raw_input received the user input as string
try:
    filepath = raw_input('Enter filepath to save sequences ... ')
    filepath = filepath + '.fasta'
    handle = open(filepath, "w")
    handle.close()

    filepathProvided = True
except IOError:
    print 'Invalid or No File provided will print results to console'
print
ranSeqCount = 10
try:
    ranSeqCount = int(raw_input('Enter number of random sequences to generate ... '))
except ValueError:
    ranSeqCount = 10
pass

if(filepathProvided):
    handle = open(filepath, "w")

if(filepathProvided):
    fasta_writer = Bio.writers.SeqRecord.fasta.WriteFasta(handle)
else:
    fasta_writer = Bio.writers.SeqRecord.fasta.WriteFasta(stdout)
print 'Sequence Count : '
print ranSeqCount

for i in range(0,ranSeqCount,1):
    fasta_writer.write(randomProteinSeqRecord(i+1))
if(filepathProvided):
    handle.close()
print 'File created at : ' + filepath

print
raw_input('Press any key to exit ...')
print

答案 1 :(得分:5)

Python依赖于缩进来确定函数(和其他块结构)的结束位置。据推测,导致错误的函数应如下所示:

def getProteinSeqRecord(residue, seqcount):
    strSeq = ""
    for i in range(0,100,1):
        index = random.randint(0, len(residue)-1)
        strSeq += residue[index]

    sequence = Seq(strSeq, IUPAC.IUPACProtein)
    seqRec = SeqRecord(sequence, id = 'randSeq' + str(seqcount), description= 'A random sequence using Amino acid residues.')
    return seqRec

答案 2 :(得分:2)

你的意图是错误的