-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest5.py
92 lines (71 loc) · 3.05 KB
/
test5.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import requests
def get_sequence_by_name_from_pdbbank_1(name: str) -> str | None:
"""
Returns the sequence corresponding to the given name by querying pdb bank.
Args:
name (str): The name in the format 'protein_id.chain_id' (e.g., '1dgw.Y').
Returns:
str | None: The sequence corresponding to the given chain, or None if not found.
"""
try:
# Split the name into protein ID and chain ID
protein_id, chain_id = name.split(".")
# Fetch the FASTA content from the RCSB PDB database
url = f"https://www.rcsb.org/fasta/entry/{protein_id}/display"
response = requests.get(url)
response.raise_for_status() # Raise an exception for HTTP errors
# Parse the FASTA content to find the sequence for the given chain
fasta_content = response.text
for entry in fasta_content.split(">")[1:]:
header, sequence = entry.split("\n", 1)
# Extract the second segment (Chain segment) from the header
segments = header.split("|")
if len(segments) >= 2 and chain_id in segments[1]:
return sequence.replace("\n", "")
# If the chain ID is not found, return None
return None
except Exception as e:
print(f"Error fetching sequence: {e}")
return None
import requests
from io import StringIO
from Bio import SeqIO
def get_sequence_by_name_from_pdbbank_2(name: str) -> str | None:
"""
Returns the sequence corresponding to the given name by querying pdb bank.
Args:
name (str): The name in the format 'protein_id.chain_id' (e.g., '1dgw.Y').
Returns:
str | None: The sequence corresponding to the given chain, or None if not found.
"""
try:
# Split the name into protein ID and chain ID
protein_id, chain_id = name.split(".")
# Fetch the FASTA content from the RCSB PDB database
url = f"https://www.rcsb.org/fasta/entry/{protein_id}/display"
response = requests.get(url)
response.raise_for_status() # Raise an exception for HTTP errors
# Parse the FASTA content using Biopython
fasta_content = response.text
fasta_io = StringIO(fasta_content)
for record in SeqIO.parse(fasta_io, "fasta"):
# Extract the second segment (Chain segment) from the description
segments = record.description.split("|")
if len(segments) >= 2 and chain_id in segments[1]:
return str(record.seq)
# If the chain ID is not found, return None
return None
except Exception as e:
print(f"Error fetching sequence: {e}")
return None
# Example usage:
# sequence = get_sequence_by_name_from_pdbbank('1dgw.Y')
# print(sequence) # Output: "MQLRRYAATLSEGDIIVIPSSFPVALKAASDLNMVGIGVNAENNERNFLAGHKENVIRQIPRQVSDLTFPGSGEEVEELLENQKESYFVDGQP"
if __name__ == "__main__":
test = "1dgw.Y"
test = "1cxz.B"
seq1 = get_sequence_by_name_from_pdbbank_1(test)
seq2 = get_sequence_by_name_from_pdbbank_2(test)
print(seq1)
print()
print(seq2)