コード例 #1
0
ファイル: plots.py プロジェクト: alubbock/thunor-web
def _get_viability_scores(datasets, drug_id, cell_line_id, viability_time):
    try:
        df_data = df_doses_assays_controls(dataset=datasets,
                                           drug_id=drug_id,
                                           cell_line_id=cell_line_id,
                                           assay=None,
                                           use_dataset_names=True)
    except NoDataException:
        return HttpResponse(
            'No viability data found for this request. '
            'This drug/cell line/time point combination '
            'may not exist.',
            status=400)
    try:
        expt_resp_data, ctrl_resp_data = viability(df_data,
                                                   time_hrs=viability_time,
                                                   include_controls=True)
    except NotImplementedError as e:
        return HttpResponse(e, status=400)
    if expt_resp_data['viability'].isnull().values.all():
        return HttpResponse(
            'No viability for this time point. The '
            'nearest time point to the time entered '
            'is '
            'used, but there must be control well '
            'measurements from the same time.',
            status=400)

    return expt_resp_data, ctrl_resp_data
コード例 #2
0
def _generate_dataset_hdf5(dataset, regenerate_cache=False):
    file_name = 'dataset_{}.h5'.format(dataset.id)
    file_type = 'dataset_hdf5'
    file_type_protocol = 1

    mod_date = timezone.now()
    file = _cached_file(dataset, file_type, file_type_protocol)

    if file and not regenerate_cache:
        full_path = file.file.name
    else:
        df_data = df_doses_assays_controls(dataset=dataset,
                                           drug_id=None,
                                           cell_line_id=None,
                                           assay=None,
                                           for_export=True)

        full_path = os.path.join(settings.DOWNLOADS_ROOT, file_name)
        write_hdf(df_data, full_path)
        df, created = HTSDatasetFile.objects.get_or_create(
            dataset=dataset,
            file_type=file_type,
            defaults={
                'file_type_protocol': file_type_protocol,
                'file': full_path
            })
        if not created:
            df.file_type_protocol = file_type_protocol
            df.file = full_path
            df.creation_date = mod_date
            df.save()

    return full_path
コード例 #3
0
ファイル: plots.py プロジェクト: alubbock/thunor-web
def ajax_get_plot(request, file_type='json'):
    if file_type == 'csv':
        permission_required = 'download_data'
    else:
        permission_required = 'view_plots'

    try:
        plot_type = request.GET['plotType']
        template = request.GET.get('theme', default_plotly_template)
        if template not in ALLOWED_TEMPLATES:
            return HttpResponse('Please select an allowed template',
                                status=400)

        dataset_id = int(request.GET['datasetId'])
        dataset2_id = request.GET.get('dataset2Id', None)
        if dataset2_id == "":
            dataset2_id = None
        if dataset2_id is not None:
            dataset2_id = int(dataset2_id)
        cell_line_id = request.GET.getlist('c')
        drug_id = request.GET.getlist('d')

        cell_line_id = [int(cl) for cl in cell_line_id]
        drug_ids = []
        for dr in drug_id:
            try:
                drug_ids.append(int(dr))
            except ValueError:
                drug_ids.append([int(d) for d in dr.split(",")])
        drug_id = drug_ids

        assay = request.GET.get('assayId')
        yaxis = request.GET.get('logTransform', 'None')

    except (KeyError, ValueError):
        raise Http404()

    try:
        dataset = HTSDataset.objects.get(pk=dataset_id)
    except HTSDataset.DoesNotExist:
        raise Http404()

    _assert_has_perm(request, dataset, permission_required)
    if not license_accepted(request, dataset):
        return HttpResponse(LICENSE_UNSIGNED.format(dataset.name), status=400)

    if plot_type == 'tc':
        if len(drug_id) != 1 or len(cell_line_id) != 1:
            return HttpResponse(
                'Please select exactly one cell line and '
                'drug for time course plot',
                status=400)

        try:
            df_data = df_doses_assays_controls(dataset=dataset,
                                               drug_id=drug_id,
                                               cell_line_id=cell_line_id,
                                               assay=assay)
        except NoDataException:
            return HttpResponse(
                'No data found for this request. This '
                'drug/cell line/assay combination may not '
                'exist.',
                status=400)
        if assay is None:
            assay = df_data.assays.index.get_level_values('assay')[0]

        overlay_dip_fit = request.GET.get('overlayDipFit', 'false') == 'true'

        if overlay_dip_fit and assay != df_data.dip_assay_name:
            return HttpResponse(
                'Can only overlay DIP rate on cell '
                'proliferation assays',
                status=400)

        plot_fig = plot_time_course(df_data,
                                    log_yaxis=yaxis == 'log2',
                                    assay_name=assay,
                                    show_dip_fit=overlay_dip_fit,
                                    subtitle=dataset.name,
                                    template=template)
    elif plot_type in ('drc', 'drpar'):
        if all(isinstance(d, int) for d in drug_id):
            plot_fig = _dose_response_plot(request, dataset, dataset2_id,
                                           permission_required, drug_id,
                                           cell_line_id, plot_type, template)
        else:
            if dataset2_id is not None:
                return HttpResponse(
                    'Please select a single dataset at a time to view drug '
                    'combination heat plots',
                    status=400)

            plot_fig = _drug_combination_heatmap(request, dataset, drug_id,
                                                 cell_line_id, template)

        if isinstance(plot_fig, HttpResponse):
            return plot_fig
    elif plot_type == 'qc':
        qc_view = request.GET.get('qcView', None)
        if qc_view == 'ctrldipbox':
            ctrl_dip_data = df_ctrl_dip_rates(dataset_id)
            if ctrl_dip_data is None:
                return HttpResponse(
                    'No control wells with DIP rates '
                    'available were detected in this '
                    'dataset.',
                    status=400)
            plot_fig = plot_ctrl_dip_by_plate(ctrl_dip_data, template=template)
        elif qc_view == 'ctrlcellbox':
            # Try to fetch from cache
            cache_key = f'dataset_{dataset_id}_plot_ctrlcellbox'
            cur_plotver = 1
            plot_cached = cache.get(cache_key)
            if plot_cached:
                plot_fig = plot_cached['plot_fig']
            if plot_cached is None or plot_cached['dataset_last_modified'] < dataset.modified_date or \
                    plot_cached['plot_version'] < cur_plotver:
                # Create plot
                groupings = dataset_groupings(dataset)
                if not groupings['singleTimepoint']:
                    return HttpResponse(
                        'This plot type is only available for '
                        'single time-point datasets',
                        status=400)
                try:
                    df_data = df_control_wells(dataset_id=dataset, assay=assay)
                except NoDataException:
                    return HttpResponse('No data found for this request.',
                                        status=400)
                if (df_data['value'] == 100.0).all():
                    return HttpResponse(
                        'The raw data for this dataset is given as relative viability, so no control '
                        'wells are available',
                        status=400)

                plot_fig = plot_ctrl_cell_counts_by_plate(
                    df_data, subtitle=dataset.name, template=template)

                # Push to cache
                cache.set(
                    cache_key, {
                        'dataset_last_modified': dataset.modified_date,
                        'plot_version': cur_plotver,
                        'plot_fig': plot_fig
                    })

        elif qc_view == 'dipplatemap':
            plate_id = request.GET.get('plateId', None)
            try:
                plate_id = int(plate_id)
            except ValueError:
                return HttpResponse('Integer plateId required', status=400)
            pl_data = ajax_load_plate(request,
                                      plate_id,
                                      return_as_platedata=True,
                                      use_names=True)
            plot_fig = plot_plate_map(pl_data,
                                      color_by='dip_rates',
                                      template=template)
        else:
            return HttpResponse('Unimplemented QC view: {}'.format(qc_view),
                                status=400)
    else:
        return HttpResponse('Unimplemented plot type: %s' % plot_type,
                            status=400)

    as_attachment = request.GET.get('download', '0') == '1'

    if file_type == 'json':
        j = json.dumps(plot_fig, cls=PlotlyJSONEncoder)
        response = HttpResponse(j, content_type='application/json')
    elif file_type == 'csv':
        response = HttpResponse(plotly_to_dataframe(plot_fig).to_csv(),
                                content_type='text/csv')
    elif file_type == 'html':
        template = 'plotly_plot{}.html'.format(
            '_standalone' if as_attachment else '')
        context = {
            'data': json.dumps(plot_fig, cls=PlotlyJSONEncoder),
            'page_title': strip_tags(plot_fig['layout']['title']['text'])
        }
        if as_attachment:
            context['plotlyjs'] = get_plotlyjs()
        response = render(request, template, context)
    else:
        return HttpResponse('Unknown file type: %s' % file_type, status=400)

    if as_attachment:
        try:
            title = plot_fig['layout']['title']['text']
        except KeyError:
            title = 'Plot'
        response['Content-Disposition'] = \
            'attachment; filename="{}.{}"'.format(strip_tags(title), file_type)

    return response