###############################################################################
#                                                                             #
#    This program is free software: you can redistribute it and/or modify     #
#    it under the terms of the GNU General Public License as published by     #
#    the Free Software Foundation, either version 3 of the License, or        #
#    (at your option) any later version.                                      #
#                                                                             #
#    This program is distributed in the hope that it will be useful,          #
#    but WITHOUT ANY WARRANTY; without even the implied warranty of           #
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            #
#    GNU General Public License for more details.                             #
#                                                                             #
#    You should have received a copy of the GNU General Public License        #
#    along with this program. If not, see <http://www.gnu.org/licenses/>.     #
#                                                                             #
###############################################################################


from biolib.common import is_float


'''Helper functions for parsing Newick information.'''


def parse_label(label):
    """Parse a Newick label which may contain a support value, taxon, and/or auxiliary information.

    Parameters
    ----------
    label : str
        Internal label in a Newick tree.

    Returns
    -------
    float
        Support value specified by label, or None
    str
        Taxon specified by label, or None
    str
        Auxiliary information, on None
    """

    support = None
    taxon = None
    auxiliary_info = None
    
    if label:
        label = label.strip()
        if '|' in label:
            label, auxiliary_info = label.split('|')

        if ':' in label:
            support, taxon = label.split(':')
            support = float(support)
        else:
            if is_float(label):
                support = float(label)
            elif label != '':
                taxon = label

    return support, taxon, auxiliary_info
    
    
def create_label(support, taxon, auxiliary_info):
    """Create label for Newick tree.
    
    Parameters
    ----------
    float
      Support value specified by label, or None
    str
      Taxon specified by label, or None
    str
      Auxiliary information, on None
        
    Returns
    -------
    str
      Valid newick label.
    """
    
    label = ''
    if support is not None and taxon:
        label = str(support) + ':' + taxon
    elif support is not None:
        label = str(support)
    elif taxon:
        label = taxon
        
    if auxiliary_info:
        label += '|' + auxiliary_info

    return label
