import { parseTsvBuffer , ParseOptions } from './ImportUtils' ; function parseSTARFusionBreakpointString ( str ) { const fields = str . split ( ':' ) ; const refName = fields [ 0 ] ; const pos = parseInt ( fields [ 1 ] , 10 ) ; const strand = fields [ 2 ] === '-' ? - 1 : 1 ; return { refName , pos , strand } ; } const numericColumns = { SpanningFragCount : true , FFPM : true , LeftBreakEntropy : true , RightBreakEntropy : true , JunctionReadCount : true , } ; export async function parseSTARFusionBuffer ( buffer , options ) { const data = await parseTsvBuffer ( buffer , { hasColumnNameLine : true , columnNameLineNumber : 1 , selectedAssemblyName : options . selectedAssemblyName , isValidRefName : ( ) => false , } ) ; data . columns [ 0 ] . name = data . columns [ 0 ] . name . replace ( '#' , '' ) ; data . columns . forEach ( col => { if ( numericColumns [ col . name ] ) { col . dataType = { type : 'Number' } ; } } ) ; data . rowSet . rows . forEach ( ( row , rowNumber ) => { const featureData = { } ; row . cells . forEach ( ( { text } , columnNumber ) => { const column = data . columns [ columnNumber ] ; if ( column . name === 'LeftBreakpoint' && text ) { const { refName , pos , strand } = parseSTARFusionBreakpointString ( text ) ; featureData . refName = refName ; featureData . start = pos ; featureData . end = pos ; featureData . strand = strand ; } else if ( column . name === 'RightBreakpoint' && text ) { const { refName , pos , strand } = parseSTARFusionBreakpointString ( text ) ; featureData . mate = { refName , start : pos , end : pos , strand , } ; } else if ( text && numericColumns [ column . name ] ) { featureData [ column . name ] = parseFloat ( text ) ; } else { featureData [ column . name ] = text ; } } ) ; featureData . uniqueId = `sf-${ rowNumber + 1 }` ; row . extendedData = { feature : featureData , } ; } ) ; return data ; }