def main(path): """ Read in the Antibody """ sheet_name = 'Sheet1' sheet = iu.readtable([path, sheet_name, 0]) properties = ('model_field', 'required', 'default', 'converter') column_definitions = { 'AR_Name': ('name', True), 'AR_LINCS_ID': 'lincs_id', 'AR_Alternative_Name': 'alternative_names', 'AR_Center_Specific_ID': ('facility_id', True, None, lambda x: x[x.index('HMSL') + 4:]), 'AR_Clone_Name': 'clone_name', 'AR_RRID': 'rrid', 'AR_Antibody_Type': 'type', 'target_protein_lincs_id': ('target_protein_lincs_id', False, None, lambda x: x[x.index('HMSL') + 4:] if x else None), 'AR_Non-Protein_Target': 'non_protein_target_name', 'AR_Target_Organism': 'target_organism', 'AR_Immunogen': 'immunogen', 'AR_Immunogen_Sequence': 'immunogen_sequence', 'AR_Antibody_Species': 'species', 'AR_Antibody_Clonality': 'clonality', 'AR_Antibody_Isotype': 'isotype', 'AR_Antibody_Production_Source_Organism': 'source_organism', 'AR_Antibody_Production_Details': 'production_details', 'AR_Antibody_Labeling': 'labeling', 'AR_Antibody_Labeling_Details': 'labeling_details', 'AR_Relevant_Citations': 'relevant_citations', 'Date Data Received': ('date_data_received', False, None, util.date_converter), 'Date Loaded': ('date_loaded', False, None, util.date_converter), 'Date Publicly Available': ('date_publicly_available', False, None, util.date_converter), 'Most Recent Update': ('date_updated', False, None, util.date_converter), 'Is Restricted': ('is_restricted', False, False, util.bool_converter) } # convert the labels to fleshed out dict's, with strategies for optional, default and converter column_definitions = util.fill_in_column_definitions( properties, column_definitions) # create a dict mapping the column ordinal to the proper column definition dict cols = util.find_columns(column_definitions, sheet.labels) rows = 0 logger.debug('cols: %s' % cols) for row in sheet: logger.debug('row %s - %s' % (rows, row)) r = util.make_row(row) dict = {} initializer = {} for i, value in enumerate(r): if i not in cols: continue properties = cols[i] logger.debug('read col: %d: %s' % (i, properties)) required = properties['required'] default = properties['default'] converter = properties['converter'] model_field = properties['model_field'] logger.debug('raw value %r' % value) if (value == None or value == 'None'): value = None if (default != None): value = default if (value == None and required == True): raise Exception('Field is required: %s, record: %d' % (properties['column_label'], rows)) if (value and converter != None): value = converter(value) logger.debug('model_field: %s, converted value %r' % (model_field, value)) initializer[model_field] = value try: logger.debug('row: %s, initializer: %s' % (rows, initializer)) target_protein_lincs_id = initializer.pop( 'target_protein_lincs_id', None) if target_protein_lincs_id: try: target_protein = Protein.objects.get( lincs_id=target_protein_lincs_id) initializer['target_protein'] = target_protein except ObjectDoesNotExist, e: logger.error( 'target_protein_lincs_id "%s" does not exist, row: %d' % (target_protein_lincs_id, i)) antibody = Antibody(**initializer) antibody.save() logger.info('antibody created: %s' % antibody) rows += 1 # create a default batch - 0 AntibodyBatch.objects.create(reagent=antibody, batch_id=0) except Exception, e: logger.error("Invalid antibody initializer: %s" % initializer) raise
def main(path): """ Read in the Antibody """ sheet_name = 'Sheet1' sheet = iu.readtable([path, sheet_name, 1]) # Note, skipping the header row by default properties = ('model_field','required','default','converter') column_definitions = { 'AR_Name': ('name',True), 'AR_LINCS_ID': 'lincs_id', 'AR_Alternative_Name': 'alternative_names', 'AR_Center_ID': ('facility_id', True), 'AR_Target_Protein': 'target_protein_name', 'AR_Target_Protein_ID': 'target_protein_uniprot_id', 'AR_Target_Gene': 'target_gene_name', 'AR_Target_Gene_ID': 'target_gene_id', 'AR_Target_Organism': 'target_organism', 'AR_Immunogen': 'immunogen', 'AR_Immunogen_Sequence': 'immunogen_sequence', 'AR_AntibodyClonality': 'antibody_clonality', 'AR_Source_Organism': 'source_organism', 'AR_Antibody_Isotype': 'antibody_isotype', 'AR_Engineering': 'engineering', 'AR_Antibody_Purity': 'antibody_purity', 'AR_Antibody_Labeling': 'antibody_labeling', 'AR_Recommended_Experiment_Type': 'recommended_experiment_type', 'AR_Relevant_Reference': 'relevant_reference', 'AR_Specificity': 'specificity', 'Date Data Received':('date_data_received',False,None,util.date_converter), 'Date Loaded': ('date_loaded',False,None,util.date_converter), 'Date Publicly Available': ('date_publicly_available',False,None,util.date_converter), 'Most Recent Update': ('date_updated',False,None,util.date_converter), 'Is Restricted':('is_restricted',False,False)} # convert the labels to fleshed out dict's, with strategies for optional, default and converter column_definitions = util.fill_in_column_definitions(properties,column_definitions) # create a dict mapping the column ordinal to the proper column definition dict cols = util.find_columns(column_definitions, sheet.labels) rows = 0 logger.debug(str(('cols: ' , cols))) for row in sheet: r = util.make_row(row) dict = {} initializer = {} for i,value in enumerate(r): if i not in cols: continue properties = cols[i] logger.debug(str(('read col: ', i, ', ', properties))) required = properties['required'] default = properties['default'] converter = properties['converter'] model_field = properties['model_field'] # Todo, refactor to a method logger.debug(str(('raw value', value))) if(converter != None): value = converter(value) if(value == None ): if( default != None ): value = default if(value == None and required == True): raise Exception('Field is required: %s, record: %d' % (properties['column_label'],rows)) logger.debug(str(('model_field: ' , model_field, ', value: ', value))) initializer[model_field] = value try: logger.debug(str(('initializer: ', initializer))) antibody = Antibody(**initializer) antibody.save() logger.info(str(('antibody created: ', antibody))) rows += 1 except Exception, e: logger.error(str(( "Invalid antibody initializer: ", initializer))) raise
def build_schema(self): schema = super(AntibodyResource,self).build_schema() schema['fields'] = get_detail_schema(Antibody(),['antibody']) return schema
def main(path): """ Read in the Antibody """ sheet_name = 'Sheet1' sheet = iu.readtable([path, sheet_name, 0]) properties = ('model_field','required','default','converter') column_definitions = { 'AR_Name': ('name',True), 'AR_LINCS_ID': 'lincs_id', 'AR_Alternative_Name': 'alternative_names', 'AR_Center_Specific_ID': ('facility_id',True,None, lambda x: x[x.index('HMSL')+4:]), 'AR_Clone_Name': 'clone_name', 'AR_RRID': 'rrid', 'AR_Antibody_Type': 'type', 'target_protein_lincs_id': ( 'target_protein_lincs_id',False,None, lambda x: x[x.index('HMSL')+4:] if x else None ), 'AR_Non-Protein_Target': 'non_protein_target_name', 'AR_Target_Organism': 'target_organism', 'AR_Immunogen': 'immunogen', 'AR_Immunogen_Sequence': 'immunogen_sequence', 'AR_Antibody_Species': 'species', 'AR_Antibody_Clonality': 'clonality', 'AR_Antibody_Isotype': 'isotype', 'AR_Antibody_Production_Source_Organism': 'source_organism', 'AR_Antibody_Production_Details': 'production_details', 'AR_Antibody_Labeling': 'labeling', 'AR_Antibody_Labeling_Details': 'labeling_details', 'AR_Relevant_Citations': 'relevant_citations', 'Date Data Received':('date_data_received',False,None,util.date_converter), 'Date Loaded': ('date_loaded',False,None,util.date_converter), 'Date Publicly Available': ('date_publicly_available',False,None,util.date_converter), 'Most Recent Update': ('date_updated',False,None,util.date_converter), 'Is Restricted':('is_restricted',False,False,util.bool_converter)} # convert the labels to fleshed out dict's, with strategies for optional, default and converter column_definitions = util.fill_in_column_definitions(properties,column_definitions) # create a dict mapping the column ordinal to the proper column definition dict cols = util.find_columns(column_definitions, sheet.labels) rows = 0 logger.debug('cols: %s' % cols) for row in sheet: logger.debug('row %s - %s' %(rows,row)) r = util.make_row(row) dict = {} initializer = {} for i,value in enumerate(r): if i not in cols: continue properties = cols[i] logger.debug('read col: %d: %s' % (i,properties)) required = properties['required'] default = properties['default'] converter = properties['converter'] model_field = properties['model_field'] logger.debug('raw value %r' % value) if(value == None or value == 'None'): value = None if( default != None ): value = default if(value == None and required == True): raise Exception('Field is required: %s, record: %d' % (properties['column_label'],rows)) if(value and converter != None): value = converter(value) logger.debug('model_field: %s, converted value %r' % (model_field, value) ) initializer[model_field] = value try: logger.debug('row: %s, initializer: %s' % (rows,initializer)) target_protein_lincs_id = initializer.pop('target_protein_lincs_id',None) if target_protein_lincs_id: try: target_protein = Protein.objects.get(lincs_id=target_protein_lincs_id) initializer['target_protein'] = target_protein except ObjectDoesNotExist, e: logger.error('target_protein_lincs_id "%s" does not exist, row: %d' % (target_protein_lincs_id,i)) antibody = Antibody(**initializer) antibody.save() logger.info('antibody created: %s' % antibody) rows += 1 # create a default batch - 0 AntibodyBatch.objects.create(reagent=antibody,batch_id=0) except Exception, e: logger.error("Invalid antibody initializer: %s" % initializer) raise