22#define MASHPL_CUH
33
44#include < stdint.h>
5+ #include < cmath>
6+ #include < functional>
57#include < iostream>
68#include < vector>
79#include < cstdio>
1315
1416namespace MashPlacement
1517{
18+ inline bool g_printBinaryNewick = false ;
19+
1620 struct Param
1721 {
1822 uint64_t kmerSize;
@@ -288,6 +292,63 @@ namespace MashPlacement
288292 };
289293 static KPlacementDeviceArraysDC kplacementDeviceArraysDC;
290294
295+ inline void printNewickFromHostAdjacency (std::ostream& out, const std::vector<std::string>& name, int * h_head,
296+ int * h_e, int * h_nxt, double * h_len, int rootNode, bool binaryNewick) {
297+ const double kLenEps = 1e-12 ;
298+ auto hasKids = [h_head, h_e, h_nxt](int node, int from) -> bool {
299+ for (int i = h_head[node]; i != -1 ; i = h_nxt[i]) {
300+ if (h_e[i] != from) {
301+ return true ;
302+ }
303+ }
304+ return false ;
305+ };
306+ std::function<void (int , int )> dfs;
307+ dfs = [&](int node, int from) {
308+ if (!hasKids (node, from)) {
309+ out << name[node];
310+ return ;
311+ }
312+ if (binaryNewick) {
313+ out << " (" ;
314+ std::vector<int > pos;
315+ for (int i = h_head[node]; i != -1 ; i = h_nxt[i]) {
316+ if (h_e[i] != from) {
317+ pos.push_back (i);
318+ }
319+ }
320+ for (size_t i = 0 ; i < pos.size (); ++i) {
321+ dfs (h_e[pos[i]], node);
322+ out << " :" << h_len[pos[i]] << (i + 1 == pos.size () ? " )" : " ," );
323+ }
324+ return ;
325+ }
326+ std::vector<std::pair<int , double >> kids;
327+ std::function<void (int , int )> addCollapsed;
328+ addCollapsed = [&](int n, int fr) {
329+ for (int i = h_head[n]; i != -1 ; i = h_nxt[i]) {
330+ int ch = h_e[i];
331+ if (ch == fr) {
332+ continue ;
333+ }
334+ double L = h_len[i];
335+ if (std::fabs (L) <= kLenEps && hasKids (ch, n)) {
336+ addCollapsed (ch, n);
337+ } else {
338+ kids.push_back ({ch, L});
339+ }
340+ }
341+ };
342+ addCollapsed (node, from);
343+ out << " (" ;
344+ for (size_t i = 0 ; i < kids.size (); ++i) {
345+ dfs (kids[i].first , node);
346+ out << " :" << kids[i].second << (i + 1 == kids.size () ? " )" : " ," );
347+ }
348+ };
349+ dfs (rootNode, -1 );
350+ }
351+
291352};
292353
293354#endif
0 commit comments