-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTreeGenerator.php
115 lines (105 loc) · 2.74 KB
/
TreeGenerator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
/**
* Created by PhpStorm.
* User: muntashir
* Date: 9/28/17
* Time: 10:39 PM
*/
namespace PHPPhylogeneticTrees;
/**
* Class TreeGenerator
*
* Generates UPGMA and/or NJ Tree based on the provided distance matrix
* and labels (species names).
*
* This function can be extended to provide a way to directly include
* distance matrix and labels (species names)
*
* @package PHPPhylogeneticTrees
*/
abstract class TreeGenerator{
const UPGMA = 'UPGMATree';
const NJ = 'NJTree';
/** @var double[][] Distance matrix */
protected $_matrix = [];
/** @var string[] Species names (they must follow the same sequence as the distance matrix) */
protected $_labels = [];
/** @var NJTree|UPGMATree */
protected $_tree;
/**
* Generate either UPGMA or NJ tree
*
* @param string $tree_type Tree::UPGMA or Tree::NJ
* @return $this
*/
public function generate_tree($tree_type){
$tree_class = "PHPPhylogeneticTrees\\$tree_type";
$this->_labels = $this->set_labels();
$this->_matrix = $this->set_matrix();
$this->_tree = new $tree_class($this->_matrix);
return $this;
}
/**
* Get raw a hierarchical structure containing only some numbers
*
* @return array|int
*/
public function getLabels(){
return $this->_tree->getLabels();
}
/**
* Get raw a hierarchical structure containing the provided labels
*
* @return mixed
*/
function getFormattedLabels(){
$labels = $this->_tree->getLabels();
$this->format_labels($labels);
return $labels;
}
/**
* This function is used to set distance matrix
*
* Use this function to set $this->_matrix
*
* Matrix format:
* <code>
* [
* [ ],
* [ 0.0427 ],
* [ 0.0858, 0.0441 ],
* [ 0.0702, 0.0358, 0.0282 ],
* [ 0.0088, 0.0476, 0.0910, 0.0751 ]
* ]
* </code>
*
* @return double[][]
*/
abstract protected function set_matrix();
/**
* This function is used to set labels or Species names
*
* The labels must be in the same order as the distance matrix
*
* @return mixed[]
*/
abstract protected function set_labels();
/**
* Replace the default numeric labels with the given labels
*
* NOTE: Overwrite this function to get
*
* @param array $labels
*/
protected function format_labels(&$labels){
if(is_array($labels)){
foreach ($labels as &$label){
if(is_array($label)){
$this->format_labels($label);
}else{
$label = $this->_labels[$label-1];
}
}
}
}
}