static/js/algorithm.js
/**
 * Compute the silhouette coefficient for each contig.
 * @function silhouetteSample2D
 * @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 This is the 2D version of the function. It takes a 2D square
 * distance matrix instead of a 1D condensed distance matrix. It is slower
 * but it can handle larger datasets.
 */
Compute the silhouette coefficient for each contig.
Compute the silhouette coefficient for each contig .
function silhouetteSample2D(x, label, dist) {
  const n = x.length;
  const count = bincount(label);
  const c = count.length;
  let distIn, distOut, li, j;
  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);
      for (j = 0; j < n; j++) {
        if (li === label[j]) distIn += dist[i][j];
        else distOut[label[j]] += dist[i][j];
      }
      for (j = 0; j < c; j++) distOut[j] /= count[j];
      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 silhouetteSample2D ( x , label , dist ) { const n = x . length ; const count = bincount ( label ) ; const c = count . length ; let distIn , distOut , li , j ; 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 ) ; for ( j = 0 ; j < n ; j ++ ) { if ( li === label [ j ] ) distIn += dist [ i ] [ j ] ; else distOut [ label [ j ] ] += dist [ i ] [ j ] ; } for ( j = 0 ; j < c ; j ++ ) distOut [ j ] /= count [ j ] ; 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 .