def export_patients(bot, patient_ids, study, owner, out_file, progress_callback):
    start_time = time.time()
    count = 0
    n_exported = 0

    prop_names = bot.list_patient_class_properties()

    writer = csv.writer(out_file)
    writer.writerow(prop_names)

    for patient_id in patient_ids:
        progress_callback(count)
        count += 1

        if study != None:
            patient_study = bot.get_study(patient_id)
            if patient_study != study and not (study == '' and patient_study == None):
                continue

        if owner:
            patient_owner = bot.get_owner(patient_id)
            if PhenoTipsBot.qualify(patient_owner) != PhenoTipsBot.qualify(owner):
                continue

        patient = bot.get(patient_id)
        patient['identifier'] = 'P' + patient['identifier'].zfill(7)
        row = []
        for prop_name in prop_names:
            row.append(patient[prop_name])
        writer.writerow(row)
        n_exported += 1

    return n_exported, timedelta(seconds=time.time() - start_time)
def get_clinvar_data(bot, patient_ids, study, owner, gene, progress_callback):
    start_time = time.time()
    count = 0

    clinvar_data = OrderedDict()

    for patient_id in patient_ids:
        count += 1
        progress_callback(count)

        if study != None:
            patient_study = bot.get_study(patient_id)
            if study != patient_study.lower() and not (study == '' and patient_study == None):
                continue

        if owner:
            patient_owner = bot.get_owner(patient_id)
            if PhenoTipsBot.qualify(patient_owner) != PhenoTipsBot.qualify(owner):
                continue

        clinvar_variant_nums = bot.list_objects(patient_id, 'Main.ClinVarVariant')
        if len(clinvar_variant_nums) == 0:
            continue

        patient_obj = bot.get(patient_id)

        for clinvar_variant_num in clinvar_variant_nums:
            clinvar_variant_obj = bot.get_object(patient_id, 'Main.ClinVarVariant', clinvar_variant_num)
            gene_symbol = clinvar_variant_obj.get('gene_symbol')

            if gene and (not gene_symbol or not gene in gene_symbol.upper().split(';')):
                continue

            #we aggregate all fields except for these
            clinvar_data_key = (
                clinvar_variant_obj['reference_sequence']    if 'reference_sequence'    in clinvar_variant_obj else None,
                clinvar_variant_obj['hgvs']                  if 'hgvs'                  in clinvar_variant_obj else None,
                clinvar_variant_obj['cis_or_trans']          if 'cis_or_trans'          in clinvar_variant_obj else None,
                clinvar_variant_obj['location']              if 'location'              in clinvar_variant_obj else None,
                patient_obj['omim_id']                       if 'omim_id'               in patient_obj         else None,
                clinvar_variant_obj['condition_category']    if 'condition_category'    in clinvar_variant_obj else None,
                clinvar_variant_obj['clinical_significance'] if 'clinical_significance' in clinvar_variant_obj else None,
                clinvar_variant_obj['collection_method']     if 'collection_method'     in clinvar_variant_obj else None,
                clinvar_variant_obj['allele_origin']         if 'allele_origin'         in clinvar_variant_obj else None,
                clinvar_variant_obj['tissue']                if 'tissue'                in clinvar_variant_obj else None,
                patient_obj        ['case_or_control']       if 'case_or_control'       in patient_obj         else None,
            )

            if not clinvar_data_key in clinvar_data:
                clinvar_data[clinvar_data_key] = []

            clinvar_data[clinvar_data_key].append((patient_obj, clinvar_variant_obj))

    return clinvar_data, timedelta(seconds=time.time() - start_time)
writer = csv.writer(sys.stdout, delimiter='\t')

fid = study if study else '0'

for patient_id in patient_ids:
    stderr.write(str(count) + '\r')
    count += 1

    if study != None:
        patient_study = bot.get_study(patient_id)
        if patient_study != study and not (study == '' and patient_study == None):
            continue

    if owner:
        patient_owner = bot.get_owner(patient_id)
        if PhenoTipsBot.qualify(patient_owner) != PhenoTipsBot.qualify(owner):
            continue

    patient = bot.get(patient_id)
    iid = patient['external_id']
    pat = '0'
    mat = '0'
    if patient.get('gender') == 'M':
        sex = 1
    elif patient.get('gender') == 'F':
        sex = 2
    else:
        sex = 0
    if patient.get('case_or_control') == 'case':
        phenotype = 2
    elif patient.get('case_or_control') == 'control':