// Path: src/edu/unc/genomics/CommandLineTool.java
// public abstract class CommandLineTool {
// 
//   /**
//    * The default bite-size to use for applications that process files in chunks
//    * TODO Read from a configuration file
//    */
//   public static final int DEFAULT_CHUNK_SIZE = 10_000_000;
// 
//   /**
//    * Do the main computation of this tool
//    * 
//    * @throws IOException
//    */
//   public abstract void run() throws IOException;
// 
//   /**
//    * Parse command-line arguments and run the tool Exit on parameter exceptions
//    * 
//    * @param args
//    */
//   public void instanceMain(String[] args) throws CommandLineToolException {
//     // Initialize the command-line options parser
//     JCommander jc = new JCommander(this);
// 
//     // Add factories for parsing Paths, Assemblies, IntervalFiles, and WigFiles
//     jc.addConverterFactory(new PathFactory());
//     jc.addConverterFactory(new AssemblyFactory());
// 
//     // Set the program name to be the class name
//     String[] nameParts = getClass().getName().split("\\.");
//     String shortName = StringUtils.join(Arrays.copyOfRange(nameParts, nameParts.length - 2, nameParts.length), '.');
//     jc.setProgramName(shortName);
// 
//     try {
//       jc.parse(args);
//     } catch (ParameterException e) {
//       System.err.println(e.getMessage());
//       jc.usage();
//       System.exit(-1);
//     }
// 
//     ValidationStringency stringency = SAMFileReader.getDefaultValidationStringency();
//     try {
//       SAMFileReader.setDefaultValidationStringency(SAMFileReader.ValidationStringency.LENIENT);
//       run();
//     } catch (IOException e) {
//       throw new CommandLineToolException("IO error while running", e);
//     } finally {
//       SAMFileReader.setDefaultValidationStringency(stringency);
//     }
//   }
// }
// 
// Path: src/edu/unc/genomics/ReadablePathValidator.java
// public class ReadablePathValidator implements IParameterValidator {
// 
//   /*
//    * (non-Javadoc)
//    * 
//    * @see com.beust.jcommander.IParameterValidator#validate(java.lang.String,
//    * java.lang.String)
//    */
//   @Override
//   public void validate(String name, String value) throws ParameterException {
//     PathConverter converter = new PathConverter();
//     Path p = converter.convert(value);
//     if (!Files.isReadable(p)) {
//       throw new ParameterException("Parameter " + name + " should be a readable file");
//     }
//   }
// 
// }
// 
// Path: src/edu/unc/utils/ArrayUtils.java
// public class ArrayUtils {
// 
//   /**
//    * Get the index of the maximum (largest) value in an array In the event of a
//    * tie, the first index is returned
//    * 
//    * @param x
//    *          a vector of values
//    * @return the index of the largest element in x
//    */
//   public static int maxIndex(float[] x) {
//     float maxValue = -Float.MAX_VALUE;
//     int maxIndex = -1;
//     for (int i = 0; i < x.length; i++) {
//       if (x[i] > maxValue) {
//         maxValue = x[i];
//         maxIndex = i;
//       }
//     }
// 
//     return maxIndex;
//   }
// 
//   public static float[] mapToFloat(int[] data) {
//     float[] ret = new float[data.length];
//     for (int i = 0; i < data.length; i++) {
//       ret[i] = data[i];
//     }
//     return ret;
//   }
// 
//   public static int[] mapToInt(float[] data) {
//     int[] ret = new int[data.length];
//     for (int i = 0; i < data.length; i++) {
//       ret[i] = (int) data[i];
//     }
//     return ret;
//   }
// }


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.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
import com.beust.jcommander.Parameter;
import edu.unc.genomics.CommandLineTool;
import edu.unc.genomics.Interval;
import edu.unc.genomics.ReadablePathValidator;
import edu.unc.genomics.io.IntervalFileReader;
import edu.unc.genomics.io.WigFileReader;
import edu.unc.genomics.io.WigFileException;
import edu.unc.utils.ArrayUtils;
package edu.unc.genomics.ngs;





/**
 * For a set of intervals, this tool finds the location of the largest value in
 * a Wig file for each interval.
 * 
 * @author timpalpant
 *
 */
public class FindAbsoluteMaxima extends CommandLineTool {

  private static final Logger log = Logger.getLogger(FindAbsoluteMaxima.class);

  @Parameter(description = "Input files", required = true)
  public List<String> inputFiles = new ArrayList<String>();
  @Parameter(names = { "-l", "--loci" }, description = "Loci file (Bed)", required = true, validateWith = ReadablePathValidator.class)
  public Path lociFile;
  @Parameter(names = { "-o", "--output" }, description = "Output file", required = true)
  public Path outputFile;

  private List<WigFileReader> wigs = new ArrayList<>();

  @Override
  public void run() throws IOException {
    log.debug("Initializing input Wig file(s)");
    for (String inputFile : inputFiles) {
      wigs.add(WigFileReader.autodetect(Paths.get(inputFile)));
    }

    log.debug("Initializing output file");
    int count = 0, skipped = 0;
    try (IntervalFileReader<? extends Interval> reader = IntervalFileReader.autodetect(lociFile);
// Path: src/edu/unc/genomics/CommandLineTool.java
// public abstract class CommandLineTool {
// 
//   /**
//    * The default bite-size to use for applications that process files in chunks
//    * TODO Read from a configuration file
//    */
//   public static final int DEFAULT_CHUNK_SIZE = 10_000_000;
// 
//   /**
//    * Do the main computation of this tool
//    * 
//    * @throws IOException
//    */
//   public abstract void run() throws IOException;
// 
//   /**
//    * Parse command-line arguments and run the tool Exit on parameter exceptions
//    * 
//    * @param args
//    */
//   public void instanceMain(String[] args) throws CommandLineToolException {
//     // Initialize the command-line options parser
//     JCommander jc = new JCommander(this);
// 
//     // Add factories for parsing Paths, Assemblies, IntervalFiles, and WigFiles
//     jc.addConverterFactory(new PathFactory());
//     jc.addConverterFactory(new AssemblyFactory());
// 
//     // Set the program name to be the class name
//     String[] nameParts = getClass().getName().split("\\.");
//     String shortName = StringUtils.join(Arrays.copyOfRange(nameParts, nameParts.length - 2, nameParts.length), '.');
//     jc.setProgramName(shortName);
// 
//     try {
//       jc.parse(args);
//     } catch (ParameterException e) {
//       System.err.println(e.getMessage());
//       jc.usage();
//       System.exit(-1);
//     }
// 
//     ValidationStringency stringency = SAMFileReader.getDefaultValidationStringency();
//     try {
//       SAMFileReader.setDefaultValidationStringency(SAMFileReader.ValidationStringency.LENIENT);
//       run();
//     } catch (IOException e) {
//       throw new CommandLineToolException("IO error while running", e);
//     } finally {
//       SAMFileReader.setDefaultValidationStringency(stringency);
//     }
//   }
// }
// 
// Path: src/edu/unc/genomics/ReadablePathValidator.java
// public class ReadablePathValidator implements IParameterValidator {
// 
//   /*
//    * (non-Javadoc)
//    * 
//    * @see com.beust.jcommander.IParameterValidator#validate(java.lang.String,
//    * java.lang.String)
//    */
//   @Override
//   public void validate(String name, String value) throws ParameterException {
//     PathConverter converter = new PathConverter();
//     Path p = converter.convert(value);
//     if (!Files.isReadable(p)) {
//       throw new ParameterException("Parameter " + name + " should be a readable file");
//     }
//   }
// 
// }
// 
// Path: src/edu/unc/utils/ArrayUtils.java
// public class ArrayUtils {
// 
//   /**
//    * Get the index of the maximum (largest) value in an array In the event of a
//    * tie, the first index is returned
//    * 
//    * @param x
//    *          a vector of values
//    * @return the index of the largest element in x
//    */
//   public static int maxIndex(float[] x) {
//     float maxValue = -Float.MAX_VALUE;
//     int maxIndex = -1;
//     for (int i = 0; i < x.length; i++) {
//       if (x[i] > maxValue) {
//         maxValue = x[i];
//         maxIndex = i;
//       }
//     }
// 
//     return maxIndex;
//   }
// 
//   public static float[] mapToFloat(int[] data) {
//     float[] ret = new float[data.length];
//     for (int i = 0; i < data.length; i++) {
//       ret[i] = data[i];
//     }
//     return ret;
//   }
// 
//   public static int[] mapToInt(float[] data) {
//     int[] ret = new int[data.length];
//     for (int i = 0; i < data.length; i++) {
//       ret[i] = (int) data[i];
//     }
//     return ret;
//   }
// }


// Path: src/edu/unc/genomics/ngs/FindAbsoluteMaxima.java
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.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
import com.beust.jcommander.Parameter;
import edu.unc.genomics.CommandLineTool;
import edu.unc.genomics.Interval;
import edu.unc.genomics.ReadablePathValidator;
import edu.unc.genomics.io.IntervalFileReader;
import edu.unc.genomics.io.WigFileReader;
import edu.unc.genomics.io.WigFileException;
import edu.unc.utils.ArrayUtils;

package edu.unc.genomics.ngs;





/**
 * For a set of intervals, this tool finds the location of the largest value in
 * a Wig file for each interval.
 * 
 * @author timpalpant
 *
 */
public class FindAbsoluteMaxima extends CommandLineTool {

  private static final Logger log = Logger.getLogger(FindAbsoluteMaxima.class);

  @Parameter(description = "Input files", required = true)
  public List<String> inputFiles = new ArrayList<String>();
  @Parameter(names = { "-l", "--loci" }, description = "Loci file (Bed)", required = true, validateWith = ReadablePathValidator.class)
  public Path lociFile;
  @Parameter(names = { "-o", "--output" }, description = "Output file", required = true)
  public Path outputFile;

  private List<WigFileReader> wigs = new ArrayList<>();

  @Override
  public void run() throws IOException {
    log.debug("Initializing input Wig file(s)");
    for (String inputFile : inputFiles) {
      wigs.add(WigFileReader.autodetect(Paths.get(inputFile)));
    }

    log.debug("Initializing output file");
    int count = 0, skipped = 0;
    try (IntervalFileReader<? extends Interval> reader = IntervalFileReader.autodetect(lociFile);
        BufferedWriter writer = Files.newBufferedWriter(outputFile, Charset.defaultCharset())) {