Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

straw to matrix #108

Merged
merged 3 commits into from
Mar 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion C++/straw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1248,7 +1248,6 @@ class HiCFile {
}

vector<chromosome> getChromosomes() {

chromosome chromosomes[chromosomeMap.size()];
auto iter = chromosomeMap.begin();
while (iter != chromosomeMap.end()) {
Expand Down Expand Up @@ -1319,6 +1318,31 @@ vector<contactRecord> straw(const string &matrixType, const string &norm, const
}
}

auto strawAsMatrix(const string &matrixType, const string &norm, const string &fileName, const string &chr1loc,
const string &chr2loc, const string &unit, int32_t binsize) {
if (!(unit == "BP" || unit == "FRAG")) {
cerr << "Norm specified incorrectly, must be one of <BP/FRAG>" << endl;
cerr << "Usage: straw [observed/oe/expected] <NONE/VC/VC_SQRT/KR> <hicFile(s)> <chr1>[:x1:x2] <chr2>[:y1:y2] <BP/FRAG> <binsize>"
<< endl;
auto res = vector<vector<float>>(1, vector<float>(1, 0));
return res;
}

HiCFile *hiCFile = new HiCFile(fileName);
string chr1, chr2;
int64_t origRegionIndices[4] = {-100LL, -100LL, -100LL, -100LL};
parsePositions((chr1loc), chr1, origRegionIndices[0], origRegionIndices[1], hiCFile->chromosomeMap);
parsePositions((chr2loc), chr2, origRegionIndices[2], origRegionIndices[3], hiCFile->chromosomeMap);

if (hiCFile->chromosomeMap[chr1].index > hiCFile->chromosomeMap[chr2].index) {
MatrixZoomData *mzd = hiCFile->getMatrixZoomData(chr2, chr1, matrixType, norm, unit, binsize);
return mzd->getRecordsAsMatrix(origRegionIndices[2], origRegionIndices[3], origRegionIndices[0], origRegionIndices[1]);
} else {
MatrixZoomData *mzd = hiCFile->getMatrixZoomData(chr1, chr2, matrixType, norm, unit, binsize);
return mzd->getRecordsAsMatrix(origRegionIndices[0], origRegionIndices[1], origRegionIndices[2], origRegionIndices[3]);
}
}

int64_t getNumRecordsForFile(const string &fileName, int32_t binsize, bool interOnly) {
HiCFile *hiCFile = new HiCFile(fileName);
int64_t totalNumRecords = 0;
Expand Down
2 changes: 1 addition & 1 deletion pybind11_python/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import setuptools
import os

__version__ = '1.2.1'
__version__ = '1.2.2'


def read(fname):
Expand Down
28 changes: 27 additions & 1 deletion pybind11_python/src/straw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1156,7 +1156,7 @@ class MatrixZoomData {
}
finalMatrix.push_back(row);
}
return py::array(py::cast(finalMatrix));;
return py::array(py::cast(finalMatrix));
}

int64_t getNumberOfTotalRecords() {
Expand Down Expand Up @@ -1322,6 +1322,31 @@ vector<contactRecord> straw(const string &matrixType, const string &norm, const
}
}

auto strawAsMatrix(const string &matrixType, const string &norm, const string &fileName, const string &chr1loc,
const string &chr2loc, const string &unit, int32_t binsize) {
if (!(unit == "BP" || unit == "FRAG")) {
cerr << "Norm specified incorrectly, must be one of <BP/FRAG>" << endl;
cerr << "Usage: straw [observed/oe/expected] <NONE/VC/VC_SQRT/KR> <hicFile(s)> <chr1>[:x1:x2] <chr2>[:y1:y2] <BP/FRAG> <binsize>"
<< endl;
auto res = vector<vector<float>>(1, vector<float>(1, 0));
return py::array(py::cast(res));
}

HiCFile *hiCFile = new HiCFile(fileName);
string chr1, chr2;
int64_t origRegionIndices[4] = {-100LL, -100LL, -100LL, -100LL};
parsePositions((chr1loc), chr1, origRegionIndices[0], origRegionIndices[1], hiCFile->chromosomeMap);
parsePositions((chr2loc), chr2, origRegionIndices[2], origRegionIndices[3], hiCFile->chromosomeMap);

if (hiCFile->chromosomeMap[chr1].index > hiCFile->chromosomeMap[chr2].index) {
MatrixZoomData *mzd = hiCFile->getMatrixZoomData(chr2, chr1, matrixType, norm, unit, binsize);
return mzd->getRecordsAsMatrix(origRegionIndices[2], origRegionIndices[3], origRegionIndices[0], origRegionIndices[1]);
} else {
MatrixZoomData *mzd = hiCFile->getMatrixZoomData(chr1, chr2, matrixType, norm, unit, binsize);
return mzd->getRecordsAsMatrix(origRegionIndices[0], origRegionIndices[1], origRegionIndices[2], origRegionIndices[3]);
}
}

int64_t getNumRecordsForFile(const string &fileName, int32_t binsize, bool interOnly) {
HiCFile *hiCFile = new HiCFile(fileName);
int64_t totalNumRecords = 0;
Expand Down Expand Up @@ -1354,6 +1379,7 @@ m.doc() = "Fast tool for reading .hic files; see https://github.com/aidenlab/str

m.def("strawC", &straw, "get contact records");
m.def("straw", &straw, "get contact records");
m.def("strawAsMatrix", &strawAsMatrix, "get contact records in numpy matrix");

py::class_<contactRecord>(m, "contactRecord")
.def(py::init<>())
Expand Down