본문 바로가기
Python

Python programming for Bioinformatics - 연습문제 6

by 코딩하는 미토콘드리아 bioinformatics 2023. 7. 24.
반응형

파이썬으로 실무에 적용 가능할 법한 bioinformatics 연습문제를 만들어서 풀어보겠습니다.

input 데이터를 사용해서 output 형태로 만들면 성공입니다.

두 문제로 이루어져 있습니다.

 

INPUT 1 : NC_004459.data

NC_004459.data
1.73MB

INPUT 2 : NC_004459.fasta

NC_004459.fasta
3.17MB

1.문제 설명 :
1)INPUT1 파일의  CDS 에서 ‘/locus_tag이름이 존재하는 것만 위치 정보를 기억하여 INPUT2 파일의 sequence 를 가져온다. , 위에서 차례대로 10sequence 갖고 fasta파일 만든다. Complement 무시하고 위치 정보만을 이용한다. sequence 70개씩 잘라서 써주며, ID 옆에는 해당 sequencelength를 써준다.

2. 결과 파일:

sequence IDlocus_tag 한다.

OUTPUT

 

INPUT 1 : NC_004459.data

1.문제 설명 :
1)INPUT1 파일에서  CDS 단위로 ‘/locus_tag가 존재하는 것만 위치 정보를 파일에 써준다. 
2)CDS 행에서 ‘..’ 기준으로 start, end로 써준다. 왼쪽 옆에 ‘complement’ 이면 ‘complement’를 써주고, 아닌 경우는 ‘none’으로 써준다.

2. 결과 파일:

첫 번째 필드는 count 이고, 필드 사이는 tab으로 띄운다.
필드 순서 : count, locus_tag, complement, start, end
=> 결과는 header 없이 2915 라인이 나옵니다.

 

OUTPUT

코드 설명

#%%
f = open('NC_004459.data')
f= f.readlines()
f = f[39:]

alist = []
for i in range(0,len(f)):
    if 'CDS' in f[i]:
        alist.append(f[i:i+8])

adict = {}
for i in range(0,len(alist)):
    complement = 0 
    for j in range(0,len(alist[i])):
        if 'complement(' in alist[i][j]:
            complement = alist[i][j].replace("CDS","")
            complement = complement.strip()
    for k in range(0,len(alist[i])):
        if 'locus' in alist[i][k] and complement !=0:
            bowl= alist[i][k].replace("CDS","")
            bowl=bowl.strip()
            adict[bowl] = complement
adict

#%%
f = open('NC_004459.fasta')
f= f.readlines()[1:]
f=''.join(f).replace("\n","")

blist=[] 

for i in adict.keys():
    #try:
    split_dict=adict[i].replace("complement","").replace("(","").replace(")","").split("..")
    blist.append(">"+i.replace("/locus_tag=","").replace("\"","")+"\t"+str(int(split_dict[1])-int(split_dict[0])+1)+"\n")
    
    blist.append(f[int(split_dict[0])-1:int(split_dict[1])]+"\n")


wf = open("EX6out.fasta","w")

for i in range(0,len(blist)):
    if i % 2 == 0:
        wf.write(blist[i])
    else:
        for j in range(0,len(blist[i]),70):
            if j+70<len(blist[i]):
                wf.write(blist[i][j:j+70]+"\n")  
            else:
                wf.write(blist[i][j:j+70])           
wf.close()


#2nd question
#%%
f = open('NC_004459.data')
f= f.readlines()
f = f[39:]

alist = []
for i in range(0,len(f)):
    if 'CDS' in f[i]:
        alist.append(f[i:i+8])

adict = {}
for i in range(0,len(alist)):
    complement = 0 
    for j in range(0,len(alist[i])):
        if '..' in alist[i][j]:
            complement = alist[i][j].replace("CDS","")
            complement = complement.strip()
    for k in range(0,len(alist[i])):
        if 'locus_tag' in alist[i][k] and complement !=0:
            bowl= alist[i][k].replace("CDS","")
            bowl=bowl.strip()
            adict[bowl] = complement
adict

#%%
blist=[]

count = 0
for i in adict.keys():
    count +=1
    if "complement" in adict[i]:
        prepro = adict[i].replace("complement(","").replace(")","")
        print(f"{count}\t" + i.replace("/locus_tag=","").replace("\"","") + "\tcomplement\t" + '\t'.join(prepro.split("..")))
    else:
        print(f"{count}\t"+i.replace("/locus_tag=","").replace("\"","") + "\tnone\t" + "\t".join(adict[i].split("..")))
#blist
반응형