Skip to content

Instantly share code, notes, and snippets.

@hiraksarkar
Created December 14, 2018 18:45
Show Gist options
  • Save hiraksarkar/da347e63a3d704478cacc982598b04c2 to your computer and use it in GitHub Desktop.
Save hiraksarkar/da347e63a3d704478cacc982598b04c2 to your computer and use it in GitHub Desktop.
Read Binary data file without boost support (using zstr header)
#include <iostream>
#include <fstream>
#include <sstream>
#include <functional>
#include <sys/stat.h>
#include <memory>
#include <vector>
#include <numeric>
#include "zstr.hpp"
int main(int argc, char* argv[]){
std::string input_file = argv[1] ;
size_t numCells{5343} ;
size_t numGenes{57875} ;
std::vector<std::vector<double> > data ;
data.resize(numCells, std::vector<double>(numGenes)) ;
for(auto& v : data)
std::memset(&v[0], 0, sizeof(v[0]) * v.size());
std::unique_ptr<std::istream> in =
std::unique_ptr<std::istream> (new zstr::ifstream(input_file, std::ios::in | std::ios::binary)) ;
size_t cellCount = 0 ;
size_t elSize = sizeof(typename std::vector<double>::value_type);
for (size_t i = 0 ; i < numCells; ++i){
std::vector<double> oneCell ;
oneCell.resize(numGenes) ;
cellCount += 1 ;
in->read(reinterpret_cast<char*>(oneCell.data()), elSize * numGenes) ;
double readCount = std::accumulate(oneCell.begin(), oneCell.end(), 0.0);
std::cout << "\nFor cell "<< cellCount << " readCount " << readCount << "\n" ;
}
return 0 ;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment