/****************************************************************************
 * Copyright (C) 2009 Peter Mortensen                                       *
 * This file is part of MSQuant.                                            *
 *                                                                          *
 * MSQuant is distributed under the terms of                                *
 * the GNU General Public License. See src/COPYING.TXT or                   *
 *  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: tryout for using the new interface, IXRawfile3, in              *
 *          the Finnigan COM component XRawFile2.dll.                       *
 *                                                                          *
 ****************************************************************************/

/****************************************************************************
 *                               CEBI                                       *
 *                    Software Development Group                            *
 *                         Peter Mortensen                                  *
 *                E-mail, through:           *
 *                 WWW: http://www.cebi.sdu.dk/                             *
 *                                                                          *
 *  Program for post-processing of result from search in mass               *
 *    spectrometric data.                                                   *
 *                                                                          *
 *    FILENAME:   XCaliburAccess.cs                                         *
 *    TYPE:       CSHARP                                                    *
 *                                                                          *
 * CREATED: PM 2009-10-13   First attempt.                                  *
 * UPDATED: PM 2009-xx-xx                                                   *
 *                                                                          *
 *                                                                          *
 *                                                                          *
 ****************************************************************************/


using System; //For Object, etc.
//using System.Collections.Generic;
//using System.Linq;
//using System.Text;

using XRAWFILE2Lib;

namespace massSpectrometryBase
{


    class XCaliburAccess
    {

        XRawfile mRawfile;
        XRAWFILE2Lib.IXRawfile3   mRawfile3;


        public XCaliburAccess()
        {
            // SFinniganRawfile2Wrap.cpp is using the same,
            //   0x5FE970B2,0x29C3,0x11D3,{0x81,0x1D,0x00,0x10,0x4B,0x30,0x48,0x96

            //This error happend:
            //  COMException was unhandled.
            //  Retrieving the COM class factory for component with CLSID
            //  {5FE970B2-29C3-11D3-811D-00104B304896} failed due to the
            //  following error: 80040154
            //
            //  80040154 is ERROR_CLASS_NOTREGISTERED
            //
            //Reason:
            //  Project NewXCalibur/Properties/Build/General/Platform target
            //  was set to "Any". It should be set to "x86" as COM components
            //  are 32 bit only ("Any" will run as 64 bit on a 64 bit
            //  version of Windows.)
            //
            mRawfile = new XRawfile();

            string filename =
              //@"H:\toDelete\2009-09-28\BAB-BM-JVO-NE-1-5-20-T-A02.RAW";
              @"L:\tempCollection\temp19,LyrisYeast\2005-03-04,LGYeastSILACtest\Lyris-YeastSILAC_Lys8-MS3.RAW";

            mRawfile.Open(filename);

            mRawfile.SetCurrentController(0, 1); //Mass spec device, first MS device.

            int lastSpectrumNumber = -17;
            mRawfile.GetLastSpectrumNumber(ref lastSpectrumNumber);

            //Input
            //int spectrumNumber = 9213; //A "SIM" spectrum.
            int spectrumNumber = 9206; //A "Full MS" spectrum.

            //Common for the two calls
            string noFilter = null;

            int noCutOff_Type = 0;
            int noCutOff = 0;
            int allPeaksReturned = 0;
            int doNotCentroid = 0;

            //Cut-off configuration
            int effective_CutOff_Type = 0;
            int effective_CutOff = 0;

            effective_CutOff_Type = noCutOff_Type;
            effective_CutOff = noCutOff;

            //Classic call. GetMassListFromScanNum()
            {
                //int absoluteCutOff_Type = 1;
                //int minimumCutOff = 1;

                //Outputs
                int outArraySize = -1;
                Object outRawData = null; //Keep compiler happy.
                double outCentroidWidth = 0.0;
                object outPeakFlags = null; //Keep compiler happy.

                mRawfile.GetMassListFromScanNum(
                  ref spectrumNumber,
                  noFilter,
                  effective_CutOff_Type,
                  effective_CutOff,
                  allPeaksReturned,
                  doNotCentroid,
                  ref outCentroidWidth,
                  ref outRawData,
                  ref outPeakFlags,
                  ref outArraySize);

                int peter2 = 2;
            }

            //New call. GetMassListFromScanNum()
            {
                //mRawfile3 = new XRAWFILE2Lib.IXRawfile3();
                //mRawfile3 = new XRawfile();

                mRawfile3.Open(filename);
                mRawfile3.SetCurrentController(0, 1); //Mass spec device, first MS device.
                int lastSpectrumNumber2 = -17;
                mRawfile3.GetLastSpectrumNumber(ref lastSpectrumNumber2);

                //Output
                int outArraySize = -1;
                Object outRawData = null; //Keep compiler happy.
                double outCentroidWidth = 0.0;
                object outPeakFlags = null; //Keep compiler happy.

                //606.84 Th: highest peak in 9206, Full ms.
                //[606.00;608.50]

                string massRangeStr = "606.00-608.50";

                //Compiles, but an instance of mRawfile3 can not be created...
                mRawfile3.GetMassListRangeFromScanNum(
                  ref spectrumNumber,
                  noFilter,
                  effective_CutOff_Type,
                  effective_CutOff,
                  allPeaksReturned,
                  doNotCentroid,
                  ref outCentroidWidth,
                  ref outRawData,
                  ref outPeakFlags,
                  massRangeStr,
                  ref outArraySize);

                //Compile error:
                // Error	1	'XRAWFILE2Lib.XRawfile' does not contain a
                // definition for 'GetMassListRangeFromScanNum' and no
                // extension method 'GetMassListRangeFromScanNum' accepting a
                // first argument of type 'XRAWFILE2Lib.XRawfile' could be
                // found (are you missing a using directive or an assembly
                // reference?)
                // D:\dproj\tryouts\NewXCalibur\NewXCalibur\src\XCaliburAccess\
                // XCaliburAccess.cs	133	26	NewXCalibur
                mRawfile.GetMassListRangeFromScanNum(
                  ref spectrumNumber,
                  noFilter,
                  effective_CutOff_Type,
                  effective_CutOff,
                  allPeaksReturned,
                  doNotCentroid,
                  ref outCentroidWidth,
                  ref outRawData,
                  ref outPeakFlags,
                  massRangeStr,
                  ref outArraySize);

                int peter2 = 2;
            }

            int peter3 = 3;
        } //Constructor.


    } //class XCaliburAccess


} //namespace massSpectrometryBase