IRootLab
An Open-Source MATLAB toolbox for vibrational biospectroscopy
dataio_txt_pir.m
Go to the documentation of this file.
1 %> @brief "Pirouette" TXT loader/saver
2 %>
3 %> This is how a dataset will look like by selecting the whole spreadsheet in Pirouette (Informetrix Inc.) and pasting
4 %> it into Excel. See Figure 1 for specification.
5 %>
6 %> @image html dataformat_pir.png
7 %> <center>Figure 1 - "Pirouette" TXT file type</center>
9  properties
10  %> (optional) Image height. If specified, will assign it the loaded dataset
11  height = [];
12  %> ='hor'. Whether the pixels are taken horizontally ('hor') or vertically ('ver') to form the image.
13  %> It was found that OPUS numbers the point in the image map left-right, bottom-up, hence 'hor'.
14  %> Same as irdata.direction (although irdata.direction has a different default).
15  direction = 'hor';
16  end;
17 
18  methods
19  %> Loader
20  function data = load(o)
21 
22  data = irdata();
23 
24  [no_cols, delimiter] = get_no_cols_deli(o.filename);
25 
26 
27  % Mounts masks
28  mask_header = ['%q' repmat('%f', 1, no_cols-2) '%q'];
29 
30  mask_data = ['%q' repmat('%f', 1, no_cols-2) '%q'];
31 
32  % Opens for the second time in order to actually read the file
33  fid = fopen(o.filename);
34  c_header = textscan(fid, mask_header, 1, 'Delimiter', delimiter);
35  x = cell2mat(c_header(2:end-1));
36  trend = 0;
37  for i = 2:length(x)
38  if trend == 0
39  trend = sign(x(i)-x(i-1));
40  elseif sign(x(i)-x(i-1)) ~= trend
41  irerror('There is something wrong with this text file!');
42  end;
43  end;
44 
45 
46 
47  c_data = textscan(fid, mask_data, 'Delimiter', delimiter);
48  fclose(fid);
49 
50  % Resolves easy ones
51  data.X = cell2mat(c_data(2:end-1));
52  data.fea_x = x;
53 
54  % first column will be parsed to extract data.groupcodes
55  % all content at first dot and beyond is discarded
56  labels = c_data{1};
57  no_obs = length(labels);
58  data.groupcodes = cell(no_obs, 1);
59  for i = 1:no_obs
60  label = labels{i};
61  idxs = regexp(label, '\.');
62 
63  if isempty(idxs)
64  idx = length(label)+1;
65  else
66  idx = idxs(end); % The aim is to take everything before the last '.'
67  end;
68 
69  data.groupcodes{i} = label(1:idx-1);
70  end;
71 
72 
73 
74  % Resolves classes
75  clalpha = c_data{end};
76  if ~iscell(clalpha)
77  clalpha = cellfun(@num2str, num2cell(clalpha), 'UniformOutput', 0);
78  end;
79  clalpha = cellfun(@strip_quotes, clalpha, 'UniformOutput', 0);
80  data.classlabels = unique(clalpha)';
81 
82  data.classes(no_obs, 1) = -1; % pre-allocates
83  for i = 1:no_obs
84  data.classes(i) = find(strcmp(data.classlabels, clalpha{i}))-1;
85  end;
86 
87 
88  data.assert_not_nan();
89  data.filename = o.filename;
90  data.filetype = 'txt2';
91  data = data.make_groupnumbers();
92  if ~isempty(o.height)
93  data.height = o.height;
94  data.direction = o.direction;
95  end;
96  end;
97 
98 
99  %> Saver
100  function o = save(o, data)
101 
102  h = fopen(o.filename, 'w');
103  if h < 1
104  irerror(sprintf('Could not create file ''%s''!', o.filename));
105  end;
106 
107  nel_group = size(data.groupcodes, 1);
108  labels = classes2labels(data.classes, data.classlabels);
109 
110  fwrite(h, [sprintf('\t') sprintf('%g\t', data.fea_x) sprintf('\n')]);
111  for i = 1:data.no
112  if i > nel_group
113  groupcode = '';
114  else
115  groupcode = data.groupcodes{i};
116  end;
117  fwrite(h, [groupcode sprintf('\t') sprintf('%g\t', data.X(i, :)) labels{i} sprintf('\n')]);
118  end;
119 
120  fclose(h);
121 
122  irverbose(sprintf('Just saved file "%s"', o.filename), 2);
123  end;
124  end
125 end
function irverbose(in s, in level)
function irerror(in s)
Dataset class.
Definition: irdata.m:30
Pre-processing block base class.
Definition: pre.m:2
function classes2labels(in classes, in labels)
Dataset loader/saver common class.
Definition: dataio.m:2
"Pirouette" TXT loader/saver
Definition: dataio_txt_pir.m:8
Analysis Session (AS) base class.
Definition: as.m:6
function strip_quotes(in s)
function get_no_cols_deli(in filename)