Esempio n. 1
0
            if ($parts[$i] != "") {
                $data[$headings[$i]][$parts[0]] = $parts[$i];
            }
        }
    }
    $count++;
}
//print_r($data);
//print_r($headings);
//print_r($x_axis);
//exit();
$height = 100;
$width = 500;
$gap = $width / (count($x_axis) - 1);
$x_offset = 100;
$port = new SVGPort("g", 1000, 1000);
$port->StartPicture();
for ($j = 1; $j < count($headings); $j++) {
    $origin_y = $height * ($j - 1);
    $port->DrawRect(array('x' => 0, 'y' => $origin_y), array('x' => $width, 'y' => $origin_y + $height), array(255, 255, 255));
    // maximum value
    $max = 0;
    foreach ($data[$headings[$j]] as $k => $v) {
        $max = max($max, $v);
    }
    $max_x = $x_axis[count($x_axis) - 1];
    $min_x = $x_axis[0];
    $pts = array();
    $pts[] = array('x' => 0, 'y' => $origin_y);
    /*
    	for ($i = 0; $i < count($data[$j]); $i++)
Esempio n. 2
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;
}