<jupyter_start><jupyter_text>Step 1: Run NEURON simulations We will use ICG project code to run the protocols of the Podlaski et. al (2017) on a subset of non-inactivating K-channels. Two subsets of such channels were hand-selected by <NAME>. The selection is stored in a pickled file in the subfolder `support_files`:<jupyter_code>import pickle

kvactonly = pickle.load(open('support_files/kvact_only.pkl', 'rb'))

pow1 = kvactonly['pow1']
pow4 = kvactonly['pow4']<jupyter_output><empty_output><jupyter_text>`pow1` contains filenames in which the gating variable appears to power 1, in `pow4` the gating variable appears to the fourth power. We will focus on the set of `pow1` channels, since iwe have more of those.<jupyter_code>print(len(pow1))
print(len(pow4))<jupyter_output><empty_output><jupyter_text>We create subdirectories into which we will store the modfiles of the respective channels (modfiles are the NEURON models). We will download all modfiles in ICG by cloning a git repo. Then we pick out the ones from the selection:<jupyter_code>!git clone https://github.com/icgenealogy/icg-channels-K
!cd icg-channels-K && git checkout 926c5758a9ba6aec8092ac8e48d2f0c985114a37
import os

!mkdir -p modfiles
!mkdir -p modfiles/pow1

# sort into subdirs
channels = os.listdir("icg-channels-K/")
for chan in channels:
    if chan in pow1:
        !cp -n -r icg-channels-K/$chan/*.mod modfiles/pow1/<jupyter_output><empty_output><jupyter_text>There is a problem with the modfile of some channels, which we will delete:<jupyter_code>!rm modfiles/pow1/112834_kir*
!rm modfiles/pow1/150284_kir*
!rm modfiles/pow1/151458_kir*
!rm modfiles/pow1/121060_chan_KIR*<jupyter_output><empty_output><jupyter_text>Next, we clone the ICG repos needed for NEURON simulations.<jupyter_code>!git clone https://github.com/icgenealogy/icg-nrn-sim.git
!cd icg-nrn-sim && git checkout 4d58a1f40f779a99bd3146fda84a9acd59b07246
!git clone https://github.com/icgenealogy/icg-channels-customcode.git
!cd icg-channels-customcode && git checkout 9b7b0ad0c7c18d121f2ec108a38735f4640c26d6
!cp -r icg-channels-customcode/* icg-nrn-sim/icg-channels-customcode/<jupyter_output><empty_output><jupyter_text>Setting up directories for the outputs that will be generated:<jupyter_code>!mkdir -p modfiles_results
!mkdir -p modfiles_results/pow1<jupyter_output><empty_output><jupyter_text>We need to define the basepath of the current directory (make sure to adjust this):<jupyter_code>bp = '/home/jm/GitHub/mackelab/InferenceNeuralDynamics/fig4_channelomics/'<jupyter_output><empty_output><jupyter_text>We are ready to run NEURON on the subset of channels. We run the modfiles for `pow1` channels only. Running NEURON for all these modfiles may take a while. It requires a functioning NEURON setup on your machine.<jupyter_code>print('{bp}/icg-nrn-sim/runbatch.pl {bp}/modfiles/pow1 {bp}/icg-nrn-sim/kvconfig.in {bp}/modfiles_results/pow1'.format(bp=bp))
# run models
!$bp/icg-nrn-sim/runbatch.pl $bp/modfiles/pow1 $bp/icg-nrn-sim/kvconfig.in $bp/modfiles_results/pow1<jupyter_output><empty_output><jupyter_text>Once the simulation have run through, we keep `modfiles_results` only.<jupyter_code># clean up

!rm -rf icg-channels-K
!rm -rf *.dat *.txt *.hoc *.in *.mod x86_64

!rm -rf icg-nrn-sim
!rm -rf modfiles<jupyter_output><empty_output><jupyter_text>Next, we will loop through the results files and store all traces in a pkl. We will use a special dict for it, that requires you to `pip install python-box`.<jupyter_code>from box import Box<jupyter_output><empty_output><jupyter_text>With that, you should be able to run:<jupyter_code>import csv
import glob
import numpy as np
import os

protocols = ['ap', 'act', 'inact', 'deact', 'ramp']


# function to load tab delim data from disc
def get_matrix(file_path):
    matrix = []
    with open(file_path,'r') as f:
        reader = csv.reader(f, delimiter='\t')
        for trow in reader:
            row = []
            for tcol in trow:
                if tcol != '':
                    row.append(float(tcol))
            matrix.append(row)
    return np.asarray(matrix)

mats = Box()
for protocol in protocols:
    mats[protocol] = Box()
    mats[protocol]['data'] = []
    mats[protocol]['names'] = []
    for fpath in sorted(glob.glob(bp + '/modfiles_results/pow1/*_' + protocol + '.i')):
        mats[protocol].names.append(os.path.basename(fpath)[:-len(protocol)-3])
        mats[protocol].data.append(get_matrix(fpath))
    mats[protocol].data = np.asarray(mats[protocol].data)
from support_files.pickle_macos import pickle_dump

pickle_dump(mats.to_dict(), 'support_files/pow1_mats_comp_lfs.pkl')<jupyter_output><empty_output><jupyter_text>Number of channels simulated:<jupyter_code>len(mats.keys())<jupyter_output><empty_output><jupyter_text>Once that executed cleanly, we can get rid of `modfiles` and `modfiles_results` if we like:<jupyter_code>!rm -rf modfiles
!rm -rf modfiles_results<jupyter_output><empty_output><jupyter_text>Let's plot some traces:<jupyter_code>from support_files.pickle_macos import pickle_load
from box import Box

mats = Box(pickle_load('support_files/pow1_mats_comp_lfs.pkl'))
protocols = ['ap', 'act', 'inact', 'deact', 'ramp']
mats['ap']['data'].shape
import matplotlib.pyplot as plt
%matplotlib inline

N = mats.ap.data.shape[0]

assert mats.ap.data.shape[0] == mats.act.data.shape[0] == mats.inact.data.shape[0] == mats.deact.data.shape[0] == mats.ramp.data.shape[0]
print('total N: {}'.format(N))

plot_subset = True
if plot_subset:
    N = 18
ncols = 6
nrows = N//ncols+1

for protocol in protocols:
    print(protocol)
   
    plt.figure(figsize=(19, N//ncols*3))
    for i in range(N):
        plt.subplot(nrows, ncols, i+1)
        mat = mats[protocol].data[i,:,:]
        plt.title(mats[protocol].names[i])
        plt.plot(mat[:,0], mat[:,1:]);  # first col are times, other cols are values
        plt.axis('off')
    plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12103843560350641, 'fraction_numerical': 0.025960890087660147, 'mean_word_length': 4.938938938938939, 'pattern_counts': {'":': 0, '<': 58, '<?xml version=': 0, '>': 58, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '14', 'dir': 'jupyter-structured-clean-dedup', 'id': '341482', 'license': 'MIT', 'n_tokens_mistral': 2244, 'n_tokens_neox': 2148, 'n_words': 527, 'path': '4_channelomics/01_neuron_sim.ipynb', 'repo_name': 'mackelab/IdentifyMechanisticModels_2020', 'size': '5946'}