ORDER BY B.person_id ASC ) B ON I.id = B.person_id ORDER BY I.id ASC ; """.format(utils.SUBARRAY_SEPARATOR)) print "Writing " + str(cursor.rowcount) + " files\n" # 8 is where the batches subarray is located subarrayPos = 8 nbBatches = utils.size_of_subarray(cursor, subarrayPos) batchPlaceholders = utils.make_placeholders(subarrayPos, nbBatches) headerPlaceholders = utils.make_headers("batch", 0, nbBatches) f = cursor.fetchone() while f: with open(output_dir + "Individuals_" + str(f[0]) + ".tsv", "w") as file: file.write("individualId,familyId,paternalId,maternalId,dateOfBirth,gender,ethnicCode,centreName,region,country,notes,{0}\n".format(headerPlaceholders)) li = utils.to_prepared_list(f) li = utils.break_subarray(li, subarrayPos, nbBatches) file.write(("""{0},,,,{1},{2},{3},{4},{5},{6},{7},"""+batchPlaceholders+"""\n""").format(*li)) f = cursor.fetchone() ################################ ### Individuals_#####_Visits ### ################################ print "Exporting Individuals Visits" cursor.execute(""" SELECT V.person_id, V.id, V.visit_date, V.form_id, V.fasting, V.descr FROM "public"."visit" AS V ORDER BY V.person_id ASC, V.id ASC ; """)
print "Writing " + str(cursor.rowcount) + " files\n" # This section will need to create the headers and the rows dynamically because # the number of columns is not known ahead of time! See utils.py for more info. # 8 is where the batches subarray is located subarrayPos = 8 nbBatches = utils.size_of_subarray(cursor, subarrayPos) batchPlaceholders = utils.make_placeholders(subarrayPos, nbBatches) headerPlaceholders = utils.make_headers("batch", 0, nbBatches) f = cursor.fetchone() current_id = -1 while f: current_id = f[0] os.makedirs(output_dir + str(current_id)) with open("{0}{1}/Individuals_{1}.tsv".format(output_dir, current_id), "w") as file: file.write("individualId,familyId,paternalId,maternalId,dateOfBirth,gender,ethnicCode,centreName,region,country,notes,{0}\n".format(headerPlaceholders)) li = utils.to_prepared_list(f) li = utils.break_subarray(li, subarrayPos, nbBatches) file.write(("""{0},,,,{1},{2},{3},{4},{5},{6},{7},"""+batchPlaceholders+"""\n""").format(*li)) f = cursor.fetchone() ################################ ### Individuals_#####_Visits ### ################################ print "Exporting Individuals Visits" cursor.execute(""" SELECT V.person_id, V.id, V.visit_date, V.form_id, V.fasting, V.descr FROM "public"."visit" AS V ORDER BY V.person_id ASC, V.id ASC """) print "Writing " + str(cursor.rowcount) + " files\n" f = cursor.fetchone()
with open("{0}{1}/Individuals_{1}.tsv".format(output_dir, current_id), "w") as file: file.write("individualId,familyId,paternalId,maternalId,dateOfBirth,gender,ethnicCode,centreName,region,country,notes,missingCallFreq,{0}\n".format(headerPlaceholders)) familyID=utils.get_individual_info(sourcesDirectory+filename, "ID_2") fatherID=utils.get_individual_info(sourcesDirectory+filename, "father") motherID=utils.get_individual_info(sourcesDirectory+filename, "mother") sex=utils.get_individual_info(sourcesDirectory+filename, "sex") missingCallFreq=utils.get_individual_info(sourcesDirectory+filename, "missing") migratedindividuals = migratedindividuals + 1 #According to Impute2 if sex=='1': sex="m" elif sex=='2': sex="f" li = utils.to_prepared_list(f + (familyID,fatherID,motherID,missingCallFreq)) li = utils.break_subarray(li, subarrayPos, nbBatches) if sex != li[2]: utils.log(logger, "Difference - Sample SEX: "+sex+ " vs. Advance SEX: "+li[2]) file.write(("""{0},{11},{12},{13},{1},{2},{3},{4},{5},{6},{7},{14},"""+batchPlaceholders+"""\n""").format(*li)) else: os.makedirs(output_dir + str(current_id)) with open("{0}{1}/Individuals_{1}.tsv".format(output_dir, current_id), "w") as file: file.write("individualId,familyId,paternalId,maternalId,dateOfBirth,gender,ethnicCode,centreName,region,country,notes,missingCallFreq,{0}\n".format(headerPlaceholders)) migratedindividuals = migratedindividuals + 1
with open("{0}{1}/Individuals_{1}.tsv".format(output_dir, current_id), "w") as file: file.write("individualId,familyId,paternalId,maternalId,dateOfBirth,gender,ethnicCode,centreName,region,country,notes,missingCallFreq,{0}\n".format(headerPlaceholders)) familyID=utils.get_individual_info(sourcesDirectory+filename, "ID_2") fatherID=utils.get_individual_info(sourcesDirectory+filename, "father") motherID=utils.get_individual_info(sourcesDirectory+filename, "mother") sex=utils.get_individual_info(sourcesDirectory+filename, "sex") missingCallFreq=utils.get_individual_info(sourcesDirectory+filename, "missing") migratedindividuals = migratedindividuals + 1 #According to Impute2 if sex=='1': sex="m" elif sex=='2': sex="f" li = utils.to_prepared_list(f + (familyID,fatherID,motherID,missingCallFreq)) li = utils.break_subarray(li, subarrayPos, nbBatches) if sex != li[2]: utils.log(logger, "Difference - Sample SEX: "+sex+ " vs. Advance SEX: "+li[2]) file.write(("""{0},{11},{12},{13},{1},{2},{3},{4},{5},{6},{7},{14},"""+batchPlaceholders+"""\n""").format(*li)) else: os.makedirs(output_dir + str(current_id)) with open("{0}{1}/Individuals_{1}.tsv".format(output_dir, current_id), "w") as file: file.write("individualId,familyId,paternalId,maternalId,dateOfBirth,gender,ethnicCode,centreName,region,country,notes,missingCallFreq,{0}\n".format(headerPlaceholders)) migratedindividuals = migratedindividuals + 1 li = utils.to_prepared_list(f)