Esempio n. 1
0
/**
 * @begin Draw tree and labels in SVG
 *
 * @param width Width (pixels) to draw tree + labels in
 * @param height
 * @param label_space Width (pixels) of space to draw leaf labels in
 * @param font_height Height of font to use to draw labels
 * @param default_labels Name of group of labels to show by default
 *
 */
function tree2svg($obj, $width = 400, $height = 400, $label_space = 150, $font_height = 10, $force_height = false, $default_labels = 'taxa')
{
    //----------------------------------------------------------------------------------------------
    $t = new Tree();
    $t->Parse($obj->tree->newick);
    $t->BuildWeights($t->GetRoot());
    $tree_width = $width - $label_space;
    if (!$force_height) {
        // adjust height to accomodate tree
        $height = $t->GetNumLeaves() * ($font_height + $font_height / 3);
        $inset = $font_height;
    } else {
        $inset = 0;
    }
    // Drawing properties
    $attr = array();
    $attr['inset'] = $inset;
    $attr['width'] = $tree_width;
    $attr['height'] = $height;
    $attr['font_height'] = $font_height;
    $attr['line_width'] = 1;
    // Don't draw labels (we do this afterwards)
    $attr['draw_leaf_labels'] = false;
    $attr['draw_internal_labels'] = false;
    $td = NULL;
    if ($t->HasBranchLengths()) {
        $td = new PhylogramTreeDrawer($t, $attr);
    } else {
        $td = new RectangleTreeDrawer($t, $attr);
    }
    $td->CalcCoordinates();
    if (!$force_height) {
        $port = new SVGPort('', $width, $td->max_height + $attr['font_height']);
    } else {
        $port = new SVGPort('', $width, $height + 2);
    }
    $port->StartGroup('tree');
    $td->Draw($port);
    $port->EndGroup();
    // labels
    if ($label_space > 0) {
        $ni = new NodeIterator($t->getRoot());
        // raw labels (OTUs)
        $port->StartGroup('otu', 'otu' == $default_labels || !isset($obj->translations));
        $q = $ni->Begin();
        while ($q != NULL) {
            if ($q->IsLeaf()) {
                $p0 = $q->GetAttribute('xy');
                $p0['x'] += $font_height / 3;
                $text = $q->Getlabel();
                $text = str_replace("_", " ", $text);
                $action = 'onclick="node_info(\'' . htmlentities($text) . '\');"';
                $port->DrawText($p0, $text, $action);
            }
            $q = $ni->Next();
        }
        $port->EndGroup();
        if ($obj->translations) {
            // Tree has a one or more translation tables
            foreach ($obj->translations as $k => $v) {
                // Draw labels as a separate group
                $port->StartGroup($k, $k == $default_labels ? true : false);
                $q = $ni->Begin();
                while ($q != NULL) {
                    if ($q->IsLeaf()) {
                        $p0 = $q->GetAttribute('xy');
                        $p0['x'] += $font_height / 3;
                        $label = $q->Getlabel();
                        if (is_array($v)) {
                            if (isset($v[$label])) {
                                $label = $v[$label];
                            } else {
                                // No translation for this OTU
                                $label = '[' . $label . ']';
                            }
                        } else {
                            if (isset($v->{$label})) {
                                $label = $v->{$label};
                            } else {
                                // No translation for this OTU
                                $label = '[' . $label . ']';
                            }
                        }
                        $action = 'onclick="node_info(\'' . $q->Getlabel() . '\');"';
                        $port->DrawText($p0, $label, $action);
                    }
                    $q = $ni->Next();
                }
                $port->EndGroup();
            }
        }
    }
    $svg = $port->GetOutput();
    return $svg;
}
Esempio n. 2
0
 function Draw($port)
 {
     parent::Draw($port);
     if ($this->draw_scale_bar) {
         $this->DrawScaleBar($port);
     }
 }