Biopython for Beginner
0
0
Entering edit mode
3.9 years ago
faisala85 • 0

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 • 1.7k views
ADD COMMENT
1
Entering edit mode

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 REPLY
0
Entering edit mode

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 REPLY
0
Entering edit mode

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 REPLY
0
Entering edit mode

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 REPLY
0
Entering edit mode

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

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

ADD REPLY
0
Entering edit mode

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 REPLY
0
Entering edit mode

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 REPLY
0
Entering edit mode

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

ADD REPLY
1
Entering edit mode

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 REPLY

Login before adding your answer.

Traffic: 2870 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6