def download_dip_rates(request, dataset_id): try: dataset = HTSDataset.objects.get(pk=dataset_id, deleted_date=None) except HTSDataset.DoesNotExist: raise Http404() _assert_has_perm(request, dataset, 'download_data') if not license_accepted(request, dataset): return _plain_response('You must accept the dataset license to ' 'download this file') try: full_path = _generate_dip_rates(dataset) except NoDataException: return _plain_response('No data found for this request') output_filename = '{}_dip_rates.tsv'.format(dataset.name) return serve_file(request, full_path, rename_to=output_filename, content_type='text/tab-separated-values')
def download_fit_params(request, dataset_id, stat_type): file_type = 'fit_params_{}_tsv'.format(stat_type) file_name = 'fit_params_{}_{}.tsv'.format(stat_type, dataset_id) file_type_protocol = 1 param_names = { 'dip': ('aa', 'aa_obs', 'emax', 'emax_rel', 'emax_obs', 'emax_obs_rel', 'einf', 'ec50', 'ic50', 'hill'), 'viability': ('aa', 'aa_obs', 'emax', 'emax_obs', 'einf', 'ec50', 'ic50', 'hill') } try: dataset = HTSDataset.objects.get(pk=dataset_id, deleted_date=None) except (HTSDataset.DoesNotExist, ValueError): return _plain_response('This dataset does not exist, or you do not ' 'have permission to access it.') _assert_has_perm(request, dataset, 'download_data') if not license_accepted(request, dataset): return _plain_response('You must accept the dataset license to ' 'download this file') mod_date = timezone.now() file = _cached_file(dataset, file_type, file_type_protocol) # Additional cache invalidation: curve fits were generated after the # cached file if file: if file.creation_date < CurveFitSet.objects.get( dataset_id=dataset_id, stat_type=stat_type).calculation_end: file = None if file: full_path = file.file.name else: try: # Fetch the DIP rates from the DB base_params = df_curve_fits(dataset.id, stat_type, drug_ids=None, cell_line_ids=None) except NoDataException: return _plain_response( 'The requested parameter set does not exist for the ' 'specified dataset') # Fit Hill curves and compute parameters fp = fit_params_from_base(base_params, custom_ic_concentrations={50}, custom_ec_concentrations={50}, include_auc=False, include_aa=True, include_hill=True, include_emax=True, include_einf=True, include_response_values=False) fp.reset_index('dataset_id', drop=True, inplace=True) # Remove -ve AA values fp.loc[fp['aa'] < 0.0, 'aa'] = np.nan # Filter for the default list of parameters only fp = fp.filter(items=param_names[stat_type]) full_path = os.path.join(settings.DOWNLOADS_ROOT, file_name) fp.to_csv(full_path, sep='\t') 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() output_filename = '{}_{}_params.tsv'.format(dataset.name, stat_type) return serve_file(request, full_path, rename_to=output_filename, content_type='text/tab-separated-values')