, Serializable {
-
- private static final long serialVersionUID = -323598431692368500L;
-
- private final String id;
- /** Point coordinates. */
- private final float[] point;
-
- /**
- * Build an instance wrapping an float array.
- * The wrapped array is referenced, it is not copied.
- * @param point the n-dimensional point in integer space
- */
- public KMeansRow(final String id, final float[] point) {
- this.point = point;
- this.id = id;
- }
-
- /**
- * Get the n-dimensional point in float space.
- * @return a reference (not a copy!) to the wrapped array
- */
- public float[] getPoint() {
- return point;
- }
-
- /** {@inheritDoc} */
- public double distanceFrom(final KMeansRow p) {
- double sumSquares = 0;
- float[] otherPoint = p.getPoint();
- for (int i = 0; i < point.length; i++) {
- sumSquares += Math.pow(point[i]-otherPoint[i], 2);
- }
- return Math.sqrt(sumSquares);
- }
-
- /** {@inheritDoc} */
- public KMeansRow centroidOf(final Collection points) {
- float[] centroid = new float[getPoint().length];
- for (KMeansRow p : points) {
- for (int i = 0; i < centroid.length; i++) {
- centroid[i] += p.getPoint()[i];
- }
- }
- for (int i = 0; i < centroid.length; i++) {
- centroid[i] /= points.size();
- }
- return new KMeansRow(id, centroid);
- }
-
- /** {@inheritDoc} */
- @Override
- public boolean equals(final Object other) {
- if (!(other instanceof KMeansRow)) {
- return false;
- }
- final float[] otherPoint = ((KMeansRow) other).getPoint();
- if (point.length != otherPoint.length) {
- return false;
- }
- for (int i = 0; i < point.length; i++) {
- if (point[i] != otherPoint[i]) {
- return false;
- }
- }
- return true;
- }
-
- /** {@inheritDoc} */
- @Override
- public int hashCode() {
- int hashCode = 0;
- for (Float i : point) {
- hashCode += i.hashCode() * 13 + 7;
- }
- return hashCode;
- }
-
- /**
- * {@inheritDoc}
- * @since 2.1
- */
- @Override
- public String toString() {
- final StringBuilder buff = new StringBuilder(id);
- for (float value : getPoint()) {
- buff.append("\t").append(value);
- }
- return buff.toString();
- }
-
- /**
- * @return the id
- */
- public String getId() {
- return id;
- }
-
-}
diff -r 1daf3026d231 -r a54db233ee3d java-genomics-toolkit/src/edu/unc/genomics/visualization/MatrixAligner.java
--- a/java-genomics-toolkit/src/edu/unc/genomics/visualization/MatrixAligner.java Mon Feb 13 21:55:55 2012 -0500
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,145 +0,0 @@
-package edu.unc.genomics.visualization;
-
-import java.io.BufferedWriter;
-import java.io.IOException;
-import java.nio.charset.Charset;
-import java.nio.file.Files;
-import java.nio.file.Path;
-import java.util.Arrays;
-import java.util.Iterator;
-import java.util.List;
-
-import org.apache.commons.lang3.ArrayUtils;
-import org.apache.log4j.Logger;
-import org.broad.igv.bbfile.WigItem;
-
-import com.beust.jcommander.Parameter;
-
-import edu.unc.genomics.BedEntry;
-import edu.unc.genomics.CommandLineTool;
-import edu.unc.genomics.ReadablePathValidator;
-import edu.unc.genomics.io.BedFile;
-import edu.unc.genomics.io.WigFile;
-import edu.unc.genomics.io.WigFileException;
-
-public class MatrixAligner extends CommandLineTool {
-
- private static final Logger log = Logger.getLogger(MatrixAligner.class);
-
- @Parameter(names = {"-i", "--input"}, description = "Input file (Wig)", required = true)
- public WigFile inputFile;
- @Parameter(names = {"-l", "--loci"}, description = "Loci file (Bed)", required = true, validateWith = ReadablePathValidator.class)
- public Path lociFile;
- @Parameter(names = {"-m", "--max"}, description = "Truncate width (base pairs)")
- public Integer maxWidth;
- @Parameter(names = {"-o", "--output"}, description = "Output file (matrix2png format)", required = true)
- public Path outputFile;
-
- private List loci;
-
- @Override
- public void run() throws IOException {
- log.debug("Loading alignment intervals");
- try (BedFile bed = new BedFile(lociFile)) {
- loci = bed.loadAll();
- }
-
- // Compute the matrix dimensions
- int leftMax = Integer.MIN_VALUE;
- int rightMax = Integer.MIN_VALUE;
- for (BedEntry entry : loci) {
- int left = Math.abs(entry.getValue().intValue()-entry.getStart());
- int right = Math.abs(entry.getValue().intValue()-entry.getStop());
- if (left > leftMax) {
- leftMax = left;
- }
- if (right > rightMax) {
- rightMax = right;
- }
- }
-
- int m = loci.size();
- int n = leftMax + rightMax + 1;
- int alignmentPoint = leftMax;
- log.info("Intervals aligned into: " + m+"x"+n + " matrix");
- log.info("Alignment point: " + alignmentPoint);
-
- int leftBound = 0;
- int rightBound = n-1;
- if (maxWidth != null && maxWidth < n) {
- log.info("Truncated to: " + m+"x"+maxWidth);
- int leftAlignDistance = alignmentPoint;
- int rightAlignDistance = n - alignmentPoint - 1;
- int halfMax = maxWidth / 2;
-
- if (halfMax < leftAlignDistance && halfMax < rightAlignDistance) {
- leftBound = alignmentPoint - halfMax;
- rightBound = alignmentPoint + halfMax;
- } else {
- if (leftAlignDistance <= rightAlignDistance) {
- rightBound = maxWidth;
- } else {
- leftBound = n - maxWidth;
- }
- }
- }
-
- log.debug("Initializing output file");
- int count = 0, skipped = 0;
- try (BufferedWriter writer = Files.newBufferedWriter(outputFile, Charset.defaultCharset())) {
- writer.write("ID");
- for (int i = leftBound-alignmentPoint; i <= rightBound-alignmentPoint; i++) {
- writer.write("\t"+i);
- }
- writer.newLine();
-
- log.debug("Iterating over all intervals");
- String[] row = new String[n];
- for (BedEntry entry : loci) {
- Iterator result = null;
- try {
- result = inputFile.query(entry);
- } catch (WigFileException e) {
- skipped++;
- continue;
- }
-
- float[] data = WigFile.flattenData(result, entry.getStart(), entry.getStop());
- // Reverse if on the crick strand
- if (entry.isCrick()) {
- ArrayUtils.reverse(data);
- }
-
- // Position the data in the matrix
- // Locus alignment point (entry value) should be positioned over the matrix alignment point
- int n1 = alignmentPoint - Math.abs(entry.getValue().intValue()-entry.getStart());
- int n2 = alignmentPoint + Math.abs(entry.getValue().intValue()-entry.getStop());
- assert data.length == n2-n1+1;
-
- Arrays.fill(row, "-");
- for (int i = 0; i < data.length; i++) {
- if (!Float.isNaN(data[i])) {
- row[n1+i] = String.valueOf(data[i]);
- }
- }
-
- // Write to output
- String id = ((entry.getId() == null) ? entry.getId() : "Row "+(count++));
- writer.write(id);
- for (int i = leftBound; i <= rightBound; i++) {
- writer.write("\t"+row[i]);
- }
- writer.newLine();
- }
- }
-
- inputFile.close();
- log.info(count + " intervals processed");
- log.info(skipped + " intervals skipped");
- }
-
- public static void main(String[] args) {
- new MatrixAligner().instanceMain(args);
- }
-
-}
diff -r 1daf3026d231 -r a54db233ee3d java-genomics-toolkit/src/edu/unc/genomics/visualization/StripMatrix.java
--- a/java-genomics-toolkit/src/edu/unc/genomics/visualization/StripMatrix.java Mon Feb 13 21:55:55 2012 -0500
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,56 +0,0 @@
-package edu.unc.genomics.visualization;
-
-import java.io.BufferedReader;
-import java.io.BufferedWriter;
-import java.io.IOException;
-import java.nio.charset.Charset;
-import java.nio.file.Files;
-import java.nio.file.Path;
-
-import org.apache.log4j.Logger;
-
-import com.beust.jcommander.Parameter;
-
-import edu.unc.genomics.CommandLineTool;
-import edu.unc.genomics.ReadablePathValidator;
-
-public class StripMatrix extends CommandLineTool {
-
- private static final Logger log = Logger.getLogger(StripMatrix.class);
-
- @Parameter(names = {"-i", "--input"}, description = "Input file (matrix2png format)", required = true, validateWith = ReadablePathValidator.class)
- public Path inputFile;
- @Parameter(names = {"-o", "--output"}, description = "Output file (tabular)", required = true)
- public Path outputFile;
-
- public void run() throws IOException {
- try (BufferedReader reader = Files.newBufferedReader(inputFile, Charset.defaultCharset())) {
- try (BufferedWriter writer = Files.newBufferedWriter(outputFile, Charset.defaultCharset())) {
- String line = reader.readLine();
- // Always copy the first (header) line
- writer.write(line);
- writer.newLine();
- while ((line = reader.readLine()) != null) {
- String[] row = line.split("\t");
- for (int i = 1; i < row.length; i++) {
- String cell = row[i];
- if (cell.equalsIgnoreCase("-")) {
- writer.write("NaN");
- } else {
- writer.write(cell);
- }
-
- if (i > 1) {
- writer.write("\t");
- }
- }
- writer.newLine();
- }
- }
- }
- }
-
- public static void main(String[] args) {
- new StripMatrix().instanceMain(args);
- }
-}
\ No newline at end of file
diff -r 1daf3026d231 -r a54db233ee3d java-genomics-toolkit/src/edu/unc/genomics/wigmath/._Add.java
Binary file java-genomics-toolkit/src/edu/unc/genomics/wigmath/._Add.java has changed
diff -r 1daf3026d231 -r a54db233ee3d java-genomics-toolkit/src/edu/unc/genomics/wigmath/._Average.java
Binary file java-genomics-toolkit/src/edu/unc/genomics/wigmath/._Average.java has changed
diff -r 1daf3026d231 -r a54db233ee3d java-genomics-toolkit/src/edu/unc/genomics/wigmath/._Divide.java
Binary file java-genomics-toolkit/src/edu/unc/genomics/wigmath/._Divide.java has changed
diff -r 1daf3026d231 -r a54db233ee3d java-genomics-toolkit/src/edu/unc/genomics/wigmath/._GaussianSmooth.java
Binary file java-genomics-toolkit/src/edu/unc/genomics/wigmath/._GaussianSmooth.java has changed
diff -r 1daf3026d231 -r a54db233ee3d java-genomics-toolkit/src/edu/unc/genomics/wigmath/._LogTransform.java
Binary file java-genomics-toolkit/src/edu/unc/genomics/wigmath/._LogTransform.java has changed
diff -r 1daf3026d231 -r a54db233ee3d java-genomics-toolkit/src/edu/unc/genomics/wigmath/._MovingAverageSmooth.java
Binary file java-genomics-toolkit/src/edu/unc/genomics/wigmath/._MovingAverageSmooth.java has changed
diff -r 1daf3026d231 -r a54db233ee3d java-genomics-toolkit/src/edu/unc/genomics/wigmath/._Multiply.java
Binary file java-genomics-toolkit/src/edu/unc/genomics/wigmath/._Multiply.java has changed
diff -r 1daf3026d231 -r a54db233ee3d java-genomics-toolkit/src/edu/unc/genomics/wigmath/._Scale.java
Binary file java-genomics-toolkit/src/edu/unc/genomics/wigmath/._Scale.java has changed
diff -r 1daf3026d231 -r a54db233ee3d java-genomics-toolkit/src/edu/unc/genomics/wigmath/._Subtract.java
Binary file java-genomics-toolkit/src/edu/unc/genomics/wigmath/._Subtract.java has changed
diff -r 1daf3026d231 -r a54db233ee3d java-genomics-toolkit/src/edu/unc/genomics/wigmath/._WigMathTool.java
Binary file java-genomics-toolkit/src/edu/unc/genomics/wigmath/._WigMathTool.java has changed
diff -r 1daf3026d231 -r a54db233ee3d java-genomics-toolkit/src/edu/unc/genomics/wigmath/._WigSummary.java
Binary file java-genomics-toolkit/src/edu/unc/genomics/wigmath/._WigSummary.java has changed
diff -r 1daf3026d231 -r a54db233ee3d java-genomics-toolkit/src/edu/unc/genomics/wigmath/._ZScore.java
Binary file java-genomics-toolkit/src/edu/unc/genomics/wigmath/._ZScore.java has changed
diff -r 1daf3026d231 -r a54db233ee3d java-genomics-toolkit/src/edu/unc/genomics/wigmath/Add.java
--- a/java-genomics-toolkit/src/edu/unc/genomics/wigmath/Add.java Mon Feb 13 21:55:55 2012 -0500
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,76 +0,0 @@
-package edu.unc.genomics.wigmath;
-
-import java.io.IOException;
-import java.nio.file.Paths;
-import java.util.ArrayList;
-import java.util.Iterator;
-import java.util.List;
-
-import org.apache.log4j.Logger;
-import org.broad.igv.bbfile.WigItem;
-
-import com.beust.jcommander.Parameter;
-
-import edu.unc.genomics.CommandLineToolException;
-import edu.unc.genomics.io.WigFile;
-import edu.unc.genomics.io.WigFileException;
-
-public class Add extends WigMathTool {
-
- private static final Logger log = Logger.getLogger(Add.class);
-
- @Parameter(description = "Input files", required = true)
- public List inputFiles = new ArrayList();
-
- @Override
- public void setup() {
- if (inputFiles.size() < 2) {
- throw new CommandLineToolException("No reason to add < 2 files.");
- }
-
- log.debug("Initializing input files");
- for (String inputFile : inputFiles) {
- try {
- addInputFile(WigFile.autodetect(Paths.get(inputFile)));
- } catch (IOException | WigFileException e) {
- log.error("Error initializing input Wig file: " + inputFile);
- e.printStackTrace();
- throw new CommandLineToolException(e.getMessage());
- }
- }
- log.debug("Initialized " + inputs.size() + " input files");
- }
-
- @Override
- public float[] compute(String chr, int start, int stop) throws IOException, WigFileException {
- log.debug("Computing sum for chunk "+chr+":"+start+"-"+stop);
-
- int length = stop - start + 1;
- float[] sum = new float[length];
-
- for (WigFile wig : inputs) {
- Iterator data = wig.query(chr, start, stop);
- while (data.hasNext()) {
- WigItem item = data.next();
- for (int i = item.getStartBase(); i <= item.getEndBase(); i++) {
- if (i-start >= 0 && i-start < sum.length) {
- sum[i-start] += item.getWigValue();
- }
- }
- }
- }
-
- return sum;
- }
-
-
- /**
- * @param args
- * @throws WigFileException
- * @throws IOException
- */
- public static void main(String[] args) throws IOException, WigFileException {
- new Add().instanceMain(args);
- }
-
-}
diff -r 1daf3026d231 -r a54db233ee3d java-genomics-toolkit/src/edu/unc/genomics/wigmath/Average.java
--- a/java-genomics-toolkit/src/edu/unc/genomics/wigmath/Average.java Mon Feb 13 21:55:55 2012 -0500
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,84 +0,0 @@
-package edu.unc.genomics.wigmath;
-
-import java.io.IOException;
-import java.nio.file.Paths;
-import java.util.ArrayList;
-import java.util.Iterator;
-import java.util.List;
-
-import org.apache.log4j.Logger;
-import org.broad.igv.bbfile.WigItem;
-
-import com.beust.jcommander.Parameter;
-
-import edu.unc.genomics.CommandLineToolException;
-import edu.unc.genomics.io.WigFile;
-import edu.unc.genomics.io.WigFileException;
-
-public class Average extends WigMathTool {
-
- private static final Logger log = Logger.getLogger(Average.class);
-
- @Parameter(description = "Input files", required = true)
- public List inputFiles = new ArrayList();
-
- int numFiles;
-
- @Override
- public void setup() {
- if (inputFiles.size() < 2) {
- throw new CommandLineToolException("No reason to add < 2 files.");
- }
-
- log.debug("Initializing input files");
- for (String inputFile : inputFiles) {
- try {
- addInputFile(WigFile.autodetect(Paths.get(inputFile)));
- } catch (IOException | WigFileException e) {
- log.error("Error initializing input Wig file: " + inputFile);
- e.printStackTrace();
- throw new CommandLineToolException(e.getMessage());
- }
- }
- log.debug("Initialized " + inputs.size() + " input files");
-
- numFiles = inputs.size();
- }
-
- @Override
- public float[] compute(String chr, int start, int stop) throws IOException, WigFileException {
- log.debug("Computing average for chunk "+chr+":"+start+"-"+stop);
-
- int length = stop - start + 1;
- float[] avg = new float[length];
-
- for (WigFile wig : inputs) {
- Iterator data = wig.query(chr, start, stop);
- while (data.hasNext()) {
- WigItem item = data.next();
- for (int i = item.getStartBase(); i <= item.getEndBase(); i++) {
- if (i-start >= 0 && i-start < avg.length) {
- avg[i-start] += item.getWigValue();
- }
- }
- }
- }
-
- for (int i = 0; i < avg.length; i++) {
- avg[i] = avg[i] / numFiles;
- }
-
- return avg;
- }
-
-
- /**
- * @param args
- * @throws WigFileException
- * @throws IOException
- */
- public static void main(String[] args) throws IOException, WigFileException {
- new Average().instanceMain(args);
- }
-
-}
diff -r 1daf3026d231 -r a54db233ee3d java-genomics-toolkit/src/edu/unc/genomics/wigmath/Divide.java
--- a/java-genomics-toolkit/src/edu/unc/genomics/wigmath/Divide.java Mon Feb 13 21:55:55 2012 -0500
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,64 +0,0 @@
-package edu.unc.genomics.wigmath;
-
-import java.io.IOException;
-import java.util.Iterator;
-
-import org.apache.log4j.Logger;
-import org.broad.igv.bbfile.WigItem;
-
-import com.beust.jcommander.Parameter;
-
-import edu.unc.genomics.io.WigFile;
-import edu.unc.genomics.io.WigFileException;
-
-public class Divide extends WigMathTool {
-
- private static final Logger log = Logger.getLogger(Divide.class);
-
- @Parameter(names = {"-n", "--numerator"}, description = "Dividend / Numerator (file 1)", required = true)
- public WigFile dividendFile;
- @Parameter(names = {"-d", "--denominator"}, description = "Divisor / Denominator (file 2)", required = true)
- public WigFile divisorFile;
-
- @Override
- public void setup() {
- inputs.add(dividendFile);
- inputs.add(divisorFile);
- log.debug("Initialized " + inputs.size() + " input files");
- }
-
- @Override
- public float[] compute(String chr, int start, int stop) throws IOException, WigFileException {
- log.debug("Computing difference for chunk "+chr+":"+start+"-"+stop);
-
- Iterator dividendData = dividendFile.query(chr, start, stop);
- Iterator divisorData = divisorFile.query(chr, start, stop);
-
- float[] result = WigFile.flattenData(dividendData, start, stop);
- while (divisorData.hasNext()) {
- WigItem item = divisorData.next();
- for (int i = item.getStartBase(); i <= item.getEndBase(); i++) {
- if (i-start >= 0 && i-start < result.length) {
- if (item.getWigValue() != 0) {
- result[i-start] /= item.getWigValue();
- } else {
- result[i-start] = Float.NaN;
- }
- }
- }
- }
-
- return result;
- }
-
-
- /**
- * @param args
- * @throws WigFileException
- * @throws IOException
- */
- public static void main(String[] args) throws IOException, WigFileException {
- new Divide().instanceMain(args);
- }
-
-}
diff -r 1daf3026d231 -r a54db233ee3d java-genomics-toolkit/src/edu/unc/genomics/wigmath/GaussianSmooth.java
--- a/java-genomics-toolkit/src/edu/unc/genomics/wigmath/GaussianSmooth.java Mon Feb 13 21:55:55 2012 -0500
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,74 +0,0 @@
-package edu.unc.genomics.wigmath;
-
-import java.io.IOException;
-import java.util.Iterator;
-
-import org.apache.log4j.Logger;
-import org.broad.igv.bbfile.WigItem;
-
-import com.beust.jcommander.Parameter;
-
-import edu.unc.genomics.io.WigFile;
-import edu.unc.genomics.io.WigFileException;
-
-public class GaussianSmooth extends WigMathTool {
-
- private static final Logger log = Logger.getLogger(GaussianSmooth.class);
-
- @Parameter(names = {"-i", "--input"}, description = "Input file", required = true)
- public WigFile inputFile;
- @Parameter(names = {"-s", "--stdev"}, description = "Standard deviation of Gaussian (bp)")
- public int stdev = 20;
-
- float[] filter;
-
- @Override
- public void setup() {
- inputs.add(inputFile);
-
- // Use a window size equal to +/- 3 SD's
- filter = new float[6*stdev+1];
- float sum = 0;
- for (int i = 0; i < filter.length; i++) {
- float x = i - 3*stdev;
- float value = (float) Math.exp(-(x*x) / (2*stdev*stdev));
- filter[i] = value;
- sum += value;
- }
- for (int i = 0; i < filter.length; i++) {
- filter[i] /= sum;
- }
- }
-
- @Override
- public float[] compute(String chr, int start, int stop) throws IOException, WigFileException {
- log.debug("Smoothing chunk "+chr+":"+start+"-"+stop);
-
- // Pad the query for smoothing
- int paddedStart = Math.max(start-3*stdev, inputFile.getChrStart(chr));
- int paddedStop = Math.min(stop+3*stdev, inputFile.getChrStop(chr));
- Iterator result = inputFile.query(chr, paddedStart, paddedStop);
- float[] data = WigFile.flattenData(result, start-3*stdev, stop+3*stdev);
-
- // Convolve the data with the filter
- float[] smoothed = new float[stop-start+1];
- for (int i = 0; i < smoothed.length; i++) {
- for (int j = 0; j < filter.length; j++) {
- smoothed[i] += data[i+j] * filter[j];
- }
- }
-
- return smoothed;
- }
-
-
- /**
- * @param args
- * @throws WigFileException
- * @throws IOException
- */
- public static void main(String[] args) throws IOException, WigFileException {
- new GaussianSmooth().instanceMain(args);
- }
-
-}
diff -r 1daf3026d231 -r a54db233ee3d java-genomics-toolkit/src/edu/unc/genomics/wigmath/LogTransform.java
--- a/java-genomics-toolkit/src/edu/unc/genomics/wigmath/LogTransform.java Mon Feb 13 21:55:55 2012 -0500
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,55 +0,0 @@
-package edu.unc.genomics.wigmath;
-
-import java.io.IOException;
-import java.util.Iterator;
-
-import org.apache.log4j.Logger;
-import org.broad.igv.bbfile.WigItem;
-
-import com.beust.jcommander.Parameter;
-
-import edu.unc.genomics.io.WigFile;
-import edu.unc.genomics.io.WigFileException;
-
-public class LogTransform extends WigMathTool {
-
- private static final Logger log = Logger.getLogger(LogTransform.class);
-
- @Parameter(names = {"-i", "--input"}, description = "Input file", required = true)
- public WigFile inputFile;
- @Parameter(names = {"-b", "--base"}, description = "Logarithm base (default = 2)")
- public double base = 2;
-
- private double baseChange;
-
- @Override
- public void setup() {
- baseChange = Math.log(base);
- inputs.add(inputFile);
- }
-
- @Override
- public float[] compute(String chr, int start, int stop) throws IOException, WigFileException {
- log.debug("Computing difference for chunk "+chr+":"+start+"-"+stop);
-
- Iterator data = inputFile.query(chr, start, stop);
- float[] result = WigFile.flattenData(data, start, stop);
-
- for (int i = 0; i < result.length; i++) {
- result[i] = (float) (Math.log(result[i]) / baseChange);
- }
-
- return result;
- }
-
-
- /**
- * @param args
- * @throws WigFileException
- * @throws IOException
- */
- public static void main(String[] args) throws IOException, WigFileException {
- new LogTransform().instanceMain(args);
- }
-
-}
diff -r 1daf3026d231 -r a54db233ee3d java-genomics-toolkit/src/edu/unc/genomics/wigmath/MovingAverageSmooth.java
--- a/java-genomics-toolkit/src/edu/unc/genomics/wigmath/MovingAverageSmooth.java Mon Feb 13 21:55:55 2012 -0500
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,67 +0,0 @@
-package edu.unc.genomics.wigmath;
-
-import java.io.IOException;
-import java.util.Iterator;
-
-import org.apache.commons.math.stat.descriptive.DescriptiveStatistics;
-import org.apache.log4j.Logger;
-import org.broad.igv.bbfile.WigItem;
-
-import com.beust.jcommander.Parameter;
-
-import edu.unc.genomics.PositiveIntegerValidator;
-import edu.unc.genomics.io.WigFile;
-import edu.unc.genomics.io.WigFileException;
-
-public class MovingAverageSmooth extends WigMathTool {
-
- private static final Logger log = Logger.getLogger(MovingAverageSmooth.class);
-
- @Parameter(names = {"-i", "--input"}, description = "Input file", required = true)
- public WigFile inputFile;
- @Parameter(names = {"-w", "--width"}, description = "Width of kernel (bp)")
- public int width = 10;
-
- WigFile input;
- DescriptiveStatistics stats;
-
- @Override
- public void setup() {
- inputs.add(inputFile);
-
- log.debug("Initializing statistics");
- stats = new DescriptiveStatistics();
- stats.setWindowSize(width);
- }
-
- @Override
- public float[] compute(String chr, int start, int stop) throws IOException, WigFileException {
- log.debug("Smoothing chunk "+chr+":"+start+"-"+stop);
- // Pad the query so that we can provide values for the ends
- int queryStart = Math.max(start-width/2, input.getChrStart(chr));
- int queryStop = Math.min(stop+width/2, input.getChrStop(chr));
- Iterator result = input.query(chr, queryStart, queryStop);
- float[] data = WigFile.flattenData(result, queryStart, queryStop);
-
- float[] smoothed = new float[stop-start+1];
- for (int bp = start; bp <= stop; bp++) {
- stats.addValue(data[bp-queryStart]);
- if (bp-start-width/2 >= 0) {
- smoothed[bp-start-width/2] = (float) stats.getMean();
- }
- }
-
- return smoothed;
- }
-
-
- /**
- * @param args
- * @throws WigFileException
- * @throws IOException
- */
- public static void main(String[] args) throws IOException, WigFileException {
- new MovingAverageSmooth().instanceMain(args);
- }
-
-}
diff -r 1daf3026d231 -r a54db233ee3d java-genomics-toolkit/src/edu/unc/genomics/wigmath/Multiply.java
--- a/java-genomics-toolkit/src/edu/unc/genomics/wigmath/Multiply.java Mon Feb 13 21:55:55 2012 -0500
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,73 +0,0 @@
-package edu.unc.genomics.wigmath;
-
-import java.io.IOException;
-import java.nio.file.Paths;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Iterator;
-import java.util.List;
-
-import org.apache.log4j.Logger;
-import org.broad.igv.bbfile.WigItem;
-
-import com.beust.jcommander.Parameter;
-
-import edu.unc.genomics.io.WigFile;
-import edu.unc.genomics.io.WigFileException;
-
-public class Multiply extends WigMathTool {
-
- private static final Logger log = Logger.getLogger(Multiply.class);
-
- @Parameter(description = "Input files", required = true)
- public List inputFiles = new ArrayList();
-
- @Override
- public void setup() {
- log.debug("Initializing input files");
- for (String inputFile : inputFiles) {
- try {
- addInputFile(WigFile.autodetect(Paths.get(inputFile)));
- } catch (IOException | WigFileException e) {
- log.error("Error initializing input Wig file: " + inputFile);
- e.printStackTrace();
- System.exit(-1);
- }
- }
- log.debug("Initialized " + inputs.size() + " input files");
- }
-
- @Override
- public float[] compute(String chr, int start, int stop) throws IOException, WigFileException {
- log.debug("Computing sum for chunk "+chr+":"+start+"-"+stop);
-
- int length = stop - start + 1;
- float[] product = new float[length];
- Arrays.fill(product, 1);
-
- for (WigFile wig : inputs) {
- Iterator data = wig.query(chr, start, stop);
- while (data.hasNext()) {
- WigItem item = data.next();
- for (int i = item.getStartBase(); i <= item.getEndBase(); i++) {
- if (i-start >= 0 && i-start < product.length) {
- product[i-start] *= item.getWigValue();
- }
- }
- }
- }
-
- return product;
- }
-
-
- /**
- * @param args
- * @throws WigFileException
- * @throws IOException
- */
- public static void main(String[] args) throws IOException, WigFileException {
- new Multiply().instanceMain(args);
- }
-
-}
diff -r 1daf3026d231 -r a54db233ee3d java-genomics-toolkit/src/edu/unc/genomics/wigmath/Scale.java
--- a/java-genomics-toolkit/src/edu/unc/genomics/wigmath/Scale.java Mon Feb 13 21:55:55 2012 -0500
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,57 +0,0 @@
-package edu.unc.genomics.wigmath;
-
-import java.io.IOException;
-import java.util.Iterator;
-
-import org.apache.log4j.Logger;
-import org.broad.igv.bbfile.WigItem;
-
-import com.beust.jcommander.Parameter;
-
-import edu.unc.genomics.io.WigFile;
-import edu.unc.genomics.io.WigFileException;
-
-public class Scale extends WigMathTool {
-
- private static final Logger log = Logger.getLogger(Scale.class);
-
- @Parameter(names = {"-i", "--input"}, description = "Input file", required = true)
- public WigFile inputFile;
- @Parameter(names = {"-m", "--multiplier"}, description = "Multiplier (scale factor, default = 1/mean)")
- public Double multiplier;
-
-
- @Override
- public void setup() {
- inputs.add(inputFile);
-
- if (multiplier == null) {
- multiplier = inputFile.numBases() / inputFile.total();
- }
- }
-
- @Override
- public float[] compute(String chr, int start, int stop) throws IOException, WigFileException {
- log.debug("Computing difference for chunk "+chr+":"+start+"-"+stop);
-
- Iterator data = inputFile.query(chr, start, stop);
- float[] result = WigFile.flattenData(data, start, stop);
-
- for (int i = 0; i < result.length; i++) {
- result[i] = (float) (multiplier * result[i]);
- }
-
- return result;
- }
-
-
- /**
- * @param args
- * @throws WigFileException
- * @throws IOException
- */
- public static void main(String[] args) throws IOException, WigFileException {
- new Scale().instanceMain(args);
- }
-
-}
diff -r 1daf3026d231 -r a54db233ee3d java-genomics-toolkit/src/edu/unc/genomics/wigmath/Subtract.java
--- a/java-genomics-toolkit/src/edu/unc/genomics/wigmath/Subtract.java Mon Feb 13 21:55:55 2012 -0500
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,61 +0,0 @@
-package edu.unc.genomics.wigmath;
-
-import java.io.IOException;
-import java.util.Iterator;
-
-import org.apache.log4j.Logger;
-import org.broad.igv.bbfile.WigItem;
-
-import com.beust.jcommander.Parameter;
-
-import edu.unc.genomics.io.WigFile;
-import edu.unc.genomics.io.WigFileException;
-
-public class Subtract extends WigMathTool {
-
- private static final Logger log = Logger.getLogger(Subtract.class);
-
- @Parameter(names = {"-m", "--minuend"}, description = "Minuend (top - file 1)", required = true)
- public WigFile minuendFile;
- @Parameter(names = {"-s", "--subtrahend"}, description = "Subtrahend (bottom - file 2)", required = true)
- public WigFile subtrahendFile;
-
- @Override
- public void setup() {
- log.debug("Initializing input files");
- inputs.add(minuendFile);
- inputs.add(subtrahendFile);
- log.debug("Initialized " + inputs.size() + " input files");
- }
-
- @Override
- public float[] compute(String chr, int start, int stop) throws IOException, WigFileException {
- log.debug("Computing difference for chunk "+chr+":"+start+"-"+stop);
-
- Iterator minuendData = minuendFile.query(chr, start, stop);
- Iterator subtrahendData = subtrahendFile.query(chr, start, stop);
-
- float[] result = WigFile.flattenData(minuendData, start, stop);
- while (subtrahendData.hasNext()) {
- WigItem item = subtrahendData.next();
- for (int i = item.getStartBase(); i <= item.getEndBase(); i++) {
- if (i-start >= 0 && i-start < result.length) {
- result[i-start] -= item.getWigValue();
- }
- }
- }
-
- return result;
- }
-
-
- /**
- * @param args
- * @throws WigFileException
- * @throws IOException
- */
- public static void main(String[] args) throws IOException, WigFileException {
- new Subtract().instanceMain(args);
- }
-
-}
diff -r 1daf3026d231 -r a54db233ee3d java-genomics-toolkit/src/edu/unc/genomics/wigmath/WigMathTool.java
--- a/java-genomics-toolkit/src/edu/unc/genomics/wigmath/WigMathTool.java Mon Feb 13 21:55:55 2012 -0500
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,170 +0,0 @@
-package edu.unc.genomics.wigmath;
-
-import java.io.BufferedWriter;
-import java.io.IOException;
-import java.nio.charset.Charset;
-import java.nio.file.Files;
-import java.nio.file.Path;
-import java.util.ArrayList;
-import java.util.HashSet;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Set;
-
-import org.apache.log4j.Logger;
-
-import com.beust.jcommander.Parameter;
-
-import edu.unc.genomics.CommandLineTool;
-import edu.unc.genomics.io.WigFile;
-import edu.unc.genomics.io.WigFileException;
-
-/**
- * Abstract class for writing programs to do computation on Wig files
- * Concrete subclasses must implement the compute method
- *
- * @author timpalpant
- *
- */
-public abstract class WigMathTool extends CommandLineTool {
-
- private static final Logger log = Logger.getLogger(WigMathTool.class);
-
- public static final int DEFAULT_CHUNK_SIZE = 500_000;
-
- // TODO: Variable resolution output
-
- @Parameter(names = {"-o", "--output"}, description = "Output file", required = true)
- public Path outputFile;
-
- protected List inputs = new ArrayList();
-
- public void addInputFile(WigFile wig) {
- inputs.add(wig);
- }
-
- /**
- * Setup the computation. Should add all input Wig files
- * with addInputFile() during setup
- */
- public abstract void setup();
-
- /**
- * Do the computation on a chunk and return the results
- * Must return (stop-start+1) values
- *
- * @param chr
- * @param start
- * @param stop
- * @return the results of the computation for this chunk
- * @throws IOException
- * @throws WigFileException
- */
- public abstract float[] compute(String chr, int start, int stop)
- throws IOException, WigFileException;
-
- @Override
- public void run() throws IOException {
- log.debug("Executing setup operations");
- setup();
-
- log.debug("Processing files and writing result to disk");
- try (BufferedWriter writer = Files.newBufferedWriter(outputFile, Charset.defaultCharset())) {
- // Write the Wig header
- writer.write("track type=wiggle_0");
- writer.newLine();
-
- Set chromosomes = getCommonChromosomes(inputs);
- log.debug("Found " + chromosomes.size() + " chromosomes in common between all inputs");
- for (String chr : chromosomes) {
- int start = getMaxChrStart(inputs, chr);
- int stop = getMinChrStop(inputs, chr);
- log.debug("Processing chromosome " + chr + " region " + start + "-" + stop);
-
- // Write the chromosome header to output
- writer.write("fixedStep chrom="+chr+" start="+start+" step=1 span=1");
- writer.newLine();
-
- // Process the chromosome in chunks
- int bp = start;
- while (bp < stop) {
- int chunkStart = bp;
- int chunkStop = Math.min(bp+DEFAULT_CHUNK_SIZE-1, stop);
- int expectedLength = chunkStop - chunkStart + 1;
- log.debug("Processing chunk "+chr+":"+chunkStart+"-"+chunkStop);
-
- float[] result = null;
- try {
- result = compute(chr, chunkStart, chunkStop);
- } catch (WigFileException e) {
- log.fatal("Wig file error while processing chunk " + chr + " region " + start + "-" + stop);
- e.printStackTrace();
- throw new RuntimeException("Wig file error while processing chunk " + chr + " region " + start + "-" + stop);
- }
-
- if (result.length != expectedLength) {
- log.error("Expected result length="+expectedLength+", got="+result.length);
- throw new RuntimeException("Result is not the expected length!");
- }
-
- for (int i = 0; i < result.length; i++) {
- writer.write(Float.toString(result[i]));
- writer.newLine();
- }
-
- bp = chunkStop + 1;
- }
- }
- }
-
- for (WigFile wig : inputs) {
- wig.close();
- }
- }
-
- public int getMaxChrStart(List wigs, String chr) {
- int max = -1;
- for (WigFile wig : wigs) {
- if (wig.getChrStart(chr) > max) {
- max = wig.getChrStart(chr);
- }
- }
-
- return max;
- }
-
- public int getMinChrStop(List wigs, String chr) {
- if (wigs.size() == 0) {
- return -1;
- }
-
- int min = Integer.MAX_VALUE;
- for (WigFile wig : wigs) {
- if (wig.getChrStop(chr) < min) {
- min = wig.getChrStop(chr);
- }
- }
-
- return min;
- }
-
- public Set getCommonChromosomes(List wigs) {
- if (wigs.size() == 0) {
- return new HashSet();
- }
-
- Set chromosomes = wigs.get(0).chromosomes();
- Iterator it = chromosomes.iterator();
- while(it.hasNext()) {
- String chr = it.next();
- for (WigFile wig : wigs) {
- if (!wig.includes(chr)) {
- it.remove();
- break;
- }
- }
- }
-
- return chromosomes;
- }
-}
diff -r 1daf3026d231 -r a54db233ee3d java-genomics-toolkit/src/edu/unc/genomics/wigmath/WigSummary.java
--- a/java-genomics-toolkit/src/edu/unc/genomics/wigmath/WigSummary.java Mon Feb 13 21:55:55 2012 -0500
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,44 +0,0 @@
-package edu.unc.genomics.wigmath;
-
-import java.io.BufferedWriter;
-import java.io.IOException;
-import java.nio.charset.Charset;
-import java.nio.file.Files;
-import java.nio.file.Path;
-
-import org.apache.log4j.Logger;
-
-import com.beust.jcommander.Parameter;
-
-import edu.unc.genomics.CommandLineTool;
-import edu.unc.genomics.io.WigFile;
-import edu.unc.genomics.io.WigFileException;
-import edu.unc.genomics.ngs.Autocorrelation;
-
-public class WigSummary extends CommandLineTool {
-
- private static final Logger log = Logger.getLogger(Autocorrelation.class);
-
- @Parameter(names = {"-i", "--input"}, description = "Input file", required = true)
- public WigFile inputFile;
- @Parameter(names = {"-o", "--output"}, description = "Output file")
- public Path outputFile;
-
- public void run() throws IOException {
- String summary = inputFile.toString();
-
- if (outputFile != null) {
- log.debug("Writing to output file");
- try (BufferedWriter writer = Files.newBufferedWriter(outputFile, Charset.defaultCharset())) {
- writer.write(summary);
- }
- } else {
- System.out.println(summary);
- }
- }
-
- public static void main(String[] args) throws IOException, WigFileException {
- new WigSummary().instanceMain(args);
- }
-
-}
diff -r 1daf3026d231 -r a54db233ee3d java-genomics-toolkit/src/edu/unc/genomics/wigmath/ZScore.java
--- a/java-genomics-toolkit/src/edu/unc/genomics/wigmath/ZScore.java Mon Feb 13 21:55:55 2012 -0500
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,60 +0,0 @@
-package edu.unc.genomics.wigmath;
-
-import java.io.IOException;
-import java.util.Iterator;
-
-import org.apache.log4j.Logger;
-import org.broad.igv.bbfile.WigItem;
-
-import com.beust.jcommander.Parameter;
-
-import edu.unc.genomics.CommandLineToolException;
-import edu.unc.genomics.io.WigFile;
-import edu.unc.genomics.io.WigFileException;
-
-public class ZScore extends WigMathTool {
-
- private static final Logger log = Logger.getLogger(ZScore.class);
-
- @Parameter(names = {"-i", "--input"}, description = "Input file", required = true)
- public WigFile inputFile;
-
- double mean;
- double stdev;
-
- @Override
- public void setup() {
- inputs.add(inputFile);
-
- mean = inputFile.mean();
- stdev = inputFile.stdev();
- if(stdev == 0) {
- log.error("Cannot Z-score a file with stdev = 0!");
- throw new CommandLineToolException("Cannot Z-score a file with stdev = 0!");
- }
- }
-
- @Override
- public float[] compute(String chr, int start, int stop) throws IOException, WigFileException {
- log.debug("Computing difference for chunk "+chr+":"+start+"-"+stop);
- Iterator data = inputFile.query(chr, start, stop);
- float[] result = WigFile.flattenData(data, start, stop);
-
- for (int i = 0; i < result.length; i++) {
- result[i] = (float)((result[i] - mean) / stdev);
- }
-
- return result;
- }
-
-
- /**
- * @param args
- * @throws WigFileException
- * @throws IOException
- */
- public static void main(String[] args) throws IOException, WigFileException {
- new ZScore().instanceMain(args);
- }
-
-}
diff -r 1daf3026d231 -r a54db233ee3d java-genomics-toolkit/src/edu/unc/utils/._RomanNumeral.java
Binary file java-genomics-toolkit/src/edu/unc/utils/._RomanNumeral.java has changed
diff -r 1daf3026d231 -r a54db233ee3d java-genomics-toolkit/src/edu/unc/utils/._SortUtils.java
Binary file java-genomics-toolkit/src/edu/unc/utils/._SortUtils.java has changed
diff -r 1daf3026d231 -r a54db233ee3d java-genomics-toolkit/src/edu/unc/utils/RomanNumeral.java
--- a/java-genomics-toolkit/src/edu/unc/utils/RomanNumeral.java Mon Feb 13 21:55:55 2012 -0500
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,57 +0,0 @@
-package edu.unc.utils;
-
-// Rudimentary Class for doing Arabic Integer -> Roman Numeral conversion
-// Adapted by Timothy Palpant
-// File : gui/componenents/calculators/Roman.java
-// Description: A static method for converting binary integers to Roman numbers.
-// Illustrates: Static inner value class, StringBuffer, throw exceptions.
-// Author : Fred Swartz - 2006-12-29 - Placed in public domain.
-public class RomanNumeral {
-
- // This could be alternatively be done with parallel arrays.
- // Another alternative would be Pair
- final static RomanValue[] ROMAN_VALUE_TABLE = {
- new RomanValue(1000, "M"),
- new RomanValue( 900, "CM"),
- new RomanValue( 500, "D"),
- new RomanValue( 400, "CD"),
- new RomanValue( 100, "C"),
- new RomanValue( 90, "XC"),
- new RomanValue( 50, "L"),
- new RomanValue( 40, "XL"),
- new RomanValue( 10, "X"),
- new RomanValue( 9, "IX"),
- new RomanValue( 5, "V"),
- new RomanValue( 4, "IV"),
- new RomanValue( 1, "I")
- };
-
- public static String int2roman(int n) {
- if (n >= 4000 || n < 1) {
- throw new NumberFormatException("Numbers must be in range 1-3999");
- }
- StringBuilder result = new StringBuilder(10);
-
- //... Start with largest value, and work toward smallest.
- for (RomanValue equiv : ROMAN_VALUE_TABLE) {
- //... Remove as many of this value as possible (maybe none).
- while (n >= equiv.intVal) {
- n -= equiv.intVal; // Subtract value.
- result.append(equiv.romVal); // Add roman equivalent.
- }
- }
- return result.toString();
- }
-
- private static class RomanValue {
- //... No need to make this fields private because they are
- // used only in this private value class.
- int intVal; // Integer value.
- String romVal; // Equivalent roman numeral.
-
- RomanValue(int dec, String rom) {
- this.intVal = dec;
- this.romVal = rom;
- }
- }
-}
\ No newline at end of file
diff -r 1daf3026d231 -r a54db233ee3d java-genomics-toolkit/src/edu/unc/utils/SortUtils.java
--- a/java-genomics-toolkit/src/edu/unc/utils/SortUtils.java Mon Feb 13 21:55:55 2012 -0500
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,60 +0,0 @@
-package edu.unc.utils;
-
-/**
- * Custom sorting utilities
- * see: http://stackoverflow.com/questions/951848/java-array-sort-quick-way-to-get-a-sorted-list-of-indices-of-an-array
- * @author timpalpant
- *
- */
-public class SortUtils {
- public static int[] indexSort(float[] main) {
- int[] index = new int[main.length];
- for (int i = 0; i < index.length; i++) {
- index[i] = i;
- }
-
- quicksort(main, index, 0, index.length-1);
-
- return index;
- }
-
- // quicksort a[left] to a[right]
- public static void quicksort(float[] a, int[] index, int left, int right) {
- if (right <= left)
- return;
- int i = partition(a, index, left, right);
- quicksort(a, index, left, i - 1);
- quicksort(a, index, i + 1, right);
- }
-
- // partition a[left] to a[right], assumes left < right
- private static int partition(float[] a, int[] index, int left, int right) {
- int i = left - 1;
- int j = right;
- while (true) {
- while (a[index[++i]] < a[index[right]])
- // find item on left to swap
- ; // a[right] acts as sentinel
- while (a[index[right]] < a[index[--j]])
- // find item on right to swap
- if (j == left)
- break; // don't go out-of-bounds
- if (i >= j)
- break; // check if pointers cross
- exch(a, index, i, j); // swap two elements into place
- }
- exch(a, index, i, right); // swap with partition element
- return i;
- }
-
- // exchange a[i] and a[j]
- private static void exch(float[] a, int[] index, int i, int j) {
- // Don't swap the data
- // float swap = a[i];
- // a[i] = a[j];
- // a[j] = swap;
- int b = index[i];
- index[i] = index[j];
- index[j] = b;
- }
-}
diff -r 1daf3026d231 -r a54db233ee3d java-genomics-toolkit/stubFile.sh
--- a/java-genomics-toolkit/stubFile.sh Mon Feb 13 21:55:55 2012 -0500
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,4 +0,0 @@
-#!/usr/bin/env bash
-
-DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
-java -Dlog4j.configuration=log4j.properties -cp "$DIR/../Resources/Java/*" edu.unc.genomics.ToolRunner
\ No newline at end of file
diff -r 1daf3026d231 -r a54db233ee3d java-genomics-toolkit/toolRunner.sh
--- a/java-genomics-toolkit/toolRunner.sh Mon Feb 13 21:55:55 2012 -0500
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,14 +0,0 @@
-#!/usr/bin/env bash
-
-if [ $# -eq 0 ]
-then
- echo "USAGE: toolRunner.sh APPNAME [ARGS]";
- exit;
-fi
-
-if [ "$1" = "list" ]
-then
- find src/edu/unc/genomics/**/*.java -exec basename -s .java {} \;
-fi
-
-java -Dlog4j.configuration=log4j.properties -cp .:build:lib/* edu.unc.genomics."$@"
\ No newline at end of file