make_motif34lib.m 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. function make_motif34lib
  2. %MAKE_MOTIF34LIB Auxiliary motif library function
  3. %
  4. % make_motif34lib;
  5. %
  6. % This function generates the motif34lib.mat library required for all
  7. % other motif computations.
  8. %
  9. %
  10. % Mika Rubinov, UNSW, 2007-2010
  11. %#ok<*ASGLU>
  12. [M3,M3n,ID3,N3]=motif3generate;
  13. [M4,M4n,ID4,N4]=motif4generate;
  14. save motif34lib;
  15. function [M,Mn,ID,N]=motif3generate
  16. n=0;
  17. M=false(54,6); %isomorphs
  18. CL=zeros(54,6,'uint8'); %canonical labels (predecessors of IDs)
  19. cl=zeros(1,6,'uint8');
  20. for i=0:2^6-1 %loop through all subgraphs
  21. m=dec2bin(i);
  22. m=[num2str(zeros(1,6-length(m)), '%d') m]; %#ok<AGROW>
  23. G=str2num ([ ...
  24. '0' ' ' m(3) ' ' m(5) ;
  25. m(1) ' ' '0' ' ' m(6) ;
  26. m(2) ' ' m(4) ' ' '0' ]); %#ok<ST2NM>
  27. Ko=sum(G,2);
  28. Ki=sum(G,1).';
  29. if all(Ko|Ki) %if subgraph weakly-connected
  30. n=n+1;
  31. cl(:)=sortrows([Ko Ki]).';
  32. CL(n,:)=cl; %assign motif label to isomorph
  33. M(n,:)=G([2:4 6:8]);
  34. end
  35. end
  36. [u1,u2,ID]=unique(CL,'rows'); %convert CLs into motif IDs
  37. %convert IDs into Sporns & Kotter classification
  38. id_mika= [1 3 4 6 7 8 11];
  39. id_olaf= -[3 6 1 11 4 7 8];
  40. for id=1:length(id_mika)
  41. ID(ID==id_mika(id))=id_olaf(id);
  42. end
  43. ID=abs(ID);
  44. [X,ind]=sortrows(ID);
  45. ID=ID(ind,:); %sort IDs
  46. M=M(ind,:); %sort isomorphs
  47. N=sum(M,2); %number of edges
  48. Mn=uint32(sum(repmat(10.^(5:-1:0),size(M,1),1).*M,2)); %M as a single number
  49. function [M,Mn,ID,N]=motif4generate
  50. n=0;
  51. M=false(3834,12); %isomorphs
  52. CL=zeros(3834,16,'uint8'); %canonical labels (predecessors of IDs)
  53. cl=zeros(1,16,'uint8');
  54. for i=0:2^12-1 %loop through all subgraphs
  55. m=dec2bin(i);
  56. m=[num2str(zeros(1,12-length(m)), '%d') m]; %#ok<AGROW>
  57. G=str2num ([ ...
  58. '0' ' ' m(4) ' ' m(7) ' ' m(10) ;
  59. m(1) ' ' '0' ' ' m(8) ' ' m(11) ;
  60. m(2) ' ' m(5) ' ' '0' ' ' m(12) ;
  61. m(3) ' ' m(6) ' ' m(9) ' ' '0' ]); %#ok<ST2NM>
  62. Gs=G+G.';
  63. v=Gs(1,:);
  64. for j=1:2
  65. v=any(Gs(v~=0,:),1)+v;
  66. end
  67. if v %if subgraph weakly connected
  68. n=n+1;
  69. G2=(G*G)~=0;
  70. Ko=sum(G,2);
  71. Ki=sum(G,1).';
  72. Ko2=sum(G2,2);
  73. Ki2=sum(G2,1).';
  74. cl(:)=sortrows([Ki Ko Ki2 Ko2]).';
  75. CL(n,:)=cl; %assign motif label to isomorph
  76. M(n,:)=G([2:5 7:10 12:15]);
  77. end
  78. end
  79. [u1,u2,ID]=unique(CL,'rows'); %convert CLs into motif IDs
  80. [X,ind]=sortrows(ID);
  81. ID=ID(ind,:); %sort IDs
  82. M=M(ind,:); %sort isomorphs
  83. N=sum(M,2); %number of edges
  84. Mn=uint64(sum(repmat(10.^(11:-1:0),size(M,1),1).*M,2)); %M as a single number