<p>If you are using a conda version of python you can use Pymol module for this, which is actually more robust in some aspects than <code>bio.PDB</code>, although errors may cause segmentation faults. The following:</p>

<pre><code>import pymol2
## paralellisable version.
with pymol2.PyMOL() as pymol:
    pymol.cmd.fetch('1a9l')
    print(pymol.cmd.get_fastastr('polymer.nucleic'))
</code></pre>

<p>Gives me:</p>

<pre><code>&gt;1a9l_A
GGGUGACUCCAGAGGUCGAGAGACCGGAGAUAUCACCC
</code></pre>

<p>Alternatively, you can use PDBe's API.</p>

<pre><code>import requests

response = requests.get('https://www.ebi.ac.uk/pdbe/api/pdb/entry/molecules/1a9l').json()['1a9l']

for entity in response:
    if entity['molecule_type'] == 'polyribonucleotide':
        print(entity['sequence'])
</code></pre>

<p>Do note that missing 'residues' (residues, small molecules and nucleotides) will cause issues. PDB files do not have missing 'residues', while mmCIF files have a dictionary that does. The API will give you the latter.</p>

CC BY-SA 4.0
2020-02-11T15:21:40.037Z
Hi, I tried conda install -c schrodinger pymol to install pymol module. On import, it shows me an error : ModuleNotFoundError: No module named 'pymol2'
Is this the way to download it for use with conda?
shome
CC BY-SA 4.0
2020-02-11T15:53:44.387Z
`conda install -c schrodinger pymol` will install both. Does `import pymol` work? That is, is the version of python you are running the conda one? If so, single threaded `pymol` in the example runs as `import pymol` without the context manager line. Doing `conda search -c schrodinger pymol` for me (Ubuntu) gives me choices including pymol 2.1.1 for  py27_2 and py36_2. Do you get the same versions?
Matteo Ferla
CC BY-SA 4.0
2020-02-11T16:02:37.113Z
yes,I get same.No import pymol is also not working,yes I am running on same version of python as conda.
shome
CC BY-SA 4.0
2020-02-11T16:11:07.523Z
Okay, just to clarify: (a) there is no sudo'ing/nohup/systemctl service going on, (b) your `which python` (or whichever you are calling) and your `which conda` point to the same folder and (c) when you type `conda list pymol` it gives it as installed, correct?
Matteo Ferla
CC BY-SA 4.0
2020-02-11T16:12:13.260Z
I should have phrased it not as `which python`, but within your run `sys.executable` gives that one.
Matteo Ferla
CC BY-SA 4.0
2020-02-11T18:29:25.997Z
yes,it shows:
# Name                    Version                   Build  Channel
pymol                     2.3.4            py37h95b93ae_0    schrodinger
shome
CC BY-SA 4.0
2020-02-12T09:42:26.763Z
Installed with conda pymol is no different than numpy or pandas, so this is odd if the python is indeed correct one (cf. `sys.executable` ) and I have never had issues with it on dozens of systems (MacOS, Debian, RedHat) and architectures (x64, arm64) w/o issues. Uninstalling and reinstalling it?
Matteo Ferla
CC BY-SA 4.0
2020-02-12T09:43:13.520Z
Does `pip freeze | grep 'pymol' -` give the same result? Does `print([k for k in sys.modules.keys() if 'pymol' in k.lower()])` gives you something? If so, `pymol2 = importlib.import_module('pymol2')` just might work, but is nearly semantically equivalent to `import pymol2` so I highly doubt it.
Matteo Ferla
CC BY-SA 4.0
2020-02-12T18:52:37.847Z
I will try to troubleshoot this issue this week.If import issue still persists,will ask you.
shome
CC BY-SA 4.0
2020-02-11T13:12:11.250Z
2020-02-11T13:12:11.250Z
Matteo Ferla
<p>That <a href="http://www.rcsb.org/structure/1A9L" rel="nofollow noreferrer">PDB entry</a> is an RNA molecule, so there is no protein sequence to extract.</p>

<p>If it extracted the RNA sequence by default here you can see there could be an ambiguity as to whether it was an RNA sequence or a protein sequence, as the alphabets share characters.</p>

CC BY-SA 4.0
2020-02-10T19:01:16.310Z
Got it..So,I can't extract RNA sequence through this option.
shome
CC BY-SA 4.0
2020-02-11T11:31:25.570Z
Not using SeqIO from Biopython, no. The "pdb-seqres" option won't work here either. You could download the flat file of PDB sequences and read them into a Python dictionary: https://www.rcsb.org/pages/download_features#FASTA. Be aware though that sometimes atoms are missing in the ATOM records, which may have been why you wanted to read directly from the atoms in the first place.
jgreener
CC BY-SA 4.0
2020-02-10T14:03:09.313Z
2020-02-10T14:03:09.313Z
jgreener
<p>I am trying to obtain the sequence from PDB file, but no output is given when I run the following code :  </p>

<pre><code>from Bio import SeqIO

with open("1a9l.pdb", "r") as handle:
    for record in SeqIO.parse(handle, "pdb-atom"):
        print(record.seq)
</code></pre>

CC BY-SA 4.0
2020-02-10T14:07:20.270Z
If you only want **protein** sequences for an PDB code, NCBI has already a dataset (in genbank format) for that. Therein the accession IDs are the code underscore chain letter...
Matteo Ferla
CC BY-SA 4.0
2020-02-10T19:00:41.057Z
I was trying to do it through the script,as it is part of pipeline..
shome
<python>
No output when trying to obtain protein sequence from PDB file
# No output when trying to obtain protein sequence from PDB file

(Asked by: username_0 on 2020-02-09)

I am trying to obtain the sequence from PDB file, but no output is given when I run the following code :  
from Bio import SeqIO

with open("1a9l.pdb", "r") as handle:
    for record in SeqIO.parse(handle, "pdb-atom"):
        print(record.seq)

**Tags:** python
> **Comments:**
> **username_1**: If you only want **protein** sequences for an PDB code, NCBI has already a dataset (in genbank format) for that. Therein the accession IDs are the code underscore chain letter...  
> **username_0**: I was trying to do it through the script,as it is part of pipeline..  

---

## 2 answers

### Answer by username_1 on 2020-02-11. Score: 2 | Accepted answer

If you are using a conda version of python you can use Pymol module for this, which is actually more robust in some aspects than bio.PDB, although errors may cause segmentation faults. The following:
import pymol2
## paralellisable version.
with pymol2.PyMOL() as pymol:
    pymol.cmd.fetch('1a9l')
    print(pymol.cmd.get_fastastr('polymer.nucleic'))

Gives me:
>1a9l_A
GGGUGACUCCAGAGGUCGAGAGACCGGAGAUAUCACCC

Alternatively, you can use PDBe's API.
import requests

response = requests.get('https://www.ebi.ac.uk/pdbe/api/pdb/entry/molecules/1a9l').json()['1a9l']

for entity in response:
    if entity['molecule_type'] == 'polyribonucleotide':
        print(entity['sequence'])

Do note that missing 'residues' (residues, small molecules and nucleotides) will cause issues. PDB files do not have missing 'residues', while mmCIF files have a dictionary that does. The API will give you the latter.

> **Comments:**
> **username_0**: Hi, I tried conda install -c schrodinger pymol to install pymol module. On import, it shows me an error : ModuleNotFoundError: No module named 'pymol2' Is this the way to download it for use with conda?  
> **username_1**: `conda install -c schrodinger pymol` will install both. Does `import pymol` work? That is, is the version of python you are running the conda one? If so, single threaded `pymol` in the example runs as `import pymol` without the context manager line. Doing `conda search -c schrodinger pymol` for me (Ubuntu) gives me choices including pymol 2.1.1 for  py27_2 and py36_2. Do you get the same versions?  
> **username_0**: yes,I get same.No import pymol is also not working,yes I am running on same version of python as conda.  
> **username_1**: Okay, just to clarify: (a) there is no sudo'ing/nohup/systemctl service going on, (b) your `which python` (or whichever you are calling) and your `which conda` point to the same folder and (c) when you type `conda list pymol` it gives it as installed, correct?  
> **username_1**: I should have phrased it not as `which python`, but within your run `sys.executable` gives that one.  
> **username_0**: yes,it shows: # Name                    Version                   Build  Channel pymol                     2.3.4            py37h95b93ae_0    schrodinger  
> **username_1**: Installed with conda pymol is no different than numpy or pandas, so this is odd if the python is indeed correct one (cf. `sys.executable` ) and I have never had issues with it on dozens of systems (MacOS, Debian, RedHat) and architectures (x64, arm64) w/o issues. Uninstalling and reinstalling it?  
> **username_1**: Does `pip freeze | grep 'pymol' -` give the same result? Does `print([k for k in sys.modules.keys() if 'pymol' in k.lower()])` gives you something? If so, `pymol2 = importlib.import_module('pymol2')` just might work, but is nearly semantically equivalent to `import pymol2` so I highly doubt it.  
> **username_0**: I will try to troubleshoot this issue this week.If import issue still persists,will ask you.  

---

### Answer by username_2 on 2020-02-10. Score: 3

That [PDB entry](http://www.rcsb.org/structure/1A9L) is an RNA molecule, so there is no protein sequence to extract.

If it extracted the RNA sequence by default here you can see there could be an ambiguity as to whether it was an RNA sequence or a protein sequence, as the alphabets share characters.

> **Comments:**
> **username_0**: Got it..So,I can't extract RNA sequence through this option.  
> **username_2**: Not using SeqIO from Biopython, no. The "pdb-seqres" option won't work here either. You could download the flat file of PDB sequences and read them into a Python dictionary: https://www.rcsb.org/pages/download_features#FASTA. Be aware though that sometimes atoms are missing in the ATOM records, which may have been why you wanted to read directly from the atoms in the first place.  

---

