htsjdk.samtools.metrics.MetricBase Java Examples

The following examples show how to use htsjdk.samtools.metrics.MetricBase. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CollectIlluminaLaneMetrics.java    From picard with MIT License 5 votes vote down vote up
@Override
protected int doWork() {
    final MetricsFile<MetricBase, Comparable<?>> laneMetricsFile = this.getMetricsFile();
    final MetricsFile<MetricBase, Comparable<?>> phasingMetricsFile = this.getMetricsFile();

    if (READ_STRUCTURE == null) {
        final File runInfo = new File(RUN_DIRECTORY + "/" + "RunInfo.xml");
        IOUtil.assertFileIsReadable(runInfo);
        try {
            final Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(runInfo);
            final NodeList reads = document.getElementsByTagName("Read");
            final List<ReadDescriptor> descriptors = new ArrayList<>(reads.getLength());
            for (int i = 0; i < reads.getLength(); i++) {
                final Node read = reads.item(i);
                final NamedNodeMap attributes = read.getAttributes();
                final int readNumber = Integer.parseInt(attributes.getNamedItem("Number").getNodeValue());
                final int numCycles = Integer.parseInt(attributes.getNamedItem("NumCycles").getNodeValue());
                final boolean isIndexedRead = attributes.getNamedItem("IsIndexedRead").getNodeValue().toUpperCase().equals("Y");
                if (readNumber != i + 1) throw new PicardException("Read number in RunInfo.xml was out of order: " + (i+1) + " != " + readNumber);
                descriptors.add(new ReadDescriptor(numCycles, isIndexedRead ? ReadType.Barcode: ReadType.Template));
            }
            READ_STRUCTURE = new ReadStructure(descriptors);
        } catch (final Exception e) {
            throw new PicardException(e.getMessage());
        }
    }

    IlluminaLaneMetricsCollector.collectLaneMetrics(RUN_DIRECTORY, OUTPUT_DIRECTORY, OUTPUT_PREFIX,
            laneMetricsFile, phasingMetricsFile,
            READ_STRUCTURE, FILE_EXTENSION == null ? "" : FILE_EXTENSION, VALIDATION_STRINGENCY, IS_NOVASEQ);
    return 0;
}
 
Example #2
Source File: CollectIlluminaLaneMetrics.java    From picard with MIT License 5 votes vote down vote up
/** Parses the tile data from the basecall directory and writes to both the lane and phasing metrics files */
public static void collectLaneMetrics(final File runDirectory, final File outputDirectory, final String outputPrefix,
                                      final MetricsFile<MetricBase, Comparable<?>> laneMetricsFile,
                                      final MetricsFile<MetricBase, Comparable<?>> phasingMetricsFile,
                                      final ReadStructure readStructure, final String fileExtension,
                                      final ValidationStringency validationStringency,
                                      final boolean isNovaSeq) {
    final Map<Integer, ? extends Collection<Tile>> laneTiles = readLaneTiles(runDirectory, readStructure, validationStringency, isNovaSeq);
    writeLaneMetrics(laneTiles, outputDirectory, outputPrefix, laneMetricsFile, fileExtension);
    writePhasingMetrics(laneTiles, outputDirectory, outputPrefix, phasingMetricsFile, fileExtension, isNovaSeq);
}
 
Example #3
Source File: CollectIlluminaLaneMetrics.java    From picard with MIT License 5 votes vote down vote up
public static File writePhasingMetrics(final Map<Integer, ? extends Collection<Tile>> laneTiles, final File outputDirectory,
                                       final String outputPrefix, final MetricsFile<MetricBase, Comparable<?>> phasingMetricsFile,
                                       final String fileExtension, final boolean isNovaSeq) {
    laneTiles.forEach((key, value) -> IlluminaPhasingMetrics.getPhasingMetricsForTiles(key.longValue(),
            value, !isNovaSeq).forEach(phasingMetricsFile::addMetric));

    return writeMetrics(phasingMetricsFile, outputDirectory, outputPrefix, IlluminaPhasingMetrics.getExtension() + fileExtension);
}
 
Example #4
Source File: CollectIlluminaLaneMetrics.java    From picard with MIT License 5 votes vote down vote up
public static File writeLaneMetrics(final Map<Integer, ? extends Collection<Tile>> laneTiles, final File outputDirectory,
                                    final String outputPrefix, final MetricsFile<MetricBase, Comparable<?>> laneMetricsFile,
                                    final String fileExtension) {
    laneTiles.entrySet().forEach(entry -> {
        final IlluminaLaneMetrics laneMetric = new IlluminaLaneMetrics();
        laneMetric.LANE = entry.getKey().longValue();
        laneMetric.CLUSTER_DENSITY = calculateLaneDensityFromTiles(entry.getValue());
        laneMetricsFile.addMetric(laneMetric);
    });

    return writeMetrics(laneMetricsFile, outputDirectory, outputPrefix, IlluminaLaneMetrics.getExtension() + fileExtension);
}
 
Example #5
Source File: CollectIlluminaLaneMetrics.java    From picard with MIT License 5 votes vote down vote up
private static File writeMetrics(final MetricsFile<MetricBase, Comparable<?>> metricsFile, final File outputDirectory,
                                 final String outputPrefix, final String outputExtension) {
    final File outputFile = new File(outputDirectory, String.format("%s.%s", outputPrefix, outputExtension));
    LOG.info(String.format("Writing %s lane metrics to %s ...", metricsFile.getMetrics().size(), outputFile));
    metricsFile.write(outputFile);
    return outputFile;
}
 
Example #6
Source File: CommandLineProgram.java    From picard with MIT License 5 votes vote down vote up
/** Gets a MetricsFile with default headers already written into it. */
protected <A extends MetricBase,B extends Comparable<?>> MetricsFile<A,B> getMetricsFile() {
    final MetricsFile<A,B> file = new MetricsFile<>();
    for (final Header h : this.defaultHeaders) {
        file.addHeader(h);
    }

    return file;
}
 
Example #7
Source File: MetricBaseTest.java    From picard with MIT License 5 votes vote down vote up
@DataProvider(name = "testMetricClasses")
public Iterator<Object[]> getMetricClasses() {
    final ClassFinder classFinder = new ClassFinder();

    classFinder.find("picard", MetricBase.class);
    return classFinder.getClasses().stream()
            .map(c -> new Object[]{c})
            .iterator();
}
 
Example #8
Source File: ConvertSequencingArtifactToOxoGTest.java    From picard with MIT License 5 votes vote down vote up
/**
 * Compare the metrics in two files, ignoring headers and histograms.
 */
public static boolean areMetricsEqual(final File file1, final File file2, final String[] columnsToCompare) throws NoSuchFieldException {
    try (Reader reader1 = new FileReader(file1);
         Reader reader2 = new FileReader(file2)) {
        final MetricsFile<MetricBase, ?> mf1 = new MetricsFile<>();
        final MetricsFile<MetricBase, ?> mf2 = new MetricsFile<>();
        mf1.read(reader1);
        mf2.read(reader2);

        return areMetricsEqual(mf1, mf2, columnsToCompare);
    } catch (IOException e) {
        throw new SAMException(e);
    }
}
 
Example #9
Source File: ConvertSequencingArtifactToOxoGTest.java    From picard with MIT License 5 votes vote down vote up
private static <T extends MetricBase> boolean areMetricsEqual(MetricsFile<T, ?> metricFile1,
                                                              MetricsFile<T, ?> metricFile2,
                                                              final String[] columnsToCompare) throws NoSuchFieldException {

    if (metricFile1.getMetrics().size() != metricFile2.getMetrics().size()) {
        return false;
    }
    if (metricFile1.getMetrics().isEmpty()) {
        return true;
    }

    T firstMetric1 = metricFile1.getMetrics().get(0);

    for (final String column : columnsToCompare) {
        if (!metricFile1.getMetricsColumnLabels().contains(column)) {
            return false;
        }
        if (!metricFile2.getMetricsColumnLabels().contains(column)) {
            return false;
        }

        final Field f = firstMetric1.getClass().getField(column);

        Function<T, String> getValue = m -> {
            try {
                return f.get(m).toString();
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            }
        };
        List<String> metric1Values = metricFile1.getMetrics().stream().map(getValue).collect(Collectors.toList());
        List<String> metric2Values = metricFile2.getMetrics().stream().map(getValue).collect(Collectors.toList());

        if (!metric1Values.equals(metric2Values)) {
            return false;
        }
    }

    return true;
}
 
Example #10
Source File: CommandLineProgram.java    From gatk with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/** Gets a MetricsFile with default headers already written into it. */
protected <A extends MetricBase,B extends Comparable<?>> MetricsFile<A,B> getMetricsFile() {
    final MetricsFile<A,B> file = new MetricsFile<>();
    for (final Header h : this.defaultHeaders) {
        file.addHeader(h);
    }

    return file;
}