Skip to content

Commit

Permalink
Merge pull request #116 from goniochromatic/fix-to_pdb_stream
Browse files Browse the repository at this point in the history
Fix to pdb stream
  • Loading branch information
rasbt authored Mar 27, 2023
2 parents 79e6599 + d3e25b0 commit d0909b9
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 20 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
- Documentation: [https://BioPandas.github.io/biopandas/](https://BioPandas.github.io/biopandas/)
- Source code repository: [https://github.com/rasbt/biopandas](https://github.com/rasbt/biopandas)
- PyPI: [https://pypi.python.org/pypi/biopandas](https://pypi.python.org/pypi/biopandas)
- How to contribute: [https://BioPandas.github.io/biopandas//contributing/](https://BioPandas.github.io/biopandas/contributing/)
- How to contribute: [https://biopandas.github.io/biopandas/CONTRIBUTING/](https://biopandas.github.io/biopandas/CONTRIBUTING/)
- Changelog: [./docs/sources/CHANGELOG.md](./docs/sources/CHANGELOG.md)

<br>
Expand Down
40 changes: 21 additions & 19 deletions biopandas/pdb/pandas_pdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def __init__(self):

@property
def df(self):
"""Acccess dictionary of pandas DataFrames for PDB record sections."""
"""Access dictionary of pandas DataFrames for PDB record sections."""
return self._df

@df.setter
Expand Down Expand Up @@ -115,22 +115,25 @@ def read_pdb_from_list(self, pdb_lines):
self.header, self.code = self._parse_header_code()
return self


def fetch_pdb(self, pdb_code: Optional[str] = None, uniprot_id: Optional[str] = None, source: str = "pdb"):
"""Fetches PDB file contents from the Protein Databank at rcsb.org or AlphaFold database at https://alphafold.ebi.ac.uk/.
"""Fetches PDB file contents from the Protein Databank at rcsb.org or AlphaFold database
at https://alphafold.ebi.ac.uk/.
.
Parameters
----------
pdb_code : str, optional
A 4-letter PDB code, e.g., `"3eiy"` to retrieve structures from the PDB. Defaults to `None`.
A 4-letter PDB code, e.g., `"3eiy"` to retrieve structures from the PDB.
Defaults to `None`.
uniprot_id : str, optional
A UniProt Identifier, e.g., `"Q5VSL9"` to retrieve structures from the AF2 database. Defaults to `None`.
A UniProt Identifier, e.g., `"Q5VSL9"` to retrieve structures from the AF2 database.
Defaults to `None`.
source : str
The source to retrieve the structure from
(`"pdb"`, `"alphafold2-v1"`, `"alphafold2-v2"`, `"alphafold2-v3"` (latest)). Defaults to `"pdb"`.
(`"pdb"`, `"alphafold2-v1"`, `"alphafold2-v2"`, `"alphafold2-v3"` (latest)).
Defaults to `"pdb"`.
Returns
---------
Expand All @@ -144,13 +147,12 @@ def fetch_pdb(self, pdb_code: Optional[str] = None, uniprot_id: Optional[str] =
invalid_input_combination_2 = pdb_code is not None and source in {
"alphafold2-v1", "alphafold2-v2", "alphafold2-v3"}


if invalid_input_identifier_1 or invalid_input_identifier_2:
raise ValueError("Please provide either a PDB code or a UniProt ID.")

if invalid_input_combination_1 :
if invalid_input_combination_1:
raise ValueError("Please use a 'pdb_code' instead of 'uniprot_id' for source='pdb'.")
elif invalid_input_combination_2 :
elif invalid_input_combination_2:
raise ValueError(f"Please use a 'uniprot_id' instead of 'pdb_code' for source={source}.")

if source == "alphafold2-v1":
Expand Down Expand Up @@ -364,7 +366,6 @@ def _fetch_af2(uniprot_id: str, af2_version: int = 3):
print('URL Error %s' % e.args)
return url, txt


def _parse_header_code(self):
"""Extract header information and PDB code."""
code, header = "", ""
Expand Down Expand Up @@ -441,7 +442,7 @@ def _construct_df(pdb_lines):
record = line[:6].rstrip()
line_ele = ["" for _ in range(len(pdb_records[record]) + 1)]
for idx, ele in enumerate(pdb_records[record]):
line_ele[idx] = line[ele["line"][0] : ele["line"][1]].strip()
line_ele[idx] = line[ele["line"][0]: ele["line"][1]].strip()
line_ele[-1] = line_num
line_lists[record].append(line_ele)
else:
Expand Down Expand Up @@ -486,7 +487,7 @@ def amino3to1(self, record="ATOM", residue_col="residue_name", fillna="?"):
Parameters
----------
record : str, default: 'ATOM'
Specfies the record DataFrame.
Specifies the record DataFrame.
residue_col : str, default: 'residue_name'
Column in `record` DataFrame to look for 3-letter amino acid
codes for the conversion.
Expand Down Expand Up @@ -786,8 +787,8 @@ def get_models(self, model_indices: List[int]) -> PandasPdb:
Parameters
----------
model_index : int
An integer representing the model index to subset to.
model_indices : List[int]
A list representing the model indexes to subset to.
Returns
---------
Expand All @@ -812,14 +813,14 @@ def get_models(self, model_indices: List[int]) -> PandasPdb:
]
return df

def to_pdb_stream(self, records: List[str] = ["ATOM", "HETATM"]) -> StringIO:
def to_pdb_stream(self, records: tuple[str] = ("ATOM", "HETATM")) -> StringIO:
"""Writes a PDB dataframe to a stream.
Parameters
------------
records : List[str]
List of record names to save to stream. Any of `["ATOM", "HETATM", "OTHERS"]`.
records : iterable, default: ('ATOM', 'HETATM')
Iterable of record names to save to stream. Any of `["ATOM", "HETATM", "OTHERS"]`.
Returns
--------
io.StringIO : Filestream of PDB file.
Expand Down Expand Up @@ -861,4 +862,5 @@ def to_pdb_stream(self, records: List[str] = ["ATOM", "HETATM"]) -> StringIO:
to_write = "\n".join(s)
output.write(to_write)
output.write("\n")
return output.seek(0)
output.seek(0)
return output
15 changes: 15 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,21 @@ The CHANGELOG for the current development version is available at
[https://github.com/rasbt/biopandas/blob/main/docs/sources/CHANGELOG.md](https://github.com/rasbt/biopandas/blob/main/docs/sources/CHANGELOG.md).


### 0.5.1 (26-03-2023)

##### Downloads

- [Source code (zip)](https://github.com/rasbt/biopandas/archive/v0.5.1.zip)
- [Source code (tar.gz)](https://github.com/rasbt/biopandas/archive/v0.5.1.tar.gz)

##### Bug Fixes

- Fix the `return` statement in `PandasPdb.to_pdb_stream()` to return `output` instead of `output.seek(0)`. (via [goniochromatic](https://github.com/github.com/goniochromatic/), PR #[116](https://github.com/rasbt/biopandas/pull/116/files))
- Change the `records` default argument in `PandasPdb.to_pdb_stream()` to be immutable. (via [goniochromatic](https://github.com/github.com/goniochromatic/), PR #[116](https://github.com/rasbt/biopandas/pull/116/files))
- Fix some typos and general style issues. (via [goniochromatic](https://github.com/github.com/goniochromatic/), PR #[116](https://github.com/rasbt/biopandas/pull/116/files))
- Fix link for "How to contribute" in `README.md`. (via [goniochromatic](https://github.com/github.com/goniochromatic/), PR #[116](https://github.com/rasbt/biopandas/pull/116/files))


### 0.5.0dev (UNRELEASED)


Expand Down

0 comments on commit d0909b9

Please sign in to comment.