def add_plot(request): # TODO: Double check that user owns viz cohort = Cohort.objects.get_all_tcga_cohort() # Fetch cohorts list used for autocomplete listing cohort_perms = Cohort_Perms.objects.filter(user=request.user).values_list('cohort', flat=True) cohort_listing = Cohort.objects.filter(id__in=cohort_perms, active=True).values('id', 'name') for cohort_item in cohort_listing: cohort_item['value'] = int(cohort_item['id']) cohort_item['label'] = cohort_item['name'].encode('utf8') del cohort_item['id'] del cohort_item['name'] viz_id = request.GET.get('viz_id', None) # Filter Options datatypes = [ {'id': 'CLIN', 'label': 'Clinical'}, {'id': 'GEXP', 'label': 'Gene Expression'}, {'id': 'MIRN', 'label': 'miRNA'}, {'id': 'METH', 'label': 'Methylation'}, {'id': 'CNVR', 'label': 'Copy Number'}, {'id': 'RPPA', 'label': 'Protein'}, {'id': 'GNAB', 'label': 'Mutation'} ] new_datatypes = SearchableFieldHelper.get_fields_for_all_datatypes() if viz_id: viz = SavedViz.objects.get(id=int(viz_id)) plot = Plot.objects.create(visualization=viz, title='', x_axis='CLIN:age_at_initial_pathologic_diagnosis', y_axis='', color_by='CLIN:Study') plot.save() plot_cohort = Plot_Cohorts.objects.create(plot=plot, cohort=cohort) plot_cohort.save() return_obj = {'plot': { 'id': int(plot.id), 'title': plot.title.encode('utf-8'), 'cohorts': [{ 'id': int(cohort.id), 'name': cohort.name.encode('utf-8') }], 'x_attr': str(plot.x_axis), 'y_attr': str(plot.y_axis), 'color_by': str(plot.color_by), 'cohort_name': str(cohort.name), 'patient_length': len(cohort.patients_set.all()), 'sample_length': len(cohort.samples_set.all()), 'comments': [], 'viz_perm': 'OWNER' }, 'cohorts': cohort_listing, 'data_types': datatypes, 'new_datatypes': new_datatypes} return render(request, 'visualizations/plot.html', return_obj, status=200) return HttpResponse(status=500)
def get_gene_datatypes(): datatype_labels = {'GEXP' : 'Gene Expression', 'METH' : 'Methylation', 'CNVR' : 'Copy Number', 'RPPA' : 'Protein', 'GNAB' : 'Mutation'} datatype_list = SearchableFieldHelper.get_fields_for_all_datatypes() if debug: print >> sys.stderr, ' attrs ' + json.dumps(datatype_list) return_list = [] for type in datatype_list: if type['datatype'] != 'CLIN' and type['datatype'] != 'MIRN' : type['label'] = datatype_labels[type['datatype']] return_list.append(type) #remove gene in fields as they are set with the variable selection for index, field in enumerate(type['fields']): if field['label'] == "Gene": del type['fields'][index] return return_list
def initialize_variable_selection_page(request, variable_list_id=0, workbook_id=0, worksheet_id=0, new_workbook=False): template = 'variables/variable_edit.html' context = {'variables' : [] } workbook_model = None worksheet_model = None existing_variable_list = None if workbook_id != 0 : try: workbook_model = Workbook.objects.get(id=workbook_id) context['workbook'] = workbook_model worksheet_model = Worksheet.objects.get(id=worksheet_id) context['worksheet'] = worksheet_model except ObjectDoesNotExist: messages.error(request, 'The workbook you were referencing does not exist.') return redirect('variables') if variable_list_id != 0: try: existing_variable_list = request.user.variablefavorite_set.get(id=variable_list_id) except ObjectDoesNotExist: messages.error(request, 'The variable favorite you were looking for does not exist.') return redirect('variables') data_attr = [ 'DNA_sequencing', 'RNA_sequencing', 'miRNA_sequencing', 'Protein', 'SNP_CN', 'DNA_methylation' ] # This is a list of specific data classifications which require additional filtering in order to # Gather categorical or numercial variables for use in the plot # Filter Options datatype_labels = {'CLIN' : 'Clinical', 'GEXP' : 'Gene Expression', 'MIRN' : 'miRNA', 'METH' : 'Methylation', 'CNVR' : 'Copy Number', 'RPPA' : 'Protein', 'GNAB' : 'Mutation'} datatype_list = SearchableFieldHelper.get_fields_for_all_datatypes() for type in datatype_list: type['label'] = datatype_labels[type['datatype']] #remove gene in fields for index, field in enumerate(type['fields']): if field['label'] == "Gene": del type['fields'][index] #get user programs and variables ownedPrograms = request.user.program_set.all().filter(active=True) sharedPrograms = Program.objects.filter(shared__matched_user=request.user, shared__active=True, active=True) programs = ownedPrograms | sharedPrograms programs = programs.distinct() #get user favorites favorite_list = VariableFavorite.get_list(user=request.user) for fav in favorite_list : fav.variables = fav.get_variables() #TODO common variables need to be refactored into an adaptive list based on common used displayed_common_variables = [ {'name' : "vital_status", 'code' : 'CLIN:vital_status', 'type' : 'C'}, {'name' : "gender", 'code' : 'CLIN:gender', 'type' : 'C'}, {'name' : "age_at_initial_pathologic_diagnosis", 'code' : 'CLIN:age_at_initial_pathologic_diagnosis', 'type' : 'N'}, {'name' : "tumor_tissue_site", 'code' : 'CLIN:tumor_tissue_site', 'type' : 'C'}, {'name' : "histological_type", 'code' : 'CLIN:histological_type', 'type' : 'C'}, {'name' : "other_diagnosis", 'code' : 'CLIN:other_dx', 'type' : 'C'}, {'name' : "tumor_status", 'code' : 'CLIN:person_neoplasm_cancer_status', 'type' : 'C'}, {'name' : "new_tumor_event_after_initial_treatment", 'code' : 'CLIN:new_tumor_event_after_initial_treatment', 'type' : 'C'}, {'name' : "histological_grade", 'code' : 'CLIN:neoplasm_histologic_grade', 'type' : 'C'}, {'name' : "residual_tumor", 'code' : 'CLIN:residual_tumor', 'type' : 'C'}, {'name' : "tobacco_smoking_history", 'code' : 'CLIN:tobacco_smoking_history', 'type' : 'C'}, {'name' : "icd-10", 'code' : 'CLIN:icd_10', 'type' : 'C'}, {'name' : "icd-o-3_site", 'code' : 'CLIN:icd_o_3_site', 'type' : 'C'}, {'name' : "icd-o-3_histology", 'code' : 'CLIN:icd_o_3_histology', 'type' : 'C'} ] common_variables = displayed_common_variables TCGA_program = {"id" : -1, "study" : {"id" :-1, "name" : ""}, "name" : "TCGA"} common_program = {"id" : -1, "study" : {"id" :-1, "name" : ""}, "name" : "Common", "variables" : common_variables} # users can select from their saved variable favorites variable_favorites = VariableFavorite.get_list(request.user) context = { 'favorite_list' : favorite_list, 'datatype_list' : datatype_list, 'programs' : programs, 'data_attr' : data_attr, 'base_url' : settings.BASE_URL, 'base_api_url' : settings.BASE_API_URL, 'TCGA_program' : TCGA_program, 'common_program' : common_program, 'variable_favorites' : variable_favorites, 'workbook' : workbook_model, 'worksheet' : worksheet_model, 'existing_variable_list' : existing_variable_list, 'new_workbook' : new_workbook } return render(request, template, context)
def genericplot(request, id=0): searches = {} viz = {} plots_data = [] plot_render_data = [] viz_perm = None users = User.objects.filter(is_superuser=0) # Filter Options datatypes = [ {'id': 'CLIN', 'label': 'Clinical'}, {'id': 'GEXP', 'label': 'Gene Expression'}, {'id': 'MIRN', 'label': 'miRNA'}, {'id': 'METH', 'label': 'Methylation'}, {'id': 'CNVR', 'label': 'Copy Number'}, {'id': 'RPPA', 'label': 'Protein'}, {'id': 'GNAB', 'label': 'Mutation'} ] new_datatypes = SearchableFieldHelper.get_fields_for_all_datatypes() # Fetch cohorts list used for autocomplete listing cohort_perms = Cohort_Perms.objects.filter(user=request.user).values_list('cohort', flat=True) cohort_listing = Cohort.objects.filter(id__in=cohort_perms, active=True).values('id', 'name') for cohort in cohort_listing: cohort['value'] = int(cohort['id']) cohort['label'] = cohort['name'].encode('utf8') del cohort['id'] del cohort['name'] if id != 0: viz = SavedViz.objects.get(id=id) plots = Plot.objects.filter(visualization=id) if viz: viz_perm = viz.get_perm(request) for plot in plots: sample_set, patient_set = union_cohort_samples_patients(plot.plot_cohorts_set.all().values_list('cohort', flat=True)) cohorts = Cohort.objects.filter(id__in=plot.plot_cohorts_set.all().values_list('cohort', flat=True)) # for c in plot.plot_cohorts_set.all(): # cohorts.append(Cohort.objects.filter(id__in=) cohort_list = [] for cohort in cohorts: cohort_list.append({ 'id': int(cohort.id), 'name': cohort.name.encode('utf-8') }) item = { 'plot_id': int(plot.id), 'title': plot.title.encode('utf-8'), 'x_attr': str(plot.x_axis), 'y_attr': str(plot.y_axis), 'color_by': str(plot.color_by) if plot.color_by else 'CLIN:Study', 'cohorts': cohort_list, # 'cohort_name': str(cohort.name), 'patient_length': len(patient_set), 'sample_length': len(sample_set), # 'notes': str(plot.notes) } plots_data.append(item) plot_render_data.append({ 'id': int(plot.id), 'title': plot.title.encode('utf-8'), 'x_attr': str(plot.x_axis), 'y_attr': str(plot.y_axis), 'color_by': str(plot.color_by) if plot.color_by else 'CLIN:Study', 'cohorts': cohort_list, # 'cohort_name': str(cohort.name), 'patient_length': len(patient_set), 'sample_length': len(sample_set), 'comments': plot.plot_comment.all(), 'viz_perm': viz_perm.perm }) else: title = request.POST.get('vis_title', 'Unititled Visualization') cohort_id = request.POST.get('cohort_id', None) if not cohort_id: cohort = Cohort.objects.get_all_tcga_cohort() else: cohort = Cohort.objects.get(id=cohort_id) viz = SavedViz.objects.create(name=title) viz.save() perm = Viz_Perms.objects.create(visualization=viz, user=request.user, perm=Viz_Perms.OWNER) perm.save() viz_perm = perm plot = Plot.objects.create(visualization=viz, title='', x_axis='CLIN:age_at_initial_pathologic_diagnosis', y_axis='', color_by='CLIN:Study') plot.save() plot_cohort = Plot_Cohorts.objects.create(plot=plot, cohort=cohort) plot_cohort.save() item = { 'plot_id': int(plot.id), 'title': plot.title.encode('utf-8'), 'x_attr': str(plot.x_axis), 'y_attr': str(plot.y_axis), 'color_by': str(plot.color_by), 'cohorts': [{ 'id': int(cohort.id), 'name': cohort.name.encode('utf-8') }], 'cohort_name': str(cohort.name), 'patient_length': len(cohort.patients_set.all()), 'sample_length': len(cohort.samples_set.all()), } plots_data.append(item) plot_render_data.append({ 'id': int(plot.id), 'title': plot.title.encode('utf-8'), 'cohorts': [{ 'id': int(cohort.id), 'name': cohort.name.encode('utf-8') }], 'cohort_name': str(cohort.name), 'patient_length': len(cohort.patients_set.all()), 'sample_length': len(cohort.samples_set.all()), 'comments': [], 'viz_perm': perm.perm, 'x_attr': str(plot.x_axis), 'y_attr': str(plot.y_axis), 'color_by': str(plot.color_by), }) return render(request, 'visualizations/genericplot.html', {'request': request, 'searches': searches, 'viz': viz, 'viz_perm': viz_perm.perm, 'plots_data': plots_data, 'plot_render_data': plot_render_data, 'users': users, 'friendly_name_map': fm_friendly_name_map, 'categorical_attributes': fm_categorical_attributes, 'numerical_attributes': fm_numerical_attributes, 'cohorts': cohort_listing, 'base_url': settings.BASE_URL, 'base_api_url': settings.BASE_API_URL, 'data_types': datatypes, 'new_datatypes': new_datatypes})