<?php
/**
 * Created by PhpStorm.
 * User: muntashir
 * Date: 9/28/17
 * Time: 10:30 PM
 */

namespace PHPPhylogeneticTrees;

/**
 * Class UPGMACluster
 *
 * UPGMA clusters or trees, built by the UPGMA algorithm
 *
 * @package PHPPhylogeneticTrees
 */
class UPGMACluster extends Cluster{
    function __construct($lab, $d_mat, UPGMACluster $left = null, UPGMACluster $right = null) { // Leaves = single sequences // $dmat = []
        $this->lab    = is_array($lab) ? array_sum($lab) : $lab;
        $this->label  = $lab;
        $this->d_matrix = $d_mat;
        $this->left   = $left;
        $this->right  = $right;
        $this->card   = ($left === null) ? 1 : $left->card + $right->card;
    }
    public function getNewick($labels) {
	return "(". $this->scanForNewick($labels). ");";
    }
    public function scanForNewick($labels) {
        $left = "";
        if(is_array($this->left->label)) {
                $left .= "(". $this->left->scanForNewick($labels). ")";
        } else {
		$left .= $labels[$this->left->label - 1];
        }

        $rignt = "";
        if(is_array($this->right->label)) {
                $rignt .= "(". $this->right->scanForNewick($labels). ")";
        } else {
		$rignt .= $labels[$this->right->label - 1];
        }

        return $left. ",". $rignt;
//      return ($this->d_left < $this->d_right) ? $left. ",". $rignt : $rignt. ",". $left;
    }
}
