try {
br = new BufferedReader(new FileReader(iFile));
pw = new PrintWriter(new BufferedWriter(new FileWriter(outputFile)));
FeatureParser parser = AbstractFeatureParser.getInstanceFor(new ResourceLocator(iFile), null);
List<Feature> features = parser.loadFeatures(br, null);
IGVBEDCodec codec = new IGVBEDCodec();
Map<String, List<BasicFeature>> genes = new HashMap<String, List<BasicFeature>>();
for (Feature f : features) {
BasicFeature transcript = (BasicFeature) f;
String geneName = transcript.getName();
List<BasicFeature> genelist = genes.get(geneName);
if (genelist == null) {
genelist = new ArrayList<BasicFeature>();
genes.put(geneName, genelist);
}
// Loop through genes with this name to find one that overlaps
boolean foundOverlap = false;
for (BasicFeature gene : genelist) {
if (gene.overlaps(transcript)) {
gene.setThickStart(Math.min(gene.getThickStart(), transcript.getThickStart()));
gene.setThickEnd(Math.max(gene.getThickEnd(), transcript.getThickEnd()));
mergeExons(gene, transcript.getExons());
foundOverlap = true;
break;
}
}
if (!foundOverlap) {
genelist.add(transcript);
}
}
for (List<BasicFeature> geneList : genes.values()) {
for (BasicFeature gene : geneList) {
pw.println(codec.encode(gene));
}
}
} finally {
if (br != null) br.close();
if (pw != null) pw.close();