-
Notifications
You must be signed in to change notification settings - Fork 0
/
convertToBinNew.m
69 lines (64 loc) · 2.6 KB
/
convertToBinNew.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
function [binNew]= convertToBin(dims, binStruct,sorted_mz_indx,sorted_mz_value,TempSp,method)
% map sorted values back to the image
dbstop if error
method = 'restrict';
% Initialise the bin that holds the alignment results
field1 = 'mz'; value1 = zeros(1,1);
field2 = 'spectra'; value2 =zeros(1,1);
field3 = 'intensity'; value3=zeros(1,1);
field4 = 'centroidspectra'; value4 =zeros(1,1);
field5 = 'centroidmz'; value5 =zeros(1,1);
binNew = repmat(struct(field1,value1,field2,value2, field3,...
value3,field4,value4,field5,value5),length(binStruct),1 );
tic;
h = waitbar(0,'Please wait...');
steps = length(binStruct);
counter=1;
binCounter = 1;
for i =1 :length(binStruct)
binTemp = binStruct{1,i};
MZ_Loc = sorted_mz_indx(counter:(counter+length(binTemp)-1));
MZ = sorted_mz_value(counter:(counter+length(binTemp)-1));
int = TempSp(counter:(counter+length(binTemp)-1));
counter = counter+length(binTemp);
j=1;
while( j <= length(binTemp))
indx = find(binTemp == binTemp(j));
j = j+length(indx);
binNew(binCounter).spectra = MZ_Loc(indx);
binNew(binCounter).mz = MZ(indx);
binNew(binCounter).intensity = int(indx);
% for k = 1:length(temp1)
% binNew(binCounter).mz(k) = mz(temp1(k), temp2(k), temp3(k));
% binNew(binCounter).intensity(k) = sp(temp1(k), temp2(k), temp3(k));
% end
if(strcmp(method , 'restrict'))
[temp1, temp2, temp3] = ind2sub(dims, MZ_Loc(indx));
if(length(temp1)>1)
[c,ia,ic]=unique([temp1'; temp2']','rows');
if(max(ic)<length(temp1))
% find duplicates
for k = 1:length(ia)
duplicatedIndex = find(ic==ia(k));
if(length(duplicatedIndex)>0)
[~, indMaxIntensity] = max(binNew(binCounter).intensity(duplicatedIndex));
binNew(binCounter).mz(setdiff(duplicatedIndex, duplicatedIndex(indMaxIntensity)))= -1;
end
end
end
end
indExclude = find(binNew(binCounter).mz == -1);
binNew(binCounter).mz(indExclude)=[];
binNew(binCounter).intensity(indExclude)=[];
binNew(binCounter).spectra(indExclude)=[];
end
binNew(binCounter).centroidmz = centroid( binNew(binCounter).mz, binNew(binCounter).intensity);
binNew(binCounter).centroidspectra = centroid( binNew(binCounter).intensity , binNew(binCounter).mz);
clear temp1 temp2 temp3
binCounter = binCounter+1;
end
waitbar(i / steps);
end
toc;
close(h)
end