mutations = ['A222V', 'D614G', 'E484Q', 'E780Q', 'G476S', 'L18F', 'N439K',
'S477', 'S477N', 'T478I', 'V483A']
combinations = []
for M in range(1, len(mutations)+1):
for subset in itertools.combinations(mutations, M):
combinations.append(subset)
combinations = ['_'.join(sorted(x)) for x in combinations]
combinations = [x.split('_') for x in list(set(combinations))]
root = "C:"
os.chdir(root)
lineages = os.listdir('Results')
combination_labels = []
combination_counts = []
for lineage in lineages:
df = pd.read_csv('Results/' + lineage).dropna()
for combination in combinations:
combination_df = df[list(df)]
for mutation in combination:
combination_df = combination_df[combination_df[mutation] == 1]
#print(combination_df.shape[0])
combination_labels.append('_'.join(combination))
combination_counts.append(combination_df.shape[0])
out_df = pd.DataFrame({'combination':combination_labels,
'count':combination_counts})
out_df['percentage'] = (out_df['count'] / df.shape[0]) * 100
out_df = out_df.sort_values('percentage', ascending = False)
out_df.to_csv('Results_2/' + lineage.replace(".csv", "") + '_3.csv',
header = True,
index = False)
The input CSV
lineage,A222V,D614G,E484Q,E780Q,G476S,L18F,N439K,S477,S477N,T478I,V483A
417941,0,1,0,0,0,0,0,0,0,0,0
Output CSV
combination,count,percentage
D614G,87355,90.7084929856806
My above code is used to count all occurences of combinations of spike protein mutations
- to determine the genotypes. However, looking at the values processed into the .csv file, i can see ' D614G' is being counted even when it is in combination with other combinations.
My question is how do i ensure once something has been counted (a row in the imported .csv file) it is not counted for a second time?
OR possibly how can I edit this code to prevent counting of singular mutations even when presented within a combination?
Thanks in advance