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