1

I wrote the following code to download multiple sequences from NCBI.

import numpy as np
from Bio import Entrez
Entrez.email ="[email protected]"
data = np.loadtxt('/home/Documents/XXX.txt', dtype="string")
data
array(['YP_615060', 'YP_615061', 'YP_615062', ..., 'YP_611146',
   'YP_611148', 'YP_611150'], 
  dtype='|S12')
ids=data[:10]
ids_1=data[10:20]
ids_1=",".join(ids_1)
ids_2=data[20:30]
ids_2=",".join(ids_2)
total=(ids, ids_1, ids_2)
for c in total:
    handle = Entrez.efetch(db="protein", id=c, rettype="fasta", retmode="txt")
handle.read()

I get an error

 File "<stdin>", line 3
handle.read()
     ^
SyntaxError: invalid syntax

I guess I am writing the 'foor' loop wrong, but I cannot get what's the problem. It is suppose to be a trivial issues, but I cannot find a way around it.

If I test the for loop and I do not call

handle.read()

running

>>>for c in total:
...     handle=Entrez.efetch(db="protein", id=c, rettype="fasta", retmode="txt")
... 

the for loop is still waiting for something. What am I missing here?

10
  • 1
    Try record = Entrez.read(handle) (see documentation) Commented Jan 9, 2015 at 11:28
  • 3
    I can't spot any syntax errors. Maybe it's on a previous line in your code? The line number is quite low, are you sure it's the correct file? Commented Jan 9, 2015 at 11:28
  • @efrem Note, that the handle.read() is not part of the loop block. Is that correct? Commented Jan 9, 2015 at 11:29
  • 1
    @ÁlvaroGómez Looks like mixed input and output form an interactive session. Commented Jan 9, 2015 at 11:31
  • 1
    See stackoverflow.com/questions/12188921/… Commented Jan 9, 2015 at 11:44

1 Answer 1

4

This is a wild guess, but your code looks like mixed input and output from an interactive session, and I can reproduce your error when pasting chunks of code in interactive mode:

Python 2.7.6 (default, Mar 22 2014, 22:59:56) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> for i in range(10):
...     print i
... print "done"
  File "<stdin>", line 3
    print "done"
        ^
SyntaxError: invalid syntax

Instead, try pasting only the for loop and it's body, then hit enter (twice), then paste the handle.read() line. (That is, assuming that this line is not meant to be part of the loop; if so, fix the indentation.) Alternatively, put all of that code into a file and run that file with python filename.py.

Sign up to request clarification or add additional context in comments.

2 Comments

you are right. I updated my question. I do not get why the for loop keeps waiting for a command. Shouldn't just run the command for each element in the string and stop?
@efrem Is it really "waiting"? When pasting code with indentation, you have to press enter twice to execute it. Or maybe that part of the code is just slow...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.