question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Stream support for exporting pdbs

See original GitHub issue

Describe the workflow you want to enable

I’d like to be able to export a pdb to a stream instead of to disk. In particular the reason why I’d like to do so is so that I can pass the stream directly to wandb.Molecule

Describe your proposed solution

The PandasPdb.to_pdb method could accept a path_or_stream: typing.Union[io.StringIO, str] instead of just a path: str argument. Internally, if path_or_stream happens to be a io.StringIO object, we don’t need an openf function and instread can just execute the internal loops seen here, where f is now the io.StringIO object.

Making this change would enable inplace filling the stream with the pdb text.

Describe alternatives you’ve considered, if relevant

Currently I am needlessly writing to disk temporarily, reopening the file, and passing its contents to the wandb.Molecule object.

Additional context

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
a-r-jcommented, Aug 9, 2022

Hey @djberenberg I’ve actually done this already. Code to follow once I find it 😃 I agree this would be a nice feature for biopandas

2reactions
a-r-jcommented, Aug 9, 2022

Here you go:


def to_pdb_stream(df: pd.DataFrame) -> StringIO:
    """Writes a PDB dataframe to a stream.

    :param df: PDB dataframe
    :type df: pandas.DataFrame
    :return: StringIO Buffer
    :rtype: StringIO
    """

    df = df.copy().drop(columns=["model_id"])
    df.residue_number = df.residue_number.astype(int)
    records = [r.strip() for r in list(set(df.record_name))]
    dfs = {r: df.loc[df.record_name == r] for r in records}

    for r in dfs:
        for col in pdb_records[r]:
            dfs[r][col["id"]] = dfs[r][col["id"]].apply(col["strf"])
            dfs[r]["OUT"] = pd.Series("", index=dfs[r].index)

        for c in dfs[r].columns:
            # fix issue where coordinates with four or more digits would
            # cause issues because the columns become too wide
            if c in {"x_coord", "y_coord", "z_coord"}:
                for idx in range(dfs[r][c].values.shape[0]):
                    if len(dfs[r][c].values[idx]) > 8:
                        dfs[r][c].values[idx] = str(
                            dfs[r][c].values[idx]).strip()

            if c not in {"line_idx", "OUT"}:
                dfs[r]["OUT"] = dfs[r]["OUT"] + dfs[r][c]

    df = pd.concat(dfs, sort=False)
    df.sort_values(by="line_idx", inplace=True)

    output = StringIO()
    s = df["OUT"].tolist()
    for idx in range(len(s)):
        if len(s[idx]) < 80:
            s[idx] = f"{s[idx]}{' ' * (80 - len(s[idx]))}"
    to_write = "\n".join(s)
    output.write(to_write)
    output.write("\n")
    return output
Read more comments on GitHub >

github_iconTop Results From Across the Web

Using Data Pump to Move PDBs Within Or Between CDBs
Using Data Pump to Move PDBs Within Or Between CDBs. Data Pump export and import operations on PDBs are identical to those on...
Read more >
PDB Export & Import - Pluggable Database - YouTube
PDB Export & Import:Steps:1. Create DUMP directory:2. Table export and import at PDB :3. Schema export and import at PDB :4. PDB export...
Read more >
Export from pdb and import into pdb - ORACLE-HELP
In this post, we will see how we can take an export from one pdb and import into another pdb. ... Step 2:...
Read more >
PDB support (including export of types) - Cerbero Blog
All the types contained in the PDB can be exported to a Profiler header by pressing Ctrl+R and executing the 'Dump types to...
Read more >
Yes, you can export to a series of PDB files as follows
Yes, you can export to a series of PDB files as follows: 1. Select all of the entries you would like to export...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found