Commit 3e5b5bfb authored by jgould's avatar jgould

use papa parse

parent cb788dc8
......@@ -66,7 +66,7 @@ module.exports = function (grunt) {
'js/hopscotch.min.js', 'js/typed.min.js',
'js/jquery.event.drag-2.2.js',
'js/clipboard.min.js', 'js/slick.min.js',
'js/js.cookie.js', 'js/d3.layout.cloud.js']
'js/js.cookie.js', 'js/d3.layout.cloud.js', 'js/papaparse.min.js']
},
morpheus: {
nonull: true,
......
This source diff could not be displayed because it is too large. You can view the blob instead.
/*!
Papa Parse
v4.1.2
https://github.com/mholt/PapaParse
*/
!function(e){"use strict";function t(t,r){if(r=r||{},r.worker&&S.WORKERS_SUPPORTED){var n=f();return n.userStep=r.step,n.userChunk=r.chunk,n.userComplete=r.complete,n.userError=r.error,r.step=m(r.step),r.chunk=m(r.chunk),r.complete=m(r.complete),r.error=m(r.error),delete r.worker,void n.postMessage({input:t,config:r,workerId:n.id})}var o=null;return"string"==typeof t?o=r.download?new i(r):new a(r):(e.File&&t instanceof File||t instanceof Object)&&(o=new s(r)),o.stream(t)}function r(e,t){function r(){"object"==typeof t&&("string"==typeof t.delimiter&&1==t.delimiter.length&&-1==S.BAD_DELIMITERS.indexOf(t.delimiter)&&(u=t.delimiter),("boolean"==typeof t.quotes||t.quotes instanceof Array)&&(o=t.quotes),"string"==typeof t.newline&&(h=t.newline))}function n(e){if("object"!=typeof e)return[];var t=[];for(var r in e)t.push(r);return t}function i(e,t){var r="";"string"==typeof e&&(e=JSON.parse(e)),"string"==typeof t&&(t=JSON.parse(t));var n=e instanceof Array&&e.length>0,i=!(t[0]instanceof Array);if(n){for(var a=0;a<e.length;a++)a>0&&(r+=u),r+=s(e[a],a);t.length>0&&(r+=h)}for(var o=0;o<t.length;o++){for(var f=n?e.length:t[o].length,c=0;f>c;c++){c>0&&(r+=u);var d=n&&i?e[c]:c;r+=s(t[o][d],c)}o<t.length-1&&(r+=h)}return r}function s(e,t){if("undefined"==typeof e||null===e)return"";e=e.toString().replace(/"/g,'""');var r="boolean"==typeof o&&o||o instanceof Array&&o[t]||a(e,S.BAD_DELIMITERS)||e.indexOf(u)>-1||" "==e.charAt(0)||" "==e.charAt(e.length-1);return r?'"'+e+'"':e}function a(e,t){for(var r=0;r<t.length;r++)if(e.indexOf(t[r])>-1)return!0;return!1}var o=!1,u=",",h="\r\n";if(r(),"string"==typeof e&&(e=JSON.parse(e)),e instanceof Array){if(!e.length||e[0]instanceof Array)return i(null,e);if("object"==typeof e[0])return i(n(e[0]),e)}else if("object"==typeof e)return"string"==typeof e.data&&(e.data=JSON.parse(e.data)),e.data instanceof Array&&(e.fields||(e.fields=e.data[0]instanceof Array?e.fields:n(e.data[0])),e.data[0]instanceof Array||"object"==typeof e.data[0]||(e.data=[e.data])),i(e.fields||[],e.data||[]);throw"exception: Unable to serialize unrecognized input"}function n(t){function r(e){var t=_(e);t.chunkSize=parseInt(t.chunkSize),e.step||e.chunk||(t.chunkSize=null),this._handle=new o(t),this._handle.streamer=this,this._config=t}this._handle=null,this._paused=!1,this._finished=!1,this._input=null,this._baseIndex=0,this._partialLine="",this._rowCount=0,this._start=0,this._nextChunk=null,this.isFirstChunk=!0,this._completeResults={data:[],errors:[],meta:{}},r.call(this,t),this.parseChunk=function(t){if(this.isFirstChunk&&m(this._config.beforeFirstChunk)){var r=this._config.beforeFirstChunk(t);void 0!==r&&(t=r)}this.isFirstChunk=!1;var n=this._partialLine+t;this._partialLine="";var i=this._handle.parse(n,this._baseIndex,!this._finished);if(!this._handle.paused()&&!this._handle.aborted()){var s=i.meta.cursor;this._finished||(this._partialLine=n.substring(s-this._baseIndex),this._baseIndex=s),i&&i.data&&(this._rowCount+=i.data.length);var a=this._finished||this._config.preview&&this._rowCount>=this._config.preview;if(y)e.postMessage({results:i,workerId:S.WORKER_ID,finished:a});else if(m(this._config.chunk)){if(this._config.chunk(i,this._handle),this._paused)return;i=void 0,this._completeResults=void 0}return this._config.step||this._config.chunk||(this._completeResults.data=this._completeResults.data.concat(i.data),this._completeResults.errors=this._completeResults.errors.concat(i.errors),this._completeResults.meta=i.meta),!a||!m(this._config.complete)||i&&i.meta.aborted||this._config.complete(this._completeResults),a||i&&i.meta.paused||this._nextChunk(),i}},this._sendError=function(t){m(this._config.error)?this._config.error(t):y&&this._config.error&&e.postMessage({workerId:S.WORKER_ID,error:t,finished:!1})}}function i(e){function t(e){var t=e.getResponseHeader("Content-Range");return parseInt(t.substr(t.lastIndexOf("/")+1))}e=e||{},e.chunkSize||(e.chunkSize=S.RemoteChunkSize),n.call(this,e);var r;this._nextChunk=k?function(){this._readChunk(),this._chunkLoaded()}:function(){this._readChunk()},this.stream=function(e){this._input=e,this._nextChunk()},this._readChunk=function(){if(this._finished)return void this._chunkLoaded();if(r=new XMLHttpRequest,k||(r.onload=g(this._chunkLoaded,this),r.onerror=g(this._chunkError,this)),r.open("GET",this._input,!k),this._config.chunkSize){var e=this._start+this._config.chunkSize-1;r.setRequestHeader("Range","bytes="+this._start+"-"+e),r.setRequestHeader("If-None-Match","webkit-no-cache")}try{r.send()}catch(t){this._chunkError(t.message)}k&&0==r.status?this._chunkError():this._start+=this._config.chunkSize},this._chunkLoaded=function(){if(4==r.readyState){if(r.status<200||r.status>=400)return void this._chunkError();this._finished=!this._config.chunkSize||this._start>t(r),this.parseChunk(r.responseText)}},this._chunkError=function(e){var t=r.statusText||e;this._sendError(t)}}function s(e){e=e||{},e.chunkSize||(e.chunkSize=S.LocalChunkSize),n.call(this,e);var t,r,i="undefined"!=typeof FileReader;this.stream=function(e){this._input=e,r=e.slice||e.webkitSlice||e.mozSlice,i?(t=new FileReader,t.onload=g(this._chunkLoaded,this),t.onerror=g(this._chunkError,this)):t=new FileReaderSync,this._nextChunk()},this._nextChunk=function(){this._finished||this._config.preview&&!(this._rowCount<this._config.preview)||this._readChunk()},this._readChunk=function(){var e=this._input;if(this._config.chunkSize){var n=Math.min(this._start+this._config.chunkSize,this._input.size);e=r.call(e,this._start,n)}var s=t.readAsText(e,this._config.encoding);i||this._chunkLoaded({target:{result:s}})},this._chunkLoaded=function(e){this._start+=this._config.chunkSize,this._finished=!this._config.chunkSize||this._start>=this._input.size,this.parseChunk(e.target.result)},this._chunkError=function(){this._sendError(t.error)}}function a(e){e=e||{},n.call(this,e);var t,r;this.stream=function(e){return t=e,r=e,this._nextChunk()},this._nextChunk=function(){if(!this._finished){var e=this._config.chunkSize,t=e?r.substr(0,e):r;return r=e?r.substr(e):"",this._finished=!r,this.parseChunk(t)}}}function o(e){function t(){if(b&&d&&(h("Delimiter","UndetectableDelimiter","Unable to auto-detect delimiting character; defaulted to '"+S.DefaultDelimiter+"'"),d=!1),e.skipEmptyLines)for(var t=0;t<b.data.length;t++)1==b.data[t].length&&""==b.data[t][0]&&b.data.splice(t--,1);return r()&&n(),i()}function r(){return e.header&&0==y.length}function n(){if(b){for(var e=0;r()&&e<b.data.length;e++)for(var t=0;t<b.data[e].length;t++)y.push(b.data[e][t]);b.data.splice(0,1)}}function i(){if(!b||!e.header&&!e.dynamicTyping)return b;for(var t=0;t<b.data.length;t++){for(var r={},n=0;n<b.data[t].length;n++){if(e.dynamicTyping){var i=b.data[t][n];b.data[t][n]="true"==i||"TRUE"==i?!0:"false"==i||"FALSE"==i?!1:o(i)}e.header&&(n>=y.length?(r.__parsed_extra||(r.__parsed_extra=[]),r.__parsed_extra.push(b.data[t][n])):r[y[n]]=b.data[t][n])}e.header&&(b.data[t]=r,n>y.length?h("FieldMismatch","TooManyFields","Too many fields: expected "+y.length+" fields but parsed "+n,t):n<y.length&&h("FieldMismatch","TooFewFields","Too few fields: expected "+y.length+" fields but parsed "+n,t))}return e.header&&b.meta&&(b.meta.fields=y),b}function s(t){for(var r,n,i,s=[","," ","|",";",S.RECORD_SEP,S.UNIT_SEP],a=0;a<s.length;a++){var o=s[a],h=0,f=0;i=void 0;for(var c=new u({delimiter:o,preview:10}).parse(t),d=0;d<c.data.length;d++){var l=c.data[d].length;f+=l,"undefined"!=typeof i?l>1&&(h+=Math.abs(l-i),i=l):i=l}c.data.length>0&&(f/=c.data.length),("undefined"==typeof n||n>h)&&f>1.99&&(n=h,r=o)}return e.delimiter=r,{successful:!!r,bestDelimiter:r}}function a(e){e=e.substr(0,1048576);var t=e.split("\r");if(1==t.length)return"\n";for(var r=0,n=0;n<t.length;n++)"\n"==t[n][0]&&r++;return r>=t.length/2?"\r\n":"\r"}function o(e){var t=l.test(e);return t?parseFloat(e):e}function h(e,t,r,n){b.errors.push({type:e,code:t,message:r,row:n})}var f,c,d,l=/^\s*-?(\d*\.?\d+|\d+\.?\d*)(e[-+]?\d+)?\s*$/i,p=this,g=0,v=!1,k=!1,y=[],b={data:[],errors:[],meta:{}};if(m(e.step)){var R=e.step;e.step=function(n){if(b=n,r())t();else{if(t(),0==b.data.length)return;g+=n.data.length,e.preview&&g>e.preview?c.abort():R(b,p)}}}this.parse=function(r,n,i){if(e.newline||(e.newline=a(r)),d=!1,!e.delimiter){var o=s(r);o.successful?e.delimiter=o.bestDelimiter:(d=!0,e.delimiter=S.DefaultDelimiter),b.meta.delimiter=e.delimiter}var h=_(e);return e.preview&&e.header&&h.preview++,f=r,c=new u(h),b=c.parse(f,n,i),t(),v?{meta:{paused:!0}}:b||{meta:{paused:!1}}},this.paused=function(){return v},this.pause=function(){v=!0,c.abort(),f=f.substr(c.getCharIndex())},this.resume=function(){v=!1,p.streamer.parseChunk(f)},this.aborted=function(){return k},this.abort=function(){k=!0,c.abort(),b.meta.aborted=!0,m(e.complete)&&e.complete(b),f=""}}function u(e){e=e||{};var t=e.delimiter,r=e.newline,n=e.comments,i=e.step,s=e.preview,a=e.fastMode;if(("string"!=typeof t||S.BAD_DELIMITERS.indexOf(t)>-1)&&(t=","),n===t)throw"Comment character same as delimiter";n===!0?n="#":("string"!=typeof n||S.BAD_DELIMITERS.indexOf(n)>-1)&&(n=!1),"\n"!=r&&"\r"!=r&&"\r\n"!=r&&(r="\n");var o=0,u=!1;this.parse=function(e,h,f){function c(e){b.push(e),S=o}function d(t){return f?p():("undefined"==typeof t&&(t=e.substr(o)),w.push(t),o=g,c(w),y&&_(),p())}function l(t){o=t,c(w),w=[],O=e.indexOf(r,o)}function p(e){return{data:b,errors:R,meta:{delimiter:t,linebreak:r,aborted:u,truncated:!!e,cursor:S+(h||0)}}}function _(){i(p()),b=[],R=[]}if("string"!=typeof e)throw"Input must be a string";var g=e.length,m=t.length,v=r.length,k=n.length,y="function"==typeof i;o=0;var b=[],R=[],w=[],S=0;if(!e)return p();if(a||a!==!1&&-1===e.indexOf('"')){for(var C=e.split(r),E=0;E<C.length;E++){var w=C[E];if(o+=w.length,E!==C.length-1)o+=r.length;else if(f)return p();if(!n||w.substr(0,k)!=n){if(y){if(b=[],c(w.split(t)),_(),u)return p()}else c(w.split(t));if(s&&E>=s)return b=b.slice(0,s),p(!0)}}return p()}for(var x=e.indexOf(t,o),O=e.indexOf(r,o);;)if('"'!=e[o])if(n&&0===w.length&&e.substr(o,k)===n){if(-1==O)return p();o=O+v,O=e.indexOf(r,o),x=e.indexOf(t,o)}else if(-1!==x&&(O>x||-1===O))w.push(e.substring(o,x)),o=x+m,x=e.indexOf(t,o);else{if(-1===O)break;if(w.push(e.substring(o,O)),l(O+v),y&&(_(),u))return p();if(s&&b.length>=s)return p(!0)}else{var I=o;for(o++;;){var I=e.indexOf('"',I+1);if(-1===I)return f||R.push({type:"Quotes",code:"MissingQuotes",message:"Quoted field unterminated",row:b.length,index:o}),d();if(I===g-1){var D=e.substring(o,I).replace(/""/g,'"');return d(D)}if('"'!=e[I+1]){if(e[I+1]==t){w.push(e.substring(o,I).replace(/""/g,'"')),o=I+1+m,x=e.indexOf(t,o),O=e.indexOf(r,o);break}if(e.substr(I+1,v)===r){if(w.push(e.substring(o,I).replace(/""/g,'"')),l(I+1+v),x=e.indexOf(t,o),y&&(_(),u))return p();if(s&&b.length>=s)return p(!0);break}}else I++}}return d()},this.abort=function(){u=!0},this.getCharIndex=function(){return o}}function h(){var e=document.getElementsByTagName("script");return e.length?e[e.length-1].src:""}function f(){if(!S.WORKERS_SUPPORTED)return!1;if(!b&&null===S.SCRIPT_PATH)throw new Error("Script path cannot be determined automatically when Papa Parse is loaded asynchronously. You need to set Papa.SCRIPT_PATH manually.");var t=S.SCRIPT_PATH||v;t+=(-1!==t.indexOf("?")?"&":"?")+"papaworker";var r=new e.Worker(t);return r.onmessage=c,r.id=w++,R[r.id]=r,r}function c(e){var t=e.data,r=R[t.workerId],n=!1;if(t.error)r.userError(t.error,t.file);else if(t.results&&t.results.data){var i=function(){n=!0,d(t.workerId,{data:[],errors:[],meta:{aborted:!0}})},s={abort:i,pause:l,resume:l};if(m(r.userStep)){for(var a=0;a<t.results.data.length&&(r.userStep({data:[t.results.data[a]],errors:t.results.errors,meta:t.results.meta},s),!n);a++);delete t.results}else m(r.userChunk)&&(r.userChunk(t.results,s,t.file),delete t.results)}t.finished&&!n&&d(t.workerId,t.results)}function d(e,t){var r=R[e];m(r.userComplete)&&r.userComplete(t),r.terminate(),delete R[e]}function l(){throw"Not implemented."}function p(t){var r=t.data;if("undefined"==typeof S.WORKER_ID&&r&&(S.WORKER_ID=r.workerId),"string"==typeof r.input)e.postMessage({workerId:S.WORKER_ID,results:S.parse(r.input,r.config),finished:!0});else if(e.File&&r.input instanceof File||r.input instanceof Object){var n=S.parse(r.input,r.config);n&&e.postMessage({workerId:S.WORKER_ID,results:n,finished:!0})}}function _(e){if("object"!=typeof e)return e;var t=e instanceof Array?[]:{};for(var r in e)t[r]=_(e[r]);return t}function g(e,t){return function(){e.apply(t,arguments)}}function m(e){return"function"==typeof e}var v,k=!e.document&&!!e.postMessage,y=k&&/(\?|&)papaworker(=|&|$)/.test(e.location.search),b=!1,R={},w=0,S={};if(S.parse=t,S.unparse=r,S.RECORD_SEP=String.fromCharCode(30),S.UNIT_SEP=String.fromCharCode(31),S.BYTE_ORDER_MARK="",S.BAD_DELIMITERS=["\r","\n",'"',S.BYTE_ORDER_MARK],S.WORKERS_SUPPORTED=!k&&!!e.Worker,S.SCRIPT_PATH=null,S.LocalChunkSize=10485760,S.RemoteChunkSize=5242880,S.DefaultDelimiter=",",S.Parser=u,S.ParserHandle=o,S.NetworkStreamer=i,S.FileStreamer=s,S.StringStreamer=a,"undefined"!=typeof module&&module.exports?module.exports=S:m(e.define)&&e.define.amd?define(function(){return S}):e.Papa=S,e.jQuery){var C=e.jQuery;C.fn.parse=function(t){function r(){if(0==a.length)return void(m(t.complete)&&t.complete());var e=a[0];if(m(t.before)){var r=t.before(e.file,e.inputElem);if("object"==typeof r){if("abort"==r.action)return void n("AbortError",e.file,e.inputElem,r.reason);if("skip"==r.action)return void i();"object"==typeof r.config&&(e.instanceConfig=C.extend(e.instanceConfig,r.config))}else if("skip"==r)return void i()}var s=e.instanceConfig.complete;e.instanceConfig.complete=function(t){m(s)&&s(t,e.file,e.inputElem),i()},S.parse(e.file,e.instanceConfig)}function n(e,r,n,i){m(t.error)&&t.error({name:e},r,n,i)}function i(){a.splice(0,1),r()}var s=t.config||{},a=[];return this.each(function(){var t="INPUT"==C(this).prop("tagName").toUpperCase()&&"file"==C(this).attr("type").toLowerCase()&&e.FileReader;if(!t||!this.files||0==this.files.length)return!0;for(var r=0;r<this.files.length;r++)a.push({file:this.files[r],inputElem:this,instanceConfig:C.extend({},s)})}),r(),this}}y?e.onmessage=p:S.WORKERS_SUPPORTED&&(v=h(),document.body?document.addEventListener("DOMContentLoaded",function(){b=!0},!0):b=!0),i.prototype=Object.create(n.prototype),i.prototype.constructor=i,s.prototype=Object.create(n.prototype),s.prototype.constructor=s,a.prototype=Object.create(a.prototype),a.prototype.constructor=a}("undefined"!=typeof window?window:this);
\ No newline at end of file
morpheus.ContentRangeNetworkInputStream = function(url, options) {
var _this = this;
this.options = options || {};
this.url = url;
this.bytePosition = 0;
this.chunkSize = options.chunkSize || 1024 * 1024 * 10; // 50 MB
this.bytesRead = 0;
this.fileSize = -1;
this.read();
};
morpheus.ContentRangeNetworkInputStream.prototype = {
read : function() {
var _this = this;
var oReq = new XMLHttpRequest();
oReq.open('GET', this.url, true);
oReq.responseType = 'arraybuffer';
oReq.onload = function(oEvent) {
var arrayBuffer = oReq.response;
var byteArray = new Uint8Array(arrayBuffer);
var contentRange = oReq.getResponseHeader('Content-Range');
var index = contentRange.lastIndexOf('/');
_this.fileSize = parseInt(contentRange.substring(index + 1));
_this.bytePosition += byteArray.length;
_this.bytesRead += byteArray.length;
_this.options.bytes(byteArray);
if (_this.bytesRead < _this.fileSize) {
_this.read();
} else {
_this.options.complete();
}
};
var end = this.bytePosition + this.chunkSize - 1;
if (this.fileSize !== -1) {
end = Math.min(end, this.fileSize - 1);
}
// byte range is inclusive
oReq
.setRequestHeader('Range', 'bytes=' + this.bytePosition + '-'
+ end);
oReq.setRequestHeader('If-None-Match', 'webkit-no-cache'); //
// https://bugs.webkit.org/show_bug.cgi?id=82672
oReq.onerror = function() {
console.log('error');
};
oReq.send(null);
}
};
morpheus.FetchNetworkInputStream = function(url, options) {
var bytesRead = 0;
var fileSize = 0;
function pump(reader) {
return reader.read().then(function(result) {
if (result.done) {
options.complete();
return;
}
var chunk = result.value;
bytesRead += chunk.byteLength;
options.bytes(chunk);
return pump(reader);
});
}
fetch(url).then(function(res) {
// _this.fileSize = res.headers.get('Content-Length');
return pump(res.body.getReader());
});
};
morpheus.FileInputStream = function(file, options) {
var _this = this;
this.options = options || {};
this.reader = new FileReader();
this.reader.onloadend = function(evt) {
if (evt.target.readyState === FileReader.DONE) {
var arrayBuffer = evt.target.result;
var byteArray = new Uint8Array(arrayBuffer);
_this.bytePosition += byteArray.length;
_this.bytesRead += byteArray.length;
_this.options.bytes(byteArray);
if (_this.bytesRead < _this.fileSize) {
_this.read();
} else {
options.complete();
}
}
};
this.file = file;
this.bytePosition = 0;
this.chunkSize = options.chunkSize || 1024 * 1024 * 10; // 10 MB
this.bytesRead = 0;
this.fileSize = file.size;
this.read();
};
morpheus.FileInputStream.prototype = {
read : function() {
var _this = this;
var end = this.bytePosition + this.chunkSize;
end = Math.min(end, this.fileSize);
// byte
// range is
// exclusive
var blob = this.file.slice(this.bytePosition, end);
this.reader.readAsArrayBuffer(blob);
}
};
\ No newline at end of file
......@@ -7,42 +7,170 @@ morpheus.GctReader.prototype = {
return 'gct';
},
read: function (fileOrUrl, callback) {
var _this = this;
var name = morpheus.Util.getBaseFileName(morpheus.Util
.getFileName(fileOrUrl));
morpheus.BufferedReader.getArrayBuffer(fileOrUrl, function (err,
arrayBuffer) {
if (err) {
callback(err);
} else {
try {
callback(null, _this._read(name,
new morpheus.BufferedReader(new Uint8Array(
arrayBuffer))));
} catch (x) {
if (x.stack) {
console.log(x.stack);
var lineNumber = 0;
var version;
var numRowAnnotations = 1; // in addition to row id
var numColumnAnnotations = 0; // in addition to column id
var nrows = -1;
var ncols = -1;
var version = 2;
var rowMetadataNames = [];
var columnMetadataNames = [];
var rowMetadata = [[]];
var columnMetadata = [[]];
var dataColumnStart;
var matrix = [];
var dataMatrixLineNumberStart;
var columnIdFieldName = 'id';
var rowIdFieldName = 'id';
var columnNamesArray;
Papa.parse(fileOrUrl, {
delimiter: "\t", // auto-detect
newline: "", // auto-detect
header: false,
dynamicTyping: false,
preview: 0,
encoding: "",
worker: false,
comments: false,
step: function (result) {
if (lineNumber === 0) {
var text = result.data[0][0].trim();
if ('#1.2' === text) {
version = 2;
} else if ('#1.3' === text) {
version = 3;
} else {
console.log('Unknown version: assuming version 2');
}
} else if (lineNumber === 1) {
var dimensions = result.data[0];
if (version === 3) {
if (dimensions.length >= 4) {
nrows = parseInt(dimensions[0]);
ncols = parseInt(dimensions[1]);
numRowAnnotations = parseInt(dimensions[2]);
numColumnAnnotations = parseInt(dimensions[3]);
} else { // no dimensions specified
numRowAnnotations = parseInt(dimensions[0]);
numColumnAnnotations = parseInt(dimensions[1]);
}
} else {
nrows = parseInt(dimensions[0]);
ncols = parseInt(dimensions[1]);
if (nrows <= 0 || ncols <= 0) {
callback(
'Number of rows and columns must be greater than 0.');
}
}
dataColumnStart = numRowAnnotations + 1;
} else if (lineNumber === 2) {
columnNamesArray = result.data[0];
if (ncols === -1) {
ncols = columnNamesArray.length - numRowAnnotations - 1;
}
if (version == 2) {
var expectedColumns = ncols + 2;
if (columnNamesArray.length !== expectedColumns) {
callback('Expected ' + (expectedColumns - 2)
+ ' column names, but read '
+ (columnNamesArray.length - 2) + ' column names.');
}
}
var name = columnNamesArray[0];
var slashIndex = name.lastIndexOf('/');
if (slashIndex != -1 && slashIndex < (name.length - 1)) {
rowIdFieldName = name.substring(0, slashIndex);
columnIdFieldName = name.substring(slashIndex + 1);
}
rowMetadataNames.push(rowIdFieldName);
columnMetadataNames.push(columnIdFieldName);
for (var j = 0; j < ncols; j++) {
var index = j + numRowAnnotations + 1;
var columnName = index < columnNamesArray.length ? columnNamesArray[index]
: null;
columnMetadata[0].push(String(columnName));
}
for (var j = 0; j < numRowAnnotations; j++) {
var rowMetadataName = '' === columnNamesArray[1] ? 'description'
: columnNamesArray[j + 1];
rowMetadataNames.push(
rowMetadataName);
rowMetadata.push([]);
}
dataMatrixLineNumberStart = 3 + numColumnAnnotations;
} else { // lines >=3
var tokens = result.data[0];
if (lineNumber < dataMatrixLineNumberStart) {
var metadataName = tokens[0];
var v = [];
columnMetadata.push(v);
columnMetadataNames.push(metadataName);
for (var j = 0; j < ncols; j++) {
v.push(String(tokens[j + dataColumnStart]));
}
} else { // data lines
if (tokens[0] !== '') {
var array = new Float32Array(ncols);
matrix.push(array);
// we iterate to numRowAnnotations + 1 to include id row
// metadata field
for (var rowAnnotationIndex = 0; rowAnnotationIndex <= numRowAnnotations; rowAnnotationIndex++) {
var rowMetadataValue = tokens[rowAnnotationIndex];
rowMetadata[rowAnnotationIndex].push(
String(rowMetadataValue));
}
for (var columnIndex = 0; columnIndex < ncols; columnIndex++) {
var token = tokens[columnIndex + dataColumnStart];
array[columnIndex] = parseFloat(token);
}
}
}
callback(x);
}
}
});
lineNumber++;
},
complete: function () {
var dataset = new morpheus.Dataset({
name: morpheus.Util.getBaseFileName(morpheus.Util
.getFileName(fileOrUrl)),
rows: matrix.length,
columns: ncols,
array: matrix,
dataType: 'Float32'
});
for (var i = 0; i < rowMetadataNames.length; i++) {
dataset.getRowMetadata().add(rowMetadataNames[i]).array = rowMetadata[i];
}
for (var i = 0; i < columnMetadataNames.length; i++) {
dataset.getColumnMetadata().add(columnMetadataNames[i]).array = columnMetadata[i];
}
morpheus.MetadataUtil.maybeConvertStrings(dataset.getRowMetadata(), 1);
morpheus.MetadataUtil.maybeConvertStrings(dataset.getColumnMetadata(),
1);
callback(null, dataset);
},
error: function (err) {
callback(err);
},
download: !(fileOrUrl instanceof File),
skipEmptyLines: false,
chunk: undefined,
fastMode: true,
beforeFirstChunk: undefined,
withCredentials: undefined
});
},
_read: function (datasetName, reader) {
var tab = /\t/;
var versionLine = $.trim(reader.readLine());
if (versionLine === '') {
throw new Error('Missing version line');
}
var tab = /\t/;
var dimensionsLine = reader.readLine();
if (dimensionsLine == null) {
throw new Error('No dimensions specified');
}
var columnNamesLine = reader.readLine();
if (columnNamesLine == null) {
throw new Error('No column annotations');
}
var version = 2;
if ('#1.2' === versionLine) {
version = 2;
......@@ -51,6 +179,10 @@ morpheus.GctReader.prototype = {
} else {
console.log('Unknown version: assuming version 2');
}
var dimensionsLine = reader.readLine();
if (dimensionsLine == null) {
throw new Error('No dimensions specified');
}
// <numRows> <tab> <numCols>
var dimensions = dimensionsLine.split(/[ \t]/);
var numRowAnnotations = 1; // in addition to row id
......@@ -75,6 +207,10 @@ morpheus.GctReader.prototype = {
'Number of rows and columns must be greater than 0.');
}
}
var columnNamesLine = reader.readLine();
if (columnNamesLine == null) {
throw new Error('No column annotations');
}
var columnNamesArray = columnNamesLine.split(tab);
if (ncols === -1) {
......@@ -281,5 +417,28 @@ morpheus.GctReader.prototype = {
return dataset;
}
},
_read: function (fileOrUrl, callback) {
var _this = this;
var name = morpheus.Util.getBaseFileName(morpheus.Util
.getFileName(fileOrUrl));
morpheus.BufferedReader.getArrayBuffer(fileOrUrl, function (err,
arrayBuffer) {
if (err) {
callback(err);
} else {
try {
callback(null, _this._read(name,
new morpheus.BufferedReader(new Uint8Array(
arrayBuffer))));
} catch (x) {
if (x.stack) {
console.log(x.stack);
}
callback(x);
}
}
});
}
};
morpheus.LineReader = function(fileOrUrl, options) {
var _this = this;
this.str = '';
var isString = _.isString(fileOrUrl);
if (typeof TextDecoder !== 'undefined') {
var textDecoder = new TextDecoder();
this.decoder = function(buf, start, end) {
return textDecoder.decode(buf.subarray(start, end));
};
} else {
this.decoder = function(buf, start, end) {
// TODO convert in chunks
var s = '';
for (var i = start; i < end; i++) {
s += String.fromCharCode(buf[i]);
}
return s;
};
}
var inputStreamOptions = {
bytes : function(buf) {
_this.str += _this.decoder(buf, 0, buf.length);
var split = morpheus.LineReader._split(_this.str);
if (split.lines.length > 0) {
var lines = split.lines;
for (var i = 0, nlines = lines.length; i < nlines; i++) {
var line = lines[i];
if (line !== '') {
options.line(line);
}
}
_this.str = split.remainder;
}
},
complete : function() {
options.complete();
}
};
isString ? new morpheus.FetchNetworkInputStream(fileOrUrl,
inputStreamOptions) : new morpheus.FileInputStream(fileOrUrl,
inputStreamOptions);
};
morpheus.LineReader._split = function(str) {
var lines = [];
var start = 0;
var length = str.length;
for (var i = 0; i < length; i++) {
var c = str[i];
if (c === '\n' || c === '\r') {
var end = i;
if ((i < length - 1) && (str[i + 1] === '\n')) { // check for
// \r\n
i++;
}
lines.push(str.substring(start, end));
start = i + 1;
}
}
return {
lines : lines,
remainder : start <= length ? str.substring(start, length) : ''
};
};
// code taken from KineticJS
morpheus.Events = function() {
morpheus.Events = function () {
};
morpheus.Events.prototype = {
/**
* Pass in a string of events delimmited by a space to bind multiple events
* at once such as 'mousedown mouseup mousemove'. Include a namespace to
* bind an event by name such as 'click.foobar'.
*
*
* @param {String}
* evtStr e.g. 'click', 'mousedown touchstart', 'mousedown.foo
* touchstart.foo'
* @param {Function}
* handler The handler function is passed an event object
*/
on : function(evtStr, handler) {
on: function (evtStr, handler) {
if (!handler) {
throw Error('Handler not specified');
}
......@@ -35,19 +35,28 @@ morpheus.Events.prototype = {
this.eventListeners[baseEvent] = [];
}
this.eventListeners[baseEvent].push({
name : name,
handler : handler
name: name,
handler: handler
});
}
return this;
},
getListeners: function () {
if (!this.eventListeners) {
this.eventListeners = {};
}
return this.eventListeners;
},
setListeners: function (eventListeners) {
this.eventListeners = eventListeners;
},
/**
* Fire an event.
*
*
* @param eventType
* @param evt
*/
trigger : function(eventType, evt) {
trigger: function (eventType, evt) {
if (!this.eventListeners) {
this.eventListeners = {};
}
......@@ -62,7 +71,7 @@ morpheus.Events.prototype = {
if (events) {
var len = events.length;
for (var i = 0; i < len; i++) {
events[i].handler.apply(this, [ evt ]);
events[i].handler.apply(this, [evt]);
}
}
return this;
......@@ -73,11 +82,11 @@ morpheus.Events.prototype = {
* mouseup mousemove'. include a namespace to remove an event binding by
* name such as 'click.foobar'. If you only give a name like '.foobar', all
* events in that namespace will be removed.
*
*
* @param {String}
* evtStr e.g. 'click', 'mousedown.foo touchstart', '.foobar'
*/
off : function(evtStr, handler) {
off: function (evtStr, handler) {
if (!this.eventListeners) {
this.eventListeners = {};
}
......@@ -105,14 +114,14 @@ morpheus.Events.prototype = {
}
return this;
},
_off : function(type, name, handler) {
_off: function (type, name, handler) {
var evtListeners = this.eventListeners[type], i, evtName;
for (i = 0; i < evtListeners.length; i++) {
evtName = evtListeners[i].name;
// check if an event name is not specified, or if one is specified,
// it matches the current event name
if ((!name || evtName === name)
&& (handler == null || handler == evtListeners[i].handler)) {
&& (handler == null || handler == evtListeners[i].handler)) {
evtListeners.splice(i, 1);
if (evtListeners.length === 0) {
delete this.eventListeners[type];
......@@ -121,5 +130,5 @@ morpheus.Events.prototype = {
i--;
}
}
},
};
\ No newline at end of file
}
};
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment