Skip to content

Commit

Permalink
Work on fixing image and variant labelling. Image aspect ratio is now…
Browse files Browse the repository at this point in the history
… preserved. Snapshot works again. Can now take notes by typing messages into command box. Better printing of variant file info
  • Loading branch information
kcleal committed Dec 15, 2023
1 parent 633cdfa commit 85f59f7
Show file tree
Hide file tree
Showing 14 changed files with 492 additions and 319 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ prep:
endif


CXXFLAGS += -Wall -std=c++17 -fno-common -fwrapv -fno-omit-frame-pointer -O2 -DNDEBUG
CXXFLAGS += -Wall -std=c++17 -fno-common -fwrapv -fno-omit-frame-pointer -O3 -DNDEBUG

CPPFLAGS += -I./lib/libBigWig -I./include -I./src -I.

Expand Down
4 changes: 0 additions & 4 deletions src/drawing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1200,7 +1200,6 @@ namespace Drawing {
void
drawLabel(const Themes::IniOptions &opts, SkCanvas *canvas, SkRect &rect, Utils::Label &label, Themes::Fonts &fonts,
const ankerl::unordered_dense::set<std::string> &seenLabels, const std::vector<std::string> &srtLabels) {

float pad = 2;
std::string cur = label.current();
if (cur.empty()) {
Expand All @@ -1216,7 +1215,6 @@ namespace Drawing {
} else {
idx = label.i + srtLabels.size();
}

float step, start;
step = -1;
start = 1;
Expand All @@ -1227,10 +1225,8 @@ namespace Drawing {
step *= -1;
step = step * 0.5;
}

SkRect bg;
float x = rect.left() + pad;

SkPaint p;
int v;
if (opts.theme.name == "igv") {
Expand Down
36 changes: 29 additions & 7 deletions src/hts_funcs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ namespace HGW {
}
}
std::cerr << "Error: could not find suitable reference genome in .gw.ini. Try a local file?\n";
std::exit(-1);
}

void applyFilters(std::vector<Parse::Parser> &filters, std::vector<Segs::Align>& readQueue, const sam_hdr_t* hdr,
Expand Down Expand Up @@ -206,7 +205,7 @@ namespace HGW {
bool coverage, bool low_mem,
std::vector<Parse::Parser> &filters, Themes::IniOptions &opts, SkCanvas *canvas,
float trackY, float yScaling, Themes::Fonts &fonts, float refSpace, BS::thread_pool &pool) {
const int BATCH = 500;
const int BATCH = 1500;
bam1_t *src;
hts_itr_t *iter_q;
Segs::ReadCollection &col = cols[idx];
Expand Down Expand Up @@ -238,7 +237,6 @@ namespace HGW {
if (j < BATCH) {
continue;
}

Segs::init_parallel(readQueue, threads, pool);
if (coverage) {
int l_arr = (int)col.covArr.size() - 1;
Expand All @@ -257,8 +255,8 @@ namespace HGW {
j = 0;
}

if(j < BATCH){
readQueue.erase(readQueue.begin() + j);
if (j < BATCH) {
readQueue.erase(readQueue.begin() + j, readQueue.end());
if (!filters.empty()) {
applyFilters(filters, readQueue, hdr_ptr, col.bamIdx, col.regionIdx);
}
Expand Down Expand Up @@ -658,6 +656,7 @@ namespace HGW {
}

void VCFfile::next() {

int res = bcf_read(fp, hdr, v);
if (cacheStdin) {
lines.push_back(bcf_dup(v));
Expand Down Expand Up @@ -782,7 +781,7 @@ namespace HGW {
}
break;
}
if (!(*seenLabels).empty() && !seenLabels->contains(label)) {
if (seenLabels != nullptr && !(*seenLabels).empty() && !seenLabels->contains(label)) {
seenLabels->insert(label);
}
}
Expand Down Expand Up @@ -1775,6 +1774,29 @@ namespace HGW {
}
}

// gets called when new image tiles are loaded, labels are parsed from filenames if possible
// variant id is either recorded in the filename, or else is the whole filename
void GwVariantTrack::appendImageLabels(int startIdx, int number) {
// rid is the file name for an image
for (int i=startIdx; i < startIdx + number; ++i) {
if (i < multiLabels.size()) {
continue;
}
if (i >= image_glob.size()) {
break;
}
std::vector<Utils::Region> rt;
Utils::FileNameInfo info = Utils::parseFilenameInfo(image_glob[i]);
std::string key = (info.rid.empty()) ? info.fileName : info.rid;
std::string label;
if (inputLabels->contains(key)) {
multiLabels.push_back((*inputLabels)[key]);
} else {
multiLabels.push_back(Utils::makeLabel(info.chrom, info.pos, label, labelChoices, key, info.varType, "", false, false));
}
}
}

void GwVariantTrack::appendVariantSite(std::string &chrom, long start, std::string &chrom2, long stop, std::string &rid, std::string &label, std::string &vartype) {
long rlen = stop - start;
std::vector<Utils::Region> v;
Expand Down Expand Up @@ -1804,7 +1826,7 @@ namespace HGW {
if (inputLabels->contains(rid)) {
multiLabels.push_back((*inputLabels)[rid]);
} else {
multiLabels.push_back(Utils::makeLabel(chrom, start, label, labelChoices, rid, vartype, "", 0));
multiLabels.push_back(Utils::makeLabel(chrom, start, label, labelChoices, rid, vartype, "", false, false));
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/hts_funcs.h
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,9 @@ namespace HGW {
std::shared_ptr<ankerl::unordered_dense::set<std::string>> t_seenLabels);
~GwVariantTrack();
bool init;
// bool useVcf;
TrackType type;
bool *trackDone;
// bool image_name_valid; // the region and variant id can be parsed from filename
int mouseOverTileIndex;
int blockStart;
std::string path, fileName;
Expand All @@ -213,6 +213,7 @@ namespace HGW {

void nextN(int number);
void iterateToIndex(int index);
void appendImageLabels(int startIdx, int number); // adds labels for use with IMAGES only

private:
void appendVariantSite(std::string &chrom, long start, std::string &chrom2, long stop, std::string &rid, std::string &label, std::string &vartype);
Expand Down
103 changes: 55 additions & 48 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -505,19 +505,19 @@ int main(int argc, char *argv[]) {
}
} else if (program.is_used("--variants")) { // plot variants as tiled images

std::vector<std::string> labels = Utils::split(iopts.labels, ',');
std::vector<std::string> labels = Utils::split_keep_empty_str(iopts.labels, ',');
plotter.setLabelChoices(labels);

std::string img;
if (program.is_used("--in-labels")) {
Utils::openLabels(program.get<std::string>("--in-labels"), plotter.inputLabels, labels, plotter.seenLabels);
Utils::openLabels(program.get<std::string>("--in-labels"), img, plotter.inputLabels, labels, plotter.seenLabels);
}
if (program.is_used("--out-labels")) {
plotter.setOutLabelFile(program.get<std::string>("--out-labels"));
}
auto variant_paths = program.get<std::vector<std::string>>("--variants");
for (auto &v : variant_paths) {
bool cacheStdin = (v == "-" || program.is_used("--out-vcf")); // todo remove caching when --out-vcf is used
plotter.addVariantTrack(v, iopts.start_index, cacheStdin);
plotter.addVariantTrack(v, iopts.start_index, cacheStdin, false);
}
plotter.mode = Manager::Show::TILED;

Expand All @@ -536,47 +536,49 @@ int main(int argc, char *argv[]) {
if (img == ".") {
img += "/";
}

plotter.addVariantTrack(img, iopts.start_index, false);
if (plotter.variantTracks.back().image_glob.size() == 1) {
plotter.opts.number.x = 1; plotter.opts.number.y = 1;
}
std::vector<std::string> labels = Utils::split_keep_empty_str(iopts.labels, ',');
plotter.setLabelChoices(labels);


std::vector<std::string> labels = Utils::split(iopts.labels, ',');
plotter.setLabelChoices(labels);

if (program.is_used("--in-labels")) {
Utils::openLabels(program.get<std::string>("--in-labels"), plotter.inputLabels, labels, plotter.seenLabels);
std::string emptylabel;
int index = 0;
for (auto &item : plotter.variantTracks.back().image_glob) {
#if defined(_WIN32) || defined(_WIN64)
const wchar_t* pc = item.filename().c_str();
std::wstring ws(pc);
std::string p(ws.begin(), ws.end());
#else
std::string p = item.filename();
#endif
if (Utils::endsWith(p, ".png")) {
std::vector<std::string> m = Utils::split(p.erase(p.size() - 4), '~');
try {
// plotter.appendVariantSite(m[1], std::stoi(m[2]), m[3], std::stoi(m[4]), m[5], emptylabel, m[0]);
} catch (...) {
// append an empty variant, use the index at the id
std::string stri = std::to_string(index);
// plotter.appendVariantSite(emptylabel, 0, emptylabel, 0, stri, emptylabel, emptylabel);
}
index += 1;
}
}
// std::cerr << " yup\n";
Utils::openLabels(program.get<std::string>("--in-labels"), img, plotter.inputLabels, labels, plotter.seenLabels);
// std::string emptylabel;
// std::cerr << " " << plotter.inputLabels.size() << std::endl;
// int index = 0;
// for (auto &item : plotter.variantTracks.back().image_glob) {
//#if defined(_WIN32) || defined(_WIN64)
// const wchar_t* pc = item.filename().c_str();
// std::wstring ws(pc);
// std::string p(ws.begin(), ws.end());
//#else
// std::string p = item.filename();
//#endif
// if (Utils::endsWith(p, ".png")) {
// std::vector<std::string> m = Utils::split(p.erase(p.size() - 4), '~');
// try {
//// plotter.appendVariantSite(m[1], std::stoi(m[2]), m[3], std::stoi(m[4]), m[5], emptylabel, m[0]);
// } catch (...) {
// // append an empty variant, use the index at the id
// std::string stri = std::to_string(index);
//// plotter.appendVariantSite(emptylabel, 0, emptylabel, 0, stri, emptylabel, emptylabel);
// }
// index += 1;
// }
// }
}
if (program.is_used("--out-labels")) {
plotter.setOutLabelFile(program.get<std::string>("--out-labels"));
}

plotter.addVariantTrack(img, iopts.start_index, false, true);
if (plotter.variantTracks.back().image_glob.size() == 1) {
plotter.opts.number.x = 1; plotter.opts.number.y = 1;
}

plotter.mode = Manager::Show::TILED;
std::cerr << ": got here\n";

int res = plotter.startUI(sContext, sSurface, program.get<int>("--delay"));
if (res < 0) {
std::cerr << "ERROR: Plot to screen returned " << res << std::endl;
Expand Down Expand Up @@ -663,11 +665,7 @@ int main(int argc, char *argv[]) {
Manager::imagePngToStdOut(img);
}
} else {
fs::path fname = "GW~";
for (auto &rgn: regions) {
fname += rgn.chrom + "~" + std::to_string(rgn.start) + "~" + std::to_string(rgn.end) + "~";
}
fname += ".png";
fs::path fname = Utils::makeFilenameFromRegions(regions);
fs::path out_path = outdir / fname;
Manager::imageToPng(img, out_path);
}
Expand Down Expand Up @@ -770,7 +768,6 @@ int main(int argc, char *argv[]) {
return -1;
}
iopts.theme.setAlphas();

auto vcf = HGW::VCFfile();
vcf.cacheStdin = false;
vcf.label_to_parse = iopts.parse_label.c_str();
Expand All @@ -779,21 +776,20 @@ int main(int argc, char *argv[]) {

bool writeLabel;
std::ofstream fLabels;

if (!iopts.parse_label.empty()) {
writeLabel = true;
fs::path file ("gw.parsed_labels.tsv");
fs::path full_path = dir / file;
std::string outname = full_path.string();
fLabels.open(full_path);
fLabels << "#chrom\tpos\tvariant_ID\tlabel\tvar_type\tlabelled_date\n";
fLabels << "#chrom\tpos\tvariant_ID\tlabel\tvar_type\tlabelled_date\tvariant_filename\n";
} else {
writeLabel = false;
}

vcf.open(v);

std::vector<Manager::GwPlot *> managers;
managers.reserve(iopts.threads);
for (int i = 0; i < iopts.threads; ++i) {
auto *m = new Manager::GwPlot(genome, bam_paths, iopts, regions, tracks);
m->opts.theme.setAlphas();
Expand All @@ -805,6 +801,17 @@ int main(int argc, char *argv[]) {
std::vector<Manager::VariantJob> jobs;
std::vector<std::string> empty_labels;
std::string dateStr;

std::string fileName;
std::filesystem::path fsp(vcf.path);
#if defined(_WIN32) || defined(_WIN64)
const wchar_t* pc = fsp.filename().c_str();
std::wstring ws(pc);
std::string p(ws.begin(), ws.end());
fileName = p;
#else
fileName = fsp.filename();
#endif
while (true) {
vcf.next();
if (vcf.done) {
Expand All @@ -819,8 +826,8 @@ int main(int argc, char *argv[]) {
job.rid = vcf.rid;
jobs.push_back(job);
if (writeLabel) {
Utils::Label l = Utils::makeLabel(vcf.chrom, vcf.start, vcf.label, empty_labels, vcf.rid, vcf.vartype, "", 0);
Utils::labelToFile(fLabels, l, dateStr);
Utils::Label l = Utils::makeLabel(vcf.chrom, vcf.start, vcf.label, empty_labels, vcf.rid, vcf.vartype, "", false, false);
Utils::labelToFile(fLabels, l, dateStr, fileName);
}
}
// shuffling might help distribute high cov regions between jobs
Expand Down Expand Up @@ -863,7 +870,7 @@ int main(int argc, char *argv[]) {
} else if (program.is_used("--variants") && program.is_used("--out-vcf") && program.is_used("--in-labels")) {

// auto v = program.get<std::string>("--variants");
// std::vector<std::string> labels = Utils::split(iopts.labels, ',');
// std::vector<std::string> labels = Utils::split_keep_empty_str(iopts.labels, ',');
// if (program.is_used("--in-labels")) {
// Utils::openLabels(program.get<std::string>("--in-labels"), plotter.inputLabels, labels, plotter.seenLabels);
// }
Expand All @@ -879,7 +886,7 @@ int main(int argc, char *argv[]) {
// if (plotter.inputLabels.contains(vcf.rid)) {
// plotter.multiLabels.push_back(plotter.inputLabels[vcf.rid]);
// } else {
// plotter.multiLabels.push_back(Utils::makeLabel(vcf.chrom, vcf.start, vcf.label, empty_labels, vcf.rid, vcf.vartype, "", 0));
// plotter.multiLabels.push_back(Utils::makeLabel(vcf.chrom, vcf.start, vcf.label, empty_labels, vcf.rid, vcf.vartype, "", false, false));
// }
// }
// if (program.is_used("--out-vcf")) {
Expand Down
2 changes: 1 addition & 1 deletion src/menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ namespace Menu {
float control_box_h = 35;
float y = v_gap;
float x = v_gap;
float m_width = 26 * fonts.overlayWidth;
float m_width = 28 * fonts.overlayWidth;
auto m_height = (float)(pad * 1.5);
SkPaint bg;
SkPaint menuBg;
Expand Down
4 changes: 0 additions & 4 deletions src/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,7 @@ namespace Parse {
bool orBlock;
std::string filter_str;
ankerl::unordered_dense::map< std::string, Property> opMap;
// robin_hood::unordered_map< std::string, Property> opMap;
ankerl::unordered_dense::map< Property, std::string> permit;
// robin_hood::unordered_map< Property, std::string> permit;
std::vector<Eval> evaluations_block;
std::vector< std::vector<int> > targetIndexes;

Expand All @@ -93,8 +91,6 @@ namespace Parse {
private:
int prep_evaluations(std::vector<Eval> &results, std::vector<std::string> &tokens);
int split_into_or(std::string &f, std::vector<Eval> &results, int nBams, int nRegions);


};

void countExpression(std::vector<Segs::ReadCollection> &collections, std::string &str, std::vector<sam_hdr_t*> hdrs,
Expand Down
Loading

0 comments on commit 85f59f7

Please sign in to comment.