// 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/RomanNumeral.java
// public class RomanNumeral {
// 
//   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") };
// 
//   /**
//    * Convert an int to Roman numeral
//    * 
//    * @param n
//    *          an integer between 1-3999
//    * @return n as a Roman numeral
//    */
//   public static String int2roman(int n) {
//     if (n >= 4000 || n < 1) {
//       throw new NumberFormatException("Numbers must be in range 1-3999");
//     }
// 
//     // ... Start with largest value, and work toward smallest.
//     StringBuilder result = new StringBuilder(10);
//     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;
//     }
//   }
// }


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 java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.log4j.Logger;
import com.beust.jcommander.Parameter;
import edu.unc.genomics.CommandLineTool;
import edu.unc.genomics.ReadablePathValidator;
import edu.unc.utils.RomanNumeral;
package edu.unc.genomics.converters;





/**
 * Convert instances of "chr12" to "chrXII" in a text file, etc.
 * 
 * @author timpalpant
 *
 */
public class RomanNumeralize extends CommandLineTool {

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

  @Parameter(names = { "-i", "--input" }, description = "Input file", required = true, validateWith = ReadablePathValidator.class)
  public Path inputFile;
  @Parameter(names = { "-o", "--output" }, description = "Output file", required = true)
  public Path outputFile;

  /**
   * Pattern for finding "chr12" tokens (will find "chr1" through "chr99")
   */
  Pattern p = Pattern.compile("chr[\\d]{1,2}");

  @Override
  public void run() throws IOException {
    try (BufferedReader reader = Files.newBufferedReader(inputFile, Charset.defaultCharset());
        BufferedWriter writer = Files.newBufferedWriter(outputFile, Charset.defaultCharset())) {
      log.debug("Copying input to output and replacing with Roman Numerals");
      String line;
      while ((line = reader.readLine()) != null) {
        Matcher m = p.matcher(line);
        StringBuffer converted = new StringBuffer(line.length());
        while (m.find()) {
          String chrNum = line.substring(m.start() + 3, m.end());
          int arabic = Integer.parseInt(chrNum);
// 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/RomanNumeral.java
// public class RomanNumeral {
// 
//   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") };
// 
//   /**
//    * Convert an int to Roman numeral
//    * 
//    * @param n
//    *          an integer between 1-3999
//    * @return n as a Roman numeral
//    */
//   public static String int2roman(int n) {
//     if (n >= 4000 || n < 1) {
//       throw new NumberFormatException("Numbers must be in range 1-3999");
//     }
// 
//     // ... Start with largest value, and work toward smallest.
//     StringBuilder result = new StringBuilder(10);
//     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;
//     }
//   }
// }


// Path: src/edu/unc/genomics/converters/RomanNumeralize.java
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 java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.log4j.Logger;
import com.beust.jcommander.Parameter;
import edu.unc.genomics.CommandLineTool;
import edu.unc.genomics.ReadablePathValidator;
import edu.unc.utils.RomanNumeral;

package edu.unc.genomics.converters;





/**
 * Convert instances of "chr12" to "chrXII" in a text file, etc.
 * 
 * @author timpalpant
 *
 */
public class RomanNumeralize extends CommandLineTool {

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

  @Parameter(names = { "-i", "--input" }, description = "Input file", required = true, validateWith = ReadablePathValidator.class)
  public Path inputFile;
  @Parameter(names = { "-o", "--output" }, description = "Output file", required = true)
  public Path outputFile;

  /**
   * Pattern for finding "chr12" tokens (will find "chr1" through "chr99")
   */
  Pattern p = Pattern.compile("chr[\\d]{1,2}");

  @Override
  public void run() throws IOException {
    try (BufferedReader reader = Files.newBufferedReader(inputFile, Charset.defaultCharset());
        BufferedWriter writer = Files.newBufferedWriter(outputFile, Charset.defaultCharset())) {
      log.debug("Copying input to output and replacing with Roman Numerals");
      String line;
      while ((line = reader.readLine()) != null) {
        Matcher m = p.matcher(line);
        StringBuffer converted = new StringBuffer(line.length());
        while (m.find()) {
          String chrNum = line.substring(m.start() + 3, m.end());
          int arabic = Integer.parseInt(chrNum);
          String roman = RomanNumeral.int2roman(arabic);