def search_to_json(self, *args, **kw): #TODO : sort by column on user's click user_lab = session.get("current_lab", None) #get parameters from ajax request search_value = kw.get("search[value]", None) if search_value == '': search_value = None #word lenght > 2 to avoid DDoS in your server.... elif search_value is not None: list_search_words = [x for x in search_value.split(" ") if len(x) > 2] draw = int(kw.get("draw", 1)) start_point = int(kw.get("start", 0)) data_by_page = int(kw.get("length", 50)) stop_point = start_point + data_by_page if user_lab: lab = DBSession.query(Labs).filter(Labs.name == user_lab).first() measurements_total = DBSession.query(Measurements).join(Measurements.attributs).filter(and_(Attributs.lab_id == lab.id, Attributs.deprecated == False)).all() measurements = DBSession.query(Measurements).join(Measurements.attributs).filter(and_(Attributs.lab_id == lab.id, Attributs.deprecated == False)).distinct()[start_point:stop_point] if search_value is not None: final_request = self.search_engine(list_search_words, lab) #query mixed with results from all the table of interest paginated_request = final_request[start_point:stop_point] searching_tosort = [SW(meas).to_json_test() for meas in paginated_request] searching = sorted(searching_tosort, key=lambda k: (k['User'], k['Type'])) return json.dumps({"draw": draw, "recordsTotal": len(measurements_total), "recordsFiltered": len(final_request), "data": searching}) searching_tosort = [SW(meas).to_json_test() for meas in measurements] searching = sorted(searching_tosort, key=lambda k: (k['User'], k['Type'])) return json.dumps({"draw": draw, "recordsTotal": len(measurements_total), "recordsFiltered": len(measurements_total), "data": searching})
def change_project_owner(self, p_id, new_owner_id, mail, key): ''' Allow to change the owner of one given project (include its sample(s) and measurement(s)) ''' try: p_target = DBSession.query(Projects).filter(Projects.id == p_id).first() #just one lab by project, so the first is the good one project_lab = p_target.labs[0] new_owner = DBSession.query(User).filter(User.id == new_owner_id).first() if project_lab in new_owner.labs: list_samples = p_target.samples for s in list_samples: list_meas = s.measurements for m in list_meas: m.user_id = new_owner.id DBSession.add(m) DBSession.flush() p_target.user_id = new_owner.id DBSession.add(p_target) DBSession.flush() print "Update done." else: raise Exception("The new owner is not a member of this lab project. Impossible to continue the operation.") except: print_traceback()
def get_dl_url(self, mail, key, meas_id): ''' provide dl URL from measurements id ''' list_ids = meas_id.split(',') user = DBSession.query(User).filter(User._email == mail).first() if user is None: return {'ERROR': "User " + mail + " not in BioRepo."} else: dico_urls = {} user_labs = user.labs for labo in user_labs: lab = DBSession.query(Labs).filter(Labs.name == labo.name).first() for m_id in list_ids: meas = DBSession.query(Measurements).join(Measurements.attributs)\ .filter(and_(Attributs.lab_id == lab.id, Attributs.deprecated == False))\ .filter(Measurements.id == m_id).first() if meas is not None: list_fus = meas.fus if len(list_fus)>0: for f in list_fus: sha1 = f.sha1 if meas.status_type: dico_urls[m_id] = "/biorepo/public/public_link?m_id=" + str(m_id) + "&sha1=" + str(sha1) else: dico_urls["ERROR " + str(m_id)] = "This file is registered as private. BioRepo can't produce a public link." else: dico_urls["ERROR " + str(m_id)] = "No file attached with this measurements. The saved description is : " + str(meas.description) else: dico_urls["ERROR " + str(m_id)] = "This measurements id does not exist or you can access to it with your current rights." #TODO test and add to devdoc #API return dico_urls
def get_samples_from_project(self, mail, key, p_id): ''' Get a JSON with all samples from a given project input : mail and biorepo key, p_id (project id) output : {"project id": [{"sample id": {"name": "my sample name", "type": "4C-seq", "protocole": "my sample protocole", "dynamic field": "my dynamic field, ..."}}, ...]} ''' user = DBSession.query(User).filter(User._email == mail).first() user_lab = user.labs list_samples = [] dico_final = {} target = DBSession.query(Projects).filter(Projects.id == p_id).first() if target is None: return {'ERROR': "This project ID does not exist."} lab_target = target.labs[0] #check if the project is owned by the user or his lab access_ok = False for l in user_lab: if l.id == lab_target.id: access_ok = True if access_ok: samples = target.samples if len(samples) == 0: return {'ERROR': 'This project id : ' + str(target.id) + ' is empty.'} for s in samples: dico_sample = {} dico_dynamic = {} sample_attributs = s.attributs sample_a_values = s.a_values for att in sample_attributs: att_id = att.id att_key = att.key for val in sample_a_values: value = val.value if val.attribut_id == att_id: #for the true weird checkbox if value == "true": dico_dynamic[att_key] = att_key else: dico_dynamic[att_key] = value #check the weird checkbox widget with "false" value if len(sample_attributs) != len(dico_dynamic.keys()): for att in sample_attributs: att_key = att.key att_widget = att.widget if att_key not in dico_dynamic.keys() and att_widget == "checkbox": dico_dynamic[att_key] = "Not " + str(att_key) elif att_key not in dico_dynamic.keys() and att_widget != "checkbox": dico_dynamic[att_key] = "Not specified" dico_sample = {"name": s.name, "type": s.type, "protocole": s.protocole} dico_sample.update(dico_dynamic) list_samples.append({s.id: dico_sample}) dico_final[p_id] = list_samples return dico_final else: return {'ERROR': "This project is not a project from your lab, you cannot access to it."}
def searchlists_to_json(self, *args, **kw): user_lab = session.get("current_lab", None) if user_lab: lab = DBSession.query(Labs).filter(Labs.name == user_lab).first() one_meas = DBSession.query(Measurements).join(Measurements.attributs).filter(and_(Attributs.lab_id == lab.id, Attributs.deprecated == False)).first() search_grid, hidden_positions, positions_not_searchable = build_search_grid(one_meas) searchlists = json.dumps([hidden_positions, positions_not_searchable]) return searchlists
def get_permissions(admin): ''' Get the right permissions for an user. @param admin : True if the user is an admin. @type admin : a boolean. ''' if admin: return DBSession.query(Permission).all() return DBSession.query(Permission).filter(Permission.name != 'admin').all()
def get_my_lab_projects(self, mail, key): ''' Get a JSON with all user's projects by lab input : mail an biorepo key output : {'lab':{'project id':{'name': "my_project_name", 'description': "my project description", "owner": "project owner name"}}} ''' dico_lab_projects = {} dico_by_labs = {} user = DBSession.query(User).filter(User._email == mail).first() if user is None: return {'ERROR': "User " + mail + " not in BioRepo."} else: user_labs = user.labs if len(user_labs) == 1: for lab in user_labs: lab_projects = lab.projects if isinstance(lab_projects, list): for p in lab_projects: u = DBSession.query(User).filter(User.id == p.user_id).first() owner = u.name dico_lab_projects[p.id] = {'name': p.project_name, 'description': p.description, 'owner': owner} else: u = DBSession.query(User).filter(User.id == p.user_id).first() owner = u.name dico_lab_projects[lab_projects.id] = {'name': lab_projects.project_name, 'description': lab_projects.description, 'owner': owner} if len(lab_projects) == 0: return {'ERROR': "No projects found for " + lab.name} dico_by_labs[lab.name] = dico_lab_projects return dico_by_labs elif len(user.labs) > 1: for l in user.labs: lab_projects = l.projects if isinstance(lab_projects, list): for p in lab_projects: u = DBSession.query(User).filter(User.id == p.user_id).first() owner = u.name dico_lab_projects[p.id] = {'name': p.project_name, 'description': p.description, 'owner': owner} else: u = DBSession.query(User).filter(User.id == p.user_id).first() owner = u.name dico_lab_projects[lab_projects.id] = {'name': lab_projects.project_name, 'description': lab_projects.description, 'owner': owner} dico_by_labs[l.name] = dico_lab_projects dico_lab_projects = {} return dico_by_labs else: return {'ERROR': "This user " + mail + " has no lab. Contact the administrator please."}
def index(self, *args, **kw): user_lab = session.get("current_lab", None) user_projects = [] u_projects = [] u_samples = [] u_meas = [] u_children = [] u_global = [] dico_final = {} #TODO : admin view - watch for a dl link if user_lab: lab = DBSession.query(Labs).filter(Labs.name == user_lab).first() lab_users = lab.users for u in lab_users: projects = DBSession.query(Projects).filter(Projects.user_id == u.id).all() if len(projects) > 0: for p in projects: for lab in p.labs: if lab.name == user_lab: user_projects.append(p) for proj in user_projects: for sample in proj.samples: if len(sample.measurements) > 0: for meas in sample.measurements: if len(meas.children) == 0: u_meas.append({"name": str(meas.name) + "(" + str(meas.id) + ")"}) else: for child in meas.children: u_children.append({"name": str(child.name) + "(" + str(child.id) + ")"}) u_meas.append({"name": str(meas.name) + "(" + str(meas.id) + ")", "children": u_children}) u_children = [] u_samples.append({"name": str(sample.name) + "(" + str(sample.type) + ")", "children": u_meas}) u_meas = [] else: u_samples.append({"name": str(sample.name + "(" + str(sample.type) + ")")}) if len(proj.samples) > 0: u_projects.append({"name": str(proj.project_name), "children": u_samples}) u_samples = [] else: u_projects.append({"name": str(proj.project_name)}) u_global.append({"name": u.firstname + " " + u.name, "children": u_projects}) u_projects = [] user_projects = [] #uncomment these 4 lines if you want to see every lab users registered in BioRepo #else: #u_global.append({"name": u.firstname + " " + u.name}) #u_projects = [] #user_projects = [] dico_final["name"] = user_lab dico_final["children"] = u_global return {"data": json.dumps(dico_final)}
def new(self, *args, **kw): tmpl_context.widget = new_attribut_form #take the logged user user = handler.user.get_user_in_session(request) #take the logged user samples samples = DBSession.query(Samples).join(Projects).join(User).filter(User.id == user.id).all() meas = DBSession.query(Measurements).all() attributs = DBSession.query(Attributs).all() tmpl_context.samples = samples tmpl_context.meas = meas tmpl_context.attributs = attributs return dict(page='samples', value=kw, title='New Sample', model='Sample')
def Gviz_link(self, sha1, meas_id, *args, **kw): ''' redirect to Gviz BAM viewer hosted on HTSstation ''' #URL example : #bbcftools.epfl.ch/bam_viewer/gviews/new?assembly_name=pombe&module=biorepo&file=XTv7U2IYVAgIWqBHyPjC meas = DBSession.query(Measurements).filter( Measurements.id == meas_id).first() list_a_values = [] #get the dynamic values for val in meas.a_values: list_a_values.append(val.id) #check if "assembly" is a dynamic key for this measurement cpt_test = 0 for a in meas.attributs: if a.key == "assembly": cpt_test += 1 #get all the values recorded for this key list_assemblies = a.values assembly = '' for v in list_assemblies: #check if the Attributs_values object is linked to this measurement if v.id in list_a_values: assembly = v.value if assembly == '': flash( "Sorry but you have to set an assembly to this measurement", "error") raise redirect("/search") else: #TODO replace this when BioRepo and HTS will be connected together #JUST CATCH THE PATH IN DB AND GIVE IT IN THE URL #/!\ /archive/projects/epfl-vital-it/biorepo_upload/ /!\ hostname = socket.gethostname().lower() #because of aliasing if hostname == "ptbbsrv2.epfl.ch": hostname = "biorepo.epfl.ch" bam_file = DBSession.query(Files_up).filter( Files_up.sha1 == sha1).first() fullname = bam_file.filename name_tmp = fullname.split('.') name = name_tmp[0] bam_name = self.BAM_visualisation(bam_file, name) bam_path = bam_file.path path_to_give = bam_path.split( "/archive/projects/epfl/biorepo_upload")[1] raise redirect( 'http://bbcftools.epfl.ch/bam_viewer/gviews/new?assembly_name=' + assembly + "&module=biorepo&file=" + path_to_give + "/" + bam_name)
def post_edit(self, *args, **kw): id_project = kw["IDselected"] project = DBSession.query(Projects).filter(Projects.id == id_project).first() if kw['project_name'] == '': flash("Bad Project : Your project must have a name", "error") raise redirect("./edit/" + id_project) project.project_name = kw["project_name"] project.description = kw["description"] samples_ids = kw.get("samples", None) if samples_ids is not None: if not isinstance(samples_ids, (list, tuple)): samples_ids = [int(samples_ids)] else: #from unicode to integer for comparison list_tmp = [] for i in samples_ids: i = int(i) list_tmp.append(i) samples_ids = list_tmp else: samples_ids = [] #check if sample is not deleted try: if kw["selected_samples"] != "[]": old_selected = [int(x) for x in kw["selected_samples"].replace("[", "").replace("]", "").split(",")] else: old_selected = [] except: flash("Samples id error, please contact the administrator to report your bug", 'error') print "Something changed with this Turbogears version.... controllers/project.py l180 --> JSON solution is better" raise redirect("./") list_names = [] for o in old_selected: if o not in samples_ids: sample1 = DBSession.query(Samples).filter(Samples.id == o).first() list_names.append(str(sample1.name)) if len(list_names) > 0: flash("If you choose to delete : " + str(list_names).replace("[", "").replace("]", "") + " from the project, this sample will be removed. The sample deletion is not enabled here. Please do it directly in the sample page delete option.", 'error') raise redirect("./edit/" + id_project) list_samples = [] for s in samples_ids: sample = DBSession.query(Samples).filter(Samples.id == s).first() list_samples.append(sample) project.samples = list_samples raise redirect("./")
def gimme_projects_plz(self, mail, *args, **kw): user = DBSession.query(User).filter(User._email == mail).first() if user is not None: user_id = user.id projects = DBSession.query(Projects).filter(Projects.user_id == user_id).all() dico_projects_by_user = {} if len(projects) != 0: for p in projects: dico_projects_by_user[p.id] = {"project_name": p.project_name} else: dico_projects_by_user = {0: {"project_name": "No projects found into BioRepo for this user"}} else: dico_projects_by_user = {"error": str(mail) + " is not regristred in BioRepo"} return dico_projects_by_user
def change_bis_sample(self, sending_s, reception_s): ''' Allow to move all the measurements of one sample to an other one and delete the sending sample after the operation. Usefull for the "_bis" samples in spreadsheet. @param principal : sending_s (sample id sending the measurements), reception_s (sample id receptioning the measurements) ''' try: # samples queries from_sample = DBSession.query(Samples).filter(Samples.id == int(sending_s)).first() to_sample = DBSession.query(Samples).filter(Samples.id == int(reception_s)).first() # get the measurements lists from_att = from_sample.attributs to_att = to_sample.attributs # lab checking if from_att[0].lab_id != to_att[0].lab_id: raise Exception("Samples from different labs. Impossible to move these measurements.") # get list of measurements objects meas_to_move = from_sample.measurements meas_in_place = to_sample.measurements # move the measurements for m in meas_to_move: if m not in meas_in_place: (to_sample.measurements).append(m) DBSession.delete(from_sample) DBSession.add(to_sample) DBSession.flush() print "---> Sample " + sending_s + " was deleted and its measurements are now into the sample " + reception_s except: print_traceback()
def drop_to_vitalit(self, mail, key, meas_ids): ''' Copy-paste file(s) attached to the measurement(s) id given in input to a /scratch accessible path for the user ''' list_ids = list(set(meas_ids.split(','))) msg = "--- DO NOT REPLY TO THIS MESSAGE PLEASE ---\n\n" user = DBSession.query(User).filter(and_(User._email == mail, User.key == key)).first() if user is None: return {'ERROR': "Your mail or you key is not correct."} labs = user.labs user_labs = [] for lab in labs: user_labs.append(lab.name) if list_ids is None or len(list_ids) == 0: return {'ERROR': "You have to give one or several id(s) with the meas_ids key."} #building dico with paths and filenames dic_paths = {} for i in list_ids: meas = DBSession.query(Measurements).filter(Measurements.id == i).first() #check lab rights att_meas = meas.attributs[0] lab_to_check = DBSession.query(Labs).filter(Labs.id == att_meas.lab_id).first() if lab_to_check.name not in user_labs: msg = msg + "ERROR : This measurement (" + str(meas.id) + ") doesn't belong to your lab. You can't access to it.\n" #check status (public/private) elif not meas.status_type: msg = msg + "ERROR : This measurement (" + str(meas.id) + ")is not public. You can't move it from BioRepo." else: if len(meas.fus) > 0: for fu in meas.fus: path_fu = fu.path + "/" + fu.sha1 filename = fu.filename dic_paths[filename] = path_fu #check /scratch repository public_path = path_dropbox(user_labs[0]) #copy-paste the file(s) for k, v in dic_paths.iteritems(): try: src = v dest = public_path + "/" + k copyfile(src, dest) msg = msg + k + " is now accessible here : " + dest + "\n" except: print ">>> COPY ERROR WITH " + k msg = msg + "ERROR " + k + " encountered a problem during the copy-paste process. Contact your admin for this file please.\n" print_traceback() #send mail with public path(s) sendMail(user._email, msg, "[BioRepo] Get your file(s) on Vital-IT /scratch")
def check_value_addition(self, list_type_att, dict_att_values_type, dict_att_values_db, lab_id): ''' Checking for value(s) addition @param principal @return an updated db ''' for key_type in list_type_att: for i in dict_att_values_type[key_type]: #get the attribut and its value(s) i_att = DBSession.query(Attributs).filter( and_(Attributs.lab_id == lab_id, Attributs.key == key_type)).first() #warning : i_att_value is a list ! i_att_value = DBSession.query(Attributs_values).filter( and_(Attributs_values.attribut_id == i_att.id, Attributs_values.value == i)).all() try: flag = False if i not in dict_att_values_db[key_type] and i != "None": for existant in i_att_value: if existant.value == i and existant.deprecated == True: existant.deprecated = False DBSession.flush() flag = True if existant.value is None: existant.deprecated = True DBSession.flush() if flag == False: new_value = Attributs_values() new_value.attribut_id = i_att.id new_value.value = i new_value.deprecated = False DBSession.add(new_value) DBSession.flush() except Exception as e: import sys, traceback a, b, c = sys.exc_info() traceback.print_exception(a, b, c) print "--- error login.py check_value_addition() ---" print "i --->", i print "key_type --->", key_type print "list_type_att --->", list_type_att print "dict_att_values_type[key_type] -->", dict_att_values_type[ key_type] print "i_att --> ", i_att print "i_att_value", i_att_value
def get_UCSC_link(obj_id): ''' Return a HTML link to UCSC ''' meas = DBSession.query(Measurements).filter(Measurements.id == obj_id).first() status = meas.status_type normal_ext = ["bed", "bedgraph", "wig"] #binary extension, except bam files binary_ext = ["bw", "bigwig", "bigbed", "bb"] if status and len(meas.fus) > 0: list_fus = meas.fus for x in list_fus: f_sha1 = x.sha1 ext = x.extension #t is type. 1 == normal extension, 2 == binary extension, 3 == bam if ext.lower() in normal_ext: return''' <a class='action UCSC_link' href="%s" target="_blank" title="view in UCSC" style="text-decoration:none" target="_blank"></a> ''' % (url('./public/UCSC_link', params=dict(sha1=f_sha1, meas_id=obj_id, t=1))) elif ext.lower() in binary_ext: return''' <a class='action UCSC_link' href="%s" target="_blank" title="view in UCSC" style="text-decoration:none" target="_blank"></a> ''' % (url('./public/UCSC_link', params=dict(sha1=f_sha1, meas_id=obj_id, t=2))) elif ext.lower() == "bam": return''' <a class='action UCSC_link' href="%s" target="_blank" title="view in UCSC" style="text-decoration:none" target="_blank"></a> ''' % (url('./public/UCSC_link', params=dict(sha1=f_sha1, meas_id=obj_id, t=3))) return ''
def BAM_visualisation(self, bam_object, filename_without_extension, *args, **kw): #find the bam.bai file associated bai = DBSession.query(Files_up).filter(and_(Files_up.filename == filename_without_extension + ".bam.bai", Files_up.extension == "bai")).first() if bai is None: flash("Sorry but " + bam_object.filename + " has no .bam.bai associated in BioRepo. Upload it and retry the operation.", "error") raise redirect(url("/search")) else: bai_sha1 = bai.sha1 bam_sha1 = bam_object.sha1 bai_path = bai.path bam_path = bam_object.path #build the full paths (bam and bam.bai) bai_full_path = bai_path + "/" + bai_sha1 bam_full_path = bam_path + "/" + bam_sha1 #creating symlink with good names and good extensions #for the bam file bam_dest = bam_path + "/" + bam_sha1 + ".bam" if os.path.islink(bam_dest): pass else: os.symlink(bam_full_path, bam_dest) bam_name = bam_sha1 + ".bam" #for the bai file bai_dest = bam_path + "/" + bam_sha1 + ".bam.bai" if os.path.islink(bai_dest): pass else: os.symlink(bai_full_path, bai_dest) return bam_name
def index(self, *args, **kw): user = handler.user.get_user_in_session(request) user_lab = session.get("current_lab", None) admins = tg.config.get('admin.mails') mail = user.email if user_lab and mail not in admins: lab = DBSession.query(Labs).filter(Labs.name == user_lab).first() projects = [p for p in user.projects if p in lab.projects] elif mail in admins: projects = DBSession.query(Projects).all() else: projects = None all_projects = [util.to_datagrid(ProjectGrid(), projects, "Projects Table", len(projects) > 0)] return dict(page='projects', model='project', form_title="new project", items=all_projects, value=kw)
def get_values_from_attributs_meas(self, att): ''' for a nice display of several attributs_values.value by one attributs.key ''' att_values = DBSession.query(Attributs_values).filter(and_(Attributs_values.attribut_id == att.id,\ Attributs_values.deprecated == False)).all() #for the boolean display if att.widget == "checkbox": for v in att_values: if v in self.meas.a_values: val = check_boolean(v.value) att_key = att.key att_key = att_key.replace("_", " ") if not val: return "NOT " + att_key else: return att_key #for the others widget's types else: list_values = [] for v in att_values: if v in self.meas.a_values: if v.value not in list_values: list_values.append(v.value) list_values = [l for l in list_values if l] final = " ; ".join(list_values) return final
def get_all(self, *args, **kw): user = handler.user.get_user_in_session(request) # user attributs #to block to one specific user attributs = DBSession.query(Attributs).all() all_attributs = [util.to_datagrid(attribut_grid, attributs, "Attributs Table", len(attributs) > 0)] return dict(page='attributs', model='attribut', form_title="new attribut", items=all_attributs, value=kw)
def get_UCSC_link(obj_id): ''' Return a HTML link to UCSC ''' meas = DBSession.query(Measurements).filter( Measurements.id == obj_id).first() status = meas.status_type normal_ext = ["bed", "bedgraph", "wig"] #binary extension, except bam files binary_ext = ["bw", "bigwig", "bigbed", "bb"] if status and len(meas.fus) > 0: list_fus = meas.fus for x in list_fus: f_sha1 = x.sha1 ext = x.extension #t is type. 1 == normal extension, 2 == binary extension, 3 == bam if ext.lower() in normal_ext: return ''' <a class='action UCSC_link' href="%s" target="_blank" title="view in UCSC" style="text-decoration:none" target="_blank"></a> ''' % ( url('./public/UCSC_link', params=dict(sha1=f_sha1, meas_id=obj_id, t=1))) elif ext.lower() in binary_ext: return ''' <a class='action UCSC_link' href="%s" target="_blank" title="view in UCSC" style="text-decoration:none" target="_blank"></a> ''' % ( url('./public/UCSC_link', params=dict(sha1=f_sha1, meas_id=obj_id, t=2))) elif ext.lower() == "bam": return ''' <a class='action UCSC_link' href="%s" target="_blank" title="view in UCSC" style="text-decoration:none" target="_blank"></a> ''' % ( url('./public/UCSC_link', params=dict(sha1=f_sha1, meas_id=obj_id, t=3))) return ''
def delete(self, *args, **kw): user = handler.user.get_user_in_session(request) project = DBSession.query(Projects).filter(Projects.id == args[0]).first() admin = isAdmin(user) if project.user_id == user.id or admin: try: flash("Your project " + str(project.project_name) + " has been deleted with success") except: flash("Your project " + (project.project_name) + " has been deleted with success") DBSession.delete(project) DBSession.flush() raise redirect('/projects') else: flash("It is not your project -> you are not allowed to delete it", 'error') raise redirect('/projects')
def BAM_visualisation(self, bam_object, filename_without_extension, *args, **kw): #find the bam.bai file associated bai = DBSession.query(Files_up).filter( and_(Files_up.filename == filename_without_extension + ".bam.bai", Files_up.extension == "bai")).first() if bai is None: flash( "Sorry but " + bam_object.filename + " has no .bam.bai associated in BioRepo. Upload it and retry the operation.", "error") raise redirect(url("/search")) else: bai_sha1 = bai.sha1 bam_sha1 = bam_object.sha1 bai_path = bai.path bam_path = bam_object.path #build the full paths (bam and bam.bai) bai_full_path = bai_path + "/" + bai_sha1 bam_full_path = bam_path + "/" + bam_sha1 #creating symlink with good names and good extensions #for the bam file bam_dest = bam_path + "/" + bam_sha1 + ".bam" if os.path.islink(bam_dest): pass else: os.symlink(bam_full_path, bam_dest) bam_name = bam_sha1 + ".bam" #for the bai file bai_dest = bam_path + "/" + bam_sha1 + ".bam.bai" if os.path.islink(bai_dest): pass else: os.symlink(bai_full_path, bai_dest) return bam_name
def new(self, *args, **kw): tmpl_context.widget = new_attribut_form #take the logged user user = handler.user.get_user_in_session(request) #take the logged user samples samples = DBSession.query(Samples).join(Projects).join(User).filter( User.id == user.id).all() meas = DBSession.query(Measurements).all() attributs = DBSession.query(Attributs).all() tmpl_context.samples = samples tmpl_context.meas = meas tmpl_context.attributs = attributs return dict(page='samples', value=kw, title='New Sample', model='Sample')
def get_user(key, mail): ''' Get the user with the the given mail, with the given key. ''' print key, "------------ key given" print mail, " ---------- mail given" return DBSession.query(User).filter(and_(User.email == mail, User.key == key)).first()
def post(self, *args, **kw): user = handler.user.get_user_in_session(request) user_lab = session.get("current_lab", None) if user_lab: lab = DBSession.query(Labs).filter(Labs.name == user_lab).first() p = Projects() if kw['project_name'] == '': flash("Bad Project : Your project must have a name", "error") raise redirect("./new") p.project_name = kw['project_name'] p.user_id = user.id p.description = kw.get('description', None) (p.labs).append(lab) DBSession.add(p) DBSession.flush() flash("Project created !") raise redirect('/projects')
def check_mail(self, mail): mail = mail.lower() user = DBSession.query(User).filter(User._email == mail).first() if user is None: #send False to the HTSstation method return {'in_biorepo': False} else: #send True to the HTSstation method return {'in_biorepo': True}
def get_user(key, mail): ''' Get the user with the the given mail, with the given key. ''' print key, "------------ key given" print mail, " ---------- mail given" return DBSession.query(User).filter( and_(User.email == mail, User.key == key)).first()
def get_projects(self): list_projects = [] for sample in self.samples: p_id = sample.project_id project = DBSession.query(Projects).filter(Projects.id == p_id).first() p_name = project.project_name if (p_name + " (p_id: " + str(p_id) + ")") not in list_projects: list_projects.append(p_name + " (p_id: " + str(p_id) + ")") return ' ; '.join(list_projects)
def get_projects(self): list_projects = [] for sample in self.samples: p_id = sample.project_id project = DBSession.query(Projects).filter( Projects.id == p_id).first() p_name = project.project_name if (p_name + " (p_id: " + str(p_id) + ")") not in list_projects: list_projects.append(p_name + " (p_id: " + str(p_id) + ")") return ' ; '.join(list_projects)
def Gviz_link(self, sha1, meas_id, *args, **kw): ''' redirect to Gviz BAM viewer hosted on HTSstation ''' #URL example : #bbcftools.epfl.ch/bam_viewer/gviews/new?assembly_name=pombe&module=biorepo&file=XTv7U2IYVAgIWqBHyPjC meas = DBSession.query(Measurements).filter(Measurements.id == meas_id).first() list_a_values = [] #get the dynamic values for val in meas.a_values: list_a_values.append(val.id) #check if "assembly" is a dynamic key for this measurement cpt_test = 0 for a in meas.attributs: if a.key == "assembly": cpt_test += 1 #get all the values recorded for this key list_assemblies = a.values assembly = '' for v in list_assemblies: #check if the Attributs_values object is linked to this measurement if v.id in list_a_values: assembly = v.value if assembly == '': flash("Sorry but you have to set an assembly to this measurement", "error") raise redirect("/search") else: #TODO replace this when BioRepo and HTS will be connected together #JUST CATCH THE PATH IN DB AND GIVE IT IN THE URL #/!\ /archive/projects/epfl-vital-it/biorepo_upload/ /!\ hostname = socket.gethostname().lower() #because of aliasing if hostname == "ptbbsrv2.epfl.ch": hostname = "biorepo.epfl.ch" bam_file = DBSession.query(Files_up).filter(Files_up.sha1 == sha1).first() fullname = bam_file.filename name_tmp = fullname.split('.') name = name_tmp[0] bam_name = self.BAM_visualisation(bam_file, name) bam_path = bam_file.path path_to_give = bam_path.split("/archive/projects/epfl/biorepo_upload")[1] raise redirect('http://bbcftools.epfl.ch/bam_viewer/gviews/new?assembly_name=' + assembly + "&module=biorepo&file=" + path_to_give + "/" + bam_name)
def check_value_addition(self, list_type_att, dict_att_values_type, dict_att_values_db, lab_id): ''' Checking for value(s) addition @param principal @return an updated db ''' for key_type in list_type_att: for i in dict_att_values_type[key_type]: #get the attribut and its value(s) i_att = DBSession.query(Attributs).filter(and_(Attributs.lab_id == lab_id, Attributs.key == key_type)).first() #warning : i_att_value is a list ! i_att_value = DBSession.query(Attributs_values).filter(and_(Attributs_values.attribut_id == i_att.id, Attributs_values.value == i)).all() try: flag = False if i not in dict_att_values_db[key_type] and i != "None": for existant in i_att_value: if existant.value == i and existant.deprecated == True: existant.deprecated = False DBSession.flush() flag = True if existant.value is None: existant.deprecated = True DBSession.flush() if flag == False: new_value = Attributs_values() new_value.attribut_id = i_att.id new_value.value = i new_value.deprecated = False DBSession.add(new_value) DBSession.flush() except Exception as e: import sys, traceback a, b, c = sys.exc_info() traceback.print_exception(a, b, c) print "--- error login.py check_value_addition() ---" print "i --->", i print "key_type --->", key_type print "list_type_att --->", list_type_att print "dict_att_values_type[key_type] -->", dict_att_values_type[key_type] print "i_att --> ", i_att print "i_att_value", i_att_value
def create_ext_lab(self, mail, key, lab_name): #utilisation (only for admins) : #wget --post-data "[email protected]&key=xxxxxxxxxxx&lab_name=bbcf" http://yourdomain.com/biorepo/create_ext_lab lab_test = DBSession.query(Labs).filter(Labs.name == lab_name).first() if lab_test is None: try: lab = Labs() lab.name = lab_name lab.path_raw = path_raw(lab_name) lab.path_processed = path_processed(lab_name) lab.path_tmp = path_tmp(lab_name) DBSession.add(lab) DBSession.flush() print "Exterior lab created :", lab_name except: print_traceback() print "Exterior lab NOT created --> ERROR" else: print "This lab : ", str(lab_name), " is in the db yet. --> ERROR"
def new(self, **kw): #get the logged user user = handler.user.get_user_in_session(request) user_lab = session.get("current_lab", None) samples = [] if user_lab is not None: lab = DBSession.query(Labs).filter(Labs.name == user_lab).first() attributs = DBSession.query(Attributs).filter(and_(Attributs.lab_id == lab.id, Attributs.deprecated == False)).all() projects = [p.id for p in user.projects if p in lab.projects] for a in attributs: for s in a.samples: if s not in samples and s.project_id in projects: samples.append(s) new_form = NewProject(action=url('/projects/post')).req() new_form.child.children[0].placeholder = "Your project name..." new_form.child.children[1].placeholder = "Your commments here..." new_form.child.children[2].options = [(sample.id, '%s' % (sample.name)) for sample in samples] return dict(page='projects', widget=new_form)
def post_delete(self, *args, **kw): for id in args: permission = DBSession.query(Permission).filter(Permission.id == id).first() if permission.name == gl.perm_admin: flash('Cannot delete admin permission') redirect('/permissions') if permission.name == gl.perm_user: flash('Cannot delete read permission') redirect('/permissions') return CrudRestController.post_delete(self, *args, **kw)
def post_delete(self, *args, **kw): for id in args: group = DBSession.query(Group).filter(Group.id == id).first() if group.name == gl.group_admins: flash('Cannot delete admin group') redirect('/groups') if group.name == gl.group_users: flash('Cannot delete users group') redirect('/groups') return CrudRestController.post_delete(self, *args, **kw)
def new(self, *args, **kw): #take the logged user user = handler.user.get_user_in_session(request) user_lab = session.get("current_lab", None) if user_lab: lab = DBSession.query(Labs).filter(Labs.name == user_lab).first() projects = DBSession.query(Projects).filter(Projects.user_id == user.id).all() for p in projects: if p not in lab.projects: projects.remove(p) new_form = build_form("new", "sample", None)(action=url('/samples/post')).req() #static fields new_form.child.children[0].options = [(project.id, '%s' % project.project_name) for project in projects] new_form.child.children[1].placeholder = "Your sample name..." new_form.child.children[2].options = get_list_types(user_lab) new_form.child.children[3].placeholder = "Your protocole here..." return dict(page='samples', widget=new_form)
def post_delete(self, *args, **kw): for id in args: permission = DBSession.query(Permission).filter( Permission.id == id).first() if permission.name == gl.perm_admin: flash('Cannot delete admin permission') redirect('/permissions') if permission.name == gl.perm_user: flash('Cannot delete read permission') redirect('/permissions') return CrudRestController.post_delete(self, *args, **kw)
def hts_boss(self, key): ''' return dico with sha1 and meas_id from HTSStation files /!\ Only Fabrice is able to use it ''' VIP_user = DBSession.query(User).filter(User.id == 9).first() if key != VIP_user.key: return {'ERROR': 'Restricted Boss method.'} dico_id_path = {} fus = DBSession.query(Files_up).filter( Files_up.path.like('/data/%')).distinct() for f in fus: sha1 = f.sha1 list_meas = f.measurements for m in list_meas: m_id = m.id if sha1 in dico_id_path.keys(): dico_id_path[sha1].append(m_id) else: dico_id_path[sha1] = [m_id] return dico_id_path
def get_user_in_session(request): ''' Get the user that is performing the current request @param request: the web request @type request: a WebOb ''' if not 'repoze.who.identity' in request.environ: print "############### abort in handler/user ################" abort(401) identity = request.environ['repoze.who.identity'] email = identity['repoze.who.userid'] user = DBSession.query(User).filter(User.email == email).first() return user
def get_all(self, *args, **kw): user = handler.user.get_user_in_session(request) # user attributs #to block to one specific user attributs = DBSession.query(Attributs).all() all_attributs = [ util.to_datagrid(attribut_grid, attributs, "Attributs Table", len(attributs) > 0) ] return dict(page='attributs', model='attribut', form_title="new attribut", items=all_attributs, value=kw)
def manage_fu_from_HTS(existing_fu, meas, filename, sha1, url_path, hts_path): #fixing bug str "true", str "false" for meas.type if isinstance(meas.type, basestring): bool_type = str2bool(meas.type) meas.type = bool_type if existing_fu: print "-------- EXISTING FILE FROM HTS--------" meas.fus.append(existing_fu) DBSession.add(meas) DBSession.flush() return existing_fu ########################################################################################### else: #new files_up building fu = Files_up() fu.path = hts_path #save the filename and the extension to the database fu.filename = filename if '.' in filename: extension = filename.split('.')[-1] fu.extension = extension else: fu.extension = "not specified" fu.sha1 = sha1 fu.url_path = url_path #add to the crossing table (measurement <-> file uploaded) meas.fus.append(fu) #adding new measurement and new files_up to the db DBSession.add(meas) DBSession.add(fu) DBSession.flush() return fu
def get_values_from_attributs_sample(self, att): ''' for a nice display of several attributs_values.value by one attributs.key ''' att_values = DBSession.query(Attributs_values).filter(and_(Attributs_values.attribut_id == att.id,\ Attributs_values.deprecated == False)).all() list_values = [] #for the boolean display if att.widget == "checkbox": for v in att_values: for s in self.samples: try: if v in self.s.a_values: val = check_boolean(v.value) att_key = att.key att_key = att_key.replace("_", " ") if not val: word = "NOT " + att_key if word not in list_values: list_values.append(word) else: if att_key not in list_values: list_values.append(att_key) #exception at the beginning of a lab when one or several checkbox doesn't get values yet except: att_key = att.key att_key = att_key.replace("_", " ") word = "NOT " + att_key if word not in list_values: list_values.append(word) else: if att_key not in list_values: list_values.append(att_key) #for the others widget's types else: for v in att_values: for s in self.samples: if v in s.a_values: if v.value not in list_values: list_values.append(v.value) list_values = [l for l in list_values if l] final = " ; ".join(list_values) return final
def get_GViz_link(obj_id): ''' Return a HTML link to Gviz HTSstation ''' meas = DBSession.query(Measurements).filter( Measurements.id == obj_id).first() status = meas.status_type if status and len(meas.fus) > 0: list_fus = meas.fus for x in list_fus: f_sha1 = x.sha1 ext = x.extension if ext.lower() == "bam": return ''' <a class='action GViz_link' href="%s" target="_blank" title="view in GViz" style="text-decoration:none" target="_blank"></a> ''' % ( url('./public/Gviz_link', params=dict(sha1=f_sha1, meas_id=obj_id))) return ''
def get_public_link(obj_id): ''' Return a HTML public download link. ''' #TODO : understand and fix the bug... meas = DBSession.query(Measurements).filter( Measurements.id == obj_id).first() status = meas.status_type f_sha1 = '' #have to be public if status and len(meas.fus) > 0: list_fus = meas.fus for x in list_fus: f_sha1 = x.sha1 return ''' <a class='action public_link' href="%s" title="public link for this measurement" style="text-decoration:none"></a> ''' % ( url('./public/public_link', params=dict(sha1=f_sha1, m_id=obj_id))) else: return '''
def build_attribut_value(self, s, lab_id, dict_fixed_values_type, list_fixed_values_type, config): ''' build Attribut values @return a list of Attribut values ''' list_objects_value = [] for att in list_fixed_values_type: #build a dictionnary of fixed values : {'key':'[value1, value2, value3, ...}'} list_values = [] list_values = (config.get(s + att, att)).split(',') dict_fixed_values_type[att] = list_values for k, list_values in dict_fixed_values_type.iteritems(): att_tmp = DBSession.query(Attributs).filter( and_(Attributs.key == k), Attributs.lab_id == lab_id).first() for v in list_values: attributs_v = Attributs_values() attributs_v.attribut_id = att_tmp.id attributs_v.value = v attributs_v.deprecated = False list_objects_value.append(attributs_v) return list_objects_value
def clone_form(user_lab, id_object): ''' to clone dynamic measurement form ''' lab = DBSession.query(Labs).filter(Labs.name == user_lab).first() #static list ] list_static = [ twf.HiddenField(id="IDselected", label_text="ID selected :"), twf.TextField(id="name", label_text="Name :", placeholder="Measurement name...", validator=twc.Validator(required=True)), twf.TextArea(id="description", label_text="Description :"), twf.MultipleSelectField( id="samples", label_text="Your samples : ", help_text="You can add some of your existing data to this project." ), twf.CheckBox( id="status_type", label_text="Privacy : ", help_text= "Check to have it available from outside EPFL (required for UCSC visualisation)" ), twf.CheckBox(id="type", label_text="Raw data : ", help_text="Check if raw data"), #twf.MultipleSelectField(id="parents", label_text="Parents : ", help_text="Parent(s) of this measurement."), twd.HidingRadioButtonList(id="upload_way", label_text='Upload my file via...', options=('my computer', 'a Vital-IT path', 'a URL'), mapping={ 'my computer': ['upload'], 'a Vital-IT path': ['vitalit_path'], 'a URL': ['url_path', 'url_up'], }), twf.FileField(id="upload", help_text='Please provide a data'), twf.TextField(id="vitalit_path", label_text="Scratch path", placeholder="/scratch/el/biorepo/dropbox/"), twf.TextField(id="url_path", label_text="File's url", placeholder="http://www..."), twf.CheckBox(id="url_up", label_text="I want to upload the file from this URL : ", help_text="tick it if you want to download it in BioRepo") ] list_dynamic = [] if lab is None: print "----- no dynamic fields detected ---------" list_fields = [list_static, list_dynamic] return list_fields else: to_clone = DBSession.query(Measurements).filter( Measurements.id == int(id_object)).first() tag = "measurements" if to_clone is not None: list_dynamic_attributes = to_clone.attributs for att in list_dynamic_attributes: if att.deprecated == False: twf_type = convert_widget(att.widget) twf_type.id = att.key list_a_values = att.values if att.widget == "textfield" or att.widget == "textarea" or att.widget == "hiding_textfield" or att.widget == "hiding_textarea": for v in list_a_values: if hasattr(v, tag): value_object = getattr(v, tag) if to_clone in value_object: twf_type.value = v.value list_dynamic.append(twf_type) elif att.widget == "checkbox" or att.widget == "hiding_checkbox": for v in list_a_values: if hasattr(v, tag): value_object = getattr(v, tag) if to_clone in value_object: #dynamic boolean are stored in varchar in the db, we have to cast them in boolean for the display value_2_display = check_boolean(v.value) twf_type.value = value_2_display list_dynamic.append(twf_type) elif att.widget == "multipleselectfield" or att.widget == "hiding_multipleselectfield": list_possible_values = [] for v in list_a_values: list_possible_values.append(v.value) twf_type.options = list_possible_values selected_values = [] for v in list_a_values: if hasattr(v, tag): value_object = getattr(v, tag) if to_clone in value_object: selected_values.append(v.value) twf_type.value = selected_values list_dynamic.append(twf_type) elif att.widget == "singleselectfield" or att.widget == "hiding_singleselectfield": list_possible_values = [] for v in list_a_values: if v.value not in list_possible_values: list_possible_values.append(v.value) twf_type.options = list_possible_values for v in list_a_values: if hasattr(v, tag): value_object = getattr(v, tag) if to_clone in value_object: twf_type.value = v.value list_dynamic.append(twf_type) else: print "Your measurement was not found. ID problem. id :", id_object raise list_fields = [list_static, list_dynamic] return list_fields
def by_email_address(cls, email): """Return the user object whose email address is ``email``.""" return DBSession.query(cls).filter(cls.email == email).first()
def new_form_parents(user_lab): '''for new form with parents''' lab = DBSession.query(Labs).filter(Labs.name == user_lab).first() #static lists list_static_samples = [ twf.SingleSelectField(id="project", label_text="Your projects : ", help_text="Select project for this sample", prompt_text=None), twf.TextField(id="name", label_text="Name :", validator=twc.Validator(required=True)), twf.SingleSelectField(id="type", label_text="Type : ", help_text="Technique used", prompt_text=None), twf.TextArea( id="protocole", label_text="Protocole :", ) ] list_static_measurements = [ twf.HiddenField(id="IDselected", label_text="ID selected :"), twf.TextField(id="name", label_text="Name :", placeholder="Measurement name...", validator=twc.Validator(required=True)), twf.TextArea(id="description", label_text="Description :"), twf.MultipleSelectField( id="samples", label_text="Your samples : ", help_text="You can add some of your existing data to this project." ), twf.CheckBox( id="status_type", label_text="Privacy : ", help_text= "Check to have it available from outside EPFL (required for UCSC visualisation)" ), twf.CheckBox(id="type", label_text="Raw data : ", help_text="Check if raw data"), twf.MultipleSelectField(id="parents", label_text="Parents : ", help_text="Parent(s) of this measurement."), twd.HidingRadioButtonList(id="upload_way", label_text='Upload my file via...', options=('my computer', 'a Vital-IT path', 'a URL'), mapping={ 'my computer': ['upload'], 'a Vital-IT path': ['vitalit_path'], 'a URL': ['url_path', 'url_up'], }), twf.FileField(id="upload", help_text='Please provide a data'), twf.TextField(id="vitalit_path", label_text="Scratch path", placeholder="/scratch/el/biorepo/dropbox/"), twf.TextField(id="url_path", label_text="File's url", placeholder="http://www..."), twf.CheckBox(id="url_up", label_text="I want to upload the file from this URL : ", help_text="tick it if you want to download it in BioRepo") ] list_dynamic_samples = [] list_hiding_samples = [] list_dynamic_measurements = [] list_hiding_meas = [] #catch the dynamic hiding fields dic_hiding_meas = session.get("hiding_meas", {}) dic_hiding_samples = session.get("hiding_sample", {}) if lab is None: print "----- no dynamic fields detected ---------" list_fields = [ list_static_samples, list_dynamic_samples, list_static_measurements, list_dynamic_measurements ] return list_fields else: lab_id = lab.id list_attributs = DBSession.query(Attributs).filter( Attributs.lab_id == lab_id).all() if len(list_attributs) > 0: #lists_construction(list_attributs) for a in list_attributs: attribut_key = a.key deprecated = a.deprecated fixed_value = a.fixed_value widget = a.widget owner = a.owner ############################# ######### NEW SAMPLE ######## ############################# if owner == "sample": #dynamic if not deprecated and fixed_value: twf_type = convert_widget(widget) twf_type.id = attribut_key if widget == "multipleselectfield" or widget == "singleselectfield": list_values = [] list_attributes_values = DBSession.query( Attributs_values).filter( Attributs_values.attribut_id == a.id).all() for av in list_attributes_values: if not av.deprecated and av.value not in list_values: list_values.append(av.value) twf_type.options = list_values list_dynamic_samples.append(twf_type) elif widget == "checkbox": list_dynamic_samples.append(twf_type) elif widget == "hiding_singleselectfield": list_values = [] list_attributes_values = DBSession.query( Attributs_values).filter( Attributs_values.attribut_id == a.id).all() for av in list_attributes_values: if not av.deprecated and av.value not in list_values: list_values.append(av.value) twf_type.options = list_values list_hiding_samples.append(twf_type) elif widget == "hiding_checkbox": list_hiding_samples.append(twf_type) else: print widget, "-----ERROR----- ELSE, type samples widget in forms.py" elif not deprecated and not fixed_value: twf_type = convert_widget(widget) twf_type.id = attribut_key if widget == "textfield" or widget == "textarea": twf_type.placeholder = "Write here..." list_dynamic_samples.append(twf_type) elif widget == "checkbox": list_dynamic_samples.append(twf_type) elif widget == "hiding_textfield" or widget == "hiding_textarea": twf_type.placeholder = "Write here..." list_hiding_samples.append(twf_type) elif widget == "hiding_checkbox": list_hiding_samples.append(twf_type) else: print widget, "WIDGET SAMPLE NOT FOUND, add an elif please" raise elif deprecated: pass else: print "WIDGET SAMPLES ERROR : widget type is not known --> ", widget raise ################################ ######## NEW MEASUREMENT ####### ################################ elif owner == "measurement": #dynamic #for attributes with fixed values if not deprecated and fixed_value: twf_type = convert_widget(widget) twf_type.id = attribut_key if widget == "multipleselectfield" or widget == "singleselectfield": list_values = [] list_attributes_values = DBSession.query( Attributs_values).filter( Attributs_values.attribut_id == a.id).all() for av in list_attributes_values: if not av.deprecated and av.value not in list_values: list_values.append(av.value) twf_type.options = list_values list_dynamic_measurements.append(twf_type) elif widget == "hiding_singleselectfield": list_values = [] list_attributes_values = DBSession.query( Attributs_values).filter( Attributs_values.attribut_id == a.id).all() for av in list_attributes_values: if not av.deprecated and av.value not in list_values: list_values.append(av.value) twf_type.options = list_values list_hiding_meas.append(twf_type) #elif widget == "checkbox": #list_dynamic_measurements.append(twf_type) else: print widget, "-----ERROR----- ELSE, type measurements widget in forms.py" raise #for others attributes elif not deprecated and not fixed_value: twf_type = convert_widget(widget) twf_type.id = attribut_key if widget == "textfield" or widget == "textarea": twf_type.placeholder = "Write here..." list_dynamic_measurements.append(twf_type) elif widget == "checkbox": list_dynamic_measurements.append(twf_type) elif widget == "hiding_textfield" or widget == "hiding_textarea": twf_type.placeholder = "Write here..." list_hiding_meas.append(twf_type) elif widget == "hiding_checkbox": list_hiding_meas.append(twf_type) else: print widget, "WIGDET MEASUREMENT NOT FOUND, add an elif please" raise elif deprecated: pass #in bugs case else: print "WIDGET MEASUREMENTS ERROR : widget type is not known --> ", widget raise #TO TEST WITH SEVERAL TWD OBJECTS #build dynamic dynamic fields #samples list_twd_s = [] for k in dic_hiding_samples: twd_object = twd.HidingRadioButtonList() twd_object.id = k dico_mapping = dic_hiding_samples[k] options = [] for key in dico_mapping.keys(): options.append(key) twd_object.options = options twd_object.mapping = dico_mapping list_twd_s.append(twd_object) list_dynamic_samples = list_dynamic_samples + list_twd_s + list_hiding_samples #measurements list_twd_m = [] for k in dic_hiding_meas: twd_object = twd.HidingRadioButtonList() twd_object.id = k dico_mapping = dic_hiding_meas[k] options = [] for key in dico_mapping.keys(): options.append(key) twd_object.options = options twd_object.mapping = dico_mapping list_twd_m.append(twd_object) list_dynamic_measurements = list_dynamic_measurements + list_twd_m + list_hiding_meas list_fields = [ list_static_samples, list_dynamic_samples, list_static_measurements, list_dynamic_measurements ] return list_fields else: print "-----forms.py----- Houston, we have a problem : The lab ", lab.name, " doesn't get any attributes -----" raise
def edit(self, *args, **kw): tmpl_context.widget = attribut_edit_form user = handler.user.get_user_in_session(request) attribut = DBSession.query(Attributs).filter( Attributs.id == args[0]).first()
def setdefaultkey(self): uid = str(uuid.uuid4()) while DBSession.query(User).filter(User.key == uid).first(): uid = str(uuid.uuid4()) return uid
def to_json_test(self): static_fields = { 'Description': self.meas.description, 'User': self.get_name(), 'Measurements': self.get_meas_name(), 'description': self.meas.description, 'Created': self.date.strftime(date_format), 'Samples': ' ; '.join(['%s' % (sample.name) for sample in self.samples]), 'Projects': self.get_projects(), 'Type': self.get_sample_type(), 'DataType': self.get_measurement_type(), 'scroll_info': genshi.Markup(self.get_img_scroll()), 'Attachment': self.get_extension, 'Actions': get_info_link(self.id) + get_dl_link(self.id) + get_public_link(self.id) + get_UCSC_link(self.id) + get_GViz_link(self.id) + get_SPAN_id(self.id) } #find None statics fields to change the display in datatables for sf in static_fields.keys(): if static_fields[sf] is None or static_fields[sf] == "": static_fields[sf] = None dyn_in_searchgrid = session.get("search_grid_fields", []) labo = session.get("current_lab") for d in dyn_in_searchgrid: new = d.replace("_", " ") dyn_in_searchgrid.remove(d) dyn_in_searchgrid.append(new) meas_dynamic_fields = {} samples_dynamic_fields = {} attributs_meas = [ a.to_json() for a in self.meas.attributs if not a.deprecated ] list_avalues_meas = self.meas.a_values for avm in list_avalues_meas: for am in attributs_meas: key = am["key"].replace("_", " ") if str(am["id"]) == str(avm.attribut_id): if am["widget"] != "checkbox" and am[ "widget"] != "hiding_checkbox": meas_dynamic_fields[key] = avm.value else: if check_boolean(avm.value): meas_dynamic_fields[key] = key else: meas_dynamic_fields[key] = "NOT " + str(key) attributs_samples = self.get_attributs_samples_json() if len(self.samples) < 2: for s in self.samples: list_avalues_samples = s.a_values for avs in list_avalues_samples: for a_s in attributs_samples: key = a_s["key"].replace("_", " ") if str(a_s["id"]) == str(avs.attribut_id): if a_s["widget"] != "checkbox" and a_s[ "widget"] != "hiding_checkbox": samples_dynamic_fields[key] = avs.value else: if check_boolean(avs.value): samples_dynamic_fields[key] = key else: samples_dynamic_fields[key] = "NOT " + str( key) else: for s in self.samples: list_avalues_samples = s.a_values for avs in list_avalues_samples: for a_s in attributs_samples: key = a_s["key"].replace("_", " ") if str(a_s["id"]) == str(avs.attribut_id): if a_s["widget"] != "checkbox" and a_s[ "widget"] != "hiding_checkbox": if key not in samples_dynamic_fields.keys(): samples_dynamic_fields[key] = [avs.value] else: if avs.value not in samples_dynamic_fields[ key]: samples_dynamic_fields[key].append( avs.value) else: if check_boolean(avs.value): if key not in samples_dynamic_fields.keys( ): samples_dynamic_fields[key] = [key] else: samples_dynamic_fields[key].append(key) else: if key not in samples_dynamic_fields.keys( ): samples_dynamic_fields[key] = [ "NOT " + str(key) ] else: samples_dynamic_fields[key].append( "NOT " + str(key)) for k in samples_dynamic_fields.keys(): samples_dynamic_fields[k] = " ; ".join( samples_dynamic_fields[k]) #Sorting dynamic fields with conf file to display in searchgrid dyn_fields = {} for k in samples_dynamic_fields: if k in dyn_in_searchgrid: dyn_fields[k.capitalize()] = samples_dynamic_fields[k] for key in meas_dynamic_fields: if key in dyn_in_searchgrid: dyn_fields[key.capitalize()] = meas_dynamic_fields[key] #for the empty fields for k in dyn_in_searchgrid: if k.capitalize() not in dyn_fields.keys(): k_db = k.replace(" ", "_") lab = DBSession.query(Labs).filter(Labs.name == labo).first() lab_id = lab.id k_obj = DBSession.query(Attributs).filter( and_(Attributs.key == k_db, Attributs.lab_id == lab_id)).first() if k_obj.widget != "checkbox" and k_obj.widget != "hiding_checkbox": dyn_fields[k.capitalize()] = None else: dyn_fields[k.capitalize()] = ["NOT " + str(k)] final = dict(static_fields.items() + dyn_fields.items()) return final
def build_search_grid(measurements): search_grid = BootstrapGrid() #static end end_fields = [('Attachment', lambda obj: genshi.Markup(obj.get_extension)), ('Description', "description"), ("Date", "created"), ("Action", lambda obj: genshi.Markup( get_info_link(obj.id) + get_dl_link(obj.id) + get_public_link(obj.id) + get_UCSC_link(obj.id) + get_GViz_link(obj.id) + get_SPAN_id(obj.id)))] #static and dynamic fields fields = [] fields_static = [("", "scroll_info"), ("User", "user"), ("Projects", lambda obj:genshi.Markup(obj.projects_display)), ("Samples", lambda obj:genshi.Markup( obj.samples_display)), ("Type", lambda obj:genshi.Markup(obj.sample_type)),\ ("Measurements", lambda obj:genshi.Markup(obj.name)), ("DataType", lambda obj:genshi.Markup(obj.measurement_type))] fields_dyn = [] list_searchable = [] positions_not_searchable = [] hidden_list = [] lab_id = None if not isinstance(measurements, list): measurements = [measurements] if len(measurements) > 0: meas = measurements[0] #dyn meas for att in meas.attributs: #get the lab_id lab_id = att.lab_id vals = lambda obj, a=att: obj.get_values_from_attributs_meas(a) fields_dyn.append((att.key, vals)) if att.searchable == True: list_searchable.append(att.key) #dyn sample if len(meas.samples) > 0: sample = (meas.samples)[0] for att in sample.attributs: val = lambda obj, a=att: obj.get_values_from_attributs_sample(a ) fields_dyn.append((att.key, val)) if att.searchable == True: list_searchable.append(att.key) ############## CUSTOMIZE THE SEARCH GRID BY LAB ####################### #/!\ the grid begins at 0 #to customize hidden fields by lab lab = DBSession.query(Labs).filter(Labs.id == lab_id).first() movable_fields = fields_static + fields_dyn if lab: if lab.name == "ptbb": pass elif lab.name == "updub": pass elif lab.name == "lvg": #move some field in the grid for f in movable_fields: if f[0] == "flag_final": new_list = value_travel_into_da_list( movable_fields, movable_fields.index(f), len(movable_fields)) movable_fields = new_list for f in movable_fields: if f[0] == "quality": new_list = value_travel_into_da_list( movable_fields, movable_fields.index(f), len(movable_fields)) movable_fields = new_list #hide Samples name for f in movable_fields: if f[0] == "Samples": i = movable_fields.index(f) hidden_list.append(i) for f in movable_fields: if f[0] == "ab_source": i = movable_fields.index(f) hidden_list.append(i) elif lab.name == "upnae": fields_to_hide = ["replica_id", "drug_dose", "lane_id", "paired_end_id", "strand",\ "source", "machine", "starting_material", "treatment", "paired_end", "polya", "strand_specific",\ "viewpoint", "protein_bait", "technical_replica_id", "feeding_type", "light_condition"] for f in movable_fields: if f[0] in fields_to_hide: i = movable_fields.index(f) hidden_list.append(i) elif lab.name == "stutz": fields_to_hide = ["article_title", "strain", "time_point", "antibody", "treatment_time",\ "phase", "medium", "background"] for f in movable_fields: if f[0] in fields_to_hide: i = movable_fields.index(f) hidden_list.append(i) elif lab.name == "shore": fields_to_hide = [ "article_title", "year", "time_point", "sequencing_method", "treatment_time", "phase" ] for f in movable_fields: if f[0] in fields_to_hide: i = movable_fields.index(f) hidden_list.append(i) #addition with the 3 common end-fields fields = movable_fields + end_fields #build the list (positions_not_searchable) to send to the js for the searchable buttons for f in fields: search_grid.fields.append(f) for i, item in enumerate(search_grid.fields): if item[0] not in list_searchable: positions_not_searchable.append(i) for f in fields_static: for i, item in enumerate(movable_fields): #and i not in list_tmp if f[0] == item[0] and f[ 0] != '' and f in fields_static and i in positions_not_searchable: positions_not_searchable.remove(i) #build the list (ignored_list) for the ignored fields total = len(search_grid.fields) - 1 hidden_list.append(total - 2) #positions_not_searchable = delete the search button of the field #hidden_list = the field does not appear anymore into the searchgrid and its search button disappears BUT it is still searchable ! return search_grid, hidden_list, positions_not_searchable
def manage_fu(existing_fu, meas, public_dirname, filename, sha1, up_data, url_path, url_up, dest_raw, dest_processed, tmp_path, lab): tmpdir_to_delete = os.path.abspath(os.path.join(tmp_path, os.path.pardir)) #fixing bug str "true", str "false" for meas.type if isinstance(meas.type, basestring): bool_type = str2bool(meas.type) meas.type = bool_type if existing_fu: print "-------- EXISTING FILE --------" #create symbolic link source = existing_fu.path + "/" + sha1 if meas.type: dest = dest_raw + sha1 #test symlin existance if os.path.islink(dest): symlink_e = "This file was already in your personal folder" else: symlink_e = "This file was added to your personal folder" os.symlink(source, dest) else: dest = dest_processed + sha1 #test symlink existance if os.path.islink(dest): symlink_e = "This file was already in your personal folder" else: symlink_e = "This file was added to your personal folder" os.symlink(source, dest) meas.fus.append(existing_fu) DBSession.add(meas) DBSession.flush() flash(symlink_e + ", measurement created") #remove the tmp file if it didn't come from HTSStation if not tmpdir_to_delete.startswith('/data'): shutil.rmtree(tmpdir_to_delete) #raise redirect("./") return existing_fu ########################################################################################### else: #new files_up building fu = Files_up() #raw test for path orientation if meas.type: fu.path = path_raw(lab) data_dirname = os.path.join(public_dirname, fu.path) else: fu.path = path_processed(lab) data_dirname = os.path.join(public_dirname, fu.path) #save the filename and the extension to the database fu.filename = filename if '.' in filename: extension = filename.split('.')[-1] fu.extension = extension # root, ext = os.path.splitext(filename) # DOUBLE_EXTENSIONS = ['tar.gz','tar.bz2','bedGraph.gz','bed.gz'] # if any([filename.endswith(x) for x in DOUBLE_EXTENSIONS]): # root, first_ext = os.path.splitext(root) # ext = first_ext + ext # fu.extension = ext # else: # fu.extension = ext else: fu.extension = "not specified" data_path = os.path.join(data_dirname, str(sha1)) fu.sha1 = sha1 fu.url_path = url_path #writing the file into the server HD #version browser try: with open(data_path, "w") as d: if up_data is not None: d.write(up_data.value) elif url_path is not None and url_up: u = urllib2.urlopen(url_path) while True: buffer = u.read(8192) if not buffer: break d.write(buffer) else: print "ERROR" #version commandline except: shutil.move(tmp_path, data_path) #symlink source = data_path if meas.type: dest = dest_raw + sha1 if os.path.islink(dest): symlink_e = "This file was already in your personal folder" else: symlink_e = "This file was added to your personal folder" os.symlink(source, dest) else: dest = dest_processed + sha1 if os.path.islink(dest): symlink_e = "This file was already in your personal folder" else: symlink_e = "This file was added to your personal folder" os.symlink(source, dest) #add to the crossing table (measurement <-> file uploaded) meas.fus.append(fu) #adding new measurement and new files_up to the db DBSession.add(meas) DBSession.add(fu) DBSession.flush() if meas.type: flash(symlink_e + ", raw data was successfully created") else: flash(symlink_e + ", processed data was successfully created") #remove the tmp file if it didn't come from HTSStation if not tmpdir_to_delete.startswith('/data'): shutil.rmtree(tmpdir_to_delete) return fu
def index(self, *args, **kw): user_lab = session.get("current_lab", None) user_projects = [] u_projects = [] u_samples = [] u_meas = [] u_children = [] u_global = [] dico_final = {} #TODO : admin view - watch for a dl link if user_lab: lab = DBSession.query(Labs).filter(Labs.name == user_lab).first() lab_users = lab.users for u in lab_users: projects = DBSession.query(Projects).filter( Projects.user_id == u.id).all() if len(projects) > 0: for p in projects: for lab in p.labs: if lab.name == user_lab: user_projects.append(p) for proj in user_projects: for sample in proj.samples: if len(sample.measurements) > 0: for meas in sample.measurements: if len(meas.children) == 0: u_meas.append({ "name": str(meas.name) + "(" + str(meas.id) + ")" }) else: for child in meas.children: u_children.append({ "name": str(child.name) + "(" + str(child.id) + ")" }) u_meas.append({ "name": str(meas.name) + "(" + str(meas.id) + ")", "children": u_children }) u_children = [] u_samples.append({ "name": str(sample.name) + "(" + str(sample.type) + ")", "children": u_meas }) u_meas = [] else: u_samples.append({ "name": str(sample.name + "(" + str(sample.type) + ")") }) if len(proj.samples) > 0: u_projects.append({ "name": str(proj.project_name), "children": u_samples }) u_samples = [] else: u_projects.append({"name": str(proj.project_name)}) u_global.append({ "name": u.firstname + " " + u.name, "children": u_projects }) u_projects = [] user_projects = [] #uncomment these 4 lines if you want to see every lab users registered in BioRepo #else: #u_global.append({"name": u.firstname + " " + u.name}) #u_projects = [] #user_projects = [] dico_final["name"] = user_lab dico_final["children"] = u_global return {"data": json.dumps(dico_final)}
def edit_form(user_lab, owner, id_object): ''' to edit dynamic form ''' lab = DBSession.query(Labs).filter(Labs.name == user_lab).first() #static lists list_static_samples = [ twf.HiddenField(id="IDselected", label_text="ID selected :"), twf.SingleSelectField( id="project", label_text="Your projects : ", help_text="the project which contains your sample is selected", prompt_text=None), twf.TextField(id="name", label_text="Name :", validator=twc.Required), twf.SingleSelectField(id="type", label_text="Type : ", help_text="What technique do you use ?", prompt_text=None), twf.TextArea( id="protocole", label_text="Protocole :", ), twf.MultipleSelectField(id="measurements", label_text="Attached measurement(s) : ") ] list_static_measurements = [ twf.HiddenField(id="IDselected", label_text="ID selected :"), twf.TextField(id="name", label_text="Name :", placeholder="Measurement name...", validator=twc.Required), twf.TextArea(id="description", label_text="Description :"), twf.MultipleSelectField( id="samples", label_text="Your samples : ", help_text="You can add some of your existing data to this project." ), twf.CheckBox( id="status_type", label_text="Privacy : ", help_text= "Check to have it available from outside EPFL (required for UCSC visualisation)" ), twf.CheckBox(id="type", label_text="Raw data : ", help_text="Check if raw data"), twf.MultipleSelectField(id="parents", label_text="Parents : ", help_text="Parent(s) of this measurement."), twf.LabelField( id="uploaded", help_text= "is attached to this measurement. If you want to change, it's better to delete this measurement and create a new one." ), twf.TextField( id="url_path", help_text= "If you want to add a new URL, your old URL will be stored into the description", placeholder="http://www...") #twf.CheckBox(id="url_up", label_text="I want to upload the file from this URL : ", #help_text="tick it if you want to download it in BioRepo") ] list_dynamic_samples = [] list_dynamic_measurements = [] if lab is None: print "----- no dynamic fields detected ---------" list_fields = [ list_static_samples, list_dynamic_samples, list_static_measurements, list_dynamic_measurements ] return list_fields else: if owner == "sample": object_edited = DBSession.query(Samples).filter( Samples.id == int(id_object)).first() list_dynamic = list_dynamic_samples tag = "samples" elif owner == "meas": object_edited = DBSession.query(Measurements).filter( Measurements.id == int(id_object)).first() list_dynamic = list_dynamic_measurements tag = "measurements" else: print "----------------- owner error : ", owner, " <----owner --------------------" raise if object_edited is not None: list_dynamic_attributes = object_edited.attributs for att in list_dynamic_attributes: if att.deprecated == False: twf_type = convert_widget(att.widget) twf_type.id = att.key list_a_values = att.values if att.widget == "textfield" or att.widget == "textarea" or att.widget == "hiding_textfield" or att.widget == "hiding_textarea": for v in list_a_values: if hasattr(v, tag): value_object = getattr(v, tag) if object_edited in value_object: twf_type.value = v.value list_dynamic.append(twf_type) elif att.widget == "checkbox" or att.widget == "hiding_checkbox": for v in list_a_values: if hasattr(v, tag): value_object = getattr(v, tag) if object_edited in value_object: #dynamic boolean are stored in varchar in the db, we have to cast them in boolean for the display value_2_display = check_boolean(v.value) twf_type.value = value_2_display list_dynamic.append(twf_type) elif att.widget == "multipleselectfield" or att.widget == "hiding_multipleselectfield": list_possible_values = [] for v in list_a_values: list_possible_values.append(v.value) twf_type.options = list_possible_values selected_values = [] for v in list_a_values: if hasattr(v, tag): value_object = getattr(v, tag) if object_edited in value_object: selected_values.append(v.value) twf_type.value = selected_values list_dynamic.append(twf_type) elif att.widget == "singleselectfield" or att.widget == "hiding_singleselectfield": list_possible_values = [] for v in list_a_values: if v.value not in list_possible_values: list_possible_values.append(v.value) twf_type.options = list_possible_values for v in list_a_values: if hasattr(v, tag): value_object = getattr(v, tag) if object_edited in value_object: twf_type.value = v.value list_dynamic.append(twf_type) else: print "Your ", owner, " was not found. ID problem. id :", id_object raise list_fields = [ list_static_samples, list_dynamic_samples, list_static_measurements, list_dynamic_measurements ] return list_fields