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