rm(list=ls());

tbd = read.table ( "_dd.tab", sep="\t", fill=T, header=T, row.names=1);
tbm = read.table( "_mm.tab", sep="\t", fill=T, header=T, row.names=1);

dd = as.matrix( tbd );
mm = as.matrix( tbm );
cnum = length( names(tbd) )

limit = 999999; #this is maximila dijkstra distance

#find the column with max dijk distance
limit.flag = c();
for  ( i in 1:cnum ) {
  if ( dd[i,1] == limit) limit.flag = c( limit.flag, i);
}
limit.flag;
dd[ limit.flag,    ];
dd[    ,limit.flag ];
 
 
#get the columns without limit.flag;
 old.col = seq(1:cnum);
 new.col = c();
 for( i in 1:cnum) {
    yes = 1;
    for ( j in 1:length(limit.flag) ) {
   	if ( old.col[i] == limit.flag[j] ) { yes = 0; }
    }
    if (yes==1){ new.col = c( new.col, old.col[i] ); }
 }
new.col;

dd2 = dd[ new.col, new.col ];
mm2 = nn[ new.col, new.col ];

product = dd2; # initilize the product matrix.

for (i in 1:length(new.col) ) {
 for (j in 1:length(new.col) ) {
     product[i,j] = dd2[i,j] * mm2[i,j];
 }
}

dx = as.dist(product, diag=T);

h = hclust(dx);
plot(h, main="hclust on ddxnn, 3 days");
