def from_http_request(args): """Returns list of Tag instances. """ tag_names = get_param_as_list(args, 'tags') tags = [] for name in tag_names: # If the name is not an empty string or just whitespace. if bool(name.strip()): tags.append(get_or_create(Tag, name=name)) return tags
def check_combination(): """Checks if a combination of accession, dataset, and selected samples has already been processed. """ try: try: tag = get_param_as_list(request.args, 'tags')[0] except IndexError as e: raise HttpRequestArgumentsException('Make sure you have exactly one tag.', e) accession = request.args.get('dataset') controls = get_param_as_list(request.args, 'A_cols') conditions = get_param_as_list(request.args, 'B_cols') if len(controls) == 0: raise HttpRequestArgumentsException('Please select 2 or more control samples.') if len(conditions) == 0: raise HttpRequestArgumentsException('Please select 2 or more condition samples.') soft_files = database.get_soft_files_by_accession(accession) matching_ids = [] for sf in soft_files: tag_matches = _tag_matches(sf.gene_signature, tag) controls_match = _samples_match(controls, sf.samples, True) conditions_match = _samples_match(conditions, sf.samples, False) if tag_matches or controls_match or conditions_match: matching_ids.append(sf.gene_signature.extraction_id) if len(matching_ids) > 0: links = [] for id_ in matching_ids: link = '%s%s/%s' % (config.SERVER_URL,config.RESULTS_URL, id_) links.append(link) return jsonify({ 'preexisting': True, 'links': links }) else: return jsonify({ 'preexisting': False }) except AttributeError as e: raise HttpRequestArgumentsException('Illegal arguments. Make sure you have only input one tag.', e)
def check_combination(): """Checks if a combination of accession, dataset, and selected samples has already been processed. """ try: try: tag = get_param_as_list(request.args, 'tags')[0] except IndexError as e: raise HttpRequestArgumentsException( 'Make sure you have exactly one tag.', e) accession = request.args.get('dataset') controls = get_param_as_list(request.args, 'A_cols') conditions = get_param_as_list(request.args, 'B_cols') if len(controls) == 0: raise HttpRequestArgumentsException( 'Please select 2 or more control samples.') if len(conditions) == 0: raise HttpRequestArgumentsException( 'Please select 2 or more condition samples.') soft_files = database.get_soft_files_by_accession(accession) matching_ids = [] for sf in soft_files: tag_matches = _tag_matches(sf.gene_signature, tag) controls_match = _samples_match(controls, sf.samples, True) conditions_match = _samples_match(conditions, sf.samples, False) if tag_matches or controls_match or conditions_match: matching_ids.append(sf.gene_signature.extraction_id) if len(matching_ids) > 0: links = [] for id_ in matching_ids: link = '%s%s/%s' % (config.SERVER_URL, config.RESULTS_URL, id_) links.append(link) return jsonify({'preexisting': True, 'links': links}) else: return jsonify({'preexisting': False}) except AttributeError as e: raise HttpRequestArgumentsException( 'Illegal arguments. Make sure you have only input one tag.', e)
def _user_is_authenticated_for_tag(args, is_authenticated): """Returns False if a tag is restricted and the user is not authenticated, True otherwise. """ tag_names = get_param_as_list(args, 'tags') for tag_name in tag_names: tag = database.get_tag_by_name(tag_name) # The tag may not exist yet. If so, we can safely assume it is not # restricted. if tag and tag.is_restricted and not is_authenticated: return False return True
def _user_is_authenticated_for_tag(args, is_authenticated): """Returns False if a tag is restricted and the user is not authenticated, True otherwise. """ tag_names = get_param_as_list(args, 'tags') for tag_name in tag_names: tag = database.get_tag_by_name(tag_name) # The tag may not exist yet. If so, we can safely assume it is not # restricted. if tag and tag.is_restricted and not is_authenticated: return False return True
def edit_result(): """Edits gene signature, removing fields that are empty and adding new ones. """ extraction_id = request.form.get('extraction_id') gene_signature = database.get_gene_signature(extraction_id) for opt_meta in gene_signature.optional_metadata: value = request.form.get(opt_meta.name) if not value: database.delete_object(opt_meta) continue opt_meta.value = value database.update_object(opt_meta) # Generate a new optional metadata field is requested. new_meta_name = request.form.get('new_metadata_name') new_meta_value = request.form.get('new_metadata_value') if new_meta_name and new_meta_value: new_opt_meta = OptionalMetadata(new_meta_name, new_meta_value) gene_signature.optional_metadata.append(new_opt_meta) database.update_object(gene_signature) # Update existing tags. tag_names = request_utils.get_param_as_list(request.form, 'tags') for tag in gene_signature.tags: if tag.name not in tag_names: database.delete_object(tag) else: tag_names.remove(tag.name) database.update_object(gene_signature) # Create any new tags. Note that any leftover tags are new ones. if len(tag_names) > 0: for tag_name in tag_names: tag = Tag(tag_name) gene_signature.tags.append(tag) database.update_object(gene_signature) return redirect( url_for('results_page.view_result', extraction_id=extraction_id))
def edit_result(): """Edits gene signature, removing fields that are empty and adding new ones. """ extraction_id = request.form.get('extraction_id') gene_signature = database.get_gene_signature(extraction_id) for opt_meta in gene_signature.optional_metadata: value = request.form.get(opt_meta.name) if not value: database.delete_object(opt_meta) continue opt_meta.value = value database.update_object(opt_meta) # Generate a new optional metadata field is requested. new_meta_name = request.form.get('new_metadata_name') new_meta_value = request.form.get('new_metadata_value') if new_meta_name and new_meta_value: new_opt_meta = OptionalMetadata(new_meta_name, new_meta_value) gene_signature.optional_metadata.append(new_opt_meta) database.update_object(gene_signature) # Update existing tags. tag_names = request_utils.get_param_as_list(request.form, 'tags') for tag in gene_signature.tags: if tag.name not in tag_names: database.delete_object(tag) else: tag_names.remove(tag.name) database.update_object(gene_signature) # Create any new tags. Note that any leftover tags are new ones. if len(tag_names) > 0: for tag_name in tag_names: tag = Tag(tag_name) gene_signature.tags.append(tag) database.update_object(gene_signature) return redirect(url_for('results_page.view_result', extraction_id=extraction_id))
def from_geo(args): """Constructs a SoftFile """ accession = args['dataset'] if not file_manager.file_exists(accession): file_manager.download(accession) dataset = database.get_geo_dataset(accession) if not dataset: platform = args['platform'] if platform.index('GPL') == 0: platform = platform[3:] title = args.get('title') summary = args.get('summary') if not title or not summary: title, summary = eutils.get_title_and_summary(accession) print('Fetched title and summary from EUtils API: %s, %s' % (title, summary)) dataset = GeoDataset(accession=accession, platform=platform, organism=args['organism'], title=title, summary=summary) else: print('Dataset %s already exists!' % (accession)) a_cols = get_param_as_list(args, 'A_cols') b_cols = get_param_as_list(args, 'B_cols') # Use get_or_create to track GSMs. We don't do this for custom files. control = [ database.get_or_create(SoftFileSample, name=sample, is_control=True) for sample in a_cols ] experimental = [ database.get_or_create(SoftFileSample, name=sample, is_control=False) for sample in b_cols ] samples = control + experimental genes, a_vals, b_vals, selections, stats = parser.parse( accession, True, dataset.platform, samples) if 'normalize' not in args or args['normalize'] == 'True': normalize = True else: normalize = False genes, a_vals, b_vals = cleaner.clean(genes, a_vals, b_vals, normalize) text_file = file_manager.write(accession, dataset.platform, normalize, genes, a_vals, b_vals, samples, selections, stats) return SoftFile(samples, dataset, text_file, genes, a_vals, b_vals, stats=stats, normalize=normalize)
def from_geo(args): """Constructs a SoftFile """ accession = args['dataset'] if not file_manager.file_exists(accession): file_manager.download(accession) dataset = database.get_geo_dataset(accession) if not dataset: platform = args['platform'] if platform.index('GPL') == 0: platform = platform[3:] title = args.get('title') summary = args.get('summary') if not title or not summary: title, summary = eutils.get_title_and_summary(accession) print('Fetched title and summary from EUtils API: %s, %s' % (title, summary)) dataset = GeoDataset( accession=accession, platform=platform, organism=args['organism'], title=title, summary=summary ) else: print 'Dataset %s already exists!' % accession a_cols = get_param_as_list(args, 'A_cols') b_cols = get_param_as_list(args, 'B_cols') # Use get_or_create to track GSMs. We don't do this for custom files. control = [database.get_or_create(SoftFileSample, name=sample, is_control=True) for sample in a_cols] experimental = [database.get_or_create(SoftFileSample, name=sample, is_control=False) for sample in b_cols] samples = control + experimental genes, a_vals, b_vals, selections, stats = parser.parse(accession, True, dataset.platform, samples) if 'normalize' not in args or args['normalize'] == 'True': normalize = True else: normalize = False genes, a_vals, b_vals = cleaner.clean(genes, a_vals, b_vals, normalize) text_file = file_manager.write(accession, dataset.platform, normalize, genes, a_vals, b_vals, samples, selections, stats) return SoftFile( samples, dataset, text_file, genes, a_vals, b_vals, stats=stats, normalize=normalize )
def test_with_brackets(self): with app.test_request_context('/fake_endpoint?p[]=foo&p[]=bar', method='GET'): x = get_param_as_list(request.args, 'p') self.assertTrue(len(x), 2) self.assertTrue(x[0], 'foo') self.assertTrue(x[1], 'bar')