annotate java-genomics-toolkit/src/edu/unc/genomics/ngs/FindAbsoluteMaxima.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.ngs;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
2
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
3 import java.io.BufferedWriter;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
4 import java.io.IOException;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
5 import java.nio.charset.Charset;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
6 import java.nio.file.Files;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
7 import java.nio.file.Path;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
8 import java.nio.file.Paths;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
9 import java.util.ArrayList;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
10 import java.util.Iterator;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
11 import java.util.List;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
12
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
13 import org.apache.log4j.Logger;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
14 import org.broad.igv.bbfile.WigItem;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
15
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
16 import com.beust.jcommander.Parameter;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
17
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
18 import edu.unc.genomics.CommandLineTool;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
19 import edu.unc.genomics.Interval;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
20 import edu.unc.genomics.io.IntervalFile;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
21 import edu.unc.genomics.io.WigFile;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
22 import edu.unc.genomics.io.WigFileException;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
23
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
24 public class FindAbsoluteMaxima extends CommandLineTool {
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
25
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
26 private static final Logger log = Logger.getLogger(FindAbsoluteMaxima.class);
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
27
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
28 @Parameter(description = "Input files", required = true)
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
29 public List<String> inputFiles = new ArrayList<String>();
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
30 @Parameter(names = {"-l", "--loci"}, description = "Loci file (Bed)", required = true)
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
31 public IntervalFile<? extends Interval> lociFile;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
32 @Parameter(names = {"-o", "--output"}, description = "Output file", required = true)
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
33 public Path outputFile;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
34
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
35 private List<WigFile> wigs = new ArrayList<>();
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
36
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
37 @Override
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
38 public void run() throws IOException {
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
39 log.debug("Initializing input Wig file(s)");
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
40 for (String inputFile : inputFiles) {
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
41 try {
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
42 WigFile wig = WigFile.autodetect(Paths.get(inputFile));
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
43 wigs.add(wig);
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
44 } catch (WigFileException e) {
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
45 log.error("Error initializing Wig input file: " + inputFile);
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
46 e.printStackTrace();
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
47 throw new RuntimeException("Error initializing Wig input file: " + inputFile);
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
48 }
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
49 }
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
50
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
51 log.debug("Initializing output file");
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
52 int count = 0, skipped = 0;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
53 try (BufferedWriter writer = Files.newBufferedWriter(outputFile, Charset.defaultCharset())) {
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
54 writer.write("#Chr\tStart\tStop\tID\tValue\tStrand");
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
55 for (String inputFile : inputFiles) {
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
56 Path p = Paths.get(inputFile);
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
57 writer.write("\t" + p.getFileName().toString());
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
58 }
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
59 writer.newLine();
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
60
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
61 log.debug("Iterating over all intervals and finding maxima");
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
62 for (Interval interval : lociFile) {
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
63 writer.write(interval.toBed());
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
64 for (WigFile wig : wigs) {
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
65 float maxValue = -Float.MAX_VALUE;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
66 int maxima = -1;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
67 try {
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
68 Iterator<WigItem> results = wig.query(interval);
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
69 while (results.hasNext()) {
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
70 WigItem item = results.next();
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
71 if (item.getWigValue() > maxValue) {
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
72 maxValue = item.getWigValue();
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
73 maxima = (item.getStartBase() + item.getEndBase()) / 2;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
74 }
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
75 }
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
76 writer.write("\t" + maxima);
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
77 } catch (WigFileException e) {
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
78 writer.write("\t" + Float.NaN);
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
79 skipped++;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
80 }
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
81 }
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
82 writer.newLine();
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
83 count++;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
84 }
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
85 }
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
86
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
87 lociFile.close();
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
88 for (WigFile wig : wigs) {
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
89 wig.close();
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
90 }
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
91 log.info(count + " intervals processed");
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
92 log.info(skipped + " interval skipped");
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
93 }
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
94
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
95 public static void main(String[] args) {
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
96 new FindAbsoluteMaxima().instanceMain(args);
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
97 }
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
98
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
99 }