Example #1
0
function main()
{
    global $matching_path;
    $have_tree = false;
    $have_taxa = false;
    $have_table = false;
    $newick = '';
    if (isset($_POST['tree'])) {
        $obj = parse_nexus(stripcslashes($_POST['tree']));
        $taxa = get_taxa_from_tree($obj);
        $newick = $obj->tree->newick;
        //echo $newick;
        //print_r($obj);
        $t = new Tree();
        $t->Parse($newick);
        $ni = new NodeIterator($t->getRoot());
        $q = $ni->Begin();
        while ($q != NULL) {
            if ($q->IsLeaf()) {
                if (isset($obj->translations->translate)) {
                    $q->SetLabel($obj->translations->translate[$q->GetLabel()]);
                }
            }
            $q = $ni->Next();
        }
        $newick = $t->WriteNewick();
        //echo $newick;
        $have_tree = true;
    }
    if (isset($_POST['taxa'])) {
        $have_taxa = true;
    }
    if (isset($_POST['table'])) {
        $table = $_POST['table'];
        $have_table = true;
    }
    if (isset($_POST['newick'])) {
        $newick = $_POST['newick'];
    }
    if ($have_tree || $have_taxa) {
        if ($have_table) {
            // get taxa
            $taxa = explode("\n", stripcslashes($_POST['taxa']));
            $n = count($taxa);
            for ($i = 0; $i < $n; $i++) {
                $taxa[$i] = trim($taxa[$i]);
            }
            // get table data
            $table = stripcslashes($_POST['table']);
            // Interpret table automatically...
            // assume column one contains OTUs, and some other column(s) have lat and long
            $data = extract_table($table);
            //print_r($data);
            if (count($taxa) != count($data)) {
                echo '<html>
	<head>
	<meta charset="utf-8" />
			<style type="text/css" title="text/css">
			body { font-family:sans-serif;padding:20px; }
			</style>
	<title>Create KML tree - Error</title>
	</head>
	<body>
	<a href=".">Back</a>
	<h1>Error</h1>
	<p>The number of taxa in the tree (' . count($taxa) . ') does not match the number in the table (' . count($data) . ')</p>
	</body>
	</html>';
                exit;
            }
            $data_lookup_by_label = array();
            foreach ($data as $d) {
                $data_lookup_by_label[$d->label] = $d;
            }
            // show matching...
            /*
            echo '			<form method="post" action=".">
            	<table border="1">';
            	
            	echo '<tr><th>Taxa in tree</th><th>Taxa in table</th><th>Latitude</th><th>Longitude</th></tr>';
            	
            	$nrows = count($data);
            	for ($i=0;$i < $nrows; $i++)
            	{
            		echo '<tr>';
            		
            		echo '<td>' . $taxa[$i] . '</td>';
            		echo '<td>' . $data[$i]->label . '</td>';
            		echo '<td>' . $data[$i]->latlong['latitude'] . '</td>';
            		echo '<td>' . $data[$i]->latlong['longitude'] . '</td>';
            		echo '</td></tr>';
            	}					
            
            					
            	
            echo '</table>
            
            	
            	<input type="submit" value="Go"></input>
            </form>';
            */
            // build graph for matching
            // Create GML file for graph linking taxon labels in tree and table
            // Taxon labels in table
            $b = array();
            foreach ($data as $row) {
                $b[] = $row->label;
            }
            $filename = 'tmp/' . uniqid() . '.gml';
            $gml = create_graph($taxa, $b);
            file_put_contents($filename, $gml);
            //echo $gml;
            // Compute maximum weight bipartite matching
            $command = $matching_path . 'matching ' . $filename;
            $output = array();
            exec($command, $output);
            //echo $command;
            $json = join("", $output);
            //echo $json;
            $match = json_decode($json);
            if (0) {
                echo '<pre>';
                $n = count($taxa);
                foreach ($match->matching as $pair) {
                    echo $taxa[$pair[0]] . "|\t" . $b[$pair[1] - $n] . "\n";
                }
                echo '</pre>';
            }
            // Mapping between tree labels and table labels
            $match_by_label = array();
            $n = count($taxa);
            foreach ($match->matching as $pair) {
                $match_by_label[$taxa[$pair[0]]] = $b[$pair[1] - $n];
            }
            // match and build KML file
            $t = new Tree();
            $t->Parse($newick);
            $t->BuildWeights($t->GetRoot());
            //echo $newick;
            $ni = new NodeIterator($t->getRoot());
            $q = $ni->Begin();
            while ($q != NULL) {
                if ($q->IsLeaf()) {
                    $data = $data_lookup_by_label[$match_by_label[$q->GetLabel()]];
                    $q->SetAttribute('lat', $data->latlong['latitude']);
                    $q->SetAttribute('long', $data->latlong['longitude']);
                }
                $q = $ni->Next();
            }
            // KML...
            //echo '<pre>';
            //$t->Dump();
            //echo '</pre>';
            $kml = tree2kml($t);
            echo '<html>
			<head>
			<meta charset="utf-8" />
			<style type="text/css" title="text/css">
			body { font-family:sans-serif;padding:20px; }
			</style>
			<title>Create KML tree - Step 3</title>
   <script type="text/javascript" src="https://www.google.com/jsapi"> </script>
   <script type="text/javascript">
      var ge;
      google.load("earth", "1");

      function init() {
         google.earth.createInstance(\'map3d\', initCB, failureCB);
      }

      function initCB(instance) {
         ge = instance;
         ge.getWindow().setVisibility(true);
         
         var treeDoc = ge.parseKml(';
            $kml_lines = explode("\n", $kml);
            $k = join(" ", $kml_lines);
            echo "'" . $k . "'";
            echo ');	
         ge.getFeatures().appendChild(treeDoc);
         
      }

      function failureCB(errorCode) {
      }

      google.setOnLoadCallback(init);
   </script>
			
			</head>
			<body>
			<a href=".">Home</a>
			<h1>Step 3: Match tree to table and create KML</h1>';
            // Display mapping
            echo '<h2>Tree and table matching</h2>';
            echo '<table border="1">';
            echo '<tr><th>Taxa in tree</th><th>Taxa in table</th><th>Latitude</th><th>Longitude</th></tr>';
            $n = count($taxa);
            foreach ($match->matching as $pair) {
                echo '<tr>';
                echo '<td>';
                echo $taxa[$pair[0]];
                echo '</td>';
                $data = $data_lookup_by_label[$match_by_label[$taxa[$pair[0]]]];
                echo '<td>' . $data->label . '</td>';
                echo '<td>' . $data->latlong['latitude'] . '</td>';
                echo '<td>' . $data->latlong['longitude'] . '</td>';
                echo '</tr>';
            }
            echo '</table>';
            echo '<h2>KML</h2>';
            echo '<textarea rows="30" cols="100">';
            echo $kml;
            echo '</textarea>';
            echo '<h2>Google Earth plugin</h2>';
            echo ' <div id="map3d" style="height: 400px; width: 600px;"></div>';
            echo '</body>
			</html>';
        } else {
            // We have the tree but no data yet
            echo '<html>
			<head>
			<meta charset="utf-8" />
			<style type="text/css" title="text/css">
			body { font-family:sans-serif;padding:20px; }
			</style>
			<title>Create KML tree - Step 2</title>
			</head>
			<body>
			<a href=".">Home</a>
			<h1>Step 2: Add table</h1>
			<form method="post" action=".">
			
				<input name="newick" type="hidden" value="' . $newick . '">
				
				<table>
				<tr><th>Taxa in tree</th><th>Paste in table with taxa (in first column), and latitude and longitude.<br/>The first row of the table must contain column headings.</th></tr>
				<tr>
				<td>
				<textarea id="taxa" name="taxa" rows="30" cols="60" readonly="readonly">' . join("\n", $taxa) . '</textarea>
				</td>
				<td>
				<textarea  id="table" name="table" rows="30" cols="60"></textarea>
				</td>
				</tr>
				</table>
				
				<input type="submit" value="Next step"></input>
			</form>
			</body>
			</html>';
        }
    } else {
        // Starting point, get tree
        echo '<html>
	<head>
	<meta charset="utf-8" />
			<style type="text/css" title="text/css">
			body { font-family:sans-serif;padding:20px; }
			</style>
	<title>Create KML tree - Step 1</title>
	</head>
	<body>
	<h1>Step 1: Paste in a tree in NEXUS format</h1>
	<form method="post" action=".">
		<textarea id="tree" name="tree" rows="30" cols="60"></textarea>
		<br />
		<input type="submit" value="Next step"></input>
	</form>
	</body>
	</html>';
    }
}
Example #2
0
<?php

require_once dirname(dirname(__FILE__)) . '/lib.php';
require_once dirname(__FILE__) . '/nexus.php';
require_once dirname(__FILE__) . '/tree2svg.php';
$have_tree = false;
if (isset($_POST['tree'])) {
    $tree = $_POST['tree'];
    $tree = stripcslashes($tree);
    $obj = parse_nexus($tree);
    $have_tree = true;
}
if (isset($_GET['url'])) {
    $nexus = get($_GET['url']);
    if ($nexus != '') {
        $obj = parse_nexus($nexus);
        $have_tree = true;
    }
}
if ($have_tree) {
    // Make SVG
    echo '<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8" />
	<title>Untitled</title>';
    echo '<script>
function node_info(otu)
    {
    	var info = document.getElementById("info");
    	info.innerHTML = otu;
Example #3
0
            $kml .= "<coordinates>\n";
            $kml .= $q->GetAttribute('long') . "," . $q->GetAttribute('lat') . "," . $q->GetAttribute('altitude') . "\n";
            $kml .= "</coordinates>\n";
            $kml .= "</Point>\n";
            $kml .= "</Placemark>\n";
        }
        $q = $ni->Next();
    }
    $kml .= "</Folder>\n";
    $kml .= "</Document>\n";
    $kml .= "</kml>\n";
    echo $kml;
}
$have_tree = false;
if (isset($_POST['tree'])) {
    $obj = parse_nexus($_POST['tree']);
    $have_tree = true;
}
if ($have_tree) {
    // Make KML
    header('Content-disposition: attachment; filename=tree.kml');
    header('Content-type: application/vnd.google-earth.kml+xml');
    $kml = tree2kml($obj, 'translate');
} else {
    echo '<html>
<head>
</head>
<body>
<p>Paste in a tree in NEXUS format.</p>
<form method="post" action="tkml.php">
	<textarea id="tree" name="tree" rows="30" cols="60"></textarea>
Example #4
0
6 \'AF261253 Mantidactylus horridus 16S ribosomal RNA gene, partial sequence; mitochondrial gene for mitochondrial product\',
7 \'GU975157 Gephyromantis ventrimaculatus voucher ZCMV 4591 16S ribosomal RNA gene, partial sequence; mitochondrial\',
8 \'FJ559200 Gephyromantis ventrimaculatus voucher ZCMV 3362 16S ribosomal RNA gene, partial sequence; mitochondrial\',
9 \'AY848373 Gephyromantis striatus voucher FA_m53 16S ribosomal RNA gene, partial sequence; mitochondrial\',
10 \'AJ314807 Mantidactylus malagasius mitochondrial partial 16S rRNA gene\',
11 \'HM364637 Gephyromantis aff. malagasius \'\'Betampona\'\' voucher MSNT<ITA-Torino\',
12 \'GU975158 Gephyromantis ventrimaculatus voucher ZCMV 4927 16S ribosomal RNA gene, partial sequence; mitochondrial\',
13 \'FJ559189 Gephyromantis cf. \'\'malagasius\'\' MV-2009 voucher FAZC-Andranobe 16S ribosomal RNA gene, partial sequence; mitochondrial\',
14 \'AJ314797 Mantidactylus malagasius mitochondrial partial 16S rRNA gene, variety coastal\',
15 \'AB325878 Gephyromantis klemmeri mitochondrial DNA, cytb gene to tRNA-Ala gene\',
16 \'FJ559237 Mantidactylus guttulatus voucher ZSM 644/2001 16S ribosomal RNA gene, partial sequence; mitochondrial\',
17 \'AY454390 Mantidactylus malagasius 16S ribosomal RNA gene, partial sequence; mitochondrial\',
18 \'AY848172 Mantidactylus guttulatus voucher FA_m23 16S ribosomal RNA gene, partial sequence; mitochondrial\',
19 \'AY848170 Mantidactylus grandidieri voucher FA_M17 16S ribosomal RNA gene, partial sequence; mitochondrial\',
20 \'AY848121 Mantidactylus grandidieri voucher RAN 42628 16S ribosomal RNA gene, partial sequence; mitochondrial\',
21 \'EF100468 Gephyromantis silvanus 16S ribosomal RNA gene, partial sequence; mitochondrial\',
22 \'AY848369 Gephyromantis silvanus voucher 2002A80 16S ribosomal RNA gene, partial sequence; mitochondrial\',
23 \'AY848370 Gephyromantis silvanus voucher 2002A81 16S ribosomal RNA gene, partial sequence; mitochondrial\',
24 \'AY848366 Gephyromantis silvanus voucher 2001_1379 16S ribosomal RNA gene, partial sequence; mitochondrial\',
25 \'AB325884 Mantidactylus grandidieri mitochondrial DNA, D-loop to tRNA-Ala gene\',
26 \'AF215314 Mantidactylus grandidieri 16S ribosomal RNA gene, partial sequence; mitochondrial\',
27 \'AY341712 Mantidactylus grandidieri 16S ribosomal RNA gene, partial sequence; mitochondrial\',
28 \'HM364634 Gephyromantis sp. 23 MV-2009 voucher MRSN:A6610 16S ribosomal RNA gene, partial sequence; mitochondrial\',
29 \'HM364633 Gephyromantis sp. 23 MV-2009 voucher MRSN:A6383 16S ribosomal RNA gene, partial sequence; mitochondrial\'
;
tree PAUP_1 = [&R] (((((1:0.021912,(2:0,3:0):0.028696):0.010761,(5:0,6:0):0.047194):0.011291,15:0.053600):0.003991,(((4:0.000173,17:0.002118):0.006109,9:0.009697):0.029928,(((7:0,8:0):-0.000237,12:0.000237):0.035129,((10:0.024184,(13:0,14:0):0.022665):0.002323,11:0.030023):0.010171):0.009920):0.002340):0,(((16:0.006860,18:0.011460):0.007728,(((19:0,20:0):0.003029,26:0.003106):0.011626,(25:0.000211,27:0.001817):0.013968):0.004472):0.039654,((((21:0,22:0):0,23:0):0,24:0):0.046011,(28:0,29:0):0.041474):0.013986):0.007324);
End;
';
    $obj = parse_nexus($str);
    print_r($obj);
}