2012年9月27日

Read Thermo RAW with MSFIleReader in C#

Introduction

Thermo now provide free API called MSFileReader to access .RAW file.

This tutorial shows how to using C# to import MSFileReader .dll and call one of its function.






Step by Step


OS: Windows 7 x64
Visual Studio: 2010
MSFileReader: 2.2


Step 1. Install MSFileReader
http://sjsupport.thermofinnigan.com/public/detail.asp?id=703


Step 2. Create a project called "testMSFileReader" as Console Application and add XRawfile2_x64.dll into Reference.

Right click your application and select "Add Reference..".

  select "XRawfile2_x64.dll" and click OK button. 


Step 3. If you got " Class not registered" error, change Platform target to x64

  Right click your application and select "Properties".

  change Platform target to x64

Step 4. Copy the code below and you should get the Number of Spectra.

==SOURCE CODE==


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MSFileReaderLib;  /* using namespace after add references "XRawfile2_x64.dll" */


namespace testMSFileReader
{
    class Program
    {
        static void Main(string[] args)
        {

            MSFileReader_XRawfile rawfile = new MSFileReader_XRawfile();
             // try other version if some function don't work
             //  IXRawfile5 raw = (IXRawfile5)new MSFileReader_XRawfile();
            rawfile.Open("C:\\test\\test.raw");

            rawfile.SetCurrentController(0, 1); /* Controller type 0 means mass spec device; Controller 1 means first MS device */
           
            int num =0; /* ref int usrd with GetNumSpectra */

            System.Console.WriteLine("call GetNumSpectra()");           
           
            rawfile.GetNumSpectra(ref num);        

            System.Console.WriteLine("Number of Spectra: "+num);  

        }
    }
}

4 則留言:

  1. Could you also share the code to get the data of intensity vs m/z? I am having a hard time initializing VARIANT type object.

    回覆刪除
    回覆
    1. VARIANT means object in C#, you should convert it to other type.
      For example, get ms/ms list by GetMassListFromScanNum(),

      ==example code START==
      int ScanNumber = 100
      double CentroidPeakWidth = 0.0;
      object MassList = null;
      object PeakFlags = null;
      int ArraySize = 0;

      rawfile.GetMassListFromScanNum(ref ScanNumber, null, 1, 0, 0, 0, ref CentroidPeakWidth, ref MassList, ref PeakFlags, ref ArraySize);
      double[,] mslist = (double[,])MassList;

      ==example code END==

      object MassList should be converted to double[,] and it means mass and intensity

      刪除
  2. further, will the following:

    for (long j = 0; j < arrLen; j++)
    {
    dMass = mslist[0, j];
    dIntensity = mslist[1, j];
    }

    is right approach to get the mass and intensity?

    回覆刪除
    回覆
    1. Yes, if your arrLen=mslist.Length

      for (int j = 0; j < mslist.Length; j++)
      {
      dMass = mslist[0, j];
      dIntensity = mslist[1, j];
      }

      刪除