Question: Biopython for Beginner
0
gravatar for faisala85
4 months ago by
faisala850
faisala850 wrote:

Hi, I'm new to Biopython and programming. I installed Biopython and Numpy on my Mac. I think I have installed it correctly since when I do some of the basic commands in the help document it returns the expected hits. However, when I run

from Bio import SeqIO

for seq_record in SeqIO.parse("ls_orchid.gbk", "genbank"):
    printseq_record.id)
    print(repr(seq_record.seq))
    print(len(seq_record))

I get the error:
Traceback (most recent call last):
  File "/Users/faisalahmad/Documents/test2.py", line 3, in <module>
    for seq_record in SeqIO.parse("ls_orchid.gbk", "genbank"):
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/biopython-1.77-py3.8-macosx-10.9-x86_64.egg/Bio/SeqIO/__init__.py", line 627, in parse
    i = iterator_generator(handle)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/biopython-1.77-py3.8-macosx-10.9-x86_64.egg/Bio/SeqIO/InsdcIO.py", line 94, in __init__
    super().__init__(source, mode="t", fmt="GenBank")
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/biopython-1.77-py3.8-macosx-10.9-x86_64.egg/Bio/SeqIO/Interfaces.py", line 42, in __init__
    self.stream = open(source, "r" + mode)
FileNotFoundError: [Errno 2] No such file or directory: 'ls_orchid.gbk'

How do I fix this?

software error • 237 views
ADD COMMENTlink modified 4 months ago by flogin250 • written 4 months ago by faisala850
1

FileNotFoundError: [Errno 2] No such file or directory: 'ls_orchid.gbk'

There is your error, the file cannot be found in the directory you're in.

ADD REPLYlink written 4 months ago by ATpoint40k

Thanks for the help. I'm using Python 3.8 IDLE. I have "ls_orchid.gbk" saved in the same directory as the script. Is there another program I should be using that is easier? How do I get it to read the file in the directory?

ADD REPLYlink written 4 months ago by faisala850

You still have to tell any program that you use where the file is. The $HOME directory is the default working directory unless you change it. I suggest you read tutorials on the very basics of python (or programming in general), this is not python-specific.

ADD REPLYlink modified 4 months ago • written 4 months ago by ATpoint40k

It is not enough to have the script and file in the same directory if your working directory is different. You will need to be inside the same folder.

If in doubt, use the full path to the file,

ADD REPLYlink written 4 months ago by Joe18k

If in doubt, use the full path to the file,

not just the file, use full path to everything :-)

ADD REPLYlink written 4 months ago by RamRS30k

I suggest to you the use of argparse to avoid manual insertion of input files (https://docs.python.org/3/library/argparse.html).

You can easily to this:

import argparse
parser = argparse.ArgumentParser(description = 'some description')
parser.add_argument("-in", "--input", help="Input gbk file", required=True)
args = parser.parse_args()
input_file = args.input

for seq_record in SeqIO.parse(input_file, "genbank"):

In this way, you just call the script and the file:

python your_script.py -in ls_orchid.gbk

You also can use de sys.argv but with argparse you can pass well-documented help messages and set the arguments in any order, as well as you do not need the put the file in the same directory of your script, just pass the path to the desired file.

ADD REPLYlink modified 4 months ago • written 4 months ago by flogin250

How does this address the file not found error? If the file does not exist, this method will produce the same error. I'm moving this answer to a comment - I'll move it back to an answer if you edit it and address the underlying problem.

ADD REPLYlink written 4 months ago by RamRS30k

by the logics, if faisala85 can pass the path and the file name, the file exists...

ADD REPLYlink written 4 months ago by flogin250
1

Not true. I can pass a made up file path and file name to a script. That doesn't prove the existence of an equivalent file at all.

ADD REPLYlink written 4 months ago by Joe18k
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 998 users visited in the last hour