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

Refactor node coloring #59

Merged
merged 6 commits into from
Jun 12, 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
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ set(LIB_SOURCES
ui/tablewidgetitemname.cpp
ui/tablewidgetitemshown.cpp
ui/verticallabel.cpp
ui/verticalscrollarea.cpp)
ui/verticalscrollarea.cpp graph/nodecolorer.cpp graph/nodecolorer.h graph/nodecolorers.h)

set(FORMS
ui/aboutdialog.ui
Expand Down
35 changes: 25 additions & 10 deletions command_line/commoncommandlinefunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,8 @@ void getSettingsUsage(QStringList * text)
*text << "";
*text << "Depth colour scheme";
*text << dashes;
*text << "These settings only apply when the depth colour scheme is used.";
*text << "--depcollow <col> Colour for nodes with depth below the low depth value " + getDefaultColour(g_settings->lowDepthColour);
*text << "--depcolhi <col> Colour for nodes with depth above the high depth value " + getDefaultColour(g_settings->highDepthColour);
*text << "These settings only apply when the depth / GC colour scheme is used.";
*text << "--colormap <mapname> Color map to use " + getDefaultColorMap(g_settings->colorMap);
*text << "--depvallow <float> Low depth value " + getRangeAndDefault(g_settings->lowDepthValue, "auto");
*text << "--depvalhi <float> High depth value " + getRangeAndDefault(g_settings->highDepthValue, "auto");
*text << "";
Expand Down Expand Up @@ -501,7 +500,7 @@ void parseSettings(QStringList arguments)
}
}

g_settings->nodeColourScheme = getColourSchemeOption("--colour", &arguments);
g_settings->initializeColorer(getColourSchemeOption("--colour", &arguments));

// For backward compatibility we allow setting blast annotation view to show from "--colour" option.
// To be removed when we can set up annotations from CLI.
Expand All @@ -527,10 +526,8 @@ void parseSettings(QStringList arguments)
if (isOptionPresent("--unicolspe", &arguments))
g_settings->uniformNodeSpecialColour = getColourOption("--unicolspe", &arguments);

if (isOptionPresent("--depcollow", &arguments))
g_settings->lowDepthColour = getColourOption("--depcollow", &arguments);
if (isOptionPresent("--depcolhi", &arguments))
g_settings->highDepthColour = getColourOption("--depcolhi", &arguments);
if (isOptionPresent("--colormap", &arguments))
g_settings->colorMap = getColorMapOption("--colormap", &arguments);
if (isOptionPresent("--depvallow", &arguments))
{
g_settings->lowDepthValue = getFloatOption("--depvallow", &arguments);
Expand Down Expand Up @@ -1038,9 +1035,9 @@ SciNot getSciNotOption(const QString& option, QStringList * arguments)
return {arguments->at(sciNotIndex)};
}

NodeColourScheme getColourSchemeOption(const QString& option, QStringList * arguments)
NodeColorScheme getColourSchemeOption(const QString& option, QStringList * arguments)
{
NodeColourScheme defaultScheme = RANDOM_COLOURS;
NodeColorScheme defaultScheme = RANDOM_COLOURS;

int optionIndex = arguments->indexOf(option);
if (optionIndex == -1)
Expand Down Expand Up @@ -1122,6 +1119,19 @@ QColor getColourOption(const QString& option, QStringList * arguments)
return {arguments->at(colIndex)};
}

ColorMap getColorMapOption(const QString& option, QStringList * arguments)
{
int optionIndex = arguments->indexOf(option);
if (optionIndex == -1)
return {};

int colIndex = optionIndex + 1;
if (colIndex >= arguments->size())
return {};

return colorMapFromName(arguments->at(colIndex));
}


QString getStringOption(const QString& option, QStringList * arguments)
{
Expand Down Expand Up @@ -1283,6 +1293,11 @@ QString getDefaultColour(QColor colour)
return "(default: " + getColourName(colour.name()) + ")";
}

QString getDefaultColorMap(ColorMap colorMap)
{
return "(default: " + getColorMapName(colorMap) + ")";
}

QString getBandageTitleAsciiArt()
{
return " ____ _ \n | _ \\ | | \n | |_) | __ _ _ __ __| | __ _ __ _ ___ \n | _ < / _` | '_ \\ / _` |/ _` |/ _` |/ _ \\\n | |_) | (_| | | | | (_| | (_| | (_| | __/\n |____/ \\__,_|_| |_|\\__,_|\\__,_|\\__, |\\___|\n __/ | \n |___/ ";
Expand Down
4 changes: 3 additions & 1 deletion command_line/commoncommandlinefunctions.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ int getIntOption(const QString& option, QStringList * arguments);
double getFloatOption(const QString& option, QStringList * arguments);
SciNot getSciNotOption(const QString& option, QStringList * arguments);
QColor getColourOption(const QString& option, QStringList * arguments);
NodeColourScheme getColourSchemeOption(const QString& option, QStringList * arguments);
ColorMap getColorMapOption(const QString& option, QStringList * arguments);
NodeColorScheme getColourSchemeOption(const QString& option, QStringList * arguments);
std::set<ViewId> getBlastAnnotationViews(const QString& option, QStringList * arguments);
GraphScope getGraphScopeOption(const QString& option, QStringList * arguments);
QString getStringOption(const QString& option, QStringList * arguments);
Expand Down Expand Up @@ -95,6 +96,7 @@ QString getRangeAndDefault(double min, double max, double defaultVal);
QString getRangeAndDefault(double min, double max, QString defaultVal);
QString getRangeAndDefault(const QString& min, QString max, QString defaultVal);
QString getDefaultColour(QColor colour);
QString getDefaultColorMap(ColorMap colorMap);

QString getBandageTitleAsciiArt();
bool isOption(QString text);
Expand Down
2 changes: 1 addition & 1 deletion command_line/image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ int bandageImage(QStringList arguments)
err << csvPath << " didn't contain color" << Qt::endl;
return 1;
}
g_settings->nodeColourScheme = CUSTOM_COLOURS;
g_settings->initializeColorer(CUSTOM_COLOURS);
}

if (errorMessage != "")
Expand Down
80 changes: 38 additions & 42 deletions graph/assemblygraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ double AssemblyGraph::getMeanDepth(bool drawnNodesOnly)
}


double AssemblyGraph::getMeanDepth(std::vector<DeBruijnNode *> nodes)
double AssemblyGraph::getMeanDepth(const std::vector<DeBruijnNode *> &nodes)
{
if (nodes.empty())
return 0.0;
Expand Down Expand Up @@ -314,22 +314,11 @@ double AssemblyGraph::getMeanDepth(const QList<DeBruijnNode *>& nodes)
}


void AssemblyGraph::resetNodeContiguityStatus()
{
void AssemblyGraph::resetNodeContiguityStatus() {
for (auto &entry : m_deBruijnGraphNodes) {
entry->resetContiguityStatus();
}
m_contiguitySearchDone = false;

resetAllNodeColours();
}

void AssemblyGraph::resetAllNodeColours()
{
for (auto &entry : m_deBruijnGraphNodes) {
if (entry->getGraphicsItemNode() != nullptr)
entry->getGraphicsItemNode()->setNodeColour();
}
}

void AssemblyGraph::determineGraphInfo()
Expand Down Expand Up @@ -659,7 +648,11 @@ bool AssemblyGraph::loadGraphFromFile(const QString& filename) {
}

determineGraphInfo();

// FIXME: get rid of this!
g_memory->clearGraphSpecificMemory();
g_settings->nodeColorer->reset();

return true;
}

Expand Down Expand Up @@ -790,45 +783,50 @@ void AssemblyGraph::addGraphicsItemsToScene(MyGraphicsScene * scene)

double meanDrawnDepth = getMeanDepth(true);

//First make the GraphicsItemNode objects
for (auto &entry : m_deBruijnGraphNodes) {
DeBruijnNode * node = entry;
// First make the GraphicsItemNode objects
for (auto *node : m_deBruijnGraphNodes) {
if (!node->isDrawn())
continue;

if (node->isDrawn())
{
if (meanDrawnDepth == 0)
node->setDepthRelativeToMeanDrawnDepth(1.0);
else
node->setDepthRelativeToMeanDrawnDepth(node->getDepth() / meanDrawnDepth);
auto * graphicsItemNode = new GraphicsItemNode(node, m_graphAttributes);
node->setGraphicsItemNode(graphicsItemNode);
graphicsItemNode->setFlag(QGraphicsItem::ItemIsSelectable);
graphicsItemNode->setFlag(QGraphicsItem::ItemIsMovable);
node->setDepthRelativeToMeanDrawnDepth(meanDrawnDepth== 0 ?
1.0 : node->getDepth() / meanDrawnDepth);
auto *graphicsItemNode = new GraphicsItemNode(node, m_graphAttributes);
node->setGraphicsItemNode(graphicsItemNode);
graphicsItemNode->setFlag(QGraphicsItem::ItemIsSelectable);
graphicsItemNode->setFlag(QGraphicsItem::ItemIsMovable);

bool colSet = false;
if (auto *rcNode = node->getReverseComplement()) {
if (auto *revCompGraphNode = rcNode->getGraphicsItemNode()) {
auto colPair = g_settings->nodeColorer->get(graphicsItemNode, revCompGraphNode);
graphicsItemNode->setNodeColour(colPair.first);
revCompGraphNode->setNodeColour(colPair.second);
colSet = true;
}
}
if (!colSet)
graphicsItemNode->setNodeColour(g_settings->nodeColorer->get(graphicsItemNode));
}

resetAllNodeColours();

//Then make the GraphicsItemEdge objects and add them to the scene first
//so they are drawn underneath
// Then make the GraphicsItemEdge objects and add them to the scene first,
// so they are drawn underneath
for (auto &entry : m_deBruijnGraphEdges) {
DeBruijnEdge * edge = entry.second;

if (edge->isDrawn())
{
if (edge->isDrawn()) {
auto * graphicsItemEdge = new GraphicsItemEdge(edge);
edge->setGraphicsItemEdge(graphicsItemEdge);
graphicsItemEdge->setFlag(QGraphicsItem::ItemIsSelectable);
scene->addItem(graphicsItemEdge);
}
}

//Now add the GraphicsItemNode objects to the scene so they are drawn
//on top
for (auto &entry : m_deBruijnGraphNodes) {
DeBruijnNode * node = entry;
if (node->hasGraphicsItem())
scene->addItem(node->getGraphicsItemNode());
// Now add the GraphicsItemNode objects to the scene, so they are drawn
// on top
for (auto *node : m_deBruijnGraphNodes) {
if (!node->hasGraphicsItem())
continue;
scene->addItem(node->getGraphicsItemNode());
}
}

Expand Down Expand Up @@ -1568,8 +1566,7 @@ void AssemblyGraph::duplicateGraphicsNode(DeBruijnNode * originalNode, DeBruijnN
newGraphicsItemNode->shiftPointsRight();
originalGraphicsItemNode->fixEdgePaths();

originalGraphicsItemNode->setNodeColour();
newGraphicsItemNode->setNodeColour();
newGraphicsItemNode->setNodeColour(originalGraphicsItemNode->m_colour);

originalGraphicsItemNode->setWidth();

Expand Down Expand Up @@ -1825,8 +1822,7 @@ bool AssemblyGraph::mergeGraphicsNodes2(QList<DeBruijnNode *> * originalNodes,
newNode->setGraphicsItemNode(newGraphicsItemNode);
newGraphicsItemNode->setFlag(QGraphicsItem::ItemIsSelectable);
newGraphicsItemNode->setFlag(QGraphicsItem::ItemIsMovable);

newGraphicsItemNode->setNodeColour();
newGraphicsItemNode->setNodeColour(g_settings->nodeColorer->get(newGraphicsItemNode));

scene->addItem(newGraphicsItemNode);

Expand Down
4 changes: 1 addition & 3 deletions graph/assemblygraph.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,9 @@ class AssemblyGraph : public QObject
static QByteArray getReverseComplement(const QByteArray& forwardSequence);
void resetEdges();
double getMeanDepth(bool drawnNodesOnly = false);
static double getMeanDepth(std::vector<DeBruijnNode *> nodes);
static double getMeanDepth(const std::vector<DeBruijnNode *> &nodes);
static double getMeanDepth(const QList<DeBruijnNode *>& nodes);
void resetNodeContiguityStatus();
void resetAllNodeColours();
void clearAllBlastHitPointers();
void determineGraphInfo();
void clearGraphInfo();
void recalculateAllDepthsRelativeToDrawnMean();
Expand Down
10 changes: 10 additions & 0 deletions graph/debruijnnode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -709,3 +709,13 @@ std::vector<DeBruijnNode *> DeBruijnNode::getAllConnectedPositiveNodes() const

return connectedPositiveNodesVector;
}

float DeBruijnNode::getGC() const {
size_t gc = 0;
for (size_t i = 0; i < m_sequence.size(); ++i) {
char c = m_sequence[i];
gc += (c == 'G' || c == 'C');
}

return float(gc) / float(m_sequence.size());
}
3 changes: 2 additions & 1 deletion graph/debruijnnode.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ class DeBruijnNode
double getDepth() const {return m_depth;}
double getDepthRelativeToMeanDrawnDepth() const {return m_depthRelativeToMeanDrawnDepth;}

float getGC() const;

const Sequence &getSequence() const;
Sequence &getSequence();

Expand All @@ -67,7 +69,6 @@ class DeBruijnNode
DeBruijnNode * getReverseComplement() const {return m_reverseComplement;}
OgdfNode * getOgdfNode() const {return m_ogdfNode;}
GraphicsItemNode * getGraphicsItemNode() const {return m_graphicsItemNode;}
bool thisOrReverseComplementHasGraphicsItemNode() const {return (m_graphicsItemNode != nullptr || getReverseComplement()->m_graphicsItemNode != nullptr);}
bool hasGraphicsItem() const {return m_graphicsItemNode != nullptr;}

auto edgeBegin() { return m_edges.begin(); }
Expand Down
Loading