::: UniChrom BEL RUS DEU ENG
Main Page / Products / < Back 

Writing UniChrom file converter for custom format support

UniChrom-DDK provides set of examples either with header files to write own analytic file format support. This instruction may help for the following tasks:

The article present fastest way to write file converter using OO library and ObjectPascal language (FPC or Delphi). No matter Windows or Linux.

unit expmain;

interface
uses
  baseconv, // base converter class
    cnvtypes, // converter constants CNV_OK, CNV_FAIL, FLAG_WRITE ... 
  ucintf; // UniChrom interface

type
  TExpFormat=class(TBaseConv)
  public
    class function FormatName:utf8string;override;
    class function FormatFlags:integer;override;
    function Imp(hSp:hSPEC;const Fname:PChar):integer;override;
    function Exp(hSp:hSPEC;const Fname:PChar):integer;override;
  end;

implementation
uses 
   SysUtils,Classes,
   ucimp; // unit containing UniChrom interface struct (guc)  

{ TExpFormat }

function TExpFormat.Imp(hSp: hSPEC; const Fname: PChar): integer;
begin
  Result:=CNV_FAIL; // We are developing exporter, not importer
end;


function TExpFormat.Exp(hSp: hSPEC; const Fname: PChar): integer;
var
  fs:TStream;
  vv:variant;
  zSp:HSPEC; // spectrum handle
  i,j,lays,size,vt:integer;
  dt:double;
  bs:string;
begin
  Result:=CNV_FAIL;
  try
    try
      fs:=TFileStream.Create(FName,fmCreate);
      
      // List of supported properties can be examined saving any dataset as .UWX or .XML
            // we are going to export all the mass-spectra attached to the layer #1
			
            // if this layer contains incapsulated spectra ?
      if gUc.GetPropValue(hSp,1,'zSpec',vv) then
      begin
        ptrUint(zSp):=ptrUint(vv);
      end else exit;

      if (zSp=HINVALID) then exit; // no spectra, just chromatogram


      // get the number of layers i.e. mass-scans
      if not guc.GetPropValue(zSp,-1,'Layers',vv) then exit;
      lays:=vv;
      for i:=1 to lays do // across scans
      begin
        if not guc.GetPropValue(zSp,i,'Size',vv) then exit;
        size:=vv;
        if not guc.GetPropValue(zSp,i,'LastChanged',vv) then exit;
        dt:=vv;
        bs:=format('%g:',[dt]); // timestamp of each layer is a scan time moment
        for j:=1 to size do // along each scan
        bs:=bs+format('(%g;%g);',[guc.GetXData(zSp,i,j), // X-data (m/z)
                            guc.GetData(zSp,i,j)]); // Y-data (intensity)

        bs:=bs+sLineBreak;
        fs.Write(bs[1],length(bs));

      end;

      Result:=CNV_OK; // Yes, all finished
    except
      on E:Exception do
        writeln(E.Message); // report problem to STDOUT
    end;
  finally
    fs.free;
  end;
end;

class function TExpFormat.FormatFlags: integer;
begin
  Result:=FLAG_WRITE; // Our file filter only EXPORTS data
end;

class function TExpFormat.FormatName: utf8string;
begin
  Result:='Export all spectra as Text (*.zzz)|*.zzz'; // Make the file type UNIQUE
end;


initialization
  CnvFact.Register(TExpFormat); // register converter with factory at DLL initialization time
end.