|
1 | 1 | from collections import Counter, defaultdict
|
| 2 | +import pandas as pd |
| 3 | +from luna.mol.entry import MolEntry |
2 | 4 |
|
3 | 5 |
|
4 | 6 | class InteractingResidues:
|
@@ -78,3 +80,73 @@ def calculate_residues_frequency(interaction_tuples):
|
78 | 80 | global_freq[k].add(entry, interacting_res_mapping[k])
|
79 | 81 |
|
80 | 82 | return global_freq
|
| 83 | + |
| 84 | + |
| 85 | +def generate_residue_matrix(interactions_mngrs, by_interaction=True): |
| 86 | + |
| 87 | + data_by_entry = defaultdict(lambda: defaultdict(int)) |
| 88 | + residues = set() |
| 89 | + |
| 90 | + for inter_mngr in interactions_mngrs: |
| 91 | + entry = inter_mngr.entry |
| 92 | + |
| 93 | + for inter in inter_mngr: |
| 94 | + # Continue if no target is in the interaction. |
| 95 | + if not inter.src_grp.has_target() and not inter.trgt_grp.has_target(): |
| 96 | + continue |
| 97 | + # Ignore interactions involving the same compounds. |
| 98 | + if inter.src_grp.compounds == inter.trgt_grp.compounds: |
| 99 | + continue |
| 100 | + |
| 101 | + if inter.src_grp.has_hetatm(): |
| 102 | + comp1 = sorted(inter.src_grp.compounds) |
| 103 | + comp2 = sorted(inter.trgt_grp.compounds) |
| 104 | + elif inter.trgt_grp.has_hetatm(): |
| 105 | + comp1 = sorted(inter.trgt_grp.compounds) |
| 106 | + comp2 = sorted(inter.src_grp.compounds) |
| 107 | + else: |
| 108 | + comp1 = sorted(inter.src_grp.compounds) |
| 109 | + comp2 = sorted(inter.trgt_grp.compounds) |
| 110 | + comp1, comp2 = sorted([comp1, comp2]) |
| 111 | + |
| 112 | + comp1 = ";".join(["%s/%s/%d%s" % (r.parent.id, r.resname, r.id[1], r.id[2].strip()) for r in comp1]) |
| 113 | + comp2 = ";".join(["%s/%s/%d%s" % (r.parent.id, r.resname, r.id[1], r.id[2].strip()) for r in comp2]) |
| 114 | + |
| 115 | + entry_id = entry.mol_id if isinstance(entry, MolEntry) else entry.to_string() |
| 116 | + if by_interaction: |
| 117 | + key = (entry_id, inter.type) |
| 118 | + else: |
| 119 | + key = entry_id |
| 120 | + |
| 121 | + data_by_entry[key][comp2] += 1 |
| 122 | + |
| 123 | + residues.add(comp2) |
| 124 | + |
| 125 | + heatmap_data = defaultdict(list) |
| 126 | + |
| 127 | + if by_interaction: |
| 128 | + entries = set([k[0] for k in data_by_entry.keys()]) |
| 129 | + interactions = set([k[1] for k in data_by_entry.keys()]) |
| 130 | + |
| 131 | + for e in entries: |
| 132 | + for i in interactions: |
| 133 | + for res in residues: |
| 134 | + heatmap_data["entry"].append(e) |
| 135 | + heatmap_data["interaction"].append(i) |
| 136 | + heatmap_data["residues"].append(res) |
| 137 | + heatmap_data["frequency"].append(data_by_entry[(e, i)][res]) |
| 138 | + |
| 139 | + else: |
| 140 | + for key in data_by_entry: |
| 141 | + for res in data_by_entry[key]: |
| 142 | + heatmap_data["entry"].append(key) |
| 143 | + |
| 144 | + heatmap_data["residues"].append(res) |
| 145 | + heatmap_data["frequency"].append(data_by_entry[key][res]) |
| 146 | + |
| 147 | + df = pd.DataFrame.from_dict(heatmap_data) |
| 148 | + |
| 149 | + if by_interaction: |
| 150 | + return pd.pivot_table(df, index=['entry', 'interaction'], columns='residues', values='frequency', fill_value=0) |
| 151 | + else: |
| 152 | + return pd.pivot_table(df, index='entry', columns='residues', values='frequency', fill_value=0) |
0 commit comments