<jupyter_start><jupyter_text>Making the multiple interface-reporting dataframes generated via snakemake clearer by adding molecule names This notebook builds on the previous notebook, [Making multiple interface-reporting dataframes for several structures using snakemake](notebooks/making_multiple_interface_dataframes_using_snakemake.ipynb); however, you don't need to have just run that Jupyter notebook as the option to get a previous set of resulting dataframes and use them. The approach to add the names of the molecules to the dataframes, in place of the chain designations, used in the second notebook, [Making dataframes dervied from PDBePISA interface lists/reports clearer by adding protein names and filtering to nucleic acid chains](working_with_dataframes_and_making_clearer.ipynb), is scaled-up here.---- If you just ran the previous notebook in this session, you don't need to run this next cell; however, it has been set up to cause no issues if you did and run it again.  If you don't yet have the dataframes resulting from the previous notebook, it will get a set of demonstration results previously generated so that the rest of the cells in this notebook will work.<jupyter_code># Check if there seems to already be result dataframes. If there
# doesn't seem to be get one and unpack it.
suffix_for_int_dfs = "_PISAinterface_summary_pickled_df.pkl"
import os
import sys
import fnmatch
dfs_file_pattern = f"*{suffix_for_int_dfs}"
interface_dfs_files = []
for file in os.listdir('.'):
    if fnmatch.fnmatch(file, dfs_file_pattern):
        interface_dfs_files.append(file)
if not interface_dfs_files:
    !curl -OL https://gist.github.com/fomightez/28636f6cc6561e4892384bddc8900eb3/raw/e82fcb71bf3afafcea5764b4b2e794d2dd8f679a/collection_of_interface_dfsDec0720211917.tar.gz
    !tar xzf collection_of_interface_dfsDec0720211917.tar.gz
    # verify it worked and previde feedback
    for file in os.listdir('.'):
        if fnmatch.fnmatch(file, dfs_file_pattern):
            interface_dfs_files.append(file)
    if interface_dfs_files:
        sys.stderr.write("A set of interface dataframes like those the "
            "previous notebook would make have been retrieved.\nYou should be "
            "able to now run this notebok.")
    else:
        sys.stderr.write("No interface dataframes are present. THIS ISN'T GOING TO WORK!")
else:
    sys.stderr.write("A set of interface dataframes are present and\n"
        "executing the cells in this notebook should work now.")<jupyter_output>% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100  8866  100  8866    0     0  30891      0 --:--:-- --:--:-- --:--:-- 30891<jupyter_text>To define the replacements to make to make the notebooks more readable, the following cell will relate the text to change to new values in a set of key-value pairings. If you ran the demonstration and are continuing to use that, you can just run the cell. If you trying to make this notebook edit your own reports, you'll need to change the text on the left of the colon to match the chain designations and the text on the right to match the protein names. (The case of the letters in the text on the left side will be ignored.)<jupyter_code>chain2name_pairs = {
    "R":"WDR5",
    "N":"RBBP5",
    "K":"MLL1",
    "T":"SET1",
    "A":"Histone H3$_a$",
    "E":"Histone H3$_b$",
    "B":"Histone H4$_a$",
    "F":"Histone H4$_b$",
    "C":"Histone H2A$_a$",
    "G":"Histone H2A$_b$",
    "D":"Histone H2B.1$_a$",
    "H":"Histone H2B.1$_b$",
    "I":"DNA$_a$",
    "J":"DNA$_b$",
}<jupyter_output><empty_output><jupyter_text>(Note for H2B 1.1, using H2B.1 from names based on https://proteininformationresource.org/cgi-bin/nbrfget?uid=B92918 and https://proteininformationresource.org/cgi-bin/nbrfget?uid=B24510, that I found from following links under PIR at https://www.uniprot.org/uniprot/P02281 and https://www.uniprot.org/uniprot/P06900, respectively.)Then run the next cell to go through every notebook and swap the text on the left side of the colon to be the text on the right side.<jupyter_code># Make a list of the report-containing notebooks and then make the text swaps
suffix_for_int_dfs = "_PISAinterface_summary_pickled_df.pkl"
import os
import sys
import pandas as pd
import glob
import re
dfs_file_pattern = f"*{suffix_for_int_dfs}"
interface_dfs_files = glob.glob(dfs_file_pattern)
def make_swaps(file_name,key_value_pairs):
    '''
    Takes a file name for a pickled dataframe and loads it into memory edits 
    every occurence of each key in each dataframe to replace it with the 
    corresponding value and re-saves the dataframe to a pickled dataframe file.
    Resulting in a dataframe that can be read later that has text with the 
    corresponding value.
    Saves the edited, pickled file. Nothing is returned from this function.
    '''
    output_file_name = "temp.pkl"
    df = pd.read_pickle(file_name)
    for k,v in key_value_pairs.items():
        df = df.replace(k, v)
    #deal with special case of 6kiw having MLL3 and not MLL1
    if "6kiv" in file_name:
        df = df.replace("MLL1","MLL3")
    df.to_pickle(output_file_name)
    # replace the original file with edited
    !mv {output_file_name} {file_name}
    # Feedback
    sys.stderr.write("Chain designations swapped for molecule names in {}.\n".format(file_name))

for dff in interface_dfs_files:
    make_swaps(dff,chain2name_pairs)<jupyter_output>Chain designations swapped for molecule names in 6kix_PISAinterface_summary_pickled_df.pkl.
Chain designations swapped for molecule names in 6kiv_PISAinterface_summary_pickled_df.pkl.
Chain designations swapped for molecule names in 6kiu_PISAinterface_summary_pickled_df.pkl.
Chain designations swapped for molecule names in 6kiw_PISAinterface_summary_pickled_df.pkl.
Chain designations swapped for molecule names in 6kiz_PISAinterface_summary_pickled_df.pkl.<jupyter_text>Check the dataframe files by examining them. Here's examining one:<jupyter_code>dfm = pd.read_pickle("6kix_PISAinterface_summary_pickled_df.pkl")
dfm<jupyter_output><empty_output><jupyter_text>The chain designations should have been swapped for the names of the molecules.Notice this just uses a short, easily-edited script to achieve this with the idea that this step wouldn't be commonly done as most structures don't share the same designations, and thus adding in molecule names would be limited by having to make the key pair dictionaries and dealing with special cases. For example, even among this group, MLL3 is found in one structure whereas MLL1 is in the others; contrast row 9 of the dataframe below with row 9 in the top of that 6kiv dataframe displayed after next code cell below. (Note the `special case of 6kiw having MLL3 and not MLL1` lines dealing with this.)<jupyter_code>dfv = pd.read_pickle("6kiv_PISAinterface_summary_pickled_df.pkl")
dfv.head(10)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.07429946221341636, 'fraction_numerical': 0.02278516841211435, 'mean_word_length': 4.147123088128186, 'pattern_counts': {'":': 14, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 5, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '616547', 'license': 'MIT', 'n_tokens_mistral': 2311, 'n_tokens_neox': 2156, 'n_words': 812, 'path': 'notebooks/making the multiple dataframes generated via snakemake clearer by adding molecule names.ipynb', 'repo_name': 'fomightez/pdbepisa-binder', 'size': '7066'}