comparing mcsa and og products
This commit is contained in:
+177
-10
@@ -1,5 +1,6 @@
|
||||
import requests, sys, json
|
||||
from prettify import warn, green, red, blue
|
||||
from grantham_matrix import min_distance_LP
|
||||
|
||||
from argparse import ArgumentParser
|
||||
|
||||
@@ -24,17 +25,23 @@ AA_SINGLE = {
|
||||
"W": ['TRYPTOPHAN', 'Trp'],
|
||||
"Y": ['TYROSINE', 'Tys'],
|
||||
"V": ['VALINE', 'Val'],
|
||||
"U": ['SELENOCYSTEINE', 'Sec']
|
||||
}
|
||||
|
||||
AA_TRIPLET = {t : s for (s, [_,t]) in list(AA_SINGLE.items())}
|
||||
AA_TRIPLET['Tyr'] = "Y"
|
||||
AA_TRIPLET['Sec'] = "U"
|
||||
|
||||
def parse_args():
|
||||
parser = ArgumentParser(
|
||||
description='Compare AAs in overlay graphs with UniProt')
|
||||
parser.add_argument('-p', '--num_uniprot_entries', help="number of uniprot entries per rhea entry", type=int, default=1)
|
||||
parser.add_argument('-t', '--num_rhea_candidates', help="number of candidate rhea entries", type=int, default=1)
|
||||
parser.add_argument('-t', '--num_candidates', help="number of candidate rhea entries", type=int, default=5)
|
||||
parser.add_argument('-md', '--max_distance', type=int, default=500)
|
||||
parser.add_argument('-v', '--verbose', action="store_true", default=False)
|
||||
return parser.parse_args(sys.argv[1:])
|
||||
|
||||
def read_tsv(filepath, top=50):
|
||||
def read_tsv_rhea(filepath, top=50):
|
||||
with open(filepath) as f:
|
||||
glob = f.read()
|
||||
|
||||
@@ -59,6 +66,52 @@ def read_tsv(filepath, top=50):
|
||||
}
|
||||
return rhea_dict
|
||||
|
||||
def read_tsv_mcsa(filepath, top=50):
|
||||
with open(filepath) as f:
|
||||
glob = f.read()
|
||||
|
||||
# mcsa dict :: msca id -> attrs
|
||||
mcsa_dict = {}
|
||||
# skip header line
|
||||
for line in glob.split("\n")[1:top]:
|
||||
values = line.split("\t")
|
||||
rhea_master_id = int(values[1])
|
||||
rhea_sub_ids = values[2].split("; ")
|
||||
predicted_ec4s = values[5]
|
||||
top_mcsa_ids = values[14].split("; ")
|
||||
expected_amino_acids_overlay = values[19].split("; ")
|
||||
expected_amino_acids_all_mcsa_entry = values[20].split("; ")
|
||||
|
||||
for mcsa_id in top_mcsa_ids:
|
||||
mcsa_dict[mcsa_id] = {
|
||||
'rhea_master_id' : rhea_master_id,
|
||||
'rhea_sub_ids' : rhea_sub_ids,
|
||||
'predicted_ec4s' : predicted_ec4s,
|
||||
'expected_amino_acids_overlay' : expected_amino_acids_overlay,
|
||||
'expected_amino_acids_all_mcsa_entry' : expected_amino_acids_all_mcsa_entry
|
||||
}
|
||||
|
||||
return mcsa_dict
|
||||
|
||||
def get_req_mcsa(mcsa_dict):
|
||||
if not mcsa_dict:
|
||||
print(red("No MCSA IDs to look up. (mcsa_dict is empty)"))
|
||||
return mcsa_dict
|
||||
url=f"https://www.ebi.ac.uk/thornton-srv/m-csa/api/entries/?format=json"
|
||||
|
||||
mcsa_ids = ",".join(sorted([x for x in list(mcsa_dict) if x]))
|
||||
url_ids = url+f"&entries.mcsa_ids={mcsa_ids}"
|
||||
print(url_ids)
|
||||
|
||||
response = requests.get(url_ids)
|
||||
if not response.ok:
|
||||
print(red(f"\tResponse not ok for MCSA."))
|
||||
return mcsa_dict
|
||||
|
||||
data = json.loads(response.text)
|
||||
print("number of results for get req mcsa:", len(data['results']))
|
||||
return data['results']
|
||||
|
||||
def get_req_rhea(rheaID, rhea_dict):
|
||||
url= f"https://www.rhea-db.org/rhea/?"
|
||||
parameter = {
|
||||
@@ -111,6 +164,29 @@ def get_req_uniprot(rheaID):
|
||||
#print(data)
|
||||
return data
|
||||
|
||||
def get_req_uniprotid(uniprotID):
|
||||
print(f"Sending GET request to UniProt for UniProt ID {blue(uniprotID)}...")
|
||||
params = {
|
||||
"fields": [
|
||||
"ft_binding",
|
||||
"ft_act_site",
|
||||
"ec",
|
||||
"sequence"
|
||||
]
|
||||
}
|
||||
headers = {
|
||||
"accept": "application/json"
|
||||
}
|
||||
base_url = f"https://rest.uniprot.org/uniprotkb/{uniprotID}"
|
||||
|
||||
response = requests.get(base_url, headers=headers, params=params)
|
||||
if not response.ok:
|
||||
response.raise_for_status()
|
||||
sys.exit()
|
||||
|
||||
data = response.json()
|
||||
return data
|
||||
|
||||
def identify_aas_in_actsite(active_sites, sequence):
|
||||
aas = []
|
||||
for site in active_sites:
|
||||
@@ -128,18 +204,17 @@ def identify_aas_in_actsite(active_sites, sequence):
|
||||
|
||||
return sorted(aas)
|
||||
|
||||
def main():
|
||||
args = parse_args()
|
||||
rhea_dict = read_tsv("./rhea_unclassified_unique_ec4_candidates.tsv", top=args.num_rhea_candidates)
|
||||
def parse_entries_by_rhea(num_rhea_candidates, num_uniprot_entries, verb=False):
|
||||
rhea_dict = read_tsv_rhea("./rhea_unclassified_unique_ec4_candidates.tsv", top=num_rhea_candidates)
|
||||
for rhea_master_id in rhea_dict:
|
||||
get_req_rhea(rhea_master_id, rhea_dict)
|
||||
|
||||
num_candidates = sum([1 for r in rhea_dict if rhea_dict[r]["num_uniprot"]<=args.num_uniprot_entries])
|
||||
print(f"\nFound {blue(num_candidates)} rhea entries with <={args.num_uniprot_entries} protein match(es) in UniProt.\n")
|
||||
num_candidates = sum([1 for r in rhea_dict if rhea_dict[r]["num_uniprot"]<=num_uniprot_entries])
|
||||
print(f"\nFound {blue(num_candidates)} rhea entries with <={num_uniprot_entries} protein match(es) in UniProt.\n")
|
||||
|
||||
num_hits = 0
|
||||
for rhea_master_id in rhea_dict:
|
||||
if rhea_dict[rhea_master_id]["num_uniprot"]<=args.num_uniprot_entries:
|
||||
if rhea_dict[rhea_master_id]["num_uniprot"]<=num_uniprot_entries:
|
||||
data = get_req_uniprot(rhea_master_id)
|
||||
result_match = False
|
||||
for result in data['results']:
|
||||
@@ -160,15 +235,86 @@ def main():
|
||||
print(green(f"\tsOG hit."))
|
||||
result_match = True
|
||||
num_hits+=1
|
||||
if args.verbose:
|
||||
if verb:
|
||||
print(f"\tMCSA ID: {rhea_dict[rhea_master_id]['top_mcsa_ids']}, UniProt ID: {result['primaryAccession']} ({result['entryType']})")
|
||||
print_AA_differences(mcsa_aas, sog_aas, aas)
|
||||
if not result_match:
|
||||
print(red(f"\tNo match found."))
|
||||
#print(red(f"\tNo match found."))
|
||||
print("")
|
||||
|
||||
print(f"\nFound a total of {warn(num_hits)} matching active sites.")
|
||||
|
||||
def parse_entries_by_mcsa(num_candidates, max_distance, verb=False):
|
||||
#mcsa_dict = read_tsv_mcsa("./rhea_unclassified_unique_ec4_candidates.tsv", num_candidates)
|
||||
mcsa_dict = [str(x) for x in list(range(1,num_candidates+1))]
|
||||
print(f"Number of mcsa IDs: {len(mcsa_dict)}")
|
||||
if len(mcsa_dict) < 100:
|
||||
mcsaid_groups = [list(mcsa_dict)]
|
||||
else: mcsaid_groups = list(zip(*(iter(mcsa_dict),) * 100))
|
||||
distances = []
|
||||
num_hits = 0
|
||||
hits = []
|
||||
for mcsa_dict in mcsaid_groups:
|
||||
results = get_req_mcsa(mcsa_dict)
|
||||
|
||||
for record in results:
|
||||
mcsa_id = record['mcsa_id']
|
||||
residues = record['residues']
|
||||
all_mcsa_ecs = record['all_ecs']
|
||||
mcsa_aas = []
|
||||
for residue in residues:
|
||||
sequences = residue['residue_sequences']
|
||||
for sequence in sequences:
|
||||
aa_code = sequence['code']
|
||||
aa_index = sequence['resid']
|
||||
mcsa_aas.append(f"{aa_code}{aa_index}")
|
||||
|
||||
uniprot_ids = record['reference_uniprot_id']
|
||||
for uniprot_id in uniprot_ids.split(", "):
|
||||
data = get_req_uniprotid(uniprot_id)
|
||||
result_match = False
|
||||
if 'features' in data:
|
||||
# has active site info
|
||||
sites = data['features']
|
||||
sequence = data['sequence']['value']
|
||||
all_uniprot_ecs = [x['value'] for x in data['proteinDescription']['recommendedName']['ecNumbers']]
|
||||
aas = identify_aas_in_actsite(sites, sequence)
|
||||
#mcsa_aas = mcsa_dict[str(mcsa_id)]['expected_amino_acids_all_mcsa_entry']
|
||||
mcsa_aas_short = list(set([aa[:3] for aa in mcsa_aas]))
|
||||
if verb:
|
||||
print(f"\tMCSA ID: {warn(mcsa_id)}, UniProt ID: {uniprot_id} ({data['entryType']})")
|
||||
(distance, pairs, unpaired) = get_AA_distances(mcsa_aas, [], aas)
|
||||
if distance <=max_distance:
|
||||
hits.append((f"{mcsa_id}\t{uniprot_id}\t{mcsa_aas}\t{aas}\t{pairs}\t{unpaired}\t{distance}",distance))
|
||||
if distance==0:
|
||||
num_hits+=1
|
||||
print_AA_differences(mcsa_aas, [], aas)
|
||||
print_AA_distances(mcsa_aas, [], aas)
|
||||
distances.append((mcsa_id, uniprot_id, distance))
|
||||
print(red(f""))
|
||||
|
||||
print(f"\nFound a total of {warn(num_hits)} matching active sites.")
|
||||
print(f"Num distances under {max_distance}:", blue(len([1 for (_,_,x) in distances if x<=max_distance])))
|
||||
print(f"Smallest distance:", green(min(distances, key=lambda x: x[2])))
|
||||
print(f"Largest distance:", red(max(distances, key=lambda x: x[2])))
|
||||
print(f"Average distance:", warn(sum([x for (_,_,x) in distances])/len(distances)))
|
||||
|
||||
# Print MCSA ID and UniProt ID for distances <= max_distance
|
||||
hits = sorted(hits, key=lambda x: x[1])
|
||||
hits = [x for (x,_) in hits]
|
||||
hits.insert(0,"MCSA ID\tUniProt ID\tMCSA AAs\tUniProt AAs\tPairs\tUnpaired\tDistance")
|
||||
blob = "\n".join(hits)
|
||||
with open(f"./distance_under_{max_distance}_from_{num_candidates}_MCSA_entries.tsv", 'w') as f:
|
||||
f.write(blob)
|
||||
|
||||
def main():
|
||||
args = parse_args()
|
||||
#parse_entries_by_rhea(args.num_candidates, args.num_uniprot_entries, args.verbose)
|
||||
parse_entries_by_mcsa(args.num_candidates, args.max_distance, verb=True)
|
||||
|
||||
|
||||
def print_AA_differences(mcsa_aas, sog_aas, uniprot_aas):
|
||||
# set to remove duplications, eg. His93 His93
|
||||
mcsa_aas2 = sorted(list(set(mcsa_aas)))
|
||||
sog_aas2 = sorted(list(set(sog_aas)))
|
||||
uni_aas2 = sorted(list(set(uniprot_aas)))
|
||||
@@ -219,5 +365,26 @@ def print_AA_differences(mcsa_aas, sog_aas, uniprot_aas):
|
||||
[print(f"\t{x}", end="") for x in pretty_uni]
|
||||
print("")
|
||||
|
||||
def get_AA_distances(mcsa_aas, sog_aas, uniprot_aas):
|
||||
mcsa_short = list([aa[:3] for aa in list(set(mcsa_aas))])
|
||||
uni_short = list([aa[:3] for aa in list(set(uniprot_aas))])
|
||||
aa_set1 = [AA_TRIPLET[t] for t in mcsa_short]
|
||||
aa_set2 = [AA_TRIPLET[t] for t in uni_short]
|
||||
(objVal, pairs, unpaired) = min_distance_LP(aa_set1, aa_set2)
|
||||
pairs = [(AA_SINGLE[a[0]][1], AA_SINGLE[b[0]][1]) for a,b in pairs]
|
||||
unpaired = [AA_SINGLE[a[0]][1] for a in unpaired]
|
||||
return (objVal, pairs, unpaired)
|
||||
|
||||
def print_AA_distances(mcsa_aas, sog_aas, uniprot_aas):
|
||||
# print distance measure between MCSA and UniProt
|
||||
(objVal, pairs, unpaired) = get_AA_distances(mcsa_aas, sog_aas, uniprot_aas)
|
||||
pairs = [(AA_SINGLE[a[0]][1], AA_SINGLE[b[0]][1]) for a,b in pairs]
|
||||
unpaired = [AA_SINGLE[a[0]][1] for a in unpaired]
|
||||
print(f"Distance measure between MCSA and UniProt: {blue(objVal)}")
|
||||
print(f"Pairs")
|
||||
print(f"\t{'\t'.join([green(x) for (x,_) in pairs])}")
|
||||
print(f"\t{'\t'.join([green(x) for (_,x) in pairs])}")
|
||||
print(f"Unpaired: {blue(unpaired)}")
|
||||
|
||||
if __name__=="__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user