<jupyter_start><jupyter_text>Load dataset<jupyter_code>import json

with open('data/data.json', 'r') as file:
    data = json.load(file)<jupyter_output><empty_output><jupyter_text>Define model<jupyter_code>import numpy as np
from sklearn.tree import DecisionTreeClassifier

amino_acids = 'ARNDCQEGHILKMFPSTWYV'
empty_acid = np.zeros(20)

def one_hot_encode_sequence(sequence):
    indices = [amino_acids.index(a) for a in sequence]
    array = np.zeros((len(sequence), 20))
    array[np.arange(len(sequence)), indices] = 1
    return array


class Model:
    
    def __init__(self):
        self.tree = DecisionTreeClassifier()
    
    def _preprocess_sequence(self, sequence):
        n_acids = len(sequence)
        sequence = one_hot_encode_sequence(sequence)
        X = []
        for i in range(n_acids):
            indices = np.arange(i-4, i+5)
            x = np.array([empty_acid if j < 0 or j >= n_acids else sequence[j] for j in indices])
            X.append(x.flatten())
        return X
        
    
    def _preprocess_sequences(self, data):
        X = []
        for protein_dict in data:
            sequence = protein_dict['sequence']
            _X = self._preprocess_sequence(sequence)
            X.extend(_X)
        return np.array(X)
    
    
    def _preprocess_labels(self, data):
        joined_labels = ''.join([p['labels'] for p in data])
        y = [l for l in joined_labels]
        return np.array(y)
    
    
    def fit(self, data):
        X = self._preprocess_sequences(data)
        y = self._preprocess_labels(data)
        self.tree.fit(X, y)
    
    def predict(self, sequence):
        X = self._preprocess_sequence(sequence)
        preds = self.tree.predict(X)
        return ''.join(preds)<jupyter_output><empty_output><jupyter_text>Perform LOOCV<jupyter_code>model = Model()

for i, protein_dict in enumerate(data):
    sequence = protein_dict['sequence']
    training_data = data[:i] + data[i+1:]
    model.fit(training_data)
    preds = model.predict(sequence)
    data[i]['preds'] =  preds<jupyter_output><empty_output><jupyter_text>Store the predictions in the JSON file<jupyter_code>import json

with open('data/data.json', 'w', encoding='utf-8') as f:
    json.dump(data, f, ensure_ascii=False, indent=4)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11726242371403661, 'fraction_numerical': 0.004795117698343505, 'mean_word_length': 3.061946902654867, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '306537', 'license': 'MIT', 'n_tokens_mistral': 797, 'n_tokens_neox': 774, 'n_words': 160, 'path': 'model.ipynb', 'repo_name': 'jonriege/predict-protein-structure', 'size': '2294'}