Commit 96cb4f15 authored by Antonio Politi's avatar Antonio Politi
Browse files

class to read and write xml from fluctualizer

parent 32b1f41f
%% test functions for reading and outputting xml files generated by FA
% class xmlFA
if ispc
fname_FA = 'C:\Users\toni\Dropbox\NPCMaturation\matlabcode\FCS\example_data\Protein\0001_R1_P1_K1_Ch2.zen.1c.xml';
fname_FA_w = 'C:\Users\toni\Dropbox\NPCMaturation\matlabcode\FCS\example_data\Protein\0001_R1_P1_K1_Ch2.zen.1c_w.xml';
fname_ML = 'C:\Users\toni\Dropbox\NPCMaturation\matlabcode\FCS\example_data\Protein\0001_R1_P1_K1_Ch2.zen.1c_opt.xml';
fname_ML_w = 'C:\Users\toni\Dropbox\NPCMaturation\matlabcode\FCS\example_data\Protein\0001_R1_P1_K1_Ch2.zen.1c_opt_w.xml';
%fname_in = 'C:\Users\toni\Dropbox\NPCMaturation\matlabcode\FCS\example_data\488_561nm_R2_P1_K1_Ch2-Ch1.zen.1c.xml';
%fname_out = 'C:\Users\toni\Dropbox\NPCMaturation\matlabcode\FCS\example_data\488_561nm_R2_P1_K1_Ch2-Ch1.zen.1c_tmp.xml';
end
%% compare fit from
MO = proteinFcsmodel();
par_FA = xmlFA.readpar(fname_FA);
par_FA_w = xmlFA.readpar(fname_FA_w);
par_ML = xmlFA.readpar(fname_ML);
par_ML_w = xmlFA.readpar(fname_ML_w);
dataC = MO.readFAxml({fname_FA});
idxs = MO.getDataIndexes(dataC{1}, MO.tauBoundary);
fitFA = MO.Gdiffcomp(par_FA(1,:), dataC{1}(idxs(1):end-idxs(2),:), 0);
fitFA_w = MO.Gdiffcomp(par_FA_w(1,:), dataC{1}(idxs(1):end-idxs(2),:), 0);
fitML = MO.Gdiffcomp(par_ML(1,:), dataC{1}(idxs(1):end-idxs(2),:), 0);
fitML_w = MO.Gdiffcomp(par_ML_w(1,:), dataC{1}(idxs(1):end-idxs(2),:), 0);
semilogx(dataC{1}(idxs(1):end-idxs(2),1), [fitFA fitFA_w fitML fitML_w]);
ylabel('residuals')
xlabel('tau (us)')
legend('fitFA','fitFA weighted', 'fitML', 'fitML weighted')
%%
xmlFA.readcor(fname_in);
%%
fit = xmlFA.readfit(fname_in);
%% write fit to xml file
clear('MO');
MO = dyeFcsmodel();
dataC = MO.readFAxml({fname_in});
%fit Ch1
parf1 = MO.fitThetaTtauT(dataC{1}(:, [1 2 3]), 5);
%fit Ch2
parf2 = MO.fitThetaTtauT(dataC{1}(:, [1 4 5]), 5);
idxs = MO.getDataIndexes(dataC{1}, MO.tauBoundary);
fit1 = MO.Gcomp(MO.parvecToparstruct(parf1), dataC{1}(idxs(1):end-idxs(2),1));
fit2 = MO.Gcomp(MO.parvecToparstruct(parf2), dataC{1}(idxs(1):end-idxs(2),1));
node = xmlFA.readnode(fname_in);
xmlFA.writepar(node, [parf1;parf2], struct2array(MO.lb), struct2array(MO.hb));
xmlFA.writefit(node, [fit1 fit2], idxs, MO.model);
xmlFA.writenode(fname_out, node);
classdef xmlFA
% XMLFA class that contains static method to access and write xml files generated from Fluctualizer
% XMLFA class that contains static method to access and write xml files
% generated from Fluctualizer, Malte Wachsmuth Software
% Note that java is base 0, matlab is base 1
methods(Static)
function docNode = readnode(fname)
docNode = xmlread(fname);
end
function ann = readannotation(fname)
% READANNOTATION reads an xml document fname and returns annotation
% ann = character ASCII code!!
docNode = xmlread(fname);
nodes = docNode.getElementsByTagName('U8');
for i=0:nodes.getLength-1
node = nodes.item(i);
switch(char(node.item(1).getTextContent))
case 'm_nAnnotation'
ann = str2num(node.item(3).getTextContent);
break;
end
end
end
function docNode = writeannotation(docNode, ann)
% ann is ASCII chracter code
nodes = docNode.getElementsByTagName('U8');
for i=0:nodes.getLength-1
node = nodes.item(i);
switch(char(node.item(1).getTextContent))
case 'm_nAnnotation'
node.item(3).setTextContent(num2str(ann));
break;
end
end
end
function par = readpar(fname)
% REAEDPAR reads an xml document fname and returns fitting parameter
% par = [par1_ch1 par2_ch1 ...; par1_ch2 ...; par1_xc ....]
......@@ -24,61 +56,175 @@ classdef xmlFA
break;
end
end
par = par(3,1:9);
par = par(:,1:9);
end
function ann = readannotation(fname)
% READANNOTATION reads an xml document fname and returns annotation
% ann = character ASCII code!!
docNode = xmlread(fname);
nodes = docNode.getElementsByTagName('U8');
for i=0:nodes.getLength-1
node = nodes.item(i);
switch(char(node.item(1).getTextContent))
case 'm_nAnnotation'
ann = str2num(node.item(3).getTextContent);
break;
end
end
end
function docNode = writepar(docNode, par)
function docNode = writepar(docNode, par, lb, hb)
% WRITEPAR write model parameter to xml file
% These are 9 parameters base of jave is 0
nodes = docNode.getElementsByTagName('Array_DBL');
for i=0:nodes.getLength-1
node = nodes.item(i);
switch(char(node.item(1).getTextContent))
case {'m_rFitParaLower1', 'm_rFitParaLower2', 'm_rFitParaLowerX'}
nodeVal = node.getElementsByTagName('Val');
for j = 0:8
nodeVal.item(j).setTextContent(num2str(lb(1,j+1)));
end
case {'m_rFitParaUpper1','m_rFitParaUpper2', 'm_rFitParaUpperX'}
nodeVal = node.getElementsByTagName('Val');
for j = 0:8
nodeVal.item(j).setTextContent(num2str(hb(1,j+1)));
end
case 'm_rFitParaValue1'
nodeVal = node.getElementsByTagName('Val');
for j = 0:8
nodeVal.item(j).setTextContent(num2str(par(1,j+1)));
end
case 'm_rFitParaValue2'
nodeVal = node.getElementsByTagName('Val');
for j = 0:8
nodeVal.item(j).setTextContent(num2str(par(2,j+1)));
if size(par,1) > 1
nodeVal = node.getElementsByTagName('Val');
for j = 0:8
nodeVal.item(j).setTextContent(num2str(par(2,j+1)));
end
end
case 'm_rFitParaValueX'
if size(par,1) > 2
nodeVal = node.getElementsByTagName('Val');
for j = 0:8
nodeVal.item(j).setTextContent(num2str(par(3,j+1)));
end
end
end
end
nodes = docNode.getElementsByTagName('Array_I32');
for i=0:nodes.getLength-1
node = nodes.item(i);
switch(char(node.item(1).getTextContent))
case {'m_bFitParaFixed1', 'm_bFitParaFixed2', 'm_bFitParaFixedX'}
nodeVal = node.getElementsByTagName('Val');
for j = 0:8
nodeVal.item(j).setTextContent(num2str(par(3,j+1)));
nodeVal.item(j).setTextContent(num2str(1));
end
break;
end
end
end
function docNode = writeannotation(docNode, ann)
% ann is ASCII chracter code
nodes = docNode.getElementsByTagName('U8');
function [fit, idx] = readfit(fname)
%READFIT rite fit for all channels to note
docNode = xmlread(fname);
nodes = docNode.getElementsByTagName('Array_DBL');
%% get nodes with Array_DBL (these are the entries for ACfits)
for i=0:nodes.getLength-1
node = nodes.item(i);
switch(char(node.item(1).getTextContent))
case 'm_nAnnotation'
node.item(3).setTextContent(num2str(ann));
case 'm_rCorrelationLagTime'
tau = read_xml_array(node);
maxV = find(tau==0);
if isempty(maxV)
maxV = length(tau);
else
maxV = maxV(1)-1;
end
case 'm_rFitCorrelation1'
fit1 = read_xml_array(node);
case 'm_rFitCorrelation2'
fit2 = read_xml_array(node);
case 'm_rFitCorrelationX'
fitX = read_xml_array(node);
break;
end
end
nodes = docNode.getElementsByTagName('U32');
for i=0:nodes.getLength-1
node = nodes.item(i);
switch(char(node.item(1).getTextContent))
case 'm_nFitFirst'
idx(1) = str2num(node.item(3).getTextContent);
case 'm_nFitSecond'
idx(2) = str2num(node.item(3).getTextContent);
break;
end
end
% this does not account for start
fit = [tau' fit1' fit2' fitX'];
fit = fit(idx(1)+1:maxV-idx(2),:);
end
function docNode = writefit(docNode, fit, idxs, model)
%WRITEFIT write fit for of all channels
% docNode - xml document node
% fit - a [N_tau 3] array containig fits for Ch1, Ch2 and XC
nodes = docNode.getElementsByTagName('Array_DBL');
%m_nFitFirst start index base 0 of fit
%m_nFitSecond end index of fit end-m_nFitSecond. 0 is complete data set
for i=0:nodes.getLength-1
node = nodes.item(i);
switch(char(node.item(1).getTextContent))
case 'm_rFitCorrelation1'
nodeVal = node.getElementsByTagName('Val');
for j = idxs(1)-1:length(fit)+idxs(1)-2
nodeVal.item(j).setTextContent(num2str(fit(j+2-idxs(1),1)));
end
case 'm_rFitCorrelation2'
if size(fit, 2) > 1 && sum(fit(:,2)) ~= 0
nodeVal = node.getElementsByTagName('Val');
for j = idxs(1)-1:length(fit)+idxs(1)-2
nodeVal.item(j).setTextContent(num2str(fit(j+2-idxs(1),2)));
end
end
case 'm_rFitCorrelationX'
if size(fit, 2) > 2 && sum(fit(:,3)) ~= 0
nodeVal = node.getElementsByTagName('Val');
for j = idxs(1)-1:length(fit)+idxs(1)-2
nodeVal.item(j).setTextContent(num2str(fit(j+2-idxs(1),3)));
end
end
end
end
nodes = docNode.getElementsByTagName('U32');
for i=0:nodes.getLength-1
node = nodes.item(i);
switch(char(node.item(1).getTextContent))
case 'm_nFitFirst'
node.item(3).setTextContent(num2str(idxs(1)-1));
case 'm_nFitSecond'
node.item(3).setTextContent(num2str(idxs(2)));
case 'm_nFitModelNo'
node.item(3).setTextContent(num2str(model));
end
end
nodes = docNode.getElementsByTagName('I32');
for i=0:nodes.getLength-1
node = nodes.item(i);
switch(char(node.item(1).getTextContent))
case 'm_bFitted1'
if sum(fit(:,1)) ~= 0
nodeVal = node.getElementsByTagName('Val');
nodeVal.item(0).setTextContent(num2str(1));
end
case 'm_bFitted2'
if size(fit,2) > 1 && sum(fit(:,2)) ~= 0
nodeVal = node.getElementsByTagName('Val');
nodeVal.item(0).setTextContent(num2str(1));
end
case 'm_bFittedX'
if size(fit,2) > 2 && sum(fit(:,3)) ~= 0
nodeVal = node.getElementsByTagName('Val');
nodeVal.item(0).setTextContent(num2str(1));
end
end
end
end
function writenode(fname, docNode)
......@@ -87,7 +233,7 @@ classdef xmlFA
function cor = readcor(fname)
% READCOR read an xml document and return correlation data. Cor is array dim = [N_time 7]
% cor = [tau AC1 AC1_error AC2 AC2_error XC XC_error]
% cor = [tau AC1 AC1_error AC2 AC2_error XC XC_error]
docNode = xmlread(fname);
nodes = docNode.getElementsByTagName('Array_DBL');
......@@ -125,18 +271,22 @@ classdef xmlFA
end
end
function xmlwriteFA(fname, docNode)
xmlwrite(fname, docNode); %CR and LF darstellung probleme
% TODO Annotation is becomes NaN should be a free space instead
end
function arrayO = read_xml_array(node, idx)
% READ_XML_ARRAY read an array as given by FA Array_DBL, e.g.
%<Array_DBL>
%<Name>m_rCorrelationLagTime</Name>
%<Dimsize>250</Dimsize>
%<Val>1.00000000000000</Val>
if nargin < 2
%first 3 entries are not the values of array
idx = 4;
end
arrayO = strsplit(char(node.getTextContent));
arrayO = str2double(arrayO(idx:end-1));
% READ_XML_ARRAY read an array as given by FA Array_DBL, e.g.
%<Array_DBL>
%<Name>m_rCorrelationLagTime</Name>
%<Dimsize>250</Dimsize>
%<Val>1.00000000000000</Val>
if nargin < 2
%first 3 entries are not the values of array
idx = 4;
end
arrayO = strsplit(char(node.getTextContent));
arrayO = str2double(arrayO(idx:end-1));
end
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment