2

Hii I am getting the following error in Biopython: 'return' outside function (filename.. line 26) Below is the code of myfile PLEASE HELP

# 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
4

3 に答える 3

8

Pythonはインデントに敏感です。コードがひどくインデントされている場合、それは機能しません。

私の驚くべきグーグルパワーは、あなたがこのページからあなたのコードを取り出したことを教えてくれますが、残念ながらコードも適切にフォーマットされていません。

しかし、ここで、私は努力をしました。私はそれを実行しなかったので、精神的にさえも、これが惨めに失敗するかどうかについては責任を負いません。

# 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
于 2011-05-19T10:02:25.367 に答える
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
于 2011-05-19T09:59:13.183 に答える
2

意図が間違っている

于 2011-05-19T09:57:16.457 に答える