from openpyxl import load_workbook

filePathRunProperties = "${runInfo}"
filePathIn = ""
filePathOut = ""

# Read the runProperties.tsv file for the file paths of the data file we read from, and the one we will write to.
fileRunProperties = open(filePathRunProperties, "r")
for l in fileRunProperties:
    row = l.split()
    if row[0] == "runDataFile":
        filePathOut = row[3]
    if row[0] == "runDataUploadedFile":
        filePathIn = row[1]
fileRunProperties.close()

# Parse excel file using openpyxl
wb = load_workbook(filename = filePathIn)
sheet_obj = wb.active

# Parse each line, calculate the average, append it to the row, and save to fileOut.
fileOut = open(filePathOut, "a")
for row in list(sheet_obj.iter_rows())[0:]:
    row = [cell.value for cell in row]
    if row[0] == "ParticipantID":
        row = "\t".join(row) + "\t" + "averageResult" + "\n"
    else:
        avgResultVal = str((int(row[2]) + int(row[3]) + int(row[4])) / 3.0)
        row = "\t".join(map(str, row)) + "\t" + avgResultVal + "\n"
    fileOut.write(str(row))

fileOut.close()
