int classCol = 11;
int famCol = 12;
Map<String, LinkedHashMap<String, String>> fileMappings = new HashMap();
AsciiLineReader reader = null;
HashMap<String, PrintWriter> writers = new HashMap();
try {
String lastChr = "";
reader = new AsciiLineReader(new FileInputStream(file));
// Skip header
reader.readLine();
String nextLine;
while ((nextLine = reader.readLine()) != null) {
String[] tokens = Globals.tabPattern.split(nextLine, -1);
String chr = tokens[chrCol];
if (!chr.equals(lastChr)) {
closeWriters(writers);
}
lastChr = chr;
String repClass = tokens[classCol];
if (repClass.contains("?")) {
continue;
}
String fileKey = chr + "." + repClass;
// Get or create file writer for the class + chr combination
PrintWriter pw = writers.get(fileKey);
if (pw == null) {
File dir = new File(file.getParent(), repClass);
if (!dir.exists()) {
dir.mkdir();
fileMappings.put(repClass, new LinkedHashMap<String, String>());
}
Map<String, String> fMap = fileMappings.get(repClass);
String fn = fileKey + ".bed";
fMap.put(chr, fn);
File outputFile = new File(dir, fn);
pw = new PrintWriter(new FileWriter(outputFile));
writers.put(fileKey, pw);
}
String name = "Repeat " + tokens[4] + ", family " + tokens[6];
pw.print(chr);
pw.print("\t");
pw.print(Integer.parseInt(tokens[startCol]));
pw.print("\t");
pw.print(Integer.parseInt(tokens[endCol]));
pw.print("\t");
pw.print(name);
pw.print("\t");
pw.print(tokens[strandCol]);
pw.println();
}
// Ouput filemapping files
for (Map.Entry<String, LinkedHashMap<String, String>> entry : fileMappings.entrySet()) {
String repClass = entry.getKey();
File dir = new File(file.getParent(), repClass);
File listFile = new File(dir, repClass + "_files.list.txt");
Properties props = new Properties();
for (Map.Entry<String, String> entry2 : entry.getValue().entrySet()) {
props.put(entry2.getKey(), entry2.getValue());
}
FileOutputStream os = new FileOutputStream(listFile);
props.store(os, "");
os.close();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
reader.close();
closeWriters(writers);
}
}