<commit_before>/*jshint browser: true, devel: true */
/*global define: false  */

define(['haml/download.haml', "defer", "galaxy", "jquery", "util", "underscore_ext",
	"loading"], function (template, defer, galaxy, $, util, _) {
	'use strict';

	var filter = _.filter,
		flatten = _.flatten,
		map = _.map,
		widget,
		aWidget;

	aWidget = {

		destroy: function () {
			this.$el.remove();
			widget = undefined;
		},

		populateLink: function (tsv, scope) {
			var $link = this.$link,
				self = this,
				prefix = 'xenaDownload', // TODO use a scrubbed column label after it is being stored in state tree
				//prefix = util.cleanName(this.ws.column.label1 + '_' + this.ws.column.label2),
				url = URL.createObjectURL(new Blob([tsv], { type: 'text/tsv' })); // use blob for bug in chrome: https://code.google.com/p/chromium/issues/detail?id=373182
			$link.attr({
				'href': url,
				'download': prefix + '.tsv'
			});
			defer(function () { // allow link to update, then click it
				var evt = document.createEvent('MouseEvents');
				self.destroy();
				evt.initMouseEvent('click', true, true, window, 1, 0, 0, 0, 0,
					false, false, false, false, 0, null);
				$link[0].dispatchEvent(evt);
			});
		},

		buildTsv: function (data, varNames, scope) {
			var self = this,
				tsv,
				rows;
			rows = map(data, function (row, i) {
				return row.join('\t');
			});
			defer(function () { // prevent client timeout
				rows.unshift(varNames.join('\t'));
				defer(function () { // prevent client timeout
					tsv = rows.join('\n');
					self.populateLink(tsv, scope);
				});
			});
		},

		xformMutationVector: function () {
			var stableVars = ['sample', 'chr', 'start', 'end', 'reference', 'alt'],
				derivedVars = this.columnUi.plotData.derivedVars.sort(util.caseInsensitiveSort),
				varNames = stableVars.concat(filter(derivedVars, function (dv) {
					return (stableVars.indexOf(dv) < 0);
				})),
				tsvData = flatten(this.columnUi.plotData.values.map(function (sample) {
					if (sample.vals) {
						if (sample.vals.length) { // value(s) for this sample & identifier
							return sample.vals.map(function (vals) {
								return varNames.map(function (varName) {
									return vals[varName];
								});
							});
						} else { // no value for this sample & identifier
							return [
								varNames.map(function (varName) {
									return (varName === 'sample') ?
										sample.sample : 'no mutation';
								})
							];
						}
					} else { // no data for this sample & identifier
						return [
							varNames.map(function (varName) {
								return (varName === 'sample') ?
									sample.sample : null;
							})
						];
					}
				}), true);
			this.buildTsv(tsvData, varNames);
		},

		xformProbeMatrix: function () {
			var heatmapData = this.columnUi.plotData.heatmapData,
				samples = this.columnUi.plotData.samples,
				fields = this.columnUi.plotData.fields,
				codes = this.columnUi.plotData.codes,
				varNames = ['sample'].concat(fields),
				tsvData = samples.map(function (sample, i) {
					return [sample].concat(fields.map(function (field, j) {
						var value = codes[field] ?
							codes[field][heatmapData[j][i]] : heatmapData[j][i];
						return value;
					}));
				});
			if (this.ws.column.dataType === 'clinicalMatrix') {
				varNames = ['sample'].concat([this.ws.column.fieldLabel.default]);
			}
			this.buildTsv(tsvData, varNames);
		},

		now: function () {<commit_msg>modify mutationVector data sliced version download to have all data in a single sample on a single line<commit_after>/*jshint browser: true, devel: true */
/*global define: false  */

define(['haml/download.haml', "defer", "galaxy", "jquery", "util", "underscore_ext",
	"loading"], function (template, defer, galaxy, $, util, _) {
	'use strict';

	var filter = _.filter,
		flatten = _.flatten,
		map = _.map,
		widget,
		aWidget;

	aWidget = {

		destroy: function () {
			this.$el.remove();
			widget = undefined;
		},

		populateLink: function (tsv, scope) {
			var $link = this.$link,
				self = this,
				prefix = 'xenaDownload', // TODO use a scrubbed column label after it is being stored in state tree
				//prefix = util.cleanName(this.ws.column.label1 + '_' + this.ws.column.label2),
				url = URL.createObjectURL(new Blob([tsv], { type: 'text/tsv' })); // use blob for bug in chrome: https://code.google.com/p/chromium/issues/detail?id=373182
			$link.attr({
				'href': url,
				'download': prefix + '.tsv'
			});
			defer(function () { // allow link to update, then click it
				var evt = document.createEvent('MouseEvents');
				self.destroy();
				evt.initMouseEvent('click', true, true, window, 1, 0, 0, 0, 0,
					false, false, false, false, 0, null);
				$link[0].dispatchEvent(evt);
			});
		},

		buildTsv: function (data, varNames, scope) {
			var self = this,
				tsv,
				rows;
			rows = map(data, function (row, i) {
				return row.join('\t');
			});
			defer(function () { // prevent client timeout
				rows.unshift(varNames.join('\t'));
				defer(function () { // prevent client timeout
					tsv = rows.join('\n');
					self.populateLink(tsv, scope);
				});
			});
		},

		xformMutationVector: function () {
			var stableVars = ['sample', 'chr', 'start', 'end', 'reference', 'alt'],
				derivedVars = this.columnUi.plotData.derivedVars.sort(util.caseInsensitiveSort),
				varNames = stableVars.concat(filter(derivedVars, function (dv) {
					return (stableVars.indexOf(dv) < 0);
				})),
				tsvData = this.columnUi.plotData.values.map(function (sample) {
					if (sample.vals) {
						if (sample.vals.length) { // value(s) for this sample & identifier
							return flatten(sample.vals.map(function (vals) {
								return varNames.map(function (varName) {
									return vals[varName];
								});
							}));
						} else { // no value for this sample & identifier
								return varNames.map(function (varName) {
									return (varName === 'sample') ?
										sample.sample : 'no mutation';
								});
						}
					} else { // no data for this sample & identifier
							return	varNames.map(function (varName) {
								return (varName === 'sample') ?
									sample.sample : null;
							});
					}
				});
			this.buildTsv(tsvData, varNames);
		},

		xformProbeMatrix: function () {
			var heatmapData = this.columnUi.plotData.heatmapData,
				samples = this.columnUi.plotData.samples,
				fields = this.columnUi.plotData.fields,
				codes = this.columnUi.plotData.codes,
				varNames = ['sample'].concat(fields),
				tsvData = samples.map(function (sample, i) {
					return [sample].concat(fields.map(function (field, j) {
						var value = codes[field] ?
							codes[field][heatmapData[j][i]] : heatmapData[j][i];
						return value;
					}));
				});
			if (this.ws.column.dataType === 'clinicalMatrix') {
				varNames = ['sample'].concat([this.ws.column.fieldLabel.default]);
			}
			this.buildTsv(tsvData, varNames);
		},

		now: function () {
{'fraction_non_alphanumeric': 0.16156511695062084, 'fraction_numerical': 0.004620271440947156, 'mean_word_length': 2.8893879842784953, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 7, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3045542', 'n_tokens_mistral': 2900, 'n_tokens_neox': 2253, 'n_words': 518}