static/js/algorithm.js
/**
 * Compute the silhouette coefficient for each contig.
 * @function silhouetteSample
 * @param {number[]} x - input data
 * @param {number[]} label - labels of input data
 * @param {number[]} dist - pairwise distances among input data
 * @return {number[]} silhouette coefficient of each contig
 * @description The silhouette coefficient measures how similar a contig is to
 * other contigs in the same bin, as in contrast to contigs in other bins.
 * @see {@link https://en.wikipedia.org/wiki/Silhouette_(clustering)}
 * @description It requires calculation of a distance matrix of all contigs,
 * which is expensive. Therefore it is pre-calculated, stored, and fed to the
 * current function.
 */
Compute the silhouette coefficient for each contig.
Compute the silhouette coefficient for each contig .
function silhouetteSample(x, label, dist) {
  const n = x.length;
  const count = bincount(label); 
  const c = count.length;
  let distIn;  
  let distOut; 
  let li;      
  let ii;      
  let idx;     
  let j, k;    
  const res = Array(n).fill();
  for (let i = 0; i < n; i++) {
    li = label[i];
    if (count[li] > 1) {
      distIn = 0;
      distOut = Array(c).fill(0);
      ii = n * i - i * (i + 3) / 2 - 1;
      for (j = 0; j < n; j++) {
        if (i < j) {
          idx = ii + j;
        } else if (i > j) {
          idx = n * j - j * (j + 3) / 2 + i - 1;
        } else {
          continue;
        }
        if (li === label[j]) {
          distIn += dist[idx];
        } else {
          distOut[label[j]] += dist[idx];
        }
      }
      for (k = 0; k < c; k++) {
        distOut[k] /= count[k];
      }
      distOut = Math.min.apply(null, distOut.filter(Boolean));
      distIn /= (count[li] - 1);
      res[i] = (distOut - distIn) / Math.max(distOut, distIn);
    } else { 
      res[i] = 0;
    }
  } 
  return res;
}
function silhouetteSample ( x , label , dist ) { const n = x . length ; const count = bincount ( label ) ; const c = count . length ; let distIn ; let distOut ; let li ; let ii ; let idx ; let j , k ; const res = Array ( n ) . fill ( ) ; for ( let i = 0 ; i < n ; i ++ ) { li = label [ i ] ; if ( count [ li ] > 1 ) { distIn = 0 ; distOut = Array ( c ) . fill ( 0 ) ; ii = n * i - i * ( i + 3 ) / 2 - 1 ; for ( j = 0 ; j < n ; j ++ ) { if ( i < j ) { idx = ii + j ; } else if ( i > j ) { idx = n * j - j * ( j + 3 ) / 2 + i - 1 ; } else { continue ; } if ( li === label [ j ] ) { distIn += dist [ idx ] ; } else { distOut [ label [ j ] ] += dist [ idx ] ; } } for ( k = 0 ; k < c ; k ++ ) { distOut [ k ] /= count [ k ] ; } distOut = Math . min . apply ( null , distOut . filter ( Boolean ) ) ; distIn /= ( count [ li ] - 1 ) ; res [ i ] = ( distOut - distIn ) / Math . max ( distOut , distIn ) ; } else { res [ i ] = 0 ; } } return res ; }
Compute the silhouette coefficient for each contig.
Compute the silhouette coefficient for each contig .
// bin sizes // intermediates // intra-bin distance // inter-bin distances // contig label // index cache (to accelerate calculation) // distance index // counters // calculate silhouette for each contig // determine index in condensed distance matrix // determine intra- or inter-bin distance // mean inter-bin distance for each other bin // minimum inter-bin distance // mean intra-bin distance // silhouette coefficient // only one contig // end for i