# include( "Common.jl" ); # ******************************************************************* # Load Spectra and return a matrix # fileName: Full name path # ******************************************************************* """ LoadImzml( fileName ) Load an imzML file as a matrix. Each column stores x-pixel position, y-pixel position, x-axis data and y-axis data. # Arguments * `fileName`: Full path name of the imzML file # Examples ```julia # Load DESI MSI Carcinoma image data spectra = LoadImzml( "80TopL, 50TopR, 70BottomL, 60BottomR-centroid.imzML" ) size( spectra ) (4, 18632) ``` """ function LoadImzml( fileName ) # Open file handles if !isfile(fileName) error("provided path is not a file") end if endswith(fileName, ".imzML") stream = open(fileName) hIbd = open(replace(fileName, ".imzML" => ".ibd")) else stream = open(fileName * ".imzML") hIbd = open(fileName * ".ibd") end # Get axes types and image dimensions axis = AxesConfigImg( stream ) imgDim = GetImgDimensions( stream ) format = [ axis[1].Format, axis[2].Format ] # Locate spectrum attributes start = position( stream ) attr = GetSpectrumAttributes( stream, hIbd ) # Load spectra seek( stream, start ) spectra = LoadImgData( stream, hIbd, attr, imgDim[3], format ) close( stream ) close( hIbd ) return spectra end # ******************************************************************* # Get axes value type # ******************************************************************* function AxesConfigImg( stream ) # Locate which axis is defined at first tag = FindTag( stream, r"^\s*<(referenceableParamGroup )" ) value = GetAttribute( tag.captures[1], "intensityArray" ) order = 1 + ( value !== nothing ) # Read first axis configuration axis = Array{ SpecDim, 1 }( undef, 2 ) axis[ order ] = ConfigureSpecDim( stream ) # Read second axis configuration FindTag( stream, r"^\s*<(referenceableParamGroup )" ) axis[ xor( order,3 ) ] = ConfigureSpecDim( stream ) return axis end # ******************************************************************* # Get vector's storage options and image dimensions # ******************************************************************* function GetImgDimensions( stream ) # Looks for "scanSettings" tag # FindImgTag( stream, "scanSettings" ) FindTag( stream, r"^\s*<(scanSettings )" ) # Initial values for dimension retrieve n = 2 dim = [ 0, 0, 0 ] currLine = "" matchInfo = RegexMatch while( n > 0 ) # Next field currLine = readline( stream ) matchInfo = match( r"^\s*<(cvParam)", currLine ) index = length( matchInfo.captures[1] ) matchInfo = GetAttribute( currLine[index:end], "accession" ) # Get axis identity if( matchInfo.captures[1] == "IMS:1000042" # max X || matchInfo.captures[1] == "IMS:1000043" ) # max Y # Read dimension's pixels axis = matchInfo.captures[1][end] - '1' index += matchInfo.offsets[1] + length( matchInfo.captures[1] ) matchInfo = GetAttribute( currLine[index:end], "value" ) dim[axis] = parse( Int32, matchInfo.captures[1] ) n -= 1 end end # Load stored spectra counter matchInfo = FindTag( stream, r"^\s*