This is a test version of Biostars. For the public version, visit https://www.biostars.org.
'SyntaxError': 'return' outside function?

I have a super large file containing more than 1000 FASTA sequences merged. I want to extract the list of accession IDs from the file. But the following code got me an error. Can anybody tell me why and how to solve this? All my sequences contains a header file like this,

>NZ_QJRW01000001 (<'gap type:'>, <'description'>, 'gap start:', XXXXX, 'gap end:', XXXXX)**

Here is a snippet from the python script I have tried,

snippet from the code

biopython pattern python regex

if you want to stop the loop at the first time, there is no match use "break". if you want to skip the line with no match and continue use "continue"

Any particular reason you are doing this with python (other than perhaps learning python)? Or is there more to this script than just accession recovery. That can be done with a simple grep and cut otherwise.

$ cat your_file | grep "^>" | cut -f1 -d " " | sed 's/>//'
NZ_QJRW01000001

1 answer

Because the return keyword is only allowed inside function definitions:

def biggerthanfive(x):
    if x > 5:
        return True
    else:
        return False

In your example, you are only using a loop that you can break, but not return from. You can also write it way more succinct.

Log in to answer this question.