annotate src/edu/unc/genomics/ngs/IntervalStats.java @ 2:e16016635b2a

Uploaded
author timpalpant
date Mon, 13 Feb 2012 22:12:06 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
1 package edu.unc.genomics.ngs;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
2
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
3 import java.io.BufferedWriter;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
4 import java.io.IOException;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
5 import java.nio.charset.Charset;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
6 import java.nio.file.Files;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
7 import java.nio.file.Path;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
8 import java.nio.file.Paths;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
9 import java.util.ArrayList;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
10 import java.util.Iterator;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
11 import java.util.List;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
12
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
13 import org.apache.commons.lang3.StringUtils;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
14 import org.apache.commons.math.stat.descriptive.SummaryStatistics;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
15 import org.apache.log4j.Logger;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
16 import org.broad.igv.bbfile.WigItem;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
17
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
18 import com.beust.jcommander.Parameter;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
19
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
20 import edu.unc.genomics.CommandLineTool;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
21 import edu.unc.genomics.Interval;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
22 import edu.unc.genomics.io.IntervalFile;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
23 import edu.unc.genomics.io.WigFile;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
24 import edu.unc.genomics.io.WigFileException;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
25
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
26 public class IntervalStats extends CommandLineTool {
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
27
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
28 private static final Logger log = Logger.getLogger(IntervalStats.class);
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
29
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
30 @Parameter(description = "Input files", required = true)
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
31 public List<String> inputFiles = new ArrayList<String>();
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
32 @Parameter(names = {"-l", "--loci"}, description = "Loci file (Bed)", required = true)
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
33 public IntervalFile<? extends Interval> lociFile;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
34 @Parameter(names = {"-o", "--output"}, description = "Output file", required = true)
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
35 public Path outputFile;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
36
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
37 private List<WigFile> wigs = new ArrayList<>();
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
38
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
39 @Override
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
40 public void run() throws IOException {
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
41 log.debug("Initializing input Wig file(s)");
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
42 for (String inputFile : inputFiles) {
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
43 try {
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
44 WigFile wig = WigFile.autodetect(Paths.get(inputFile));
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
45 wigs.add(wig);
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
46 } catch (WigFileException e) {
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
47 log.error("Error initializing Wig input file: " + inputFile);
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
48 e.printStackTrace();
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
49 throw new RuntimeException("Error initializing Wig input file: " + inputFile);
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
50 }
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
51 }
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
52
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
53 log.debug("Initializing output file");
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
54 int count = 0, skipped = 0;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
55 try (BufferedWriter writer = Files.newBufferedWriter(outputFile, Charset.defaultCharset())) {
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
56 writer.write("#Chr\tStart\tStop\tID\tValue\tStrand");
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
57 for (String inputFile : inputFiles) {
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
58 Path p = Paths.get(inputFile);
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
59 writer.write("\t" + p.getFileName().toString());
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
60 }
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
61 writer.newLine();
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
62
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
63 log.debug("Iterating over all intervals and computing statistics");
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
64 SummaryStatistics stats = new SummaryStatistics();
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
65 for (Interval interval : lociFile) {
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
66 List<Double> means = new ArrayList<>(wigs.size());
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
67 for (WigFile wig : wigs) {
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
68 stats.clear();
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
69 try {
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
70 Iterator<WigItem> result = wig.query(interval);
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
71 while(result.hasNext()) {
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
72 WigItem item = result.next();
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
73 for (int i = item.getStartBase(); i <= item.getEndBase(); i++) {
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
74 stats.addValue(item.getWigValue());
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
75 }
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
76 }
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
77 means.add(stats.getMean());
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
78 } catch (WigFileException e) {
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
79 means.add(Double.NaN);
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
80 skipped++;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
81 }
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
82 }
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
83
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
84 writer.write(interval.toBed() + "\t" + StringUtils.join(means, "\t"));
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
85 writer.newLine();
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
86 count++;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
87 }
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
88 }
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
89
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
90 lociFile.close();
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
91 for (WigFile wig : wigs) {
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
92 wig.close();
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
93 }
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
94 log.info(count + " intervals processed");
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
95 log.info(skipped + " interval skipped");
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
96 }
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
97
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
98 public static void main(String[] args) {
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
99 new IntervalStats().instanceMain(args);
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
100 }
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
101
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
102 }