def _build_gene_tree(meth, msa, out_tree):
    """ Build phylogenetic tree for multiple alignmnet of protein sequences [28]

    :param msa: a Multiple sequence alignment [28.1]
    :type msa: kbtypes.KBaseTrees.MSA
    :ui_name msa: MSA ID
    :param out_tree: Output gene tree ID. If empty, an ID will be chosen randomly. [28.2]
    :type out_tree: kbtypes.KBaseTrees.Tree
    :ui_name out_tree: Output gene tree ID
    :return: Species Tree Result
    :rtype: kbtypes.Unicode
    :output_widget: kbaseTree
    """
    meth.stages = 1
    token, workspace = meth.token, meth.workspace_id
    if not out_tree:
        out_tree = "genetree_" + ''.join([chr(random.randrange(0, 26) + ord('A')) for _ in xrange(8)])
    treeClient = KBaseTrees(url = service.URLS.trees, token = token)
    construct_tree_for_alignment_params = {
        'msa_ref': workspace + '/' + msa, 
        'tree_method': 'FastTree', 
        'out_workspace': workspace, 
        'out_tree_id': out_tree
    }
    job_id = treeClient.construct_tree_for_alignment(construct_tree_for_alignment_params)
    return json.dumps({'treeID': out_tree, 'workspaceID': workspace, 'height':'500px', 'jobID': job_id})
def _insert_genome_into_species_tree(meth, genome, neighbor_count, out_tree):
    """ Insert a Genome into a global genome tree composed of 49 conserved COGs [20]

    :param genome: a Genome to insert into the tree [20.1]
    :type genome: kbtypes.KBaseGenomes.Genome
    :ui_name genome: Genome ID
    :param neighbor_count: number of closest public genomes the tree will contain. (optional, default value is 100) [20.2]
    :type neighbor_count: kbtypes.Unicode
    :ui_name neighbor_count: Neighbor public genome count
    :param out_tree: Output species tree ID. If empty, an ID will be chosen randomly. [20.3]
    :type out_tree: kbtypes.KBaseTrees.Tree
    :ui_name out_tree: Output species tree ID
    :return: Species Tree Result
    :rtype: kbtypes.Unicode
    :output_widget: kbaseTree
    """
    meth.stages = 2
    meth.advance("Instantiating tree construction job...")
    token, workspace = meth.token, meth.workspace_id
    if not out_tree:
        out_tree = "sptree_" + ''.join([chr(random.randrange(0, 26) + ord('A')) for _ in xrange(8)])
    treeClient = KBaseTrees(url = service.URLS.trees, token = token)
    construct_species_tree_params = {
        'new_genomes': [workspace + '/' + genome], 
        'use_ribosomal_s9_only': 0, 
        'out_workspace': workspace, 
        'out_tree_id': out_tree, 
    }
    if neighbor_count:
        construct_species_tree_params['nearest_genome_count'] = int(neighbor_count)
    job_id = treeClient.construct_species_tree(construct_species_tree_params)
    return json.dumps({'treeID': out_tree, 'workspaceID': workspace, 'height':'500px', 'jobID': job_id})
def _align_protein_sequences(meth, feature_set, alignment_method, out_msa):
    """Construct multiple sequence alignment object based on set of proteins. [27]

    :param feature_set: An object with protein features [27.1]
    :type feature_set: kbtypes.KBaseSearch.FeatureSet
    :ui_name feature_set: Feture Set Object
    :param alignment_method: name of alignment method (one of Muscle, Clustal, ProbCons, T-Coffee, Mafft), leave it blank for default Clustal method [27.2]
    :type alignment_method: kbtypes.Unicode
    :ui_name alignment_method: Multiple Alignment Method
    :param out_msa: Multiple sequence alignment object ID. If empty, an ID will be chosen randomly. [27.3]
    :type out_msa: kbtypes.KBaseTrees.MSA
    :ui_name out_msa: Output MSA ID
    :return: Preparation message
    :rtype: kbtypes.Unicode
    :output_widget: kbaseMSA
    """
    if not alignment_method:
        alignment_method = 'Clustal'
    if not out_msa:
        out_msa = "msa_" + ''.join([chr(random.randrange(0, 26) + ord('A')) for _ in xrange(8)])
    meth.stages = 1
    token, workspace = meth.token, meth.workspace_id
    ws = workspaceService(service.URLS.workspace, token=token)
    elements = ws.get_objects([{'ref': workspace+'/'+feature_set}])[0]['data']['elements']
    gene_sequences = {}
    for key in elements:
        elem = elements[key]['data']
        id = elem['id']
        if 'genome_ref' in elem:
            genome_obj_name = ws.get_object_info([{'ref' : elem['genome_ref']}],0)[0][1]
            id = genome_obj_name + '/' + id
        seq = elements[key]['data']['protein_translation']
        gene_sequences[id] = seq
    treeClient = KBaseTrees(url = service.URLS.trees, token = token)
    construct_multiple_alignment_params = {
        'gene_sequences': gene_sequences,                                  
        'alignment_method': alignment_method, 
        'out_workspace': workspace, 
        'out_msa_id': out_msa 
    }
    job_id = treeClient.construct_multiple_alignment(construct_multiple_alignment_params)
    return json.dumps({'workspaceID': workspace, 'msaID': out_msa, 'jobID' : job_id})
def _insert_genome_set_into_species_tree(meth, genome_set, neighbor_count, out_tree):
    """ Insert a Genome Set into a global genome tree composed of 50 conserved COGs [23]

    :param genome_set: a Genome Set to insert into the tree [23.1]
    :type genome_set: kbtypes.KBaseSearch.GenomeSet
    :ui_name genome_set: Genome Set ID
    :param neighbor_count: number of closest public genomes the tree will contain. (optional, default value is 100) [23.2]
    :type neighbor_count: kbtypes.Unicode
    :ui_name neighbor_count: Neighbor public genome count
    :param out_tree: Output species tree ID. If empty, an ID will be chosen randomly. [23.3]
    :type out_tree: kbtypes.KBaseTrees.Tree
    :ui_name out_tree: Output species tree ID
    :return: Species Tree Result
    :rtype: kbtypes.Unicode
    :output_widget: kbaseTree
    """
    meth.stages = 2
    meth.advance("Instantiating tree construction job...")
    token, workspace = meth.token, meth.workspace_id
    if not out_tree:
        out_tree = "sptree_" + ''.join([chr(random.randrange(0, 26) + ord('A')) for _ in xrange(8)])
    
    ws = workspaceService(service.URLS.workspace, token=token)
    data = ws.get_objects([{'ref': workspace+'/'+genome_set}])[0]
    genome_set_elements = data['data']['elements']
    genome_refs = []
    for key in genome_set_elements:
        genome_refs.append(genome_set_elements[key]['ref'])
    treeClient = KBaseTrees(url = service.URLS.trees, token = token)
    construct_species_tree_params = {
        'new_genomes': genome_refs, 
        'use_ribosomal_s9_only': 0, 
        'out_workspace': workspace, 
        'out_tree_id': out_tree, 
    }
    if neighbor_count:
        construct_species_tree_params['nearest_genome_count'] = int(neighbor_count)
    job_id = treeClient.construct_species_tree(construct_species_tree_params)
    return json.dumps({'treeID': out_tree, 'workspaceID': workspace, 'height':'500px', 'jobID': job_id})