gene to fasta Blast result version

이번 스크립트는 중간에 gene list 파일을 만들 필요 없는 스크립트.
-중복이 되는 유전자가 있으면 제거하고 중복되지 않게 유전자 서열 저장

import glob,sys
from Bio import SeqIO

blast_result_file = sys.argv[1]
db_fasta_file = sys.argv[2]
output_file = sys.argv[3]

lists = []
for x in open(blast_result_file):
        lists.append((x.split('\t')[1]).strip())

if len(lists) > 1:
        lists.sort()
        last = lists[-1]
        for i in range(len(lists)-2,-1,-1):
                if last == lists[i]:
                        del lists[i]
                else:
                        last=lists[i]


db = SeqIO.parse(open(db_fasta_file), format='fasta')

ow = open(output+'.txt','w')

for rec in db:
        name = rec.description
        seq = rec.seq.tostring()

        for x in lists:
                if name.strip() == x.strip() or (name.strip()).find(x.strip()) != -1:
                        if name[:1] == '>':
                                ow.write(name+'\n'+seq+'\n')
                        else:
                                ow.write('>'+name+'\n'+seq+'\n')

ow.close()


사용법은 지난번 스크립트와 동일
ex) this.py blast_output_file db_file output_file

blast_output_file의 형식은 blast돌릴때 -m 8 형식입니다.
db_file은 fasta 형식이면 됩니다. 물론 blast_output_file에 gene 이름이 있어야
db_file에서도 찾아주겠죠..

ex) Blast프로그램 query db -m 8 -o blast_output_file
여기서 나온 blast_output_file에서 두번째 컬럼의 gene 이름으로 db_file에서
서열을 추출하는 방식입니다.
크리에이티브 커먼즈 라이센스
Creative Commons License

Posted by gwlee

2009/05/22 11:48 2009/05/22 11:48
, , ,
Response
0 Trackbacks , 0 Comments
RSS :
http://thegreatgoodplace.com/tt/study/rss/response/146

Trackback URL : http://thegreatgoodplace.com/tt/study/trackback/146

Leave a comment

유전자 리스트에서 서열 저장

Blast 수행 이후 필요한 유전자의 서열들을 확보하는 스크립트

유전자 리스트(검색하는 DB 파일의 유전자 리스트로부터 유래된) 파일과
검색하고자 하는 db 파일 그리고 유전자 이름과 서열을 FASTA 형식으로
저장하는 파일 이름 필요
저장 파일의 확장자는 항상 ".txt"가 붙음
결과로 나오는 저장파일의 유전자 서열들에 대해서는 중복으로 존재 가능합니다.

import glob,sys
from Bio import SeqIO

list_file = sys.argv[1]
db_file = sys.argv[2]
output_file = sys.argv[3]

lists = []
for x in open(list_file):
        lists.append(x.strip())

db = SeqIO.parse(open(db_file), format='fasta')

ow = open(output+'.txt','w')

for rec in db:
        name = rec.description
        seq = rec.seq.tostring()

       
for x in lists:
                if name.strip() == x.strip() or (name.strip()).find(x.strip()) != -1:
                        if name[:1] == '>':
                                ow.write(name+'\n'+seq+'\n')
                        else:
                                ow.write('>'+name+'\n'+seq+'\n')
   
ow.close()


사용 예
[test]$ python this.py gene_list_file db_file output_file_name

-빨간색으로 강조된 부분은 필요에 따라 수정 할것.

-파이썬 파일을 실행하는 경우 각 파일들이 실행되어지는
 폴더에 있지 아니한 경우 절대 경로를 지정해줘야 잘 작동할께요.. 아마도..
 
약간의 응용을 하면 input file을 blast 결과파일로도 할 수 있음 (단, output format -m 8로 했을 경우)

input_file인 gene_list_file의 format

gene_list_file 보기


db_file의 경우 일반 fasta format이면 사용 가능


ex) Blast프로그램 query db -m 8 -o blast_output_file
여기서 나온 blast_output_file에서 두번째 컬럼의 gene 이름으로 db_file에서
서열을 추출하는 방식입니다.



gene_list에서 중복으로 나오는 유전자 서열이 있을경우

중복 서열 제거 스크립트


 

크리에이티브 커먼즈 라이센스
Creative Commons License

Posted by gwlee

2009/05/13 14:34 2009/05/13 14:34
,
Response
0 Trackbacks , 0 Comments
RSS :
http://thegreatgoodplace.com/tt/study/rss/response/133

Trackback URL : http://thegreatgoodplace.com/tt/study/trackback/133

Leave a comment

Fasta 형식의 파일에서 빈서열 제거

가끔씩 Blast를 수행하고자 formatdb를 수행 할 때,
다음과 같은 에러를 접한 적이 있으리라 본다.

[formatdb] WARNING: Cannot add sequence number XXXXX XX.XXX.XXX.
 because it has zero-length.

[formatdb] FATAL ERROR: Fatal error when adding sequence to BLAST database.


formatdb를 수행하려는 fasta 서열에
빈 서열을 가지고 있기 때문에 나오는 에러로
빈 서열을 제거하면 OK!

vi check.py

import glob,sys
from Bio import SeqIO

file = sys.argv[1]

ow = open(file.split('.')[0]+'.check','w')
seqs = SeqIO.parse(open(file), format='fasta')
 for rec in seqs:
        name = rec.description
        seq = rec.seq.tostring()

        if len(seq.strip()) != 0:
                ow.write('>'+name+'\n')
                ow.write(seq+'\n')

ow.close()



[test]$python check.py test.fasta

크리에이티브 커먼즈 라이센스
Creative Commons License

Posted by gwlee

2009/05/08 19:30 2009/05/08 19:30
, , ,
Response
0 Trackbacks , 0 Comments
RSS :
http://thegreatgoodplace.com/tt/study/rss/response/128

Trackback URL : http://thegreatgoodplace.com/tt/study/trackback/128

Leave a comment

블로그 이미지

Stay Hungry Stary Foolish!

- gwlee

TC-Cumulus by reznoa requires Flash Player 9 or better.

Site Stats

Total hits:
62336
Today:
17
Yesterday:
65