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
0046
0047 varargin=upper(varargin);
0048
0049
0050
0051 toKeep={};
0052 toMerge={};
0053 I=regexp(varargin,'=','split');
0054 for i=1:numel(varargin)
0055 if numel(I{i})==1
0056 toKeep=[toKeep;I{i}];
0057 else
0058 J=regexp(I{i}(1),' ','split');
0059 K=regexp(I{i}(2),' ','split');
0060 toKeep=[toKeep;J{1}(:)];
0061 toMerge=[toMerge;K{1}(:)];
0062 end
0063 end
0064
0065
0066 if ~isempty(intersect(toKeep,toMerge))
0067 dispEM('There are inconsistencies where one or more compartment(s) should be both kept and merged to another');
0068 end
0069
0070
0071
0072 uComps=upper(geneScoreStructure.compartments);
0073 J=[uComps;{'OTHER'}];
0074
0075 if ~isempty(setdiff([toKeep;toMerge],J))
0076 dispEM('There are compartment in the rules that are not in geneScoreStructure.compartments');
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 dispEM('It is not allowed to have rules like "A B=C D" (map more than one compartment to more than one compartment)');
0099 end
0100
0101 if ~all(k) || ~all(l)
0102 dispEM('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');
0103 end
0104
0105
0106
0107 S=max(geneScoreStructure.scores(:,K),[],2);
0108 for j=1:numel(L)
0109
0110
0111 geneScoreStructure.scores(:,L(j))=max(geneScoreStructure.scores(:,L(j)),S./numel(L));
0112 end
0113
0114
0115 geneScoreStructure.compartments(K)=[];
0116 geneScoreStructure.scores(:,K)=[];
0117 uComps(K)=[];
0118 end
0119 end
0120
0121
0122
0123 J=find(~ismember(uComps,toKeep));
0124 if any(J)
0125 if any(otherIndex)
0126 K=regexp(I{otherIndex}(1),' ','split');
0127 [l L]=ismember(K{1},uComps);
0128 if l==1 && numel(l)==1
0129 S=max(geneScoreStructure.scores(:,J),[],2);
0130 geneScoreStructure.scores(:,L)=max(geneScoreStructure.scores(:,L),S);
0131 else
0132 dispEM('Could not map "other" to more than one compartment');
0133 end
0134 else
0135 dispEM('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',false);
0136 end
0137
0138
0139 geneScoreStructure.compartments(J)=[];
0140 geneScoreStructure.scores(:,J)=[];
0141 end
0142
0143
0144 I=max(geneScoreStructure.scores,[],2);
0145 geneScoreStructure.scores=bsxfun(@times, geneScoreStructure.scores, 1./I);
0146
0147
0148
0149 I=find(isnan(geneScoreStructure.scores(:,1)));
0150 if any(I)
0151 dispEM('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:',false,geneScoreStructure.genes(I));
0152 geneScoreStructure.scores(I,:)=[];
0153 geneScoreStructure.genes(I)=[];
0154 end