Esempio n. 1
0
def start_qc_analysis(rawfile, storedfile, wf_id, dbfn_id):
    analysis = Analysis(user_id=settings.QC_USER_ID,
                        name='{}_{}_{}'.format(rawfile.producer.name,
                                               rawfile.name, rawfile.date))
    analysis.save()
    jobutil.create_file_job('run_longit_qc_workflow', storedfile.id,
                            analysis.id, wf_id, dbfn_id)
Esempio n. 2
0
    def save(self, commit=True):
        instance = super().save(commit=False)
        instance.user = self.user

        if self.analysis:
            analysis = self.analysis.clone()
        else:
            # Create empty analysis for template
            user_settings = UserSettings.get_for_user(self.user)
            genome_build = user_settings.default_genome_build  # Doesn't matter for templates
            analysis = Analysis(genome_build=genome_build,
                                name=f"analysis for template {instance.name}")

        analysis.template_type = AnalysisTemplateType.TEMPLATE
        analysis.set_defaults_and_save(self.user)
        assign_permission_to_user_and_groups(self.user, analysis)
        instance.analysis = analysis
        if commit:
            instance.save()
        return instance
Esempio n. 3
0
def workbook(request, workbook_id=0):
    template = 'workbooks/workbook.html'
    command  = request.path.rsplit('/',1)[1]

    if request.method == "POST" :
        if command == "create" :
            workbook_model = Workbook.createDefault(name="Untitled Workbook", description="", user=request.user)
        elif command == "edit" :
            workbook_model = Workbook.edit(id=workbook_id, name=request.POST.get('name'), description=request.POST.get('description'))
        elif command == "copy" :
            workbook_model = Workbook.copy(id=workbook_id, user=request.user)
        elif command == "delete" :
            Workbook.destroy(id=workbook_id)

        if command == "delete":
            redirect_url = reverse('workbooks')
            return redirect(redirect_url)
        else :
            redirect_url = reverse('workbook_detail', kwargs={'workbook_id':workbook_model.id})
            return redirect(redirect_url)

    elif request.method == "GET" :
        if workbook_id:
            try :
                ownedWorkbooks  = request.user.workbook_set.all().filter(active=True)
                sharedWorkbooks = Workbook.objects.filter(shared__matched_user=request.user, shared__active=True, active=True)
                publicWorkbooks = Workbook.objects.all().filter(is_public=True,active=True)

                workbooks = ownedWorkbooks | sharedWorkbooks | publicWorkbooks
                workbooks = workbooks.distinct()

                workbook_model = workbooks.get(id=workbook_id)
                workbook_model.worksheets = workbook_model.get_deep_worksheets()

                is_shareable = workbook_model.is_shareable(request)

                shared = None
                if workbook_model.owner.id != request.user.id and not workbook_model.is_public:
                    shared = request.user.shared_resource_set.get(workbook__id=workbook_id)

                plot_types = Analysis.get_types()

                return render(request, template, {'workbook'    : workbook_model,
                                                  'datatypes'   : get_gene_datatypes(),
                                                  'is_shareable': is_shareable,
                                                  'shared'      : shared,
                                                  'plot_types'  : plot_types})
            except ObjectDoesNotExist:
                redirect_url = reverse('workbooks')
                return redirect(redirect_url)
        else :
            redirect_url = reverse('workbooks')
            return redirect(redirect_url)
Esempio n. 4
0
def create_classification_for_analysis(request, analysis_id):
    classification = create_classification_object(request)
    analysis = Analysis.get_for_user(request.user, pk=analysis_id)

    if analysis.can_write(request.user):
        AnalysisClassification.objects.create(analysis=analysis,
                                              classification=classification)

        # Remove "Requires classification" tag
        VariantTag.objects.filter(
            variant=classification.variant,
            analysis=analysis,
            tag_id=settings.TAG_REQUIRES_CLASSIFICATION).delete()
    return redirect(classification)
Esempio n. 5
0
def workbook_create_with_analysis(request):
    analysis_type   = request.POST.get('analysis')

    allowed_types = Analysis.get_types()
    redirect_url = reverse('sample_analyses')
    for type in allowed_types :
        if analysis_type == type['name'] :
            workbook_model  = Workbook.create(name="Untitled Workbook", description="this is an untitled workbook with a \"" + analysis_type + "\" plot added to the first worksheet. Click Edit Details to change your workbook title and description.", user=request.user)
            worksheet_model = Worksheet.objects.create(name="worksheet 1", description="", workbook=workbook_model)
            worksheet_model.set_plot(type=analysis_type)
            redirect_url = reverse('workbook_detail', kwargs={'workbook_id':workbook_model.id})
            break

    return redirect(redirect_url)
Esempio n. 6
0
def worksheet_display(request, workbook_id=0, worksheet_id=0):
    template = 'workbooks/workbook.html'
    workbook_model = Workbook.deep_get(workbook_id)
    workbook_model.mark_viewed(request)
    is_shareable = workbook_model.is_shareable(request)

    for worksheet in workbook_model.worksheets:
        if str(worksheet.id) == worksheet_id:
            display_worksheet = worksheet

    plot_types = Analysis.get_types()
    return render(request, template, {'workbook'            : workbook_model,
                                      'is_shareable'        : is_shareable,
                                      'datatypes'           : get_gene_datatypes(),
                                      'display_worksheet'   : display_worksheet,
                                      'plot_types'          : plot_types})
Esempio n. 7
0
def worksheet_display(request, workbook_id=0, worksheet_id=0):
    template = 'workbooks/workbook.html'
    workbook_model = Workbook.deep_get(workbook_id)
    workbook_model.mark_viewed(request)
    is_shareable = workbook_model.is_shareable(request)

    for worksheet in workbook_model.worksheets:
        if str(worksheet.id) == worksheet_id:
            display_worksheet = worksheet

    plot_types = Analysis.get_types()
    return render(
        request, template, {
            'workbook': workbook_model,
            'is_shareable': is_shareable,
            'datatypes': get_gene_datatypes(workbook_model.build),
            'display_worksheet': display_worksheet,
            'plot_types': plot_types
        })
Esempio n. 8
0
def workbook_create_with_analysis(request):
    analysis_type = request.POST.get('analysis')

    allowed_types = Analysis.get_types()
    redirect_url = reverse('sample_analyses')
    for type in allowed_types:
        if analysis_type == type['name']:
            workbook_model = Workbook.create(
                name="Untitled Workbook",
                description="this is an untitled workbook with a \"" +
                analysis_type +
                "\" plot added to the first worksheet. Click Edit Details to change your workbook title and description.",
                user=request.user)
            worksheet_model = Worksheet.objects.create(name="worksheet 1",
                                                       description="",
                                                       workbook=workbook_model)
            worksheet_model.set_plot(type=analysis_type)
            redirect_url = reverse('workbook_detail',
                                   kwargs={'workbook_id': workbook_model.id})
            break

    return redirect(redirect_url)
Esempio n. 9
0
    def __init__(self, **kwargs):
        user = kwargs.get("user")
        super().__init__(user)
        fields = self.get_field_names()

        self.genome_builds = list(GenomeBuild.builds_with_annotation())
        if len(self.genome_builds) == 1:  # No need to show
            genome_build_colmodel = self._overrides.get('genome_build', {})
            genome_build_colmodel['hidden'] = True
            self._overrides['genome_build'] = genome_build_colmodel
        user_grid_config = UserGridConfig.get(user, self.caption)
        if user_grid_config.show_group_data:
            qs = Analysis.filter_for_user(user)
        else:
            qs = Analysis.objects.filter(user=user)
        qs = qs.filter(genome_build__in=self.genome_builds)
        qs = qs.filter(visible=True,
                       template_type__isnull=True)  # Hide templates
        q_last_lock = Q(analysislock=F("last_lock")) | Q(
            analysislock__isnull=True)
        qs = qs.annotate(last_lock=Max("analysislock__pk")).filter(q_last_lock)
        self.queryset = qs.values(*fields)
        self.extra_config.update({'sortname': 'modified', 'sortorder': 'desc'})
Esempio n. 10
0
    def setUpClass(cls):
        super().setUpClass()

        user = User.objects.get_or_create(username='******')[0]
        cls.grch37 = GenomeBuild.get_name_or_alias("GRCh37")
        cls.annotation_version_grch37 = get_fake_annotation_version(cls.grch37)
        gene_annotation_release = cls.annotation_version_grch37.gene_annotation_version.gene_annotation_release
        cls.transcript_version = create_fake_transcript_version(cls.grch37,
                                                                release=gene_annotation_release)
        cls.gene_symbol = cls.transcript_version.gene_version.gene_symbol
        cls.trio = create_fake_trio(user, cls.grch37)

        cls.analysis = Analysis(genome_build=cls.grch37)
        cls.analysis.set_defaults_and_save(user)

        cls.sample = cls.trio.get_samples()[0]

        # Gene List
        cls.gene_list = GeneList.objects.get_or_create(name="fake list",
                                                       user=cls.analysis.user,
                                                       import_status=ImportStatus.SUCCESS)[0]
        GeneListGeneSymbol.objects.get_or_create(gene_list=cls.gene_list, gene_symbol=cls.gene_symbol)

        # Need some overlapping variants so gene list will work
        create_fake_variants(cls.grch37)
        # Note: Variant probably doesn't overlap with gene, just want a random one
        variant = Variant.objects.filter(Variant.get_no_reference_q()).first()
        annotation_run = AnnotationRun.objects.create()
        VariantGeneOverlap.objects.create(version=cls.annotation_version_grch37.variant_annotation_version,
                                          annotation_run=annotation_run,
                                          gene=cls.transcript_version.gene,
                                          variant=variant)

        # Tag that variant
        cls.tag = Tag.objects.get_or_create(pk="foo")[0]
        VariantTag.objects.create(genome_build=cls.grch37, analysis=cls.analysis,
                                  variant=variant, tag=cls.tag, user=user)
Esempio n. 11
0
 def get_user_queryset(self, user):
     qs = Analysis.filter_for_user(user)
     if template_type := self.forwarded.get('template_type', None):
         qs = qs.filter(template_type=template_type)
Esempio n. 12
0
def start_qc_analysis(rawfile, storedfile, wf_id, dbfn_id):
    analysis = Analysis(user_id=settings.QC_USER_ID,
                        name='{}_{}_{}'.format(rawfile.producer.name, rawfile.name, rawfile.date))
    analysis.save()
    jobutil.create_file_job('run_longit_qc_workflow', storedfile.id,
                            analysis.id, wf_id, dbfn_id)
Esempio n. 13
0
def workbook(request, workbook_id=0):
    template = 'workbooks/workbook.html'
    command  = request.path.rsplit('/',1)[1]

    if request.method == "POST":
        if command == "create":
            workbook_model = Workbook.createDefault(name="Untitled Workbook", description="", user=request.user)
        elif command == "edit":
            workbook_name = request.POST.get('name')
            workbook_desc = request.POST.get('description')
            whitelist = re.compile(WHITELIST_RE, re.UNICODE)
            match_name = whitelist.search(unicode(workbook_name))
            match_desc = whitelist.search(unicode(workbook_desc))

            if match_name or match_desc:
                # XSS risk, log and fail this cohort save
                matches = ""
                fields = ""
                if match_name:
                    match_name = whitelist.findall(unicode(workbook_name))
                    logger.error('[ERROR] While saving a workbook, saw a malformed name: ' + workbook_name + ', characters: ' + match_name.__str__())
                    matches = "name contains"
                    fields = "name"
                if match_desc:
                    match_desc = whitelist.findall(unicode(workbook_desc))
                    logger.error('[ERROR] While saving a workbook, saw a malformed description: ' + workbook_desc + ', characters: ' + match_desc.__str__())
                    matches = "name and description contain" if match_name else "description contains"
                    fields += (" and description" if match_name else "description")

                err_msg = "Your workbook's %s invalid characters; please choose another %s." % (matches, fields,)
                messages.error(request, err_msg)
                redirect_url = reverse('workbook_detail', kwargs={'workbook_id':workbook_id})
                return redirect(redirect_url)

            workbook_model = Workbook.edit(id=workbook_id, name=workbook_name, description=workbook_desc)
        elif command == "copy":
            workbook_model = Workbook.copy(id=workbook_id, user=request.user)
        elif command == "delete":
            Workbook.destroy(id=workbook_id)

        if command == "delete":
            redirect_url = reverse('workbooks')
        else:
            redirect_url = reverse('workbook_detail', kwargs={'workbook_id':workbook_model.id})

        return redirect(redirect_url)

    elif request.method == "GET" :
        if workbook_id:
            try :
                ownedWorkbooks  = request.user.workbook_set.all().filter(active=True)
                sharedWorkbooks = Workbook.objects.filter(shared__matched_user=request.user, shared__active=True, active=True)
                publicWorkbooks = Workbook.objects.all().filter(is_public=True,active=True)

                workbooks = ownedWorkbooks | sharedWorkbooks | publicWorkbooks
                workbooks = workbooks.distinct()

                workbook_model = workbooks.get(id=workbook_id)
                workbook_model.worksheets = workbook_model.get_deep_worksheets()

                is_shareable = workbook_model.is_shareable(request)

                shared = None
                if workbook_model.owner.id != request.user.id and not workbook_model.is_public:
                    shared = request.user.shared_resource_set.get(workbook__id=workbook_id)

                plot_types = Analysis.get_types()

                return render(request, template, {'workbook'    : workbook_model,
                                                  'datatypes'   : get_gene_datatypes(),
                                                  'is_shareable': is_shareable,
                                                  'shared'      : shared,
                                                  'plot_types'  : plot_types})
            except ObjectDoesNotExist:
                redirect_url = reverse('workbooks')
                return redirect(redirect_url)
        else :
            redirect_url = reverse('workbooks')
            return redirect(redirect_url)
Esempio n. 14
0
def worksheet_plots(request, workbook_id=0, worksheet_id=0, plot_id=0):
    command = request.path.rsplit('/', 1)[1]
    json_response = False
    default_name = "Untitled Workbook"
    result = {}
    try:
        workbook_model = Workbook.objects.get(
            id=workbook_id) if workbook_id else None

        if request.method == "POST":
            workbook_model.save()
            if command == "delete":
                var = Worksheet_plot.objects.get(id=plot_id).delete()
                result['message'] = "This plot has been deleted from workbook."
            else:
                if "attrs" in request.body:
                    json_response = True
                    attrs = json.loads(request.body)['attrs']
                    settings = json.loads(request.body)['settings']
                    if plot_id:
                        plot_model = Worksheet_plot.objects.get(id=plot_id)
                        plot_model.settings_json = settings
                        if attrs['cohorts']:
                            try:
                                Worksheet_plot_cohort.objects.filter(
                                    plot=plot_model).delete()
                                for obj in attrs['cohorts']:
                                    wpc = Worksheet_plot_cohort(
                                        plot=plot_model, cohort_id=obj['id'])
                                    wpc.save()
                            except ObjectDoesNotExist:
                                None

                        plot_model.save()
                        result['updated'] = "success"

        elif request.method == "GET":
            json_response = True
            plot_type = escape(request.GET.get('type', 'default'))

            if plot_type not in Analysis.get_types_list():
                raise Exception(
                    "Plot type {} was not found in the allowed list of plot types!"
                    .format(plot_type))

            worksheet_model = Worksheet.objects.get(id=worksheet_id)
            plots = worksheet_model.worksheet_plot_set.all()
            for p in plots:
                p.active = False
                p.save()

            plots = worksheet_model.worksheet_plot_set.filter(type=plot_type)
            if len(plots) == 0:
                model = Worksheet_plot(type=plot_type,
                                       worksheet=worksheet_model)
                model.save()
            else:
                model = plots[0]
                model.active = True
                model.save()

            result['data'] = model.toJSON()
        else:
            result['error'] = "method not correct"

        if json_response:
            return HttpResponse(json.dumps(result), status=200)
        else:
            redirect_url = reverse('worksheet_display',
                                   kwargs={
                                       'workbook_id': workbook_model.id,
                                       'worksheet_id': worksheet_model.id
                                   })
            return redirect(redirect_url)

    except Exception as e:
        logger.error("[ERROR] While accessing workbooks/worksheets:")
        logger.exception(e)
        messages.error(
            request,
            "An error occurred while trying to process this workbook request.")
        return redirect('workbooks')
Esempio n. 15
0
def workbook(request, workbook_id=0):
    template = 'workbooks/workbook.html'
    command = request.path.rsplit('/', 1)[1]
    workbook_model = None

    try:

        if request.method == "POST":
            if command == "create":
                workbook_model = Workbook.createDefault(
                    name="Untitled Workbook",
                    description="",
                    user=request.user)
            elif command == "edit":
                # Truncate incoming name and desc fields in case someone tried to send ones which were too long
                workbook_name = request.POST.get('name')[0:2000]
                workbook_desc = request.POST.get('description')[0:2000]
                workbook_build = request.POST.get('build')

                blacklist = re.compile(BLACKLIST_RE, re.UNICODE)
                match_name = blacklist.search(unicode(workbook_name))
                match_desc = blacklist.search(unicode(workbook_desc))

                if match_name or match_desc:
                    # XSS risk, log and fail this cohort save
                    matches = ""
                    fields = ""
                    if match_name:
                        match_name = blacklist.findall(unicode(workbook_name))
                        logger.error(
                            '[ERROR] While saving a workbook, saw a malformed name: '
                            + workbook_name + ', characters: ' +
                            str(match_name))
                        matches = "name contains"
                        fields = "name"
                    if match_desc:
                        match_desc = blacklist.findall(unicode(workbook_desc))
                        logger.error(
                            '[ERROR] While saving a workbook, saw a malformed description: '
                            + workbook_desc + ', characters: ' +
                            str(match_desc))
                        matches = "name and description contain" if match_name else "description contains"
                        fields += (" and description"
                                   if match_name else "description")

                    err_msg = "Your workbook's %s invalid characters; please choose another %s." % (
                        matches,
                        fields,
                    )
                    messages.error(request, err_msg)
                    redirect_url = reverse('workbook_detail',
                                           kwargs={'workbook_id': workbook_id})
                    return redirect(redirect_url)

                workbook_model = Workbook.edit(id=workbook_id,
                                               name=workbook_name,
                                               description=workbook_desc,
                                               build=workbook_build)
            elif command == "copy":
                workbook_model = Workbook.copy(id=workbook_id,
                                               user=request.user)
            elif command == "delete":
                Workbook.destroy(id=workbook_id)

            if command == "delete":
                redirect_url = reverse('workbooks')
            else:
                redirect_url = reverse(
                    'workbook_detail',
                    kwargs={'workbook_id': workbook_model.id})

            return redirect(redirect_url)

        elif request.method == "GET":
            if workbook_id:
                try:
                    ownedWorkbooks = request.user.workbook_set.filter(
                        active=True)
                    sharedWorkbooks = Workbook.objects.filter(
                        shared__matched_user=request.user,
                        shared__active=True,
                        active=True)
                    publicWorkbooks = Workbook.objects.filter(is_public=True,
                                                              active=True)

                    workbooks = ownedWorkbooks | sharedWorkbooks | publicWorkbooks
                    workbooks = workbooks.distinct()

                    workbook_model = workbooks.get(id=workbook_id)
                    workbook_model.worksheets = workbook_model.get_deep_worksheets(
                    )

                    is_shareable = workbook_model.is_shareable(request)

                    shared = None
                    if workbook_model.owner.id != request.user.id and not workbook_model.is_public:
                        shared = request.user.shared_resource_set.get(
                            workbook__id=workbook_id)

                    plot_types = Analysis.get_types()

                    return render(
                        request, template, {
                            'workbook': workbook_model,
                            'datatypes': get_gene_datatypes(
                                workbook_model.build),
                            'is_shareable': is_shareable,
                            'shared': shared,
                            'plot_types': plot_types
                        })
                except ObjectDoesNotExist:
                    redirect_url = reverse('workbooks')
                    return redirect(redirect_url)
            else:
                redirect_url = reverse('workbooks')
                return redirect(redirect_url)

    except Exception as e:
        logger.error("[ERROR] Exception when viewing a workbook: ")
        logger.exception(e)
        messages.error(
            request,
            "An error was encountered while trying to view this workbook.")
    finally:
        redirect_url = reverse('workbooks')

    return redirect(redirect_url)