<commit_before>/*global define: false */

define(['rx.dom', 'underscore_ext'], function (Rx, _) {
	'use strict';

	// HELPERS

	var null_cohort = '(unassigned)';

	function json_resp(xhr) {
		return JSON.parse(xhr.response);
	}

	function quote(s) {
		return '"' + s + '"'; // XXX should escape "
	}

	function sep(l) {
		return _.map(l, quote).join(' ');
	}

	function listfmt(l) {
		return '(' + sep(l) + ')';
	}

	function arrayfmt(l) {
		return '[' + sep(l) + ']';
	}

	function nanstr(v) {
		if (v === "NaN") {
			return undefined;
		}
		return v;
	}

	// XXX should make this the default quote(), since all null
	// values should be mapped to nil. Should drop null_cohort, and
	// deal with null->"" in the cohort selection UI code. option
	// elements always have string values, so need a special null value.
	function quote_cohort(cohort) {
		return (cohort === null_cohort) ? 'nil' : quote(cohort);
	}

	function parse_host(dsID) {
		return dsID.match(/([^\/]*\/\/[^\/]*)\/(.*)/);
	}

	// Returns a object with key equal to the serialization of
	// the request, and value equal to a thunk that returns
	// an Observable of the data.
	function reqObj(req, fn) { // TODO may not belong in this file
		return {
			id: JSON.stringify(req),
			query:  Rx.Observable.defer(_.partial(fn, req))
		};
	}

	function indexFeatures(features) {
		return _.object(_.map(features, function (f) {
			return [f.NAME, f.SHORTTITLE || f.NAME];
		}));
	}

	function xena_dataset_list_transform(host, list) {
		return _.map(list, function (ds) {
			var text = JSON.parse(ds.TEXT) || {};

			// merge curated fields over raw metadata
			// XXX note that we're case sensitive on raw metadata
			ds = _.extend(text, _.dissoc(ds, 'TEXT'));
			return {
				dsID: host + '/' + ds.NAME,
				title: ds.label || ds.NAME,
				// XXX wonky fix to work around dataSubType.
				// Use basename of ds.DATASUBTYPE if it's there. Otherwise
				// default to cna if there's a gene view, and clinical otherwise.
				dataSubType: ds.DATASUBTYPE ?
					ds.DATASUBTYPE.split(/[\/]/).reverse()[0] :
					(ds.PROBEMAP ? 'cna' : 'clinical')
			};
		});
	}

	function xena_get(host, query) {
		return {
			url: host + '/data/' + encodeURIComponent(query),
			method: 'GET'
		};
	}

	function xena_post(host, query) {
		return {
			headers: {'Content-Type': 'text/plain' },
			url: host + '/data/',
			body: query,
			method: 'POST'
		};
	}

	// QUERY STRINGS

	function dataset_samples_query(dataset) {
		return '(map :NAME (query {:select [:sample.name] ' +
		       '                   :from [:dataset] ' +
		       '                   :where [:= :dataset.name ' + quote(dataset) + ']' +
		       '                   :left-join [:sample [:= :dataset.id :dataset_id]]}))';
	}

	function all_samples_query(cohort) {
		return '(map :VALUE ' +
		       '  (query ' +
		       '    {:select [:%distinct.value] ' +
		       '     :from [:dataset] ' +
		       '     :where [:= :cohort ' + quote_cohort(cohort) + '] ' +
		       '     :left-join ' +
		       '       [[{:select [:id :dataset_id] ' +
		       '          :from [:field] ' +
		       '          :where [:= :name "sampleID"]} :F] [:= :dataset.id :dataset_id] ' +
		       '        :feature [:= :feature.field_id :F.id] ' +
		       '        :code [:= :feature.id :feature_id]]}))';
	}

	function all_cohorts_query() {
		return '(query {:select [:name [#sql/call [:ifnull :cohort "' + null_cohort + '"] :cohort]] ' +
		       '        :from [:dataset]})';
	}

	function dataset_list_query(cohort) {
		return  '(query {:select [:name :shorttitle :datasubtype :probemap :text] ' +
				'        :from [:dataset] ' +
				'        :where [:= :cohort ' + quote_cohort(cohort) + ']})';
	}

	function dataset_probe_string(dataset, samples, probes) {
		return '(fetch (quote ({' +
					['table', quote(dataset),
					'columns', listfmt(probes),
					'samples', listfmt(samples)].join(' ')
					+ '})))';
	}<commit_msg>Update clinical query for new server code.<commit_after>/*global define: false */

define(['rx.dom', 'underscore_ext'], function (Rx, _) {
	'use strict';

	// HELPERS

	var null_cohort = '(unassigned)';

	function json_resp(xhr) {
		return JSON.parse(xhr.response);
	}

	function quote(s) {
		return '"' + s + '"'; // XXX should escape "
	}

	function sep(l) {
		return _.map(l, quote).join(' ');
	}

	function listfmt(l) {
		return '(' + sep(l) + ')';
	}

	function arrayfmt(l) {
		return '[' + sep(l) + ']';
	}

	function nanstr(v) {
		if (v === "NaN") {
			return undefined;
		}
		return v;
	}

	// XXX should make this the default quote(), since all null
	// values should be mapped to nil. Should drop null_cohort, and
	// deal with null->"" in the cohort selection UI code. option
	// elements always have string values, so need a special null value.
	function quote_cohort(cohort) {
		return (cohort === null_cohort) ? 'nil' : quote(cohort);
	}

	function parse_host(dsID) {
		return dsID.match(/([^\/]*\/\/[^\/]*)\/(.*)/);
	}

	// Returns a object with key equal to the serialization of
	// the request, and value equal to a thunk that returns
	// an Observable of the data.
	function reqObj(req, fn) { // TODO may not belong in this file
		return {
			id: JSON.stringify(req),
			query:  Rx.Observable.defer(_.partial(fn, req))
		};
	}

	function indexFeatures(features) {
		return _.object(_.map(features, function (f) {
			return [f.NAME, f.SHORTTITLE || f.NAME];
		}));
	}

	function xena_dataset_list_transform(host, list) {
		return _.map(list, function (ds) {
			var text = JSON.parse(ds.TEXT) || {};

			// merge curated fields over raw metadata
			// XXX note that we're case sensitive on raw metadata
			ds = _.extend(text, _.dissoc(ds, 'TEXT'));
			return {
				dsID: host + '/' + ds.NAME,
				title: ds.label || ds.NAME,
				// XXX wonky fix to work around dataSubType.
				// Use basename of ds.DATASUBTYPE if it's there. Otherwise
				// default to cna if there's a gene view, and clinical otherwise.
				dataSubType: ds.DATASUBTYPE ?
					ds.DATASUBTYPE.split(/[\/]/).reverse()[0] :
					(ds.PROBEMAP ? 'cna' : 'clinical')
			};
		});
	}

	function xena_get(host, query) {
		return {
			url: host + '/data/' + encodeURIComponent(query),
			method: 'GET'
		};
	}

	function xena_post(host, query) {
		return {
			headers: {'Content-Type': 'text/plain' },
			url: host + '/data/',
			body: query,
			method: 'POST'
		};
	}

	// QUERY STRINGS

	function dataset_samples_query(dataset) {
		return '(map :NAME (query {:select [:sample.name] ' +
		       '                   :from [:dataset] ' +
		       '                   :where [:= :dataset.name ' + quote(dataset) + ']' +
		       '                   :left-join [:sample [:= :dataset.id :dataset_id]]}))';
	}

	function all_samples_query(cohort) {
		return '(map :VALUE ' +
		       '  (query ' +
		       '    {:select [:%distinct.value] ' +
		       '     :from [:dataset] ' +
		       '     :where [:= :cohort ' + quote_cohort(cohort) + '] ' +
		       '     :left-join ' +
		       '       [[{:select [:id :dataset_id] ' +
		       '          :from [:field] ' +
		       '          :where [:= :name "sampleID"]} :F] [:= :dataset.id :dataset_id] ' +
		       '        :feature [:= :feature.field_id :F.id] ' +
		       '        :code [:= :feature.id :feature_id]]}))';
	}

	function all_cohorts_query() {
		return '(query {:select [:name [#sql/call [:ifnull :cohort "' + null_cohort + '"] :cohort]] ' +
		       '        :from [:dataset]})';
	}

	function dataset_list_query(cohort) {
		return  '(query {:select [:name :shorttitle :datasubtype :probemap :text] ' +
				'        :from [:dataset] ' +
				'        :where [:= :cohort ' + quote_cohort(cohort) + ']})';
	}

	function dataset_probe_string(dataset, samples, probes) {
		return '(fetch (quote ({' +
					[':table', quote(dataset),
					':columns', listfmt(probes),
					':samples', listfmt(samples)].join(' ')
					+ '})))';
	}
{'fraction_non_alphanumeric': 0.1761723700887199, 'fraction_numerical': 0.00025348542458808617, 'mean_word_length': 2.5545045045045045, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5782673', 'n_tokens_mistral': 3140, 'n_tokens_neox': 2726, 'n_words': 670}