이번 포스팅에서는 간단하게 Seq 객체를 다루는 법을 살펴볼 것이다.
Seq class는 BioPython의 class 중 하나로 말 그대로 서열의 속성같은 것을 가지고 있는 class이다. 서열답게 전사,번역,역상보적인 결합 등을 method로 손쉽게 구할 수 있다. 그리고 Seq 클래스는 파이썬의 기본적인 문자열의 속성까지 어느정도 계승했으므로 인덱스를 이용한 스플라이싱, len()함수 등을 그대로 쓸 수 있다.
https://rosalind.info/problems/rvco/
ROSALIND | Complementing a Strand of DNA
It appears that your browser has JavaScript disabled. Rosalind requires your browser to be JavaScript enabled. Complementing a Strand of DNA solved by 1112 The Second Strand Figure 1. Base pairing across the two strands of DNA. Figure 2. The double helix o
rosalind.info
이 문제는 주어진 서열과 그 서열의 역상보적인 서열이 같은 개수를 구하는 문제이다. 이는 Seq 클래스의 .reverse_complement() attribute를 쓰면 손쉽게 구할 수 있다.
from Bio import SeqIO
with open(r"파일경로",'r') as f:
records = SeqIO.parse(f,'fasta')
ans=0
for record in records:
if record.seq==record.seq.reverse_complement():
ans+=1
print(ans)
https://rosalind.info/problems/orfr/
ROSALIND | Finding Genes with ORFs
It appears that your browser has JavaScript disabled. Rosalind requires your browser to be JavaScript enabled. Finding Genes with ORFs solved by 754 Classifying Open Reading Frames Figure 1. The reading frame in the top strand starting with ATG and ending
rosalind.info
이 문제는 주어진 서열에서 오픈리딩프레임을 따져가며 만들 수 있는 모든 단백질 서열 중 가장 길이가 긴 것을 출력하는 문제이다. 이 문제 또한 Seq 객채의 attribute들을 쓰면 쉽게 구할 수 있다. 조금 까다로운 점은 ORF를 따지기에 서열의 길이가 3의 배수로 떨어지지 않을 때이다. 이 경우 애매하게 남은 것들을 잘라버리거나 의미없는 문자를 추가해서 3의 배수로 맞춰줘야한다. 필자는 의미 없는 문자 N을 개수만큼 더해줘서 해결했다.
from Bio.Seq import Seq
def find_orf(seq):
ans=[]
for i in range(len(seq)-3):
if seq[i:i+3] == 'ATG':
add_N = (3-(len(seq[i:])%3))%3
trailed_seq = Seq(seq[i:]+'N'*(add_N))
protein = trailed_seq.translate(to_stop=True)
ans.append(protein)
return ans
with open(r"파일경로",'r') as f:
DNA_seq = Seq(f.readline().rstrip())
DNA_rcseq = DNA_seq.reverse_complement()
ans=find_orf(DNA_seq)+find_orf(DNA_rcseq)
print(sorted(ans,key=len,reverse=True)[0])
'생물정보학 > 바이오파이썬' 카테고리의 다른 글
Gloabal multiple alignment (0) | 2024.12.07 |
---|---|
Suboptimal local alignment (2) | 2024.12.07 |
FASTQ 파일 다루기 (1) | 2024.11.30 |
단백질 번역하기 (2) | 2024.11.29 |
FASTQ 형식 소개 (0) | 2024.11.24 |