# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: 'Python 3.9.1 64-bit (''mypython'': conda)'
#     name: python37364bit00d4d3a58f944c1c8375f1b9a9835895
# ---

# # select Multi domain protein from pisces

# ## Read pisces text

import numpy as np
import pandas as pd
from prody import parsePDB
import os
import random
import time

pisces_df = pd.read_csv('../../../pisces/20210225/cullpdb_pc20_res2.0_R0.25_d210225_chains7584', delim_whitespace=True)
pisces_df['PDB_ID'] = pisces_df['IDs'].str[: 4]
pisces_df['Chain'] = pisces_df['IDs'].str[4]

pisces_df

# ## Read CATH text

# Read CATH domain text
cath_domain_df = pd.read_csv('../../../CATH/20191109/cath-domain-list.txt', delim_whitespace=True, skiprows=16, header=None)
cath_domain_df = cath_domain_df.drop(list(range(1, 12)), axis=1)
cath_domain_df = cath_domain_df.rename({0: 'CATH_Domain'}, axis=1)
cath_domain_df['IDs'] = cath_domain_df['CATH_Domain'].str[: 5].str.upper()
domain_num_df = pd.DataFrame(cath_domain_df.groupby('IDs').apply(len)).rename({0: 'Domain_num'}, axis=1).reset_index()
domain_num_df

# ## Concat pisces df and CATH df

cdf = pd.merge(pisces_df, domain_num_df, on='IDs', how='inner')
cdf

# ## select Multidomain entries

multidomain_df = cdf.query('Domain_num > 1')
multidomain_df

multidomain_df.sort_values('Domain_num')[-10: ]

multidomain_df_outpath = '../../../pisces/20210225/multidomain_cullpdb_pc20_res2.0_R0.25.csv'
multidomain_df.to_csv(multidomain_df_outpath)

# ## Get fasta sequence from multidomain df

from Bio import SeqIO
from pathlib import Path
pdb_fasta_path = '../../../PDBseq/pdb_seqres.txt'
out_fasta_dir = Path('../../../pisces/20210225/multi-domain_fasta/')
records_dict = SeqIO.to_dict(SeqIO.parse(pdb_fasta_path, 'fasta'))

for index, rows in multidomain_df.iterrows():
    seq_id = rows['PDB_ID'].lower() + '_' + rows['Chain']
    try:
        seq = records_dict[seq_id]
    except KeyError:
        print(seq_id)
    else:
        out_path = (out_fasta_dir / seq_id.upper()).with_suffix('.fasta')
        # SeqIO.write(seq, out_path, 'fasta')

StarcoderdataJupyterScriptsDedupFiltered