application_vnd.jupyter.stderr_output_1.png
import pandas as pd

def sequence_length_filter(df):
    """checks every protein sequence in 'Sequence' Column via for loop
    stores length of each sequence in sequence_length object
    if sequence_length is more than 6000 or less than 50
    then drops that row where that particular sequence belongs
    updates the dataframe"""
    df.dropna(inplace=True)
    row_index = 0
    for sequence in df['Sequence']:
        sequence_length = len(str(sequence))
        if sequence_length > 6000 or sequence_length < 50:
            df.drop(df.index[row_index], inplace=True)
            row_index += 1
    return df

def irregular_sequence_filter(df):
    """checks every protein sequence in 'Sequence' Column via for loop
    then enumerate through every character and index of that character in that sequence within same for loop
    uses drop method to drop the particular row from dataframe where sequence character 'X' and 'Z' matches
    updates the dataframe"""
    for sequence in df['Sequence']:
        for index, character in enumerate(sequence):
            if character == 'U':
                df.drop(df.loc[df['Sequence'] == sequence].index, inplace=True)
            elif character == 'X':
                df.drop(df.loc[df['Sequence'] == sequence].index, inplace=True)
            elif character == 'Z':
                df.drop(df.loc[df['Sequence'] == sequence].index, inplace=True)
    return df

df_binding = sequence_length_filter(pd.read_csv('/kaggle/input/rna-binding-and-non-bindinghuman/NON_RNA_binding_human.csv'))
df_binding = irregular_sequence_filter(df_binding)
df_binding['class'] = 'RNA binding'
df_nonBinding = sequence_length_filter(pd.read_csv('/kaggle/input/rna-binding-and-non-bindinghuman/RNA_binding_human.csv'))
df_nonBinding = irregular_sequence_filter(df_nonBinding)
df_nonBinding['class'] = 'NON-RNA binding'

df_merged = pd.concat([df_binding, df_nonBinding], ignore_index=True, sort=False)
df = df_merged.sample(frac=1).reset_index(drop=True)

def integer_encoding(data):
    """
    - Encodes code sequence to integer values.
    - 20 common amino acids are taken into consideration
    and rest 4 are categorized as 0.
    """
    encode_list = []
    for row in data['Sequence']:
        row_encode = []
        for code in row:
            row_encode.append(char_dict.get(code, 0))
        encode_list.append(row_encode)
    return encode_list
train_encode = integer_encoding(df)

codes = ['A', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'V', 'W', 'Y']
def create_dict(codes):
    char_dict = {}
    for index, val in enumerate(codes):
        char_dict[val] = index + 1
    return char_dict
char_dict = create_dict(codes)
print(char_dict)