Source code for MSQuant: rawDataFileHandling.vb, MSQuant/msquant/src/main/massbase/rawDataFileHandling.vb.

Table of contents page.

Home page for MSQuant.

'****************************************************************************
'* Copyright (C) 2004 Peter Mortensen and Matthias Mann                     *
'* This file is part of MSQuant.                                            *
'*                                                                          *
'* MSQuant is distributed under the terms of                                *
'* the GNU General Public License. See src/COPYING.TXT or                   *
'* <http://www.gnu.org/licenses/gpl.txt> for details.                       *
'*                                                                          *
'* MSQuant is free software; you can redistribute it                        *
'* and/or modify it under the terms of the GNU                              *
'* General Public License as published by the Free                          *
'* Software Foundation; either version 2 of the                             *
'* License, or (at your option) any later version.                          *
'*                                                                          *
'* MSQuant is distributed in the hope that it will be                       *
'* useful, but WITHOUT ANY WARRANTY; without even the                       *
'* implied warranty of MERCHANTABILITY or FITNESS FOR                       *
'* A PARTICULAR PURPOSE.  See the GNU General Public                        *
'* License for more details.                                                *
'*                                                                          *
'* You should have received a copy of the GNU General                       *
'* Public License along with MSQuant; if not, write to                      *
'* the Free Software Foundation, Inc., 59 Temple                            *
'* Place, Suite 330, Boston, MA  02111-1307  USA                            *
'*                                                                          *
'* Purpose: Holds Class rawDataFileHandling, see below for documentation.   * 
'*                                                                          *
'****************************************************************************

'****************************************************************************
'*                               CEBI                                       *
'*                    Software Development Group                            *
'*                         Peter Mortensen                                  *
'*                E-mail: NUKESPAMMERSdrmortensen@get2netZZZZZZ.dk          *
'*                 WWW: http://www.cebi.sdu.dk/                             *
'*                                                                          *
'*  Program for post-processing of result from search in mass               *
'*    spectrometric data.                                                   *
'*                                                                          *
'*    FILENAME:   rawDataFileHandling.vb                                    *
'*    TYPE:       VISUAL_BASIC                                              *
'*                                                                          *
'* CREATED: PM 2003-06-04   Vrs 1.0.                                        *
'* UPDATED: PM 2007-07-10   Note in Future section about 3 nearly           *
'*                          redundant structures.                           *
'*                                                                          *
'****************************************************************************

'Future: 
' 1. Perhaps include handling of association of raw data files
'    and Mascot result files. 
' 2. Adding the same file a second time may not result in it being
'    added to the list.
' 3. Perhaps merge the ***three*** different structures that have some
'    information about a spectrum: spectrumSpecStructure,
'    spectrumSpecificationStructure, spectrumInfoStructure2,
' 4. Perhaps merge retentionTimeRangeStructure and
'    spectrumRangeStructure.
'
' 5. Perhaps do some explicit cleaning up of COM objects 
'    using Marshal.ReleaseComObject.
'
'    Ref'ed in <http://news.zdnet.co.uk/software/0,1000000121,2110599-3,00.htm?r=1021>
'    On the other hand we have logic to reuse the same 
'    instance of the COM object and may not need it. Contra: 
'    could it solve the general memory problem? Perhaps the COM 
'    objects somehow make a lot of string or data points array 
'    hand around?
'
' 6. 


Option Strict On
Option Explicit On 

Imports System.IO 'For "File".

'Changed PM_FAST_SERIALISATION_AVOID_DOTNET 2006-12-28
Imports SimmoTech.Utils.Serialization 'For SerializationWriter


'****************************************************************************
'd$ <summary>
'd$   Purpose: Namespace for lower layers of mass spectrometric
'd$            applications: raw data file handling, descriptive statistics,
'd$            fragment masses, digestion, file associations, etc.
'd$   <see cref="T:VBXMLDoc.CVBXMLDoc" />.
'd$   <isUnitTest></isUnitTest>
'd$   <applicationname>test_rawDataFileHandling</applicationname>
'd$   <author>Peter Mortensen</author>
'd$   <seealso>http://www.cebi.sdu.dk/</seealso>
'd$   <codetype>PLATFORM independent / GUI</codetype>
'd$ </summary>
Namespace massSpectrometryBase


    'Do we need this anymore ??
    '****************************************************************************
    'd$ <summary>
    'd$   Purpose: 
    'd$     Description of a set. It is e.g. used for describing how many
    'd$     spectra there are within a particular retention time interval.
    'd$ </summary>
    Public Enum setEnum
        enumEmptySet = 211
        enumExactlyOneItemInSet
        enumSeveralItemsInSet
        enumIllegalSetDescription
    End Enum


    'Changed PM_REFACTOR_COMVERSIONS 2007-07-24
    '****************************************************************************
    'd$ <summary>
    'd$   Purpose: 
    'd$     
    'd$     
    'd$ </summary>
    Public Enum COMversionEnum
        enumAnalystQS10 = 349
        enumAnalystQS11
        enumAnalystQS20

        enumXCalibur143RC
        enumXCalibur20
        enumXCalibur20SR1
        'enumXCalibur20SR2  same SR1
        'enumXCalibur205    same SR1

        enumMassLynx40
        enumMassLynx41
    End Enum 'COMversionEnum


    '****************************************************************************
    'd$ <summary>
    'd$   Purpose: 
    'd$     Encapsulates representation of a file specification
    'd$  
    'd$   Future:
    'd$     1. Should it be a class instead?
    'd$     2. Constructors in structures? If yes: constr. with 1 argument (convenient
    'd$        and safe initialisation).
    'd$     3. ID or tag to identify a raw file.
    'd$  
    'd$   <see cref="T:VBXMLDoc.CVBXMLDoc" />.
    'd$   <applicationname>test_rawDataFileHandling</applicationname>
    'd$   <author>Peter Mortensen</author>
    'd$   <seealso>http://www.cebi.sdu.dk/</seealso>  
    'd$   <codetype>PLATFORM independent</codetype>
    'd$ </summary>
    <Serializable()> _
    Public Structure fileSpecStructure

        'Note: if fields are added addToStream() should be updated!!!


        'Changed PM_FAST_SERIALISATION_READ 2007-01-02
        '****************************************************************************
        '*    For fileSpecStructure.                                                *
        '****************************************************************************
        Public Shared Function readFromStream( _
          ByRef anInReader As SerializationReader) _
          As fileSpecStructure

            Dim toReturn As fileSpecStructure

            toReturn.path = anInReader.ReadString()
            toReturn.tag2 = anInReader.ReadInt32()
            toReturn.OStoken = anInReader.ReadInt32()

            Return toReturn
        End Function 'readFromStream. For fileSpecStructure.


        'Changed PM_FAST_SERIALISATION_AVOID_DOTNET 2006-12-28
        '****************************************************************************
        '*    For fileSpecStruct.                                                   *
        '****************************************************************************
        Public Sub addToStream(ByRef anInOutWriter As SerializationWriter)
            anInOutWriter.Write(path)
            anInOutWriter.Write(tag2)
            anInOutWriter.Write(OStoken)
        End Sub 'addToStream


        'd$ <summary>Some kind of path: current representation</summary>
        Dim path As String 'Current representation

        'd$ <summary>Some number that the client uses to identify a
        'd$          particular raw data file.</summary>
        Dim tag2 As Integer

        'd$ <summary>XYZ.</summary>
        Dim OStoken As Integer 'What is this?

    End Structure 'fileSpecStructure


    '****************************************************************************
    'd$ <summary>
    'd$   Purpose: 
    'd$     Organises values specifying a particular spectrum
    'd$ </summary>
    Public Structure spectrumSpecStructure
        Dim specNum As Integer 'If zero: specified by retentionTime. The meaning
        '  this field is xyz.
        '  Note: spectrum number is what is called cycle number in the analyst world
        Dim retentionTime_secs As Double 'If zero: specified by specNum


        'Changed PM_REFACTOR 2007-07-11
        'At least for MS3, but may also in the future come from
        'merged structures spectrumSpecificationStructure and spectrumInfoStructure2.
        Dim supposedPrecursorMass As Double 'Typically a mass from a MS2 spectrum, a
        '  precursor for a MS3 spectrum.
        Dim experimentNumber As Integer

    End Structure 'spectrumSpecStructure


    '****************************************************************************
    'd$ <summary>
    'd$   Purpose: 
    'd$     Organises values specifying a particular spectrum
    'd$ </summary>
    'd$ 
    'd$ Should we merge this structure and spectrumSpecStructure??
    'd$ 
    Public Structure spectrumSpecificationStructure
        Dim spectrumType As spectrumTypeEnum

        Dim experimentNumber As Integer 'This comes from the Analyst
        '  world. The model is: an LCMS run consists of many cycles (e.g 2667
        '  cycles). A cycle starts with an MS spectrum whose experiment number 
        '  is 0. The next experiment (spectrum) within a cycle is an MS-MS
        '  experiment (spectrum) and the experiment is 1. There can be more than
        '  one MS-MS in cycle; with experiment numbers 2, 3, 4, ....
        '
        '  Perhaps we should begin with 0 for a particular spectrum type??
    End Structure 'spectrumSpecStructure


    'Not currently used...
    Public Structure spectrumInfoStructure2
        'd$ <summary>Name of spectrum(?)</summary>
        Dim title As String 'Current representation

        'Use a spectrumSpecStructure to reduce redundancy??

        'd$ <summary>Retention time for the spectrum.</summary>
        Dim retentionTime As Integer

        'd$ <summary>Spectrum number in the current raw file</summary>
        Dim spectNum As Integer
    End Structure 'spectrumInfoStructure


    '****************************************************************************
    'd$ <summary>
    'd$   Purpose: 
    'd$     Organises values specifying a range of retention time. For easier
    'd$     parameter passsing, better maintainability and better internal documentation.
    'd$   
    'd$   Future:
    'd$     1. constructors for shorter/better client side code.
    'd$ </summary>
    Public Structure retentionTimeRangeStructure
        Dim startTime_secs As Double
        Dim endTime_secs As Double
    End Structure 'retentionTimeRangeStructure


    '****************************************************************************
    'd$ <summary>
    'd$   Purpose: 
    'd$     XYZ.
    'd$   
    'd$   Future:
    'd$     1. 
    'd$ </summary>
    Public Structure spectrumRangeStructure
        Dim startSpectrum As Integer
        Dim endSpectrum As Integer

        'Note: as of 2004-11-25 not implemented for Micromass...
        Dim firstRetentionTimeSeconds As Double
        Dim lastRetentionTimeSeconds As Double
    End Structure 'spectrumRangeStructure


    'dataPointsInfoStructure moved to signal.vb

    '****************************************************************************
    'd$ <summary>
    'd$   Purpose: 
    'd$     Abstract base class for handling raw MS datafiles.
    'd$     
    'd$     Present an interface to the outside that several datafiles 
    'd$     can be accessed at the same time even if the underlying 
    'd$     software can not handle it (this is achieved by caching of 
    'd$     datapoints).
    'd$     
    'd$     Operations include: caching of data points.
    'd$     
    'd$     It is not purely abstract - it contains all the caching logic.
    'd$     
    'd$     Derived classes support vendor specific datafiles, e.g. 
    'd$     Sciex/WIFF files or Finnegan/Xcalibur files.
    'd$ 
    'd$     Possible future: 
    'd$       1. Some kind of background loading of data (for caching)
    'd$  
    'd$   <see cref="T:VBXMLDoc.CVBXMLDoc" />.
    'd$   <applicationname>test_rawDataFileHandling</applicationname>
    'd$   <author>Peter Mortensen</author>
    'd$   <seealso>http://www.cebi.sdu.dk/</seealso>  
    'd$   <codetype>PLATFORM independent</codetype>
    'd$  'd$  'd$ </summary>
    Public MustInherit Class rawDataFileHandling

        'Dim mRawFileList As ArrayList
        Dim mRawFileList As Hashtable

        Dim mCurrentSpectrumNumber As Integer

        Dim mTag As Integer 'Last tag specified by a client. Tag: specified
        '  a raw data file.

        'Changed PM_IRINA_SAMPLE_NUMBER_TROUBLE 2003-12-02
        Protected mSampleNumberInRawFile As Integer 'Is an Analyst oriented feature. Is
        '  there something similar for Finnegan?. It has the following layered
        '  structure (all one-to-many relations):
        '    
        ' <to be completed/verified>
        '    File
        '      Sample
        '        Cycle  (usually one MS spectrum and one or more MS-MS spectra.)
        '
        '
        '
        '  What about period??

        Protected mOpenRawCalls As Integer 'For run-time detection of client protocol errors.

        'Changed PM_REFACTOR_FINNEGAN_MSMS 2003-08-18
        Protected mExperimentInfo As spectrumSpecificationStructure

        Protected mReadOnly_CurrentFile As fileSpecStructure

        'Changed PM_GENERALISED_SPECTRUMCLASSIFICATION 2005-03-14
        Protected mSpectrumClassifier As spectrumClassifier

        'Changed PM_PHOSPHO_SCORING 2005-08-12
        Private mCurSpectrumClassification As SpectrumClassificationStructure 'For
        '  setSpectrumCONV().

        Protected mCOMversion As COMversionEnum


        '****************************************************************************
        '*  P U B L I C   S E C T I O N                                             *


        '****************************************************************************
        '*  SUBROUTINE NAME:   New                                                  *
        'd$ <summary>Constructor</summary>
        'd$    to add/parameters: anInSpectrumClassifier. Is allowed to be nothing.
        Public Sub New(ByRef anInSpectrumClassifier As spectrumClassifier, _
          ByVal aCOMversion As COMversionEnum)

            MyBase.New() 'Is this necessary? Yes!

            'InitializeComponent()    Is this necessary??

            'Note: lazy instantiation, mRawFileList is NOT created in constructor.

            mCurrentSpectrumNumber = -1  'For error detection
            mTag = -1 'For error detection
            mOpenRawCalls = 0

            'Changed PM_IRINA_SAMPLE_NUMBER_TROUBLE 2003-12-02
            mSampleNumberInRawFile = 1 'Default if not set. Should we require
            '  it to be always set by the client???

            mExperimentInfo.experimentNumber = -1 'Invalid value for error detection.
            mExperimentInfo.spectrumType = spectrumTypeEnum.enumIsUndefinedSpectrumType

            'Changed PM_GENERALISED_SPECTRUMCLASSIFICATION 2005-03-14
            mSpectrumClassifier = anInSpectrumClassifier

            'Changed PM_COMVERSIONS 2007-07-24
            mCOMversion = aCOMversion


            mCurSpectrumClassification.mainClassification = _
              spectrumTypeEnum.enumIsUndefinedSpectrumType

            mCurSpectrumClassification.MSsubType = _
              spectrumSubTypeEnum.enumST_DoesNotApply
        End Sub 'New()


        'Changed PM_JA_FINNEGAN_ASSERT_RESOURCE_LEAK 2003-12-19
        '****************************************************************************
        '*  SUBROUTINE NAME:   doCleanUp                                            *
        'd$ <summary>Sort of destructor. For clients to signal that resources
        'd$          can be freed up. Note: this is essential for using Finnegan data.
        'd$   </summary>
        Public MustOverride Sub doCleanUp()


        '****************************************************************************
        '*  SUBROUTINE NAME:   MaybeToBeChanged_MSMSretentionTime                   *
        'd$ <summary>xyz
        'd$   </summary>
        Public MustOverride Function MaybeToBeChanged_MSMSretentionTime( _
          ByVal aMSMSspecification As Integer, _
          ByVal aExtraNumber As Integer) As Double


        '****************************************************************************
        '*  SUBROUTINE NAME: AddRawFileRef                                          *
        'd$ <summary>
        'd$   Purpose: Add a reference to a raw datafile to the list that this
        'd$            object handles/hides (essentially a path of some sort -
        'd$            full or partial??). Second level initialisation, defining
        'd$            list of raw files.
        'd$   <see cref="T:VBXMLDoc.CVBXMLDoc" />.
        'd$ </summary>
        'd$ <param name="RemoveMode">
        'd$   Parameter of type <see cref="T:System.Object" />
        'd$ </param>
        'd$ <param name="aPath">
        'd$   Parameter of type <see cref="T:Extensibility.ex_ConnectMode" />.
        'd$   For now it is a full path, but may be replaced by 
        'd$   on object that hides full paths and partial paths and 
        'd$   perhaps even works with file system reference number 
        'd$   like on a Macintosh. There may even be a class for 
        'd$   this in the .NET framework class library.
        'd$ </param>
        'd$ <remarks>
        'd$   <para>
        'd$   </para>
        'd$   <para>
        'd$   </para>
        'd$   <seealso cref="E:EnvDTE.BuildEvents.OnBuildDone" /> event. This
        'd$ </remarks>
        Public Overridable Sub AddRawFileRef(ByVal aFileSpec As fileSpecStructure)

            'What about multiple of the exactly the same path? Should we check for that?

            'Lazy instantiation
            If mRawFileList Is Nothing Then
                'mRawFileList = New ArrayList()
                mRawFileList = New Hashtable
            End If
            'mRawFileList.Add(aFileSpec)

            'Possible to check:
            ' 1. Uniqueness of IDs.
            ' 2. Uniqueness of paths.
            ' 3. Existence of file

            'Would be so much more compact in Perl!
            If mRawFileList.Contains(aFileSpec.tag2) Then 'What is the
                '  difference between Contains and ContainsKey?.
                Trace.Assert(False, "PIL ASSERT. Non-unique key, " _
                  & aFileSpec.tag2 & ", for path " & aFileSpec.path & ".")
            Else
                mRawFileList.Add(aFileSpec.tag2, aFileSpec.path)
            End If
        End Sub 'AddRawFileRef


        'Changed PM_REFACTOR 2003-11-05
        'Changed PM_REFACTOR 2003-09-23
        'Utility
        '****************************************************************************
        '*    <placeholder for header>                                              *
        '****************************************************************************
        Public Shared Function getFullRawFilePath( _
          ByRef anInRawFilesMap() As massSpectrometryBase.fileSpecStructure, _
          ByVal aRawFileID As Integer) As String

            'Changed PM_SEVERALWIFFS_EXPORT 2003-08-29
            'Iteration is slow, but there are very few elements in aRawFilesMap.
            Dim len2 As Integer = anInRawFilesMap.Length()
            Dim j As Integer
            Dim fullRawFilePath As String = Nothing 'Keep compiler happy.
            For j = 0 To len2 - 1
                Dim tag As Integer = anInRawFilesMap(j).tag2
                If aRawFileID = tag Then
                    fullRawFilePath = anInRawFilesMap(j).path
                    'How to break?
                End If
            Next j
            Return fullRawFilePath
        End Function 'getFullRawFilePath


        'Later: 
        '  1. SetTag.   set mTag
        '  2. DataPoints without tag
        '  3. 


        '****************************************************************************
        '*  SUBROUTINE NAME:   newCurrentFile                                       *
        'd$ <summary> Selects current file. For use of subsequent calls that
        'd$           assumes a current file.
        'd$ </summary>
        'd$ <param name="aStartRetentionTime_Seconds">
        'd$   some parameter <see cref="T:xyz.abc" />
        'd$ </param>
        Public Sub newCurrentFile(ByVal aTag As Integer)



            'Changed PM_QTSAR_TROUBLE 2008-01-16. "Or True": test only!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

            If aTag <> mTag Then 'That is: no need to open a file if it is
                '  already open...

                Dim fs As fileSpecStructure = Nothing 'Keep compiler happy.
                If mRawFileList.Contains(aTag) Then
                    fs.tag2 = aTag
                    fs.path = CStr(mRawFileList.Item(aTag))

                    mReadOnly_CurrentFile = fs

                    Trace.Assert(File.Exists(fs.path), "PIL ASSERT. File does not exist, " _
                      & fs.path & ".")
                Else
                    Trace.Assert(False, "PIL ASSERT. Key is not in hash. Key=" _
                      & aTag & ".")
                End If

                'DirectCast(mRawFileList.Item(0), fileSpecStruct))

                Me.openRawFile(fs) 'Derived classes implements.....

                mTag = aTag
            Else
                'File already open. Nothing to do.
                Dim peter45 As Integer = 45
            End If
        End Sub 'newCurrentFile


        '****************************************************************************
        '*  SUBROUTINE NAME:   SpectrumNumber2RetentionTime                         *
        'd$ <summary>   For current(??) raw file: convert from spectrum number to
        'd$             retention time (sec).
        'd$ </summary>
        Public Function SpectrumNumber2RetentionTime( _
          ByVal aSpectrumNumber As Integer, _
          ByVal anInExperimentNumber As Integer) As Double

            Return derived_SpectrumNumber2RetentionTime( _
              aSpectrumNumber, anInExperimentNumber)
        End Function 'SpectrumNumber2RetentionTime


        '****************************************************************************
        '*  SUBROUTINE NAME:   newSpectrumCommon                                    *
        'd$ <summary> Common function where the control flow of all newSpectrum
        'd$           eventually ends up.
        'd$ 
        'd$           Selects a spectrum to work on for subsequent
        'd$           calls to e.g. DataPoints().
        'd$           Returns retention time in seconds.
        'd$ 
        'd$           Note: aInfoRetentionTime_Seconds is not strictly needed, but 
        'd$                 can be used by some derived classes if they prefer to
        'd$                 use the retentiontime instead of MS spectrumnumber.
        'd$ </summary>
        'd$ <param name="">
        'd$   some parameter <see cref="T:xyz.abc" />
        'd$ </param>
        Private Function newSpectrumCommon( _
          ByVal aSpectrumNumber As Integer, _
          ByVal aSpectrumSpecification As spectrumSpecificationStructure, _
          ByVal aInfoRetentionTime_Seconds As Double, _
          ByRef anOutSpectrumClassification As SpectrumClassificationStructure) _
          As Double

            'Changed PM_REFACTOR_FINNEGAN_MSMS 2003-08-18
            If True Then 'This block: test only.

                'For now we assume that the experient number is 1
                'for MS-MS and 0 for MS....

                Select Case aSpectrumSpecification.spectrumType
                    Case spectrumTypeEnum.enumIsMSspectrum
                        Dim peter7 As Integer = 7
                        Trace.Assert( _
                          aSpectrumSpecification.experimentNumber = 0, _
                          "PIL ASSERT. Unexpected experiment number for a MS spectrum: " & _
                          aSpectrumSpecification.experimentNumber & "")

                    Case spectrumTypeEnum.enumIsFragmentSpectrum

                        'Changed PM_EXPERIEMTN_ASSERT 2003-08-20
                        'In our setup there experiment numbers start at 1 and
                        'there can be several: 1, 2, 3, ...
                        Trace.Assert( _
                          aSpectrumSpecification.experimentNumber >= 1 AndAlso _
                          aSpectrumSpecification.experimentNumber <= 10, _
                          "PIL ASSERT. Unexpected experiment number for a MS-MS spectrum: " & _
                          aSpectrumSpecification.experimentNumber & "")
                    Case Else
                        Trace.Assert(False, _
                          "Select Case never fall-through. spectrumType is neither MS nor MS-MS.")
                End Select
            End If

            mExperimentInfo = aSpectrumSpecification
            mCurrentSpectrumNumber = aSpectrumNumber

            Dim retentionTime_Secs As Double = _
              derived_NewSpectrum(aSpectrumNumber, aInfoRetentionTime_Seconds, _
                anOutSpectrumClassification)

            'Changed PM_PTM_FOR_ALL 2007-06-21
            'Client side check to catch this bug earlier than 
            '  in getCurSpectrumClassification() - derived_NewSpectrum must
            '  set anOutSpectrumClassification.
            Trace.Assert(anOutSpectrumClassification.mainClassification <> _
                         spectrumTypeEnum.enumIsUndefinedSpectrumType, _
              "PIL ASSERT. mainClassification is undefined.")
            Trace.Assert(anOutSpectrumClassification.MSsubType <> _
                         spectrumSubTypeEnum.enumST_DoesNotApply, _
              "PIL ASSERT. MSsubType is undefined.")

            'Why not simply set mCurSpectrumClassification instead of
            'having the parameter anOutSpectrumClassification?

            Trace.Assert(retentionTime_Secs > 0.0, _
              "PIL ASSERT. retentionTime_Secs > 0.0.")
            Return retentionTime_Secs
        End Function 'newSpectrumCommon


        '****************************************************************************
        '*  SUBROUTINE NAME:   newSpectrumByRetentionTime                           *
        'd$ <summary> This version: xxx. Selects a spectrum to work on for subsequent
        'd$           calls to e.g. DataPoints().
        'd$           Returns .
        'd$ </summary>
        'd$ <param name="aStartRetentionTime_Seconds">
        'd$   some parameter <see cref="T:xyz.abc" />
        'd$ </param>
        Public Function newSpectrumByRetentionTime( _
          ByVal aStartRetentionTime_Seconds As Double, _
          ByVal aDummy As spectrumSpecStructure, _
          ByVal anInSpectrumSpecification As spectrumSpecificationStructure, _
          ByRef anOutSpectrumClassification As SpectrumClassificationStructure) _
          As Integer

            Dim toReturn As Integer

            'Note: for QSTAR/Analyst the returned spectrum number is the
            '      group number - in practice the MS spectrum.
            Dim spectrumNum As Integer = _
              Me.retentionTime2SpectrumNumber(aStartRetentionTime_Seconds)

            toReturn = spectrumNum

            'Note: actual retention time is lost...
            Dim retentionTime As Double = _
              Me.newSpectrumCommon( _
                spectrumNum, anInSpectrumSpecification, _
                aStartRetentionTime_Seconds, _
                anOutSpectrumClassification)

            Trace.Assert(spectrumNum > 0, "PIL ASSERT. spectrumNum > 0.")

            'Me.spectrumSpecification(aSpectrumSpecification)
            'Me.newSpectrumByRetentionTime(aStartRetentionTime_Seconds, aDummy)

            Return spectrumNum
        End Function 'newSpectrumByRetentionTime


        '****************************************************************************
        '*  SUBROUTINE NAME:   newSpectrumByRetentionTime                           *
        'd$ <summary> Selects a spectrum to work on for subsequent
        'd$           calls to e.g. DataPoints().
        'd$           Returns .
        'd$ </summary>
        'd$ <param name="aStartRetentionTime_Seconds">
        'd$   some parameter <see cref="T:xyz.abc" />
        'd$ </param>
        Public Function newSpectrumByRetentionTime( _
          ByVal aStartRetentionTime_Seconds As Double, _
          ByVal aDummy As spectrumSpecStructure, _
          ByRef anOutSpectrumClassification As SpectrumClassificationStructure) _
          As Integer

            Dim defaultSpectrumTypeIsMS As spectrumSpecificationStructure
            defaultSpectrumTypeIsMS.experimentNumber = 0 'For MS spectrum, at least Analyst....
            defaultSpectrumTypeIsMS.spectrumType = spectrumTypeEnum.enumIsMSspectrum

            Return Me.newSpectrumByRetentionTime( _
              aStartRetentionTime_Seconds, aDummy, defaultSpectrumTypeIsMS, _
              anOutSpectrumClassification)
        End Function 'newSpectrumByRetentionTime


        '****************************************************************************
        '*  SUBROUTINE NAME:   newSpectrumBySpectrumNumber                          *
        'd$ <summary> Selects a spectrum to work on for subsequent
        'd$           calls to e.g. DataPoints().
        'd$           Returns retention time in seconds.
        'd$ </summary>
        'd$ <param name="">
        'd$   some parameter <see cref="T:xyz.abc" />
        'd$ </param>
        Public Function newSpectrumBySpectrumNumber( _
          ByVal aSpectrumNumber As Integer, _
          ByRef anOutSpectrumClassification As SpectrumClassificationStructure) _
          As Double

            Dim defaultSpectrumTypeIsMS As spectrumSpecificationStructure
            defaultSpectrumTypeIsMS.experimentNumber = 0 'For MS spectrum, at least Analyst....
            defaultSpectrumTypeIsMS.spectrumType = spectrumTypeEnum.enumIsMSspectrum

            'We only do this because some derived classes are depended
            'on this value, e.g. Micromass.
            Dim infoRetentiontime As Double = _
              Me.SpectrumNumber2RetentionTime(aSpectrumNumber, 0)

            Return Me.newSpectrumCommon( _
              aSpectrumNumber, defaultSpectrumTypeIsMS, infoRetentiontime, _
              anOutSpectrumClassification)
        End Function 'newSpectrumBySpectrumNumber


        'Should this function exist?
        '****************************************************************************
        '*  SUBROUTINE NAME:   currentSpectrumNumber                                *
        'd$ <summary> xyz
        'd$ </summary>
        Public Function currentSpectrumNumber() As Integer

            Return mCurrentSpectrumNumber
        End Function 'currentSpectrumNumber


        '****************************************************************************
        '*  SUBROUTINE NAME:   spectrumInfo                                         *
        'd$ <summary> Returns information about the current spectrum: title, 
        'd$           retention time, spectrum number, xxx, xxx, xxx
        'd$ </summary>
        Public Sub spectrumInfo2(ByRef aSpectrumInfo As spectrumInfoStructure2)

            'Not implemented.
            'Currently not used anywhere.
            Trace.Assert(False, "Stop!", "PIL ASSERT. Internal/development assert for stopping execution......")
        End Sub 'spectrumInfo


        '****************************************************************************
        '*  SUBROUTINE NAME:   setLCtimeWindow                                      *
        'd$ <summary> 
        'd$          Note that this class does not do any quantitation to find
        'd$          the LC profile (quantitation), it just takes care of the
        'd$          administrative work of keeping track of the LC time window: what
        'd$          spectra, proper handling at the edges (start and end of a run), xxx, xxx
        'd$ </summary>
        Public Sub setLCtimeWindow2(ByRef aSpectrumInfo As spectrumInfoStructure2)
            Trace.Assert(False, "Stop!", _
              "PIL ASSERT. Internal/development assert for stopping execution......")
        End Sub 'setLCtimeWindow


        '****************************************************************************
        '*  SUBROUTINE NAME:   spectraRange_External                                *
        'd$ <summary>  The term "external" refers to externally (or internally
        'd$            computed) retention times (as opposed to retention times
        'd$            determined by an MS-MS event.
        'd$            Can we find a better name??
        'd$ </summary>
        Public Sub spectraRange_External( _
          ByVal aRentionTimeRange As retentionTimeRangeStructure, _
          ByRef anOutStartSpectrum As spectrumSpecStructure, _
          ByRef anOutEndSpectrum As spectrumSpecStructure)

            'Note: handles retention times outside the range of the file -
            '  the effective retention times are truncated to fit the file.

            Dim setDescr As setEnum
            spectraRange( _
                 aRentionTimeRange, _
                 anOutStartSpectrum, _
                 anOutEndSpectrum, _
                 setDescr)

            'For now: for external we expect the range to sufficiently large to
            '         always have some spectra in it, at the very least
            '         the MS-MS spectrum.
            Trace.Assert(setDescr <> setEnum.enumEmptySet, "PIL ASSERT. <message>.")
        End Sub 'spectraRange_External


        '****************************************************************************
        '*  SUBROUTINE NAME:   spectraRange_Internal                                *
        'd$ <summary>  The term "internal" refers to retention times
        'd$            determined by an MS-MS event -not supplied by some
        'd$            externally estimated retention time.
        'd$            Can we find a better name??
        'd$          </summary>
        Public Sub spectraRange_Internal( _
          ByVal aRetentionTime_secs As Double, _
          ByVal aPreTimeOffset_secs As Double, ByVal aPostTimeOffSet_secs As Double, _
          ByRef anOutStartSpectrum As spectrumSpecStructure, _
          ByRef anOutEndSpectrum As spectrumSpecStructure)

            'Note: should be able to handle retention times outside the range of the file -
            '  the effective retention times should be truncated to fit the file.

            Dim rentionTimeRange As retentionTimeRangeStructure
            rentionTimeRange.startTime_secs = aRetentionTime_secs - aPreTimeOffset_secs
            rentionTimeRange.endTime_secs = aRetentionTime_secs + aPostTimeOffSet_secs

            Dim setDescr As setEnum
            spectraRange( _
                 rentionTimeRange, _
                 anOutStartSpectrum, _
                 anOutEndSpectrum, _
                 setDescr)
        End Sub 'spectraRange_Internal


        '****************************************************************************
        '*  SUBROUTINE NAME:   maxYValue                                            *
        'd$ <summary>  For current file and spectrum: finds max intensity in
        'd$            specified mass range.
        'd$ </summary>
        'Note: original used the utility function in the Analyst COM component,.
        '          mANALYSTCOM_TheData.GetYValueRangeForXValueRange( _
        '
        '  but we find it by first getting the datapoints. A side effect of
        '  that call is to get 
        Public Function maxYValueInMassRange( _
          ByVal aStartMass As Double, ByVal anEndMass As Double) As Double

            Trace.Assert(mTag > 0, "PIL ASSERT. mTag > 0. In maxYValueInMassRange().")

            'Return value is integer because currently our systems have counting detectors...
            'This may be changed by adding a new function with a return value of double, etc..

            'Changed PM_REFACTOR 2006-10-12
            'Dim tempPoints As ArrayList = Nothing 'Keep compiler happy.
            Dim tempPoints As _
              Generic.List(Of massSpectrometryBase.signalStructure) = _
                Nothing 'Keep compiler happy.

            Dim dataPointsInfo As dataPointsInfoStructure2
            Me.DataPoints( _
              aStartMass, anEndMass, _
              mTag, _
              tempPoints, _
              dataPointsInfo)

            Dim toReturn As Double = dataPointsInfo.maximumSignal.Ysig
            Dim len As Integer = tempPoints.Count
            Trace.Assert(toReturn > 0.0 Or len = 0, _
              "PIL ASSERT. Unexpected max Y " & _
              "(all data points in mass range are probably identical zero): " & _
              toReturn & _
              ". Or the spectrum is empty - has " & len & _
              "points in the mass range " & aStartMass & " Th to " & _
              anEndMass & " Th.")

            Return toReturn
        End Function 'maxYValueInMassRange


        '****************************************************************************
        '*  SUBROUTINE NAME:   DataPoints                                           *
        'd$ <summary>  See the other version of DataPoints() for reference.
        'd$ </summary>
        'd$ <param name="anOutDatapoints">
        'd$   Items are of type signalStructure, <see cref="T:xyz.abc" />
        'd$ </param>
        Public Sub DataPoints( _
          ByVal aStartMass As Double, ByVal anEndMass As Double, _
          ByVal aTag As Integer, _
          ByVal aStartRetentionTime_Seconds As Double, _
          ByRef anOutDatapoints As _
            Generic.List(Of massSpectrometryBase.signalStructure), _
          ByRef aDataPointsInfo As dataPointsInfoStructure2 _
          )
            'Old:
            '  ByRef anOutDatapoints As ArrayList

            'Not currently used....
            Trace.Assert(False, "Stop!", "PIL ASSERT. Internal/development assert for stopping execution......")

            Dim dummy4 As spectrumSpecStructure
            Dim dummy5 As SpectrumClassificationStructure
            dummy5.dataSourceName = Nothing 'Keep compiler happy.

            Me.newSpectrumByRetentionTime( _
              aStartRetentionTime_Seconds, dummy4, dummy5)

            DataPoints( _
              aStartMass, anEndMass, aTag, anOutDatapoints, aDataPointsInfo)
        End Sub 'DataPoints


        '****************************************************************************
        '*  SUBROUTINE NAME:   DataPoints                                           *
        'd$ <summary> Returns the requested datapoints from one of the data
        'd$           files. Caching may or may not be used.
        'd$ </summary>
        'd$ <param name="anOutDatapoints">
        'd$   Items are of type signalStructure, <see cref="T:xyz.abc" />
        'd$ </param>
        Public Sub DataPoints( _
          ByVal aStartMass As Double, ByVal anEndMass As Double, _
          ByVal aTag As Integer, _
          ByRef anOutDatapoints As _
            Generic.List(Of massSpectrometryBase.signalStructure), _
          ByRef aDataPointsInfo As dataPointsInfoStructure2)

            'Old:
            '  ByRef anOutDatapoints As ArrayList


            'Me.openRawFile(DirectCast(mRawFileList.Item(0), fileSpecStruct))
            'Me.openRawFile(DirectCast(mRawFileList.Item(0), fileSpecStruct))

            'Check that the client has called newSpectrum()...
            Trace.Assert(mCurrentSpectrumNumber >= 0, _
              "PIL ASSERT. (Internal) client error: spectrum not set!.")

            Me.newCurrentFile(aTag)

            'For caching: see if we already have it in memory. For instance
            '  the caching strategy might be to read in ALL datapoints
            '  the first time a given WIFF file is accessed.

            'anOutDatapoints = New ArrayList
            anOutDatapoints = _
              New Generic.List(Of massSpectrometryBase.signalStructure)

            Me.returnDataPoints( _
              aStartMass, anEndMass, anOutDatapoints, aDataPointsInfo)

            'Require derived to return reasonable values for the case when
            'there are no data points in the mass range.
            Trace.Assert(aDataPointsInfo.maximumSignal.Ysig >= 0.0 Or anOutDatapoints.Count = 0, _
              "PIL ASSERT. dataPointsInfo.maxY >= 0.0. Value is " & _
              aDataPointsInfo.maximumSignal.Ysig & ". In DataPoints().")

            'Changed PM_MSQA_ARITOVERFLOW 2003-10-02
            If True Then 'For assert
                Dim len As Integer = anOutDatapoints.Count
                If len > 0 Then
                    Dim firstDataPoint As signalStructure = anOutDatapoints(0)
                    Dim lastDataPoint As signalStructure = anOutDatapoints(len - 1)

                    Trace.Assert(firstDataPoint.Xsig > aStartMass, _
                      "PIL ASSERT. Mass of returned datapoint (" & firstDataPoint.Xsig & _
                      ") outside mass range, [" & aStartMass & "-" & anEndMass & "].")
                    Trace.Assert(lastDataPoint.Xsig < anEndMass, _
                      "PIL ASSERT. Mass of returned datapoint (" & firstDataPoint.Xsig & _
                      ") outside mass range, [" & aStartMass & "-" & anEndMass & "].")
                End If
            End If
        End Sub 'DataPoints


        '****************************************************************************
        '*  SUBROUTINE NAME:   rawFiles                                             *
        'd$ <summary>Returns number of raw datafiles. This function was invented
        'd$    for unit-testing - is there a more elegant way??
        'd$ </summary>
        Public Function rawFiles() As Integer
            Dim toReturn As Integer = 0
            If mRawFileList Is Nothing Then

            Else
                toReturn = mRawFileList.Count
            End If
            Return toReturn
        End Function


        'Changed PM_REFACTOR_FINNEGAN 2003-07-06
        '****************************************************************************
        '*  SUBROUTINE NAME:   DataTitle                                            *
        'd$ <summary>What is returned?? Some kind of title, but for what?
        'd$ </summary>
        Public MustOverride Function DataTitle() As String


        'Changed PM_DTASC_SUPPORT 2006-11-07
        '****************************************************************************
        '*  SUBROUTINE NAME:   classifySpectrum                                     *
        'd$ <summary>What is returned?? Some kind of title, but for what?
        'd$ </summary>
        Public Overridable Function classifySpectrum( _
          ByVal aSpectrumNumber As Integer) _
          As SpectrumClassificationStructure

            'Default implementation if derived class do not provide it.
            Dim toReturn As SpectrumClassificationStructure
            toReturn.dataSourceName = "no data source"
            toReturn.mainClassification = spectrumTypeEnum.enumIsUndefinedSpectrumType
            toReturn.MSsubType = spectrumSubTypeEnum.enumST_DoesNotApply

            Return toReturn
        End Function 'classifySpectrum


        '****************************************************************************
        '*  P R O T E C T E D   S E C T I O N  ,   B E G I N                        *


        '****************************************************************************
        '*  SUBROUTINE NAME:   openRawFile                                          *
        'd$ <summary> Prepare a raw file for reading. Actual action is dependent
        'd$           on the file type - derived class knows what to do.
        'd$ </summary>
        Protected MustOverride Sub openRawFile(ByVal aFileSpec As fileSpecStructure)

        '****************************************************************************
        '*  SUBROUTINE NAME:   closeRawFile                                         *
        'd$ <summary>Tell derived class to XYZ, if any
        'd$ </summary>
        Protected MustOverride Sub closeRawFile(ByVal aFileSpec As fileSpecStructure)

        '****************************************************************************
        '*  SUBROUTINE NAME:   retentionTime2SpectrumNumber                         *
        'd$ <summary>   For current(??) raw file: convert from retention time to
        'd$             spectrum number.
        'd$ </summary>
        Protected MustOverride Function retentionTime2SpectrumNumber( _
          ByVal aRetentionTime_Secs As Double) As Integer


        '****************************************************************************
        '*  SUBROUTINE NAME:   derived_SpectrumNumber2RetentionTime                 *
        'd$ <summary>   For current(??) raw file: convert from spectrum number to
        'd$             retention time (sec).
        'd$ </summary>
        Protected MustOverride Function derived_SpectrumNumber2RetentionTime( _
          ByVal aSpectrumNumber As Integer, _
          ByVal anInExperimentNumber As Integer) As Double


        '****************************************************************************
        '*  SUBROUTINE NAME:   derived_NewSpectrum                                  *
        'd$ <summary>   .
        'd$           Note: aInfoRetentionTime_Seconds is not strictly needed, but 
        'd$                 can be used by some derived classes if they prefer to
        'd$                 use the retentiontime instead of MS spectrumnumber.
        'd$ </summary>
        Protected MustOverride Function derived_NewSpectrum( _
          ByVal aSpectrumNumber As Integer, _
          ByVal aInfoRetentionTime_Seconds As Double, _
          ByRef anOutSpectrumClassification As SpectrumClassificationStructure) _
          As Double


        'Changed PM_REFACTOR_FINNEGAN 2003-08-21
        '****************************************************************************
        '*  SUBROUTINE NAME:   nextMSspectrum                                       *
        'd$ <summary>   Does NOT change state.....
        'd$ </summary>
        Public MustOverride Function nextMSspectrum( _
          ByVal aSpectrumNumber As Integer, _
          ByRef anOutSpectrumClassification As SpectrumClassificationStructure) As Integer



        'Changed PM_MORE_MS3_DTASUPERCHARGE 2004-05-10
        '  Old function unchanged to protect clients from changes.
        '****************************************************************************
        '*  SUBROUTINE NAME:   nextFragmentspectrum                                 *
        'd$ <summary>   Does NOT change state.....
        'd$ </summary>
        Public Function nextFragmentspectrum( _
          ByVal aSpectrumNumber As Integer, _
          ByRef anOutSupposedPrecursorMass As Double, _
          ByRef anOutExperimentNumber As Integer) As Integer

            Dim curClassification As SpectrumClassificationStructure
            curClassification.dataSourceName = Nothing 'Keep compiler happy.

            Dim spectrumNum As Integer = nextFragmentspectrum2( _
              aSpectrumNumber, anOutSupposedPrecursorMass, curClassification, _
              anOutExperimentNumber)

            'Changed PM_CORR_TROUBLE 2008-03-06. For now.
            Trace.Assert(spectrumNum > 0, _
              "PIL ASSERT. nextFragmentspectrum2() returned negative value - " & _
              "no fragment spectra past spectrum " & aSpectrumNumber & "...")

            Return spectrumNum
        End Function 'nextFragmentspectrum


        'Changed PM_SUPERCHARGE 2003-11-03
        '****************************************************************************
        '*  SUBROUTINE NAME:   nextFragmentspectrum                                 *
        'd$ <summary>   Does NOT change state.....
        'd$ </summary>
        Public MustOverride Function nextFragmentspectrum2( _
                  ByVal aSpectrumNumber As Integer, _
                  ByRef anOutSupposedPrecursorMass As Double, _
                  ByRef anOutCurClassification As SpectrumClassificationStructure, _
                  ByRef anOutExperimentNumber As Integer) As Integer


        'Changed PM_REFACTOR_FINNEGAN 2003-08-21
        '****************************************************************************
        '*  SUBROUTINE NAME:   spectrumNumberForMSspectrum                          *
        'd$ <summary>   Does NOT change state.....
        'd$ </summary>
        Public MustOverride Function spectrumNumberForMSspectrum( _
          ByVal aSpectrumNumber As Integer, _
          ByRef anOutSpectrumClassification As SpectrumClassificationStructure, _
          ByVal aForceOneLevelUp As Boolean) _
          As Integer


        '****************************************************************************
        '*  SUBROUTINE NAME:   spectrumNumberForMSspectrum_ForceTrueMSspectrum      *
        'd$ <summary>  Finds the first true MS spectrum (e.g. not just the parent
        'd$            MS2 spectrum number for a MS3 spectrum).
        'd$ </summary>
        Public Function spectrumNumberForMSspectrum_ForceTrueMSspectrum( _
          ByVal aSpectrumNumber As Integer, _
          ByRef anOutSpectrumClassification As SpectrumClassificationStructure, _
          ByVal aForceOneLevelUp As Boolean) _
          As Integer

            'Dim classificationMS As SpectrumClassificationStructure

            Dim someMSspectrumNumber As Integer = _
               Me.spectrumNumberForMSspectrum( _
                 aSpectrumNumber, anOutSpectrumClassification, aForceOneLevelUp)

            'Changed PM_QUANT_ASSERT_FOR_MS3DATA 2005-09-21
            Trace.Assert( _
                anOutSpectrumClassification.mainClassification = _
                spectrumTypeEnum.enumIsMSspectrum, _
              "PIL ASSERT. Spectrum type is not MS.....")

            'For MS3: we don't want to start at some MS2 spectrum; we
            '         go one level higher up, to a SIM spectrum or full
            '         scan spectrum.

            'Changed PM_PHASEOUT_FRAGNORMAL 2006-04-04

            Dim subType As spectrumSubTypeEnum = _
              anOutSpectrumClassification.MSsubType
            If ( _
                 subType = _
                 spectrumSubTypeEnum.enumIsFragmentCID _
               ) _
               Then

                Dim MS2spectrumNumber As Integer = someMSspectrumNumber
                someMSspectrumNumber = _
                   Me.spectrumNumberForMSspectrum( _
                     MS2spectrumNumber, anOutSpectrumClassification, _
                     aForceOneLevelUp)
            End If
            Return someMSspectrumNumber
        End Function 'spectrumNumberForMSspectrum_ForceTrueMSspectrum


        'Changed PM_REFACTOR_FINNEGAN 2003-08-21
        '****************************************************************************
        '*  SUBROUTINE NAME:   getSpectrumRange                                     *
        'd$ <summary>   Does NOT change state.....
        'd$ </summary>
        Public MustOverride Function getSpectrumRange() _
          As spectrumRangeStructure


        '****************************************************************************
        '*  SUBROUTINE NAME:   returnDataPoints                                     *
        'd$ <summary>Retrieve datapoints from the current spectrum.
        'd$ </summary>
        'd$ <param name="anOutDatapoints">
        'd$   Items are of type signalStructure, <see cref="T:xyz.abc" />
        'd$ </param>
        Protected MustOverride Sub returnDataPoints( _
          ByVal aStartMass As Double, ByVal anEndMass As Double, _
          ByRef anOutDatapoints As _
            Generic.List(Of massSpectrometryBase.signalStructure), _
          ByRef aDataPointsInfo As dataPointsInfoStructure2)

        'Old:
        '  ByRef anOutDatapoints As ArrayList


        '****************************************************************************
        '*  P R O T E C T E D   S E C T I O N  ,   E N D                            *


        ''****************************************************************************
        ''*  SUBROUTINE NAME:   spectrumSpecification                                *
        'd$ '<summary>   .
        'd$ '</summary>
        'Protected MustOverride Sub spectrumSpecification( _
        '  ByVal aSpectrumSpecification As spectrumSpecificationStructure)


        'd$ ****************************************************************************
        'd$ *  SUBROUTINE NAME:   spectraRange                                         *
        'd$ ''<summary>   
        'd$         Note: anOutStartSpectrum and anOutEndSpectrum are both inclusive.
        'd$ ''</summary>
        'Protected MustOverride Sub spectraRange( _
        '  ByVal aRentionTimeRange As retentionTimeRangeStructure, _
        '  ByRef anOutStartSpectrum As spectrumSpecStructure, _
        '  ByRef anOutEndSpectrum As spectrumSpecStructure)
        '
        'For now: base class implementation .
        Private Sub spectraRange( _
          ByVal aRentionTimeRange As retentionTimeRangeStructure, _
          ByRef anOutStartSpectrum As spectrumSpecStructure, _
          ByRef anOutEndSpectrum As spectrumSpecStructure, _
          ByVal aSpectrasDescription As setEnum)

            'For now: like the old implementation. (is it really needed to do this 
            '         iteration of cycles/spectrum numbers?? - why not just finding 
            '         the end points and  adjust by 1 as needed?)

            ' Protected MustOverride Function retentionTime2SpectrumNumber( _
            'ByVal aRetentionTime_Secs As Double) As Integer

            Dim srange As spectrumRangeStructure = getSpectrumRange()

            Dim rightTooFar As Boolean = False
            Dim leftTooFar As Boolean = False
            If True Then

                'Why do we need to do this when it is already in srange???? -
                '  srange.firstRetentionTimeSeconds and srange.lastRetentionTimeSeconds.
                'Perhaps these should be computed elsewhere as the end time is tricky....
                Dim startSpectrumRet As Double = _
                  Me.SpectrumNumber2RetentionTime(srange.startSpectrum, 0)
                Dim endSpectrumRet As Double = _
                  Me.SpectrumNumber2RetentionTime(srange.endSpectrum, 0)



                'Changed PM_REFACTOR 2006-08-28
                Dim startSpectrumRTminutes As Double = startSpectrumRet / 60.0
                Dim endSpectrumRTminutes As Double = endSpectrumRet / 60
                Dim rightRTminutes As Double = _
                  aRentionTimeRange.endTime_secs / 60
                Dim leftRTminutes As Double = _
                  aRentionTimeRange.startTime_secs / 60
                rightTooFar = _
                  Not aRentionTimeRange.endTime_secs <= endSpectrumRet
                leftTooFar = _
                  Not aRentionTimeRange.startTime_secs >= startSpectrumRet

                If rightTooFar Or leftTooFar Then 'Guard to save one 
                    '  string allocation...
                    '  Use a helper function instead?, called from the string building
                    '  in the assert?

                    'Changed PM_RT_OUT_OF_FILE 2006-08-28. No longer necessary
                    'as we now take it into account below.
                    ' Dim outsideStr As String = "is outside the range for the raw file, " & _
                    '     "[" & startSpectrumRTminutes.ToString("0.000") & "; " & _
                    '     endSpectrumRTminutes.ToString("0.000") & "] minutes, " & _
                    '     "[" & _
                    '     startSpectrumRet.ToString("0.00000") & "; " & _
                    '     endSpectrumRet.ToString("0.00000") & "] seconds."
                    ' 
                    ' Trace.Assert(Not rightTooFar, _
                    '   "PIL ASSERT. Right retention time, " & _
                    '   rightRTminutes.ToString("0.000") & _
                    '   " minutes/" & _
                    '   (aRentionTimeRange.endTime_secs).ToString("0.00000") & _
                    '   " seconds, " & outsideStr _
                    '   )
                    ' 
                    ' Trace.Assert(Not leftTooFar, _
                    '   "PIL ASSERT. Left retention time, " & _
                    '   leftRTminutes.ToString("0.000") & _
                    '   " minutes/" & _
                    '   (aRentionTimeRange.startTime_secs).ToString("0.00000") & _
                    '   " seconds, " & outsideStr _
                    '   )
                End If
            End If

            Dim middleTime_secs As Double = _
              0.5 * (aRentionTimeRange.endTime_secs + aRentionTimeRange.startTime_secs)
            Trace.Assert(middleTime_secs > 0.01, _
              "PIL ASSERT. middleTime_secs>0.01. In spectraRange()")

            'Changed PM_RT_OUT_OF_FILE 2008-02-25
            'Avoid ASSERT later. For e.g. -60s/+90s the middle is shifted 15 secs to 
            '  the right and can point outside the raw file.
            Dim marginSecs As Double = 2.0 'Make it work even if pointed to the very last
            '  spectrum (this assumes spectra are less than 2 seconds apart). Would fail 
            '  somehow if user specified very narrow retention time (dialog 
            '  Correlations/Left LC window, etc).
            Dim retNearEndSecs As Double = srange.lastRetentionTimeSeconds - marginSecs
            If middleTime_secs > retNearEndSecs Then
                middleTime_secs = retNearEndSecs
            End If

            Dim middleSpectrumNumber As Integer = _
              retentionTime2SpectrumNumber(middleTime_secs)

            'Changed PM_RT_OUT_OF_FILE 2006-08-28
            Dim leftSpectrumNumber As Integer = 0
            If Not leftTooFar Then
                leftSpectrumNumber = middleSpectrumNumber + 1

                While derived_SpectrumNumber2RetentionTime(leftSpectrumNumber, 0) >= _
                  aRentionTimeRange.startTime_secs AndAlso _
                  leftSpectrumNumber > 0

                    leftSpectrumNumber -= 1
                End While
                anOutStartSpectrum.specNum = leftSpectrumNumber + 1 '+1: to have it inclusive.
            Else
                anOutStartSpectrum.specNum = srange.startSpectrum
                leftSpectrumNumber = srange.startSpectrum 'For the code below.
                'Expected to be one.
            End If

            'Changed PM_RT_OUT_OF_FILE 2006-08-28
            Dim rightSpectrumNumber As Integer = 0
            If Not rightTooFar Then
                rightSpectrumNumber = middleSpectrumNumber - 1 '-1: to be sure we find the
                '    spectrum that is just higher than the upper bound.
                While derived_SpectrumNumber2RetentionTime(rightSpectrumNumber, 0) <= _
                  aRentionTimeRange.endTime_secs AndAlso _
                  rightSpectrumNumber < srange.endSpectrum

                    rightSpectrumNumber += 1
                End While
                anOutEndSpectrum.specNum = rightSpectrumNumber - 1 '-1: to have it inclusive.
            Else
                anOutEndSpectrum.specNum = srange.endSpectrum
                rightSpectrumNumber = srange.endSpectrum 'For the code below.
            End If

            aSpectrasDescription = setEnum.enumIllegalSetDescription 'Just in case
            If anOutEndSpectrum.specNum > anOutStartSpectrum.specNum Then
                'As they are inclusive and within the range specified by the input to
                'this function there must be at least two.
                aSpectrasDescription = setEnum.enumSeveralItemsInSet
            Else
                If anOutEndSpectrum.specNum = anOutStartSpectrum.specNum Then
                    aSpectrasDescription = setEnum.enumExactlyOneItemInSet
                Else
                    If True Then
                        aSpectrasDescription = setEnum.enumEmptySet
                    Else

                    End If
                End If
            End If

            'Even if it is empty (will nearly always be if start and end times 
            'the same - very narrow input range) we fill in some information. We
            'find the closest spectrum and use that.
            If (aSpectrasDescription = setEnum.enumEmptySet) Then

                Dim leftDistance As Double = _
                  aRentionTimeRange.startTime_secs - _
                  derived_SpectrumNumber2RetentionTime(leftSpectrumNumber, 0)
                Trace.Assert(leftDistance >= 0, "PIL ASSERT. leftDistance >=0.")

                Dim rightDistance As Double = _
                  derived_SpectrumNumber2RetentionTime(rightSpectrumNumber, 0) - _
                  aRentionTimeRange.endTime_secs
                'Trace.Assert(rightDistance >= 0, "PIL ASSERT. rightDistance >=0.")

                Dim spectrumNumberToUse As Integer
                If leftDistance < rightDistance Then
                    spectrumNumberToUse = leftSpectrumNumber
                Else
                    spectrumNumberToUse = rightSpectrumNumber
                End If

                anOutStartSpectrum.specNum = spectrumNumberToUse
                anOutEndSpectrum.specNum = spectrumNumberToUse
            End If

            anOutStartSpectrum.retentionTime_secs = _
              derived_SpectrumNumber2RetentionTime( _
                anOutStartSpectrum.specNum, 0)

            anOutEndSpectrum.retentionTime_secs = _
              derived_SpectrumNumber2RetentionTime( _
                anOutEndSpectrum.specNum, 0)
        End Sub 'spectraRange


        '****************************************************************************
        '*  SUBROUTINE NAME:   setSampleNumberInRawFile                             *
        'd$ <summary>Record this for at least Analyst files (where a physical file
        'd$          can be divided into several section, several socalled "samples").
        'd$ </summary>
        'd$ <param name="anOutDatapoints">
        'd$   Items are of type signalStructure, <see cref="T:xyz.abc" />
        'd$ </param>
        Public Sub setSampleNumberInRawFile(ByVal aSampleNumberInRawFile As Integer)

            mSampleNumberInRawFile = aSampleNumberInRawFile
        End Sub 'setSampleNumberInRawFile


        'Changed PM_SEVERALWIFFS 2003-08-27
        '****************************************************************************
        '*  SUBROUTINE NAME:   getCurrentRawFileTag                             *
        'd$ <summary>What does it do?.
        'd$ </summary>
        'd$ <param name="anOutDatapoints">
        'd$   Items are of type signalStructure, <see cref="T:xyz.abc" />
        'd$ </param>
        Public Function getCurrentRawFileTag() As Integer
            Return mTag
        End Function 'getCurrentRawFileTag


        ''****************************************************************************
        ''*  SUBROUTINE NAME:   getCurrentSpectrumNumber                             *
        'd$ '<summary>   .
        'd$ '</summary>
        Public Function getCurrentSpectrumNumber() As Integer
            Return mCurrentSpectrumNumber
        End Function 'getCurrentSpectrumNumber


        ''****************************************************************************
        ''*  SUBROUTINE NAME:   getCurrentFullRawFileName                            *
        'd$ '<summary>   .
        'd$ '</summary>
        Public Function getCurrentFullRawFileName() As String

            Dim toReturn As String = "" 'Default if not found
            If mRawFileList.Contains(mTag) Then
                toReturn = CStr(mRawFileList.Item(mTag))
            Else
                'Client error to call before a file has been selected...
                Trace.Assert(False, "PIL ASSERT. This should never happen...")
            End If
            Return toReturn
        End Function 'getCurrentSpectrumNumber


        'Changed PM_REFACTOR 2005-08-15
        '****************************************************************************
        '*  SUBROUTINE NAME:   setSpectrumCONV                                      *
        'd$ <summary>Convenience function. Result of refactoring - there may be
        'd$          very similar functions already....
        'd$ </summary>
        'd$ <param name="xyz">
        'd$ </param>
        Public Sub setSpectrumCONV( _
          ByVal aRTsecs As Double, ByVal anExperiment As Integer, _
          ByRef anOutSpectrumNumber As Integer)

            Dim spectrumSpec As spectrumSpecificationStructure
            spectrumSpec.experimentNumber = anExperiment '-1 to offset??
            spectrumSpec.spectrumType = spectrumTypeEnum.enumIsFragmentSpectrum

            '************************** I M P O R T A N T **************************
            'Note: theExperiment is significant (at least for Analyst). If the 
            '      effective value for Analyst/SetSpectrum is 0 then it seems 
            '      an MS spectrum is used, whereas if the value 1 is used then 
            '      an MS-MS spectrum is used.
            Dim dummy1 As spectrumSpecStructure
            dummy1.retentionTime_secs = -2312331.2
            dummy1.specNum = -812320

            'Is usually spectrum number for MS-MS, but can also be other
            'types of spectra.
            anOutSpectrumNumber = _
              Me.newSpectrumByRetentionTime( _
                aRTsecs, dummy1, spectrumSpec, mCurSpectrumClassification)
            '  mCurSpectrumClassification is changed.


        End Sub 'setSpectrumCONV


        'Changed PM_REFACTOR 2005-08-15
        '****************************************************************************
        '*  SUBROUTINE NAME:   datapointsCONV                                       *
        'd$ <summary>Convenience function. Result of refactoring - there may be
        'd$          a very similar function already....
        'd$ </summary>
        'd$ <param name="xyz">
        'd$ </param>
        Public Sub datapointsCONV( _
          ByVal aRawFileRef As Integer, _
          ByRef anOutSignal As Generic.List(Of massSpectrometryBase.signalStructure), _
          ByRef anOutDataPointsInfo As dataPointsInfoStructure2, _
          ByRef anOutLowestX As Double, _
          ByRef anOutHighestX As Double)

            'Old:
            '  ByRef anOutSignal As ArrayList, _

            'Changed PM_REFACTOR 2006-10-12
            'anOutSignal = New ArrayList
            anOutSignal = New Generic.List(Of massSpectrometryBase.signalStructure)

            Me.DataPoints( _
              0.0, 4000.0, aRawFileRef, anOutSignal, anOutDataPointsInfo)
            anOutLowestX = anOutDataPointsInfo.minX2
            anOutHighestX = anOutDataPointsInfo.maxX2
        End Sub 'datapointsCONV


        'Changed PM_REFACTOR 2005-08-17
        '****************************************************************************
        '*  SUBROUTINE NAME:   getCurSpectrumClassification                                       *
        'd$ <summary>
        'd$          Note: currently only works for 
        'd$ </summary>
        'd$ <param name="xyz">
        'd$ </param>
        Public Function getCurSpectrumClassification() _
          As SpectrumClassificationStructure

            Trace.Assert(mCurSpectrumClassification.mainClassification <> _
                         spectrumTypeEnum.enumIsUndefinedSpectrumType, _
              "PIL ASSERT. mainClassification is undefined.")

            Trace.Assert(mCurSpectrumClassification.MSsubType <> _
                         spectrumSubTypeEnum.enumST_DoesNotApply, _
              "PIL ASSERT. MSsubType is undefined.")

            Return mCurSpectrumClassification
        End Function 'getCurSpectrumClassification


        'Changed PM_REFACTOR 2007-07-11
        '****************************************************************************
        '*  SUBROUTINE NAME:   getCurSpectrumClassification                                       *
        'd$ <summary>
        'd$          Note: currently only works for 
        'd$ </summary>
        'd$ <param name="xyz">
        'd$ </param>
        Public Shared Function MS3spectraList( _
          ByVal aSpectrumNumber As Integer, _
        ByVal aRawDataFileHandling As rawDataFileHandling) _
          As Generic.List(Of spectrumSpecStructure)

            Dim toReturn As Generic.List(Of spectrumSpecStructure) = _
              New Generic.List(Of spectrumSpecStructure)

            Dim done As Boolean = False

            Dim spectrumNum2 As Integer = aSpectrumNumber
            While Not done
                Dim curClassification As SpectrumClassificationStructure
                curClassification.dataSourceName = Nothing 'Keep compiler happy.

                Dim supposedPrecursorMass As Double
                Dim experimentNumber As Integer
                Dim ms3spectrumNumber As Integer = _
                  aRawDataFileHandling.nextFragmentspectrum2( _
                    spectrumNum2, supposedPrecursorMass, _
                    curClassification, experimentNumber)

                'Changed PM_LASTFRAGMENTSPECTRUM_INFILE_ASSERT 2008-05-22. Check return
                '  value and take appropriate action...
                ''Changed PM_CORR_TROUBLE 2008-03-06. For now.
                'Trace.Assert(ms3spectrumNumber > 0, _
                '  "PIL ASSERT. nextFragmentspectrum2() returned negative value - " & _
                '  "no fragment spectra past spectrum " & aSpectrumNumber & "...")
                If ms3spectrumNumber >= 0 Then

                    If curClassification.MSsubType = _
                       spectrumSubTypeEnum.enumIsFragmentMS3 Then

                        Dim someItem As spectrumSpecStructure

                        someItem.supposedPrecursorMass = supposedPrecursorMass

                        someItem.experimentNumber = experimentNumber 'Do we really need this??
                        someItem.specNum = ms3spectrumNumber
                        someItem.retentionTime_secs = -77777.777

                        toReturn.Add(someItem)

                        spectrumNum2 += 1 'This assumes MS3 is the lowest spectrum level...
                    Else
                        done = True
                    End If

                Else
                    'Assume curClassification is undefined - thus the above "if"
                    'condition is protected...
                    done = True
                End If

            End While

            Dim len As Integer = toReturn.Count()
            If len > 1 Then
                Dim peter1 As Integer = 1 'More than one MS3 spectrum!
            End If

            Return toReturn
        End Function 'MS3spectraList


        'Changed PM_COMVERSIONS 2007-07-24
        'Should we have a function in each derived class?. Then we could
        'save on the casting.
        '****************************************************************************
        '*  SUBROUTINE NAME:   newCOMobject                                         *
        'd$ <summary>
        'd$          
        'd$ </summary>
        'd$ <param name="xyz">
        'd$ </param>
        Public Shared Function newCOMobject(ByVal aCOMversion As COMversionEnum) _
        As Object

            Dim toReturn As Object = Nothing

            Select Case aCOMversion
                Case COMversionEnum.enumAnalystQS10
                    'Later

                Case COMversionEnum.enumAnalystQS11
                    toReturn = New QS11analystCOM

                Case COMversionEnum.enumAnalystQS20
                    toReturn = New QS20analystCOM

                Case COMversionEnum.enumXCalibur143RC
                    'Later

                Case COMversionEnum.enumXCalibur20
                    'Later

                Case COMversionEnum.enumXCalibur20SR1
                    'Later

                Case COMversionEnum.enumMassLynx40
                    'Later

                Case COMversionEnum.enumMassLynx41
                    'Later

                Case Else
                    Trace.Assert(False, "PIL ASSERT. Select Case never fall-through")
            End Select

            Trace.Assert(Not toReturn Is Nothing, _
              "PIL ASSERT. Return value is Nothing, from newCOMobject().")

            Return toReturn
        End Function 'newCOMobject




    End Class 'rawDataFileHandling

End Namespace 'massSpectrometryBase

    

    

Generated by script codePublish.pl at 2008-09-23T11:59:18.