예제 #1
0
  static void calculateEigenvector(String file, String chr, int binsize) throws IOException {
    if (!file.endsWith("hic")) {
      System.err.println("Only 'hic' files are supported");
      System.exit(-1);
    }
    // Load the expected density function, if it exists.
    Map<Integer, DensityFunction> zoomToDensityMap = null;
    String densityFile = file + ".densities";
    if (FileUtils.resourceExists(densityFile)) {
      InputStream is = null;
      try {
        is = ParsingUtils.openInputStream(densityFile);
        zoomToDensityMap = DensityUtil.readDensities(is);

      } finally {
        if (is != null) is.close();
      }
    } else {
      System.err.println("Densities file doesn't exist");
      System.exit(-1);
    }

    SeekableStream ss = IGVSeekableStreamFactory.getStreamFor(file);
    Dataset dataset = (new DatasetReader(ss)).read();
    Chromosome[] tmp = dataset.getChromosomes();

    Map<String, Chromosome> chromosomeMap = new HashMap<String, Chromosome>();
    for (Chromosome c : tmp) {
      chromosomeMap.put(c.getName(), c);
    }

    if (!chromosomeMap.containsKey(chr)) {
      System.err.println("Unknown chromosome: " + chr);
      System.exit(-1);
    }
    int zoomIdx = 0;
    boolean found = false;
    for (; zoomIdx < HiCGlobals.zoomBinSizes.length; zoomIdx++) {
      if (HiCGlobals.zoomBinSizes[zoomIdx] == binsize) {
        found = true;
        break;
      }
    }

    if (!found) {
      System.err.println("Unknown bin size: " + binsize);
      System.exit(-1);
    }

    Matrix matrix = dataset.getMatrix(chromosomeMap.get(chr), chromosomeMap.get(chr));
    MatrixZoomData zd = matrix.getObservedMatrix(zoomIdx);
    final DensityFunction df = zoomToDensityMap.get(zd.getZoom());
    double[] eigenvector = zd.computeEigenvector(df, 0);
    for (double ev : eigenvector) System.out.print(ev + " ");
    System.out.println();
  }