# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# ## Generate features for each protein sequence
# With each protein sequence generated and compiled using Bio.Entrez, the following script will generate corresponding features to train the prediction model and visualization of each protein's gene expression. 
#
# After this, WIDI's MODEL.

# +
# import relevant libraries

import Bio 
from Bio.SeqUtils.ProtParam import ProteinAnalysis
import pandas as pd

# +
# test Biopython functionality
ProtA = ProteinAnalysis("MAEGEITTFTALTEKFNLPPGNYKKPKLLYCSNGGHFLRILPDGTVDGT"
                    "RDRSDQHIQLQLSAESVGEVYIKSTETGQYLAMDTSGLLYGSQTPSEEC"
                    "LFLERLEENHYNTYTSKKHAEKNWFVGLKKNGSCKRGPRTHYGQKAILF"
                    "LPLPV")

MW = ProtA.molecular_weight()
count_AA = ProtA.count_amino_acids()
arom = ProtA.aromaticity()
iso_el = ProtA.isoelectric_point()

print(MW)
print(count_AA)
print(arom)
print(iso_el)
# -

# PSEUDOCODE: 
#
# take 1 protein sequence from dataframe (1 column, iterate through rows):
#     for each protein sequence
#         run MW
#         run count AA
#         run arom
#         run iso_el
#             output to new columns

# +
# generate overall structure for features generation

d = pd.read_csv('compiled_features.csv')
df = pd.DataFrame(d)

MW_features = []
count_features = []
arom_features = []
iso_features = []

for i in df['SEQUENCE'].values:
    ProtA = ProteinAnalysis(i)

    MW = ProtA.molecular_weight()
    MW_features.append(MW)
    
    #count_AA = ProtA.count_amino_acids()
    #count_features.append(count_AA)
    
    arom = ProtA.aromaticity()
    arom_features.append(arom)
    
    iso_e = ProtA.isoelectric_point()
    iso_features.append(iso_e)
    
df['MW'] = MW_features
#df['COUNT_AA'] = count_features
df['AROM'] = arom_features
df['ISO_E'] = iso_features


# -

def create_BioPy_features(sequence):
    """
    Taken-in dataframe with a column specified as 'SEQUENCE' and uses Biopython to 
    generate additional features from each protein sequence to help better train a machine
    learning model to predict log2FC
    """
    
    # initiate results lists
    MW_features = []
    count_features = []
    arom_features = []
    iso_features = []
    
    # Determine features for each element of 'SEQUENCE' column of dataframe
    ProtA = ProteinAnalysis(i)

    molwt_BP = ProtA.molecular_weight()

    aromatics = ProtA.aromaticity()

    isoelectric_point = ProtA.isoelectric_point()
    
    return molwt_BP, aromatics, isoelectric_point


# +
# testing new function

d = pd.read_csv('compiled_features.csv')
df = pd.DataFrame(d)

new_df = create_BioPy_features(df)
new_df

# +
df.head()
df.to_csv('compiled_features_complete.csv')

df
# -

df.iloc[0]

df['PROT_SEQ']

df.values



StarcoderdataJupyterScriptsDedupFiltered