0001 function geneScoreStructure=mapCompartments(geneScoreStructure,varargin)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045 varargin=upper(varargin);
0046
0047
0048
0049 toKeep={};
0050 toMerge={};
0051 I=regexp(varargin,'=','split');
0052 for i=1:numel(varargin)
0053 if numel(I{i})==1
0054 toKeep=[toKeep;I{i}];
0055 else
0056 J=regexp(I{i}(1),' ','split');
0057 K=regexp(I{i}(2),' ','split');
0058 toKeep=[toKeep;J{1}(:)];
0059 toMerge=[toMerge;K{1}(:)];
0060 end
0061 end
0062
0063
0064 if ~isempty(intersect(toKeep,toMerge))
0065 EM='There are inconsistencies where one or more compartment(s) should be both kept and merged to another';
0066 dispEM(EM);
0067 end
0068
0069
0070
0071 uComps=upper(geneScoreStructure.compartments);
0072 J=[uComps,{'OTHER'}];
0073
0074 if ~isempty(setdiff([toKeep;toMerge],J))
0075 EM='There are compartment in the rules that are not in geneScoreStructure.compartments';
0076 dispEM(EM);
0077 end
0078
0079
0080 otherIndex=[];
0081
0082 for i=1:numel(I)
0083 if numel(I{i})>1
0084
0085 J=regexp(I{i}(2),' ','split');
0086 if strcmpi(J{1},'other')
0087 otherIndex=i;
0088 continue;
0089 end
0090 [k, K]=ismember(J{1},uComps);
0091
0092
0093 J=regexp(I{i}(1),' ','split');
0094 [l, L]=ismember(J{1},uComps);
0095
0096
0097 if numel(K)>1 && numel(L)>1
0098 EM='It is not allowed to have rules like "A B=C D" (map more than one compartment to more than one compartment)';
0099 dispEM(EM);
0100 end
0101
0102 if ~all(k) || ~all(l)
0103 EM='Error in mapping. This most likely means that some compartment(s) are mapped to different compartments in different rules. Use A B=C if you want to map C to several compartments';
0104 dispEM(EM);
0105 end
0106
0107
0108
0109 S=max(geneScoreStructure.scores(:,K),[],2);
0110 for j=1:numel(L)
0111
0112
0113 geneScoreStructure.scores(:,L(j))=max(geneScoreStructure.scores(:,L(j)),S./numel(L));
0114 end
0115
0116
0117 geneScoreStructure.compartments(K)=[];
0118 geneScoreStructure.scores(:,K)=[];
0119 uComps(K)=[];
0120 end
0121 end
0122
0123
0124
0125 J=find(~ismember(uComps,toKeep));
0126 if any(J)
0127 if any(otherIndex)
0128 K=regexp(I{otherIndex}(1),' ','split');
0129 [l, L]=ismember(K{1},uComps);
0130 if l==1 && numel(l)==1
0131 S=max(geneScoreStructure.scores(:,J),[],2);
0132 geneScoreStructure.scores(:,L)=max(geneScoreStructure.scores(:,L),S);
0133 else
0134 EM='Could not map "other" to more than one compartment';
0135 dispEM(EM);
0136 end
0137 else
0138 EM='There are compartments that are not defined if they should be kept or removed. Use "A=other" or define more rules if you do not want them to be deleted';
0139 dispEM(EM,false);
0140 end
0141
0142
0143 geneScoreStructure.compartments(J)=[];
0144 geneScoreStructure.scores(:,J)=[];
0145 end
0146
0147
0148 I=max(geneScoreStructure.scores,[],2);
0149 geneScoreStructure.scores=bsxfun(@times, geneScoreStructure.scores, 1./I);
0150
0151
0152
0153 I=find(isnan(geneScoreStructure.scores(:,1)));
0154 if any(I)
0155 EM='The following genes had score 0.0 in all compartments. They have been removed from the structure. Consider using more rules or "A=other" in order to prevent this:';
0156 dispEM(EM,false,geneScoreStructure.genes(I));
0157 geneScoreStructure.scores(I,:)=[];
0158 geneScoreStructure.genes(I)=[];
0159 end
0160 end