import { types , getEnv , getParent } from 'mobx-state-tree' ; import { openLocation } from '@jbrowse/core/util/io' ; import { getSession } from '@jbrowse/core/util' ; const IMPORT_SIZE_LIMIT = 30000000 ; const fileTypes = [ 'CSV' , 'TSV' , 'VCF' , 'BED' , 'BEDPE' , 'STAR-Fusion' ] ; const fileTypeParsers = { CSV : ( ) => import ( '../importAdapters/ImportUtils' ) . then ( r => r . parseCsvBuffer ) , TSV : ( ) => import ( '../importAdapters/ImportUtils' ) . then ( r => r . parseTsvBuffer ) , VCF : ( ) => import ( '../importAdapters/VcfImport' ) . then ( r => r . parseVcfBuffer ) , BED : ( ) => import ( '../importAdapters/BedImport' ) . then ( r => r . parseBedBuffer ) , BEDPE : ( ) => import ( '../importAdapters/BedImport' ) . then ( r => r . parseBedPEBuffer ) , 'STAR-Fusion' : ( ) => import ( '../importAdapters/STARFusionImport' ) . then ( r => r . parseSTARFusionBuffer ) , } ; const fileTypesRegexp = new RegExp ( `\\.(${ fileTypes . join ( '|' ) })(\\.gz)?$` , 'i' ) ; const ImportWizard = types . model ( 'SpreadsheetImportWizard' , { fileType : types . optional ( types . enumeration ( fileTypes ) , 'CSV' ) , hasColumnNameLine : true , columnNameLineNumber : 1 , selectedAssemblyName : types . maybe ( types . string ) , } ) . volatile ( ( ) => ( { fileTypes , fileSource : undefined as any , error : undefined as unknown , loading : false , } ) ) . views ( self => ( { get isReadyToOpen ( ) { return ( ! self . error && self . fileSource && ( self . fileSource . blobId || self . fileSource . localPath || self . fileSource . uri ) ) ; } , get canCancel ( ) { return getParent ( self ) . readyToDisplay ; } , get fileName ( ) { return ( self . fileSource . uri || self . fileSource . localPath || ( self . fileSource . blobId && self . fileSource . name ) ) ; } , get requiresUnzip ( ) { return this . fileName . endsWith ( 'gz' ) ; } , isValidRefName ( refName , assemblyName ? ) { return getSession ( self ) . assemblyManager . isValidRefName ( refName , assemblyName ) ; } , } ) ) . actions ( self => ( { setSelectedAssemblyName ( s ) { self . selectedAssemblyName = s ; } , setFileSource ( newSource ) { self . fileSource = newSource ; self . error = undefined ; if ( self . fileSource ) { const name = self . fileName ; if ( name ) { const match = fileTypesRegexp . exec ( name ) ; if ( match && match [ 1 ] ) { if ( match [ 1 ] === 'tsv' && name . includes ( 'star-fusion' ) ) { self . fileType = 'STAR-Fusion' ; } else { self . fileType = match [ 1 ] . toUpperCase ( ) ; } } } } } , toggleHasColumnNameLine ( ) { self . hasColumnNameLine = ! self . hasColumnNameLine ; } , setColumnNameLineNumber ( newnumber ) { if ( newnumber > 0 ) { self . columnNameLineNumber = newnumber ; } } , setFileType ( typeName ) { self . fileType = typeName ; } , setError ( error ) { console . error ( error ) ; self . loading = false ; self . error = error ; } , setLoaded ( ) { self . loading = false ; self . error = undefined ; } , cancelButton ( ) { self . error = undefined ; getParent ( self ) . setDisplayMode ( ) ; } , async import ( assemblyName ) { if ( ! self . fileSource ) { return ; } if ( self . loading ) { throw new Error ( 'Cannot import, load already in progress' ) ; } self . selectedAssemblyName = assemblyName ; self . loading = true ; const type = self . fileType as keyof typeof fileTypeParsers ; const typeParser = await fileTypeParsers [ type ] ( ) ; if ( ! typeParser ) { throw new Error ( `cannot open files of type '${ self . fileType }'` ) ; } const { unzip } = await import ( '@gmod/bgzf-filehandle' ) ; const filehandle = openLocation ( self . fileSource , getEnv ( self ) . pluginManager ) ; try { await filehandle . stat ( ) . then ( stat => { if ( stat . size > IMPORT_SIZE_LIMIT ) { throw new Error ( `File is too big. Tabular files are limited to at most ${ ( IMPORT_SIZE_LIMIT / 1000 ) . toLocaleString ( ) }kb.` ) ; } } ) ; } catch ( e ) { console . warn ( e ) ; } await filehandle . readFile ( ) . then ( buffer => ( self . requiresUnzip ? unzip ( buffer ) : buffer ) ) . then ( buffer => typeParser ( buffer , self ) ) . then ( spreadsheet => { this . setLoaded ( ) ; getParent ( self ) . displaySpreadsheet ( spreadsheet ) ; } ) ; } , } ) ) ; export default ImportWizard ;