From f4b89f79de4c3588d8286f5cb347de26f972eef6 Mon Sep 17 00:00:00 2001 From: Phillip Seitzer Date: Wed, 1 Jun 2022 10:01:49 -0700 Subject: [PATCH] Issue 539: Start working on feature to organize peak groups by compound. --- src/maven/tabledockwidget.cpp | 89 ++++++++++++++++++++++++++++++++++- src/maven/tabledockwidget.h | 4 +- 2 files changed, 91 insertions(+), 2 deletions(-) diff --git a/src/maven/tabledockwidget.cpp b/src/maven/tabledockwidget.cpp index eb75df3d..a0015d10 100644 --- a/src/maven/tabledockwidget.cpp +++ b/src/maven/tabledockwidget.cpp @@ -1762,6 +1762,13 @@ void TableDockWidget::contextMenuEvent ( QContextMenuEvent * event ) QMenu analysis("Cluster Analysis"); QAction* zz0 = analysis.addAction("Cluster Groups by Retention Time"); connect(zz0, SIGNAL(triggered()), this ,SLOT(clusterGroups())); + +// //TODO: do not reenable until Issue #544 is complete +// QAction* zz3 = analysis.addAction("Set Child Groups by Compound"); +// connect(zz3, SIGNAL(triggered()), this, SLOT(clusterByCompounds())); +// QAction* zz4 = analysis.addAction("Promote all Child Groups"); +// connect(zz4, SIGNAL(triggered()), this, SLOT(unchildrenizeGroups())); + QAction* zz1 = analysis.addAction("Collapse All"); connect(zz1, SIGNAL(triggered()), treeWidget,SLOT(collapseAll())); QAction* zz2 = analysis.addAction("Expand All"); @@ -2144,7 +2151,87 @@ void TableDockWidget::clusterGroups() { //run clustering PeakGroup::clusterGroups(allgroups,samples,maxRtDiff,minSampleCorrelation,minRtCorrelation,ppm); - _mainwindow->setProgressBar("Clustering done!",allgroups.size(),allgroups.size()); + _mainwindow->setProgressBar("Clustering done!", allgroups.size(), allgroups.size()); + showAllGroups(); +} + +// Issue 539 / 544: Try clustering +//TODO: these methods are unreliable / will cause null access violations until #544 is completed +void TableDockWidget::clusterByCompounds() { + qDebug() << "TableDockWidget::clusterByCompounds()"; + + map> groupsByCompound{}; + + vector updated_all_groups{}; + + //organize peakgroups by common compound + for (auto pg : allgroups) { + pg->parent = nullptr; + if (pg->compound) { + Compound *compound = pg->compound; + vector compoundGroups{}; + if (groupsByCompound.find(compound) == groupsByCompound.end()) { + groupsByCompound.insert(make_pair(compound, compoundGroups)); + } + groupsByCompound.at(compound).push_back(pg); + for (auto pg_child : pg->children) { + //TODO: add children + } + } else { + updated_all_groups.push_back(pg); + } + } + + for (auto it = groupsByCompound.begin(); it != groupsByCompound.end(); ++it){ + vector groups = it->second; + + PeakGroup* pg = groups.at(0); + + if (groups.size() > 1) { + sort(groups.begin(), groups.end(), [](PeakGroup* lhs, PeakGroup* rhs){ + + if (abs(lhs->fragMatchScore.mergedScore-rhs->fragMatchScore.mergedScore) < 1e-6){ + return lhs->compound->name.compare(rhs->compound->name); + } + return lhs->fragMatchScore.mergedScore < rhs->fragMatchScore.mergedScore ? -1 : 1; + + }); + + //add children + for (unsigned int i = 1; i < groups.size(); i++) { + PeakGroup *pg_child = groups.at(i); + pg->children.push_back(*pg_child); + } + } + + updated_all_groups.push_back(pg); + + } + + allgroups = updated_all_groups; + + showAllGroups(); + +} + +void TableDockWidget::unchildrenizeGroups() { + qDebug() << "TableDockWidget::unchildrenizeGroups()"; + + vector updated_all_groups{}; + + for (auto pg : allgroups) { + updated_all_groups.push_back(pg); + + for (auto child : pg->children) { + updated_all_groups.push_back(&child); + } + + pg->children = {}; + pg->setParent(nullptr); + } + + allgroups = updated_all_groups; + showAllGroups(); } diff --git a/src/maven/tabledockwidget.h b/src/maven/tabledockwidget.h index d3b3dacb..10f43a08 100644 --- a/src/maven/tabledockwidget.h +++ b/src/maven/tabledockwidget.h @@ -100,7 +100,9 @@ public slots: void deleteSelected(); void align(); void deleteAll(); - void clusterGroups(); + void clusterGroups(); + void clusterByCompounds(); + void unchildrenizeGroups(); void findMatchingCompounds(); void filterPeakTable(); void loadSpreadsheet(QString fileName);