import csv

"""
Explanation: Solution of Singh et al. 2015
Compute the mean RecombinantFraction for each Line and InfectionStatus. Print the results like:
Line 45 Average Recombination Rate:
W :  0.187
I :  0.191
To read the data, we're going to use the csv library. First, we need to import it:
End of explanation
"""


my_data = {}

with open('../data/Singh2015_data.csv') as csvfile:
    reader = csv.DictReader(csvfile)
    for row in reader:
        my_line = row['Line']
        my_status = row['InfectionStatus']
        my_recomb = float(row['RecombinantFraction'])
        # Test by printing the values
        print(my_line, my_status, my_recomb)
        # just print the first row
        break

"""
Explanation: Then, we need to go through all the rows in the file, and for each add the RecombinantFraction to the right Line and InfectionStatus. To do so, we need to choose a data structure. Here we use a dictionary, where the keys are given by Line, and each value of the dictionary is another dictionary where the keys W and I index lists of RecombinantFraction.
End of explanation
"""


with open('../data/Singh2015_data.csv') as csvfile:
    reader = csv.DictReader(csvfile)
    for row in reader:
        my_line = row['Line']
        my_status = row['InfectionStatus']
        my_recomb = float(row['RecombinantFraction'])
        # if my_line is not present in the dictionary:
        if my_line not in my_data:
            # create and initialize with a dictionary containing
            # two empty lists
            my_data[my_line] = {'W': [], 'I':[]}
        # Now insert the value in the right list
        my_data[my_line][my_status].append(my_recomb)

"""
Explanation: Now we need to perform operations for each row. First, we're going to check whether my_data already contains the Line for that row. If not, we'll create the key-value in the dictionary. Then, we're going to add the value to the list.
End of explanation
"""


my_data

"""
Explanation: Now we should have the data organized in a nice structure:
End of explanation
"""


for line in my_data:
    print('Line', line, 'Average Recombination Rate:')
    # extract the relevant data
    my_subset = my_data[line]
    for status in ['W', 'I']:
        print(status, ':', end = '') # to prevent new line
        my_mean = sum(my_subset[status])
        my_num_elements = len(my_subset[status])
        my_mean = my_mean / my_num_elements
        print(' ', round(my_mean, 3))
    print('') # to separate the lines

"""
Explanation: Time to calculate the means and print the results:
End of explanation
"""


