Java Code Examples for htsjdk.samtools.util.IOUtil#openFileForBufferedReading()

The following examples show how to use htsjdk.samtools.util.IOUtil#openFileForBufferedReading() . 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: MergeDgeSparse.java    From Drop-seq with MIT License 6 votes vote down vote up
private Set<String> loadSelectedCellsLists(final List<File> files) {
    final Set<String> ret = new HashSet<>();
    final Pattern comment = Pattern.compile("#");
    final Pattern whitespace = Pattern.compile("\\s");
    for (final File file : files) {
        final BufferedReader reader = IOUtil.openFileForBufferedReading(file);
        String line;
        try {
            while ((line = reader.readLine()) != null) {
                // Remove trailing comments
                String[] fields = comment.split(line, 2);
                if (!fields[0].isEmpty()) {
                    // Remove trailing whitespace
                    fields = whitespace.split(fields[0], 2);
                    if (!fields[0].isEmpty())
			ret.add(fields[0]);
                }
            }
        } catch (IOException e) {
            throw new RuntimeIOException("Exception reading " + file.getAbsolutePath(), e);
        }
    }
    return ret;
}
 
Example 2
Source File: NonNFastaSizeTest.java    From picard with MIT License 6 votes vote down vote up
@Test
public void noIntervals() throws IOException {
       final File input = new File(REFERENCE);
       final File outfile   = File.createTempFile("nonNcount", ".txt");
       outfile.deleteOnExit();
       final String[] args = new String[] {
               "INPUT="  + input.getAbsolutePath(),
               "OUTPUT=" + outfile.getAbsolutePath()
       };
       Assert.assertEquals(new NonNFastaSize().instanceMain(args), 0);

       final BufferedReader reader = IOUtil.openFileForBufferedReading(outfile);
       final String count = reader.readLine();

       try {
           Assert.assertEquals(Long.parseLong(count), 1008);
       } catch (Exception e) {
           System.err.println("Failed to read in count because of error: " + e.getMessage());
       }
   }
 
Example 3
Source File: NonNFastaSizeTest.java    From picard with MIT License 6 votes vote down vote up
@Test
public void withIntervals() throws IOException {
    final File input = new File(REFERENCE);
    final File outfile = File.createTempFile("nonNcount", ".txt");
    final File intervals = new File("testdata/picard/reference/test.intervals");
    outfile.deleteOnExit();
    final String[] args = new String[] {
            "INPUT="  + input.getAbsolutePath(),
            "OUTPUT=" + outfile.getAbsolutePath(),
            "INTERVALS=" + intervals.getAbsolutePath()
    };
    Assert.assertEquals(new NonNFastaSize().instanceMain(args), 0);

    final BufferedReader reader = IOUtil.openFileForBufferedReading(outfile);
    final String count = reader.readLine();

    try {
        Assert.assertEquals(Long.parseLong(count), 53);
    } catch (Exception e) {
        System.err.println("Failed to read in count because of error: " + e.getMessage());
    }
}
 
Example 4
Source File: FilterGtf.java    From Drop-seq with MIT License 5 votes vote down vote up
@Override
protected int doWork() {
    IOUtil.assertFileIsReadable(this.GTF);
    IOUtil.assertFileIsWritable(this.OUTPUT);

    geneBiotypesToFilter.addAll(UNDESIRED_GENE_BIOTYPE);
    if (SEQUENCE_DICTIONARY != null) {
        sequenceDictionary = DropSeqSamUtil.loadSequenceDictionary(SEQUENCE_DICTIONARY);
    }

    // Don't use TabbedInputParser because we want to preserve comments
    // Don't use GTFReader because 1) we want to preserve comments; and 2) what is needed at this point is not
    // complicated and custom parsing will be more straightforward.
    final BufferedReader reader = IOUtil.openFileForBufferedReading(GTF);
    final BufferedWriter writer = IOUtil.openFileForBufferedWriting(OUTPUT);
    String inputLine;
    try {
        while ((inputLine = reader.readLine()) != null ) {
            if (inputLine.startsWith("#") || goodLine(inputLine)) {
                writer.write(inputLine);
                writer.newLine();
            }
        }
        CloserUtil.close(reader);
        writer.close();
    } catch (IOException e) {
        throw new RuntimeIOException(e);
    }
    return 0;
}
 
Example 5
Source File: DgeHeaderMerger.java    From Drop-seq with MIT License 5 votes vote down vote up
public static DgeHeader mergeDgeHeaders(final List<File> input, final List<String> prefix, final Stringency stringency) {
    final DgeHeaderMerger headerMerger = new DgeHeaderMerger(stringency);
    final DgeHeaderCodec codec = new DgeHeaderCodec();
    for (int i = 0; i < input.size(); ++i) {
        final File file = input.get(i);
        final BufferedReader reader = IOUtil.openFileForBufferedReading(file);
        final DgeHeader dgeHeader = codec.decode(reader, file.getAbsolutePath());
        CloserUtil.close(reader);
        if (!prefix.isEmpty()) {
            if (dgeHeader.getNumLibraries() > 1) {
                throw new DgeMergerException("Cannot set PREFIX when input DGE has more than one LIBRARY");
            } else if (dgeHeader.getNumLibraries() == 1){
                final DgeHeaderLibrary lib = dgeHeader.getLibrary(0);
                if (stringency == DgeHeaderMerger.Stringency.STRICT && lib.getPrefix() != null) {
                    throw new DgeMergerException("Overwriting prefix in DGE header for " + file.getAbsolutePath());
                }
                lib.setPrefix(prefix.get(i));
            }
        }
        if (dgeHeader.getNumLibraries() == 1 && dgeHeader.getLibrary(0).getInputDge() == null) {
            dgeHeader.getLibrary(0).setInputDge(file);
        }
        headerMerger.mergeDgeHeader(dgeHeader);
    }
    return headerMerger.getMergedHeader();

}
 
Example 6
Source File: DGEMatrix.java    From Drop-seq with MIT License 5 votes vote down vote up
public static DgeHeader parseDgeHeader(final File input) {
    BufferedReader reader = null;
    try {
        reader = IOUtil.openFileForBufferedReading(input);
        return new DgeHeaderCodec().decode(reader, input.getAbsolutePath());
    } finally {
        CloserUtil.close(reader);
    }
}
 
Example 7
Source File: DGEMatrix.java    From Drop-seq with MIT License 5 votes vote down vote up
private static FileFormat detectFileFormat(final File input) {
     BufferedReader reader = null;
     try {
         reader = IOUtil.openFileForBufferedReading(input);
         if (MatrixMarketReader.isMatrixMarketReal(reader))
	return FileFormat.MM_SPARSE;
else if (MatrixMarketReader.isMatrixMarketInteger(reader))
	return FileFormat.MM_SPARSE_10X;
else
	return FileFormat.DENSE;
     } finally {
         CloserUtil.close(reader);
     }
 }
 
Example 8
Source File: SelectCellsByNumTranscripts.java    From Drop-seq with MIT License 5 votes vote down vote up
public static List<String> readBarcodes(final File file) {
    try {
        IOUtil.assertFileIsReadable(file);
        final BufferedReader reader = IOUtil.openFileForBufferedReading(file);
        final List<String> ret = new ArrayList<>();
        String barcode;
        while ((barcode = reader.readLine()) != null)
if (!barcode.isEmpty())
	ret.add(barcode);
        CloserUtil.close(reader);
        return ret;
    } catch (IOException e) {
        throw new RuntimeIOException("Exception reading " + file.getAbsolutePath());
    }
}
 
Example 9
Source File: BaseDistributionMetricCollection.java    From Drop-seq with MIT License 5 votes vote down vote up
public static BaseDistributionMetricCollection readBaseDistribution(final File reportFile) {
    BaseDistributionMetricCollection metricCollection = new BaseDistributionMetricCollection();

    String[] columns = null;
    char[] bases = null;
    try {
        BufferedReader input = IOUtil.openFileForBufferedReading(reportFile);
        try  {
            String line;
            while ((line = input.readLine()) != null) {
                line=line.trim();
                if (columns == null && line.startsWith("position")) {
                    columns = line.split("\t");
                    if (columns.length != 6) {
                        throw new RuntimeException("Report header for " + reportFile.getAbsolutePath() + " contains " + columns.length + " columns");
                    }
                    bases = new char[columns.length];
                    for (int idx=1; idx<columns.length; idx++) {
                        bases[idx] = columns[idx].charAt(0);
                    }
                    continue;
                } else if (columns == null) {
                    throw new RuntimeException("The first line in " + reportFile.getAbsolutePath() + " does not look like a header");
                }
                String[] strLine = line.split("\t");
                int position = Integer.parseInt(strLine[0]);
                for (int idx=1; idx<6; idx++) {
                    int count = Integer.parseInt(strLine[idx]);
                    metricCollection.addBase(bases[idx], position, count);
                }
            }
        } finally {
            input.close();
        }
    } catch (IOException ex) {
        throw new TranscriptomeException("Error reading the file: " + reportFile.getAbsolutePath());
    }

    return (metricCollection);
}
 
Example 10
Source File: MatrixMarketReader.java    From Drop-seq with MIT License 5 votes vote down vote up
private static boolean isFileTypeHelper(final File file, final Function<BufferedReader, Boolean> fun) {
    BufferedReader reader = IOUtil.openFileForBufferedReading(file);
    try {
        return fun.apply(reader);
    } finally {
        CloserUtil.close(reader);
    }
}
 
Example 11
Source File: FilterGtfTest.java    From Drop-seq with MIT License 5 votes vote down vote up
private void runClp(int expectedOutputLines, FilterGtf filterGtf) throws IOException {
    Assert.assertEquals(filterGtf.doWork(), 0);
    final BufferedReader reader = IOUtil.openFileForBufferedReading(filterGtf.OUTPUT);
    int lines = 0;
    while (reader.readLine() != null) {
        ++lines;
    }
    CloserUtil.close(reader);
    Assert.assertEquals(lines, expectedOutputLines);
}
 
Example 12
Source File: ModularFileParser.java    From Drop-seq with MIT License 4 votes vote down vote up
public ModularFileParser(Parser parser, File inFile, int linesToSkip)  {
	this.parser=parser;
		in = IOUtil.openFileForBufferedReading(inFile);
	this.linesToSkip=linesToSkip;
}
 
Example 13
Source File: CollectF1R2CountsIntegrationTest.java    From gatk with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Test
public void testHistograms() throws IOException {
    final File outputTarGz = createTempFile("f1r2", ".tar.gz");
    final String sample = "SAMPLE";
    final File sam = createSyntheticSam(30, 1, sample);

    final String[] args = {
            "-R", hg19_chr1_1M_Reference,
            "-I", sam.getAbsolutePath(),
            "-O", outputTarGz.getAbsolutePath()
    };

    runCommandLine(args);

    final File extractedDir = createTempDir("extracted");
    IOUtils.extractTarGz(outputTarGz.toPath(), extractedDir.toPath());

    final File altHistogramFile = F1R2CountsCollector.getAltHistogramsFromExtractedTar(extractedDir).stream().findFirst().get();

    final MetricsFile<?, Integer> referenceSiteMetrics = new MetricsFile<>();
    final Reader in = IOUtil.openFileForBufferedReading(altHistogramFile);
    referenceSiteMetrics.read(in);
    CloserUtil.close(in);

    final MetricsFile<?, Integer> altSiteMetrics = new MetricsFile<>();
    final Reader altMetricsReader = IOUtil.openFileForBufferedReading(altHistogramFile);
    altSiteMetrics.read(altMetricsReader);
    CloserUtil.close(altMetricsReader);

    final List<Histogram<Integer>> histograms = altSiteMetrics.getAllHistograms();

    // TODO: should there be 64*3*2 = 384 histograms or just the non-zero ones?
    final String[] expectedTransitions = new String[]{"CAC_T_F2R1", "TAA_G_F2R1", "AGC_C_F2R1", "ACA_A_F2R1"};
    // Assert.assertEquals(histograms.size(), expectedTransitions.length, "alt histogram must contain the expected number of contexts");

    for (String transition : expectedTransitions ){
        Optional<Histogram<Integer>> histogram = histograms.stream()
                .filter(h -> h.getValueLabel().equals(transition))
                .findFirst();
        Assert.assertTrue(histogram.isPresent(), "histogram must exist");
        Assert.assertEquals((int) histogram.get().getSumOfValues(), 1, "histogram must only contain one read");
    }
}
 
Example 14
Source File: MatrixMarketReader.java    From Drop-seq with MIT License 2 votes vote down vote up
/**
 *
 * @param inputFile If has extension .gz, will be opened as a gzip file.
 * @param rowNamesLabel Header label for row names.  If null, "ROWS" is expected.
 * @param colNamesLabel Header label for column names.  If null, "COLS" is expected.
 */
public MatrixMarketReader(final File inputFile, final String rowNamesLabel, final String colNamesLabel) {
    this(IOUtil.openFileForBufferedReading(inputFile), inputFile.getAbsolutePath(), rowNamesLabel, colNamesLabel);
}