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(); }
public static void main(String[] argv) throws IOException, CmdLineParser.UnknownOptionException, CmdLineParser.IllegalOptionValueException { if (argv.length < 4) { System.out.println("Usage: hictools pre <options> <inputFile> <outputFile> <genomeID>"); System.out.println(" <options>: -d only calculate intra chromosome (diagonal) [false]"); System.out.println( " : -o calculate densities (observed/expected), write to file [false]"); System.out.println(" : -t <int> only write cells with count above threshold t [0]"); System.out.println( " : -c <chromosome ID> only calculate map on specific chromosome"); System.exit(0); } Globals.setHeadless(true); CommandLineParser parser = new CommandLineParser(); parser.parse(argv); String[] args = parser.getRemainingArgs(); if (args[0].equals("sort")) { AlignmentsSorter.sort(args[1], args[2], null); } else if (args[0].equals("pairsToBin")) { String ifile = args[1]; String ofile = args[2]; String genomeId = args[3]; List<Chromosome> chromosomes = loadChromosomes(genomeId); AsciiToBinConverter.convert(ifile, ofile, chromosomes); } else if (args[0].equals("binToPairs")) { String ifile = args[1]; String ofile = args[2]; AsciiToBinConverter.convertBack(ifile, ofile); } else if (args[0].equals("printmatrix")) { if (args.length < 5) { System.err.println( "Usage: hictools printmatrix <observed/oe/pearson> hicFile chr1 chr2 binsize"); System.exit(-1); } String type = args[1]; String file = args[2]; String chr1 = args[3]; String chr2 = args[4]; String binSizeSt = args[5]; int binSize = 0; try { binSize = Integer.parseInt(binSizeSt); } catch (NumberFormatException e) { System.err.println("Integer expected. Found: " + binSizeSt); System.exit(-1); } dumpMatrix(file, chr1, chr2, binSize, type); } else if (args[0].equals("eigenvector")) { if (args.length < 4) { System.err.println("Usage: hictools eigenvector hicFile chr binsize"); } String file = args[1]; String chr = args[2]; String binSizeSt = args[3]; int binSize = 0; try { binSize = Integer.parseInt(binSizeSt); } catch (NumberFormatException e) { System.err.println("Integer expected. Found: " + binSizeSt); System.exit(-1); } calculateEigenvector(file, chr, binSize); } else if (args[0].equals("pre")) { String genomeId = ""; try { genomeId = args[3]; } catch (ArrayIndexOutOfBoundsException e) { System.err.println("No genome ID given"); System.exit(0); } List<Chromosome> chromosomes = loadChromosomes(genomeId); long genomeLength = 0; for (Chromosome c : chromosomes) { if (c != null) genomeLength += c.getSize(); } chromosomes.set(0, new Chromosome(0, "All", (int) (genomeLength / 1000))); String[] tokens = args[1].split(","); List<String> files = new ArrayList<String>(tokens.length); for (String f : tokens) { files.add(f); } Preprocessor preprocessor = new Preprocessor(new File(args[2]), chromosomes); preprocessor.setIncludedChromosomes(parser.getChromosomeOption()); preprocessor.setCountThreshold(parser.getCountThresholdOption()); preprocessor.setNumberOfThreads(parser.getThreadedOption()); preprocessor.setDiagonalsOnly(parser.getDiagonalsOption()); preprocessor.setLoadDensities(parser.getDensitiesOption()); preprocessor.preprocess(files); } }
static void dumpMatrix(String file, String chr1, String chr2, int binsize, String type) 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; if (type.equals("oe") || type.equals("pearson")) { 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, cannot calculate O/E or Pearson's"); 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(chr1)) { System.err.println("Unknown chromosome: " + chr1); System.exit(-1); } else if (!chromosomeMap.containsKey(chr2)) { System.err.println("Unknown chromosome: " + chr2); System.exit(-1); } if (type.equals("oe") || type.equals("pearson")) { if (!chr1.equals(chr2)) { System.err.println("Chromosome " + chr1 + " not equal to Chromosome " + chr2); System.err.println("Currently only intrachromosomal O/E and Pearson's are supported."); 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); } Matrix matrix = dataset.getMatrix(chromosomeMap.get(chr1), chromosomeMap.get(chr2)); MatrixZoomData zd = matrix.getObservedMatrix(zoomIdx); if (type.equals("oe") || type.equals("pearson")) { final DensityFunction df = zoomToDensityMap.get(zd.getZoom()); if (df == null) { System.err.println("Densities not calculated to this resolution."); System.exit(-1); } zd.dumpOE(df, type.equals("oe")); } else zd.dump(); }