def get_extended_public_program_name_set_for_user_extended_projects(
        user_extended_projects):
    """
    When working with user data where the user program is an extension of a public project,
    we need to follow the extension chain back up to the public project. This allows us to plot
    data contained in the public project against user data. Since public projects are identified
    by unique *names*, we resolve to the *names* of these projects

    Returns:
        Set of program names in lower case.
    """
    program_set = set()
    projects = Project.objects.filter(id__in=user_extended_projects)

    root_projects = []
    tcga_projects = fetch_isbcgc_project_set()

    for project in projects:
        rad = project.get_my_root_and_depth()['root']
        if rad in tcga_projects:
            root_projects.append(rad)

    extended_projects = Project.objects.filter(id__in=root_projects)

    for ext_proj in extended_projects:
        pub_program = ext_proj.program
        p = Program.objects.get(id=pub_program.id)
        program_set.update([p.name.lower()])

    return program_set
def get_confirmed_project_ids_for_cohorts(cohort_id_array):
    """
    Returns the project ID numbers that are referred to by the samples
    in a list of cohorts.
    
    Returns:
        List of project ID numbers.
    """
    cohort_vals = ()
    cohort_params = ""

    for cohort in cohort_id_array:
        cohort_params += "%s,"
        cohort_vals += (cohort, )

    cohort_params = cohort_params[:-1]
    db = get_sql_connection()
    cursor = db.cursor()

    tcga_studies = fetch_isbcgc_project_set()

    query_str = "SELECT DISTINCT project_id FROM cohorts_samples WHERE cohort_id IN (" + cohort_params + ");"
    cursor.execute(query_str, cohort_vals)

    # Only samples whose source studies are TCGA studies, or extended from them, should be used
    confirmed_study_ids = []
    unconfirmed_study_ids = []
    user_only_study_ids = []

    for row in cursor.fetchall():
        if row[0] in tcga_studies:
            if row[0] not in confirmed_study_ids:
                confirmed_study_ids.append(row[0])
        elif row[0] not in unconfirmed_study_ids:
            unconfirmed_study_ids.append(row[0])

    if len(unconfirmed_study_ids) > 0:
        projects = Project.objects.filter(id__in=unconfirmed_study_ids)

        for project in projects:
            if project.get_my_root_and_depth()['root'] in tcga_studies:
                confirmed_study_ids.append(project.id)
            else:
                user_only_study_ids.append(project.id)

    logger.info(
        "In get_confirmed_project_ids_for_cohorts, confirmed study IDs: {}".
        format(str(confirmed_study_ids)))

    return confirmed_study_ids, user_only_study_ids