Example #1
0
def variable_fav_save(request, variable_fav_id=0):
    data   = json.loads(request.body)
    result = {}

    name = data['name']
    whitelist = re.compile(WHITELIST_RE, re.UNICODE)
    match = whitelist.search(unicode(name))
    if match:
        # XSS risk, log and fail this cohort save
        match = whitelist.findall(unicode(name))
        logger.error(
            '[ERROR] While saving a variable list, saw a malformed name: ' + name + ', characters: ' + match.__str__())
        messages.error(request, "Your variable list's name contains invalid characters; please choose another name.")
        result['error'] = "Your variable list's name contains invalid characters; please choose another name."
        return HttpResponse(json.dumps(result), status=200)

    if variable_fav_id:
        try:
            variable_model = VariableFavorite.objects.get(id=variable_fav_id)
            if variable_model.user == request.user :
                variable_model.update(name = data['name'], variables = data['variables'])
                result['model'] = { 'id' : variable_model.id, 'name' : variable_model.name }
            else :
                result['error'] = 'You do not have permission to update this gene favorite list'
                messages.error(request, 'You do not have permission to update this gene favorite list')
        except ObjectDoesNotExist:
            messages.error(request, 'The gene list you want does not exist.')
            result['error'] = 'You do not have permission to update this gene favorite list'
    else:
        variable_model = VariableFavorite.create(name        = data['name'],
                                                 variables   = data['variables'],
                                                 user        = request.user)
        result['model'] = { 'id' : variable_model['id'], 'name' : variable_model['name'] }

    return HttpResponse(json.dumps(result), status=200)
Example #2
0
def variable_fav_list(request, workbook_id=0, worksheet_id=0, new_workbook=0):
    template = 'variables/variable_list.html'
    context  = {}

    variable_list = VariableFavorite.get_list(request.user)
    if len(variable_list) == 0:
        variable_list = None
    context['variable_list']=variable_list

    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
            context['base_url']  = settings.BASE_URL

            if variable_list:
                template = 'variables/variables_select.html'
            else:
                return initialize_variable_selection_page(request, workbook_id=workbook_id, worksheet_id=worksheet_id)

        except ObjectDoesNotExist:
            messages.error(request, 'The workbook and worksheet you were referencing does not exist.')
            return redirect('variables')
    elif new_workbook :
        context['new_workbook'] = True
        if variable_list :
            template = 'variables/variables_select.html'
        else:
            return initialize_variable_selection_page(request, new_workbook=True)

    return render(request, template, context)
Example #3
0
def variable_fav_list(request, workbook_id=0, worksheet_id=0, new_workbook=0):
    template = 'variables/variable_list.html'
    context  = {}

    variable_list = VariableFavorite.get_list(request.user)
    if len(variable_list) == 0 :
        variable_list = None
    context['variable_list']=variable_list

    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
            context['base_url']  = settings.BASE_URL

            if variable_list :
                template = 'variables/variables_select.html'
            else :
                return initialize_variable_selection_page(request, workbook_id=workbook_id, worksheet_id=worksheet_id)

        except ObjectDoesNotExist:
            messages.error(request, 'The workbook and worksheet you were referencing does not exist.')
            return redirect('variables')
    elif new_workbook :
        context['new_workbook'] = True
        if variable_list :
            template = 'variables/variables_select.html'
        else :
            return initialize_variable_selection_page(request, new_workbook=True)

    return render(request, template, context)
Example #4
0
def variable_fav_save(request, variable_fav_id=0):
    data   = json.loads(request.body)
    result = {}

    name = data['name']
    whitelist = re.compile(WHITELIST_RE, re.UNICODE)
    match = whitelist.search(unicode(name))
    if match:
        # XSS risk, log and fail this cohort save
        match = whitelist.findall(unicode(name))
        logger.error(
            '[ERROR] While saving a variable list, saw a malformed name: ' + name + ', characters: ' + match.__str__())
        messages.error(request, "Your variable list's name contains invalid characters; please choose another name.")
        result['error'] = "Your variable list's name contains invalid characters; please choose another name."
        return HttpResponse(json.dumps(result), status=200)

    if variable_fav_id:
        try:
            variable_model = VariableFavorite.objects.get(id=variable_fav_id)
            if variable_model.user == request.user :
                variable_model.update(name = data['name'], variables = data['variables'])
                result['model'] = { 'id' : variable_model.id, 'name' : variable_model.name }
            else :
                result['error'] = 'You do not have permission to update this gene favorite list'
                messages.error(request, 'You do not have permission to update this gene favorite list')
        except ObjectDoesNotExist:
            messages.error(request, 'The gene list you want does not exist.')
            result['error'] = 'You do not have permission to update this gene favorite list'
    else :
        variable_model = VariableFavorite.create(name        = data['name'],
                                                 variables   = data['variables'],
                                                 user        = request.user)
        result['model'] = { 'id' : variable_model['id'], 'name' : variable_model['name'] }

    return HttpResponse(json.dumps(result), status=200)
Example #5
0
def variable_fav_detail(request,
                        variable_fav_id,
                        workbook_id=0,
                        worksheet_id=0,
                        new_workbook=0):
    template = 'variables/variable_detail.html'
    context = {}
    if new_workbook:
        context['new_workbook'] = True

    if workbook_id:
        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')
    try:
        variable_fav = VariableFavorite.get_deep(id=variable_fav_id,
                                                 user=request.user)
        context['variables'] = variable_fav
        variable_fav.mark_viewed(request)
    except ObjectDoesNotExist:
        messages.error(
            request,
            'The variable favorite you were looking for does not exist.')
        return redirect('variables')

    return render(request, template, context)
Example #6
0
def variable_fav_save(request, variable_fav_id=0):
    data   = json.loads(request.body)
    result = {}
    if variable_fav_id :
        try:
            variable_model = VariableFavorite.objects.get(id=variable_fav_id)
            if variable_model.user == request.user :
                variable_model.update(name = data['name'], variables = data['variables'])
                result['model'] = { 'id' : variable_model.id, 'name' : variable_model.name }
            else :
                result['error'] = 'You do not have permission to update this gene favorite list'
                messages.error(request, 'You do not have permission to update this gene favorite list')
        except ObjectDoesNotExist:
            messages.error(request, 'The gene list you want does not exist.')
            result['error'] = 'You do not have permission to update this gene favorite list'
    else :
        variable_model = VariableFavorite.create(name        = data['name'],
                                                 variables   = data['variables'],
                                                 user        = request.user)
        result['model'] = { 'id' : variable_model['id'], 'name' : variable_model['name'] }

    return HttpResponse(json.dumps(result), status=200)
Example #7
0
def variable_fav_detail(request, variable_fav_id, workbook_id=0, worksheet_id=0, new_workbook=0):
    template = 'variables/variable_detail.html'
    context  = {}
    if new_workbook :
        context['new_workbook'] = True

    if workbook_id :
        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')
    try:
        variable_fav = VariableFavorite.get_deep(variable_fav_id)
        context['variables'] = variable_fav
        variable_fav.mark_viewed(request)
    except ObjectDoesNotExist:
        messages.error(request, 'The variable favorite you were looking for does not exist.')
        return redirect('variables')

    return render(request, template, context)
Example #8
0
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)
            if existing_variable_list.version != 'v2':
                messages.warning(request, 'Version 1 Variable lists cannot be edited due to changes in available variables.')
                return redirect('variables')
        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]


    # Public programs
    isb_user = Django_User.objects.filter(username='******').first()
    public_programs = Program.objects.filter(active=True, is_public=True, owner=isb_user)

    # User programs
    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
    user_programs = programs.distinct()

    # User favorites
    favorite_list = VariableFavorite.get_list(user=request.user, version='v2')
    for fav in favorite_list:
        fav.variables = fav.get_variables()

    full_fave_count =  len(VariableFavorite.get_list(user=request.user))

    program_attrs = {}

    for prog in public_programs:
        program_attrs[prog.id] = fetch_program_attr(prog.id)
        attr_codes = ClinicalColumnFeatureSupport.get_features_ids_for_column_names(program_attrs[prog.id].keys())
        if 'not_found_columns' in attr_codes:
            new_keys = [x for x in program_attrs[prog.id].keys() if x not in attr_codes['not_found_columns']]
            attr_codes = ClinicalColumnFeatureSupport.get_features_ids_for_column_names(new_keys)
        for attr in program_attrs[prog.id]:
            if attr in attr_codes['clinical_feature_ids']:
                program_attrs[prog.id][attr]['data_code'] = attr_codes['clinical_feature_ids'][attr]
            else:
                program_attrs[prog.id][attr]['data_code'] = 'v2:CLIN:'+attr

    # users can select from their saved variable favorites
    variable_favorites = VariableFavorite.get_list(request.user)

    context = {
        'favorite_list'         : favorite_list,
        'full_favorite_list_count': full_fave_count,
        'datatype_list'         : datatype_list,
        'data_attr'             : data_attr,
        'public_programs'       : public_programs,
        'user_programs'         : programs,
        'base_url'                  : settings.BASE_URL,
        'base_api_url'              : settings.BASE_API_URL,
        'variable_favorites'        : variable_favorites,
        'workbook'                  : workbook_model,
        'worksheet'                 : worksheet_model,
        'existing_variable_list'    : existing_variable_list,
        'new_workbook'              : new_workbook,
        'program_attrs'         : program_attrs
    }

    return render(request, template, context)
Example #9
0
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)