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

The following examples show how to use htsjdk.samtools.util.IOUtil#openFileForReading() . 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: SparseDge.java    From Drop-seq with MIT License 6 votes vote down vote up
/**
  * Load a DGE into a sparse in-memory format.
  * @param input Either tabular DGE text, or Drop-seq Matrix Market sparse format.  May be gzipped.
  * @param geneEnumerator Genes are assigned indices by this.
  */
 public SparseDge(final File input, final GeneEnumerator geneEnumerator) {
     try {
         this.input = input;
         final BufferedInputStream inputStream = new BufferedInputStream(IOUtil.openFileForReading(input));
         final RawLoadedDge rawLoadedDge;
         if (MatrixMarketReader.isMatrixMarketInteger(input))
	rawLoadedDge = loadDropSeqSparseDge(inputStream, input, geneEnumerator);
else
	rawLoadedDge = loadTabularDge(inputStream, input, geneEnumerator);
         CloserUtil.close(inputStream);
         header = rawLoadedDge.header;
         triplets = rawLoadedDge.rawTriplets;
         sortAndFilterRawDge(rawLoadedDge);
     } catch (Exception e) {
         throw new RuntimeException("Problem reading " + input.getAbsolutePath(), e);
     }
 }
 
Example 2
Source File: FindMendelianViolationsTest.java    From picard with MIT License 6 votes vote down vote up
/** returns the number of lines in the file that contain a regular expression (decorated with "MV=" and
 * expected to be in an INFO field in a vcf)
 *
 * @param file File to examine
 * @param regex String containing a regular expression to look for in the file
 * @return the number of lines that contain regex
 */
private int grep(final File file, final String regex) {

    int results = 0;
    final Pattern pattern = Pattern.compile(".*"+regex+".*");
    try (final LineIteratorImpl li = new LineIteratorImpl(new AsciiLineReader(IOUtil.openFileForReading(file)))) {

        while (li.hasNext()) {
            final String line = li.next();
            if (pattern.matcher(line).matches()) {
                results++;
            }
        }
    } catch (final IOException e) {
        e.printStackTrace();
    }
    return results;
}
 
Example 3
Source File: SamBamUtils.java    From chipster with MIT License 6 votes vote down vote up
public static void sortSamBam(File samBamFile, File sortedBamFile) {
	
	SAMFileReader.setDefaultValidationStringency(ValidationStringency.SILENT);
	SAMFileReader reader = new SAMFileReader(IOUtil.openFileForReading(samBamFile));
	SAMFileWriter writer = null;
	try {
		
		reader.getFileHeader().setSortOrder(SAMFileHeader.SortOrder.coordinate);
		writer = new SAMFileWriterFactory().makeBAMWriter(reader.getFileHeader(), false, sortedBamFile);
		Iterator<SAMRecord> iterator = reader.iterator();
		while (iterator.hasNext()) {
			writer.addAlignment(iterator.next());
		}
		
	} finally {
		closeIfPossible(reader);
		closeIfPossible(writer);
	}
}
 
Example 4
Source File: BasicInputParser.java    From picard with MIT License 5 votes vote down vote up
private static InputStream[] filesToInputStreams(final File[] files) {
    final InputStream[] result = new InputStream[files.length];
    for (int i = 0; i < files.length; i++) {
        result[i] = IOUtil.openFileForReading(files[i]);
    }
    return result;
}
 
Example 5
Source File: UpdateVcfSequenceDictionaryTest.java    From picard with MIT License 5 votes vote down vote up
/**
 * Utility for unzipping a zipped file's contents into a human readable, unzipped file
 *
 * @param zippedFile    input zipped file
 * @param unzippedFile  unzipped file
 * @throws IOException
 */
private void unzipFile(final File zippedFile, final File unzippedFile) throws IOException {
    final InputStream gzInputStream = IOUtil.openFileForReading(zippedFile);
    final FileOutputStream fileOutputStream = new FileOutputStream(unzippedFile.getAbsolutePath());
    final byte[] buffer = new byte[1024];
    int len;
    while ((len = gzInputStream.read(buffer)) > 0) {
        fileOutputStream.write(buffer, 0, len);
    }
    gzInputStream.close();
    fileOutputStream.close();
}
 
Example 6
Source File: TextFileParsersTest.java    From picard with MIT License 5 votes vote down vote up
@DataProvider(name = "basicInputParserData")
private Object[][] getBasicInputParserData()
{
    return new Object[][] {
            {new File(testFile1)},
            {IOUtil.openFileForReading(new File(testFile1))}
    };
}
 
Example 7
Source File: TextFileParsersTest.java    From picard with MIT License 5 votes vote down vote up
@DataProvider(name = "multiFileParsingData")
private Object[][] getMultiFileParsingData()
{
    return new Object[][] {
            {new File(testFile1), new File(testFile1)},
            {IOUtil.openFileForReading(new File(testFile1)), IOUtil.openFileForReading(new File(testFile1))}
    };
}
 
Example 8
Source File: TextFileParsersTest.java    From picard with MIT License 5 votes vote down vote up
@DataProvider(name = "noGroupingData")
private Object[][] getNoGroupingData()
{
    return new Object[][] {
            {new File(testFile3)},
            {IOUtil.openFileForReading(new File(testFile3))}
    };
}
 
Example 9
Source File: TextFileParsersTest.java    From picard with MIT License 5 votes vote down vote up
@DataProvider(name = "leadingWhiteSpaceData")
private Object[][] getLeadingWhiteSpaceData()
{
    return new Object[][] {
            {new File(testFile2)},
            {IOUtil.openFileForReading(new File(testFile2))}
    };
}
 
Example 10
Source File: TextFileParsersTest.java    From picard with MIT License 5 votes vote down vote up
@DataProvider(name = "tooManyWordsData")
private Object[][] getTooManyWordsData()
{
    return new Object[][] {
            {new File(testFile1)},
            {IOUtil.openFileForReading(new File(testFile1))}
    };
}
 
Example 11
Source File: TextFileParsersTest.java    From picard with MIT License 5 votes vote down vote up
@DataProvider(name = "tabbedData")
private Object[][] getTabbedData()
{
    return new Object[][] {
            {new File(testFile4)},
            {IOUtil.openFileForReading(new File(testFile4))}
    };
}
 
Example 12
Source File: SamBamUtils.java    From chipster with MIT License 5 votes vote down vote up
public void normaliseBam(File bamFile, File normalisedBamFile) {

		// Read in a BAM file and its header
		SAMFileReader.setDefaultValidationStringency(ValidationStringency.SILENT);
		SAMFileReader reader = new SAMFileReader(IOUtil.openFileForReading(bamFile));
		SAMFileWriter writer = null;
		try {
			SAMFileHeader normalisedHeader = reader.getFileHeader();

			// Alter the chromosome names in header's SAMSequenceDictionary
			SAMSequenceDictionary normalisedDictionary = new SAMSequenceDictionary();
			for (SAMSequenceRecord sequenceRecord : normalisedHeader.getSequenceDictionary().getSequences()) {

				// Normalise chromosome
				String sequenceName = chromosomeNormaliser.normaliseChromosome(sequenceRecord.getSequenceName());
				normalisedDictionary.addSequence(new SAMSequenceRecord(sequenceName, sequenceRecord.getSequenceLength()));
			}
			normalisedHeader.setSequenceDictionary(normalisedDictionary);

			// Write new BAM file with normalised chromosome names
			writer = new SAMFileWriterFactory().makeBAMWriter(normalisedHeader, true, normalisedBamFile);
			for (final SAMRecord rec : reader) {
				rec.setHeader(normalisedHeader);
				writer.addAlignment(rec);
			}
			
		} finally {
			closeIfPossible(reader);
			closeIfPossible(writer);
		}
	}
 
Example 13
Source File: DGEMatrix.java    From Drop-seq with MIT License 4 votes vote down vote up
public static DGEMatrix parseDenseFile(final File input, final String cellBarcodePrefix, final boolean verbose) {
      // get lines in the file to determine the row dimensions.
      if (verbose) log.info("Getting lines in input file");
      int lines = DGEMatrix.countLines(input);

      // The reading of the header here is only to remove the header from the input stream.
      final BufferedInputStream inputStream = new BufferedInputStream(IOUtil.openFileForReading(input));
      new DgeHeaderCodec().decode(inputStream, input.getAbsolutePath());


      TabbedInputParser parser = new TabbedInputParser(false, inputStream);
      if (!parser.hasNext())  {
          parser.close();
          return new DGEMatrix(new ArrayList<String>(), new ArrayList<String>(), CRSMatrix.zero(0, 0));
      }

      String [] header = parser.next();
      int numCells= header.length-1;

      ArrayList<String> cellBarcodes = new ArrayList<> (numCells);
      for (int i=1; i<header.length; i++)
          if (cellBarcodePrefix!=null)
              cellBarcodes.add(cellBarcodePrefix + header[i]);
          else
              cellBarcodes.add(header[i]);
      if (verbose) log.info("Found [" + (lines-1) + "] genes and [" + cellBarcodes.size() +"] cells");

      // initialize the sparse matrix
      CRSMatrix m = CRSMatrix.zero(lines-1, cellBarcodes.size());

      List<String> geneNames = new ArrayList<>();

      int rowIdx=0;
      while(parser.hasNext()) {
          String [] line =parser.next();
          String gene = line[0];
          geneNames.add(gene);
          for (int columnIdx=1; columnIdx<line.length; columnIdx++) {
              double expression=Double.parseDouble(line[columnIdx]);
              if (expression!=0)
                  m.set(rowIdx, columnIdx-1, expression);
          }
          rowIdx++;
          if (rowIdx%1000==0)
          	if (verbose) log.info("Parsed [" + rowIdx +"] lines of DGE File [" + input.getName() +"]");
      }

      parser.close();
return (new DGEMatrix(cellBarcodes, geneNames, m));
  }
 
Example 14
Source File: DgeIterator.java    From Drop-seq with MIT License 4 votes vote down vote up
public DgeIterator (final File input) {
	this(new BufferedInputStream(IOUtil.openFileForReading(input)), input.getAbsolutePath());
}