annotate java-genomics-toolkit/src/edu/unc/genomics/converters/RomanNumeralize.java @ 0:1daf3026d231

Upload alpha version
author timpalpant
date Mon, 13 Feb 2012 21:55:55 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
1 package edu.unc.genomics.converters;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
2
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
3 import java.io.BufferedReader;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
4 import java.io.BufferedWriter;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
5 import java.io.IOException;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
6 import java.nio.charset.Charset;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
7 import java.nio.file.Files;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
8 import java.nio.file.Path;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
9 import java.util.regex.Matcher;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
10 import java.util.regex.Pattern;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
11
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
12 import org.apache.log4j.Logger;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
13
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
14 import com.beust.jcommander.Parameter;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
15
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
16 import edu.unc.genomics.CommandLineTool;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
17 import edu.unc.genomics.ReadablePathValidator;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
18 import edu.unc.utils.RomanNumeral;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
19
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
20 public class RomanNumeralize extends CommandLineTool {
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
21
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
22 private static final Logger log = Logger.getLogger(RomanNumeralize.class);
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
23
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
24 @Parameter(names = {"-i", "--input"}, description = "Input file", required = true, validateWith = ReadablePathValidator.class)
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
25 public Path inputFile;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
26 @Parameter(names = {"-o", "--output"}, description = "Output file", required = true)
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
27 public Path outputFile;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
28
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
29 /**
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
30 * Pattern for finding "chr12" tokens (will find "chr1" through "chr99")
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
31 */
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
32 Pattern p = Pattern.compile("/chr[\\d]{1,2}/i");
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
33
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
34 @Override
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
35 public void run() throws IOException {
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
36 try (BufferedReader reader = Files.newBufferedReader(inputFile, Charset.defaultCharset());
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
37 BufferedWriter writer = Files.newBufferedWriter(outputFile, Charset.defaultCharset())) {
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
38 log.debug("Copying input to output and replacing with Roman Numerals");
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
39 String line;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
40 while ((line = reader.readLine()) != null) {
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
41 Matcher m = p.matcher(line);
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
42 StringBuffer converted = new StringBuffer(line.length());
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
43 while (m.find()) {
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
44 String chrNum = line.substring(m.start()+3, m.end());
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
45 int arabic = Integer.parseInt(chrNum);
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
46 String roman = RomanNumeral.int2roman(arabic);
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
47 m.appendReplacement(converted, "chr"+roman);
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
48 }
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
49 m.appendTail(converted);
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
50
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
51 writer.write(converted.toString());
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
52 writer.newLine();
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
53 }
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
54 }
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
55 }
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
56
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
57 public static void main(String[] args) {
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
58 new RomanNumeralize().instanceMain(args);
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
59 }
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
60
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
61 }