def model_ids_for_cell_type(cells_df, cell_type_tag): model_ids = [] type_cells = cells_df[cells_df.transgenic_line.str.contains(cell_type_tag)] glif_api = GlifApi() for neuron_id in type_cells.index.values: if glif_api.get_neuronal_models([neuron_id]): models_metadata = glif_api.get_neuronal_models([neuron_id])[0] for model in models_metadata['neuronal_models']: model_ids.append(model['id']) return np.array(model_ids)
def get_files_from_LIMS_public(output_path, glif_sp_ids=None, type='mouse'): '''This will grab cre positive data config files from LIMS and sort them and put them in the specified output folder. input: output_path: string specifies path for files to be placed in glif_sp_ids: list of strings or integers specimen ids of cells specifically want to grab. If none it will get all available on the Allen Institue Cell Types Database. type: string can be 'mouse' or 'human'. Note that if mouse is specified is will only grab cre positive mouse cells (code can be altered to get cre negative cells). output: Does not return values but creates the specified 'output_path' folder. Inside the folder a series of folders are created with the name format: specimenid_cre. Inside those inner folders are the neuron configs of the available GLIF models along with the preprocessor files. ''' glif_api = GlifApi() ctc = CellTypesCache(manifest_file=os.path.join(relative_path,'cell_types_manifest.json')) # select the specimen ids to grab from the data base (cre positive or human which have at least 1 GLIF model) if glif_sp_ids==None: #if no specimen id's are specified grab all data in the cell types manifest specimen_id_list = [] if type=='mouse': for c in ctc.get_cells(): if c['reporter_status']=='cre reporter positive': specimen_id_list.append(c['id']) elif type=='human': print 'getting human' for c in ctc.get_cells(species=['H**o Sapiens']): #print c specimen_id_list.append(c['id']) print specimen_id_list # reduce list to cells that have a GLIF model glif_sp_ids=[] for sp in specimen_id_list: models=glif_api.get_neuronal_models(sp)[0] for m in models['neuronal_models']: if 'LIF' in m['name']: glif_sp_ids.append(m['specimen_id']) glif_sp_ids=list(set(glif_sp_ids)) print len(glif_sp_ids), 'cre positive specimens with at least 1 LIF model' # create the overall output directory if it doesn't exist try: os.makedirs(output_path) except: pass # go get the files corresponding to the specimen ids from the Allen Cell Types Database # and put them into a specified output directory for id in glif_sp_ids: model_query=glif_api.get_neuronal_models(id)[0]['neuronal_models'] df=pd.DataFrame(model_query) for mt_id, short_name in zip(model_template_ids, model_names): dff=df[df['neuronal_model_template_id']==mt_id] if len(dff)>=2: print dff raise Exception("This is public data, there should not be more than 1 model") elif len(dff)==1: use_me=dff #go get the file path=use_me['well_known_files'].iloc[0][0]['path'] if type=='mouse': cre=(str(use_me['name'].values).split(')_'))[1].split(';')[0] elif type=='human': cre='human' else: raise Exception('specified species not known') # convert old non complete cre names if 'Ntsr1-Cre' in cre: cre='Ntsr1-Cre_GN220' if 'Chat-IRES-Cre' in cre: cre='Chat-IRES-Cre-neo' dir_name=os.path.join(output_path, str(id)+'_'+cre) try: os.makedirs(dir_name) except: pass if path.endswith('_neuron_config.json'): pass else: print path raise Exception('the file doesnt end with _neuron_config.json') try: copyfile(path, os.path.join(dir_name, str(id)+'_'+cre+'_'+short_name+'_neuron_config.json')) except: print 'couldnt make ', os.path.join(dir_name, str(id)+'_'+cre+'_'+short_name+'_neuron_config.json') if mt_id==model_template_ids[0]: model_path=os.path.dirname(path) pp_path=os.path.join(model_path, os.listdir(model_path)[np.where([fname.endswith('_preprocessor_values.json') for fname in os.listdir(model_path)])[0][0]]) try: copyfile(pp_path, os.path.join(dir_name, str(id)+'_'+cre+'_preprocessor_values.json')) except: print 'couldnt make ', os.path.join(dir_name, str(id)+'_'+cre+'_preprocessor_values.json') raise Exception('there should be a preprocessed file') elif len(dff)<1: use_me=pd.DataFrame() path=None
# model name and model template id mapping LIF = 'LIF' LIF_R = 'LIF-R' LIF_ASC = 'LIF-ASC' LIF_R_ASC = 'LIF-R-ASC' LIF_R_ASC_A = 'LIF-R-ASC-A' model_id2name = { 395310469: LIF, 395310479: LIF_R, 395310475: LIF_ASC, 471355161: LIF_R_ASC, 395310498: LIF_R_ASC_A } glif_api = GlifApi() for cell_result in glif_api.get_neuronal_models(cell_ids): #[325464516] cell_id = cell_result['id'] for curr_model in cell_result['neuronal_models']: if model_id2name[ curr_model['neuronal_model_template_id']] != options.model: continue model_id = curr_model['id'] neuron_config = glif_api.get_neuron_configs([model_id])[model_id] for stim in options.stimulus.split(','): simulate = stimulus[stim] output = simulate(cell_id, options.model, neuron_config) plt.figure('Cell ' + str(cell_id) + ' ' + options.model + ' ' + stim) plotter.plt_comparison_neurons(np.array(output['I']) * 1.0e12, output['times'], output['voltages'],