def readDSPlateSetting(plateL, confDir, startAtZero = False, dateFormat='%m%y%d', default={'Name':"A", 'Medium': "Complet", 'Serum':10}, addPlateWellsToDB=False, indices={'Xenobiotic':3, 'Dose':4}): ''' Function to go from as many csv files as there are parameters describing plate setup, to a dictionary The csv file should not contain spaces in the cells The file 'wells' should contain the information on where physically on the plate the wells with the numbers are located. The numbers are the numbers as the images are saved. ''' result = defaultdict(dict) idL = {} well_lines_dict = {} for plate in plateL: result[plate]={} idL[plate]={} #getting where the existing wells are try: file_ =open(os.path.join(confDir, "%s.csv" % plate)) well_lines = file_.readlines(); file_.close() except OSError: raise OSError("can't find proper file") else: well_lines=np.array([line.strip("\n").strip('\t').split("-") for line in well_lines[1:]]) print well_lines well_lines_dict[plate]=well_lines nb_col = 24; nb_row=16 if addPlateWellsToDB: date = datetime.datetime.strptime(plate[2:6]+plate[-2:], dateFormat) p = Plate(name = plate, date = date, nb_col=nb_col, nb_row=nb_row) p.save() # current_parameters = defaultdict(list) # for parameter in WELL_PARAMETERS: # print 'Loading %s parameter'%parameter # try: # f=open(os.path.join(confDir, "%s_%s.csv" % (plate, parameter))) # current_lines=f.readlines()[1:]; f.close() # except IOError: # pass # else: # current_parameters[parameter]=np.array([line.strip("\n").split(",") for line in current_lines]).ravel() # current_parameters[parameter] = np.delete(current_parameters[parameter], # np.where(current_parameters[parameter]=='')) for k, line in enumerate(well_lines): result[plate][k+1]=defaultdict(dict) for i,param in enumerate(WELL_PARAMETERS): try: result[plate][k+1][param]=line[indices[param]].replace(' ', '') except KeyError: result[plate][k+1][param]=default[param] if addPlateWellsToDB: medium = result[plate][k+1]['Medium'] serum = int(result[plate][k+1]['Serum']) conds = Cond.objects.filter(medium = medium) if len(conds)==0: cond = addConds(medium, serum) elif len(conds)==1: if conds[0].serum!=serum: cond =addConds(medium, serum) else: cond = conds[0] else: conds = conds.filter(serum =serum) if len(conds)==0: cond =addConds(medium, serum) elif len(conds)>1: raise else: cond = conds[0] if addPlateWellsToDB and 'Xenobiotic' in WELL_PARAMETERS: xb = result[plate][k+1]['Xenobiotic'] try: dose = float(result[plate][k+1]['Dose'].replace(',', '.')) except ValueError: #then it means there are letters in the field try: dose = float(result[plate][k+1]['Dose'].replace(',', '.')[:-2]) except ValueError: #then it meants it's an empty field dose=0 else: unit = result[plate][k+1]['Dose'][-2:] if unit=='uM': dose*=10**(-6) elif unit=='nM': dose*=10**(-9) elif unit=='pM': dose*=10**(-12) treatments = Treatment.objects.filter(xb = xb) if len(treatments)==0: treatment = addTreatment(xb, dose) elif len(treatments)==1: if treatments[0].dose != dose: treatment = addTreatment(xb, dose) else: treatment =treatments[0] else: treatments = treatments.filter(dose =dose) if len(treatments)==0: treatment = addTreatment(xb, dose) elif len(treatments)>1: raise else: treatment =treatments[0] if addPlateWellsToDB: clone = result[plate][k+1]['Name'].split("_")[0] w=Well(num=k+1, plate=p, treatment = treatment, clone = clone) w.save() #this is how to add many to many relations w.cond.add(cond); w.save() idL[plate][k+1]=w.id if addPlateWellsToDB: return result, {plate:np.reshape(range(1,nb_col*nb_row+1),(nb_row,nb_col))}, idL else: return result,{plate:np.reshape(range(1,nb_col*nb_row+1),(nb_row,nb_col))}, {}
def readXBPlateSetting(plateL, confDir, startAtZero = False, plateName=None, dateFormat='%d%m%y', default={'Name':"Cl1", 'Medium': "Indtp", "Serum":10}, addPlateWellsToDB=False): ''' Function to go from as many csv files as there are parameters describing plate setup, to a dictionary The csv file should not contain spaces in the cells The file 'wells' should contain the information on where physically on the plate the wells with the numbers are located. The numbers are the numbers as the images are saved. ''' result = defaultdict(dict) idL = {} well_lines_dict = {} for plate in plateL: result[plate]={} idL[plate]={} #getting where the existing wells are try: file_ =open(os.path.join(confDir, "%s_Wells.csv" % plate)) well_lines = file_.readlines(); file_.close() except OSError: nb_col = 12; nb_row=8 well_lines = np.reshape(range(1,97),(8,12)) else: well_lines=np.array([line.strip("\n").split(",") for line in well_lines[1:]], dtype=int) print well_lines well_lines_dict[plate]=well_lines try: a=int(well_lines[0][11]) except: try: a = int(well_lines[0][7]) except: raise AttributeError("Can't find the number of rows") else: nb_col=8 else: nb_col=12 nb_row = len(well_lines) #dire le nb de lignes dans le fichier - mais en fait on a toujours huit colonnes # #opening file with well names (clone name usually) # filename = os.path.join(confDir, "%s_Name.csv" % plate) # # f=open(filename) # lines=f.readlines(); f.close() # # lines=np.array([line.strip("\n").split(",") for line in lines][1:]).ravel() # lines = np.delete(lines, np.where(lines=='')) if addPlateWellsToDB: p = Plate(name = plateName, date = datetime.datetime.strptime(plate.split('_')[0], dateFormat), nb_col=nb_col, nb_row=nb_row) p.save() current_parameters = defaultdict(list) for parameter in WELL_PARAMETERS: print 'Loading %s parameter'%parameter try: f=open(os.path.join(confDir, "%s_%s.csv" % (plate, parameter))) current_lines=f.readlines()[1:]; f.close() except IOError: pass else: current_parameters[parameter]=np.array([line.strip("\n").split(",") for line in current_lines]).ravel() current_parameters[parameter] = np.delete(current_parameters[parameter], np.where(current_parameters[parameter]=='')) for k in well_lines.ravel(): result[plate][k]=defaultdict(dict) for i,param in enumerate(WELL_PARAMETERS): try: result[plate][k][param]=current_parameters[param][k-1] except IndexError: result[plate][k][param]=default[param] if addPlateWellsToDB: medium = result[plate][k]['Medium'] serum = int(result[plate][k]['Serum']) conds = Cond.objects.filter(medium = medium) if len(conds)==0: cond = addConds(medium, serum) elif len(conds)==1: if conds[0].serum!=serum: cond =addConds(medium, serum) else: cond = conds[0] else: conds = conds.filter(serum =serum) if len(conds)==0: cond =addConds(medium, serum) elif len(conds)>1: raise else: cond = conds[0] if addPlateWellsToDB and 'Xenobiotic' in WELL_PARAMETERS: xb = result[plate][k]['Xenobiotic'] dose = int(result[plate][k]['Dose']) treatments = Treatment.objects.filter(xb = xb) if len(treatments)==0: treatment = addTreatment(xb, dose) elif len(treatments)==1: if treatments[0].dose != dose: treatment = addTreatment(xb, dose) else: treatment =treatments[0] else: treatments = treatments.filter(dose =dose) if len(treatments)==0: treatment = addTreatment(xb, dose) elif len(treatments)>1: raise else: treatment =treatments[0] if addPlateWellsToDB: clone = result[plate][k]['Name'].split("_")[0] w=Well(num=k, plate=p, treatment = treatment, clone = clone) w.save() #this is how to add many to many relations w.cond.add(cond); w.save() idL[plate][k]=w.id if addPlateWellsToDB: return result, well_lines_dict, idL else: return result,well_lines_dict, {}