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
1 Trackbacks , 0 Comments
RSS :
http://thegreatgoodplace.com/tt/study/rss/response/146

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

Trackbacks List

  1. Lowes t online phentermine price.

    Tracked from Buy c 20heap phentermine online. 2010/03/04 01:31 Delete

    Order phentermine online uk. Buy phentermine online. Order phentermine online. Online phentermine.

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

대용량 파일을 위한 shelve


출처: 파이썬마을


파이썬에 이런 모듈이..
참.. 대단한듯...
-파이썬에 감탄하면 지는거다..
난 자바를 공부하고 있어야돼... ㅠ.ㅠ

쉽게말해 대용량 사전
-사전이라는 자료형 은근히 편했는데..


실제 사용 예로 EMBL CDS fasta파일을 읽어서 E. coli에서 나온 것만 골라서
뽑아주는 소스는 이렇게 만들 수 있습니다.

코드:

from Bio import SeqIO
import shelve

seqs = SeqIO.parse(open('/data/embl/cds_nr.fasta'), format='fasta')
db = shelve.open('ecoli.db')

for rec in seqs:
    if ('Escherichia coli' in rec.description and
        'hypothetical' not in rec.description):
            name = rec.description.split()[0].split(':')[1]
            seq = rec.seq.tostring()
            db[name] = seq


나중에 EMBL ID로 검색할 때는 다음과 같이 open만 하면 검색할 수 있습니다.

코드:

import shelve
db = shelve.open('ecoli.db')
genes = ['100746833', '1008171518']
for gene in genes:
    print db[gene]



32G가 30여분 소요 됐다니.. 난 4.1G(nr, 한국시간 2009.3.5에 다운로드) 밖에 안돼네.. ㅋㅋ

5분이면 끝나야 하는거 아니야..;;
왜 안끝나..;;
크리에이티브 커먼즈 라이센스
Creative Commons License

Posted by gwlee

2009/03/05 20:42 2009/03/05 20:42
,
Response
0 Trackbacks , 0 Comments
RSS :
http://thegreatgoodplace.com/tt/study/rss/response/79

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

Leave a comment

리스트에서 중복된 원소 삭제

if len(list) > 1:
  list.sort()

  last = list[-1]

  for i in range(len(list)-2,-1,-1):
    if last == list[i]:
      del list[i]
    else:
      last=list[i]


리스트에 존재하는 중복되는 항목을 삭제


Python Grimoire 중에서...


Comment 달린것입니다.
tempDic = {}
   if len( list ) > 1:
      list.sort() for i in list:
         tempDic[i] = ''
         print tempDic.keys()
크리에이티브 커먼즈 라이센스
Creative Commons License

Posted by gwlee

2008/08/25 02:32 2008/08/25 02:32
Response
0 Trackbacks , 0 Comments
RSS :
http://thegreatgoodplace.com/tt/study/rss/response/41

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

Leave a comment

Win32com Python


파이썬에서 좀더 다양한 윈도우 프로그래밍이 가능하게 하는 모듈?
여하튼.... 좋은거~ ㅋ

Win32com

다양한 윈도우 프로그램과 연동되어
프로그래밍을 더 시키게 만든 녀석.. ㅡ.-a

여하튼.... python 2.3이후부터는 없는줄 알았는데..
역시 sourceForge에는 있었다는...;;;

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

Posted by gwlee

2008/08/25 01:33 2008/08/25 01:33
,
Response
0 Trackbacks , 0 Comments
RSS :
http://thegreatgoodplace.com/tt/study/rss/response/19

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

Leave a comment

Median 구하기



mwultong Blog


Median 구하기

a = []
a.sort
t_len = len(a)
if (t_len == 0):
   print 'None'
t_center = t_len/2
if (t_len % 2 == 1):
   print a[t_center]
else:
   print (a[t_center-1]+a[t_center]) / 2.0



문자열 순서

a = ''
a = a[::-1]
a=''.join(reversed(a))
print a






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

Posted by gwlee

2008/08/25 01:32 2008/08/25 01:32
,
Response
0 Trackbacks , 0 Comments
RSS :
http://thegreatgoodplace.com/tt/study/rss/response/18

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

Leave a comment

KOG Category 결정 script

NCBI의 Clusters of Orthologous Groups of proteins
로컬에서도 돌리고 싶은 이유로 유사한 결과가 도출되게
짜본 스크립트 입니다.

- 지금 올려놓은 소스는 KOG ( Eukaryotic Clusters )에 해당하는 것입니다.

※ NCBI에선 어떻게 하는지는 모르겠지만, 이 script로도 KOGnitor와 유사한
   결과를 도출 할 수 있습니다. :)

Source Open



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

Posted by gwlee

2008/08/25 01:29 2008/08/25 01:29
,
Response
0 Trackbacks , 0 Comments
RSS :
http://thegreatgoodplace.com/tt/study/rss/response/16

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

Leave a comment

Python tip

파이썬 마을같은 좋은 사이트도 있지만,
개인적으로 자신들만의 팁같은 것들을 올려주시는 분들이 많은 관계로.. ㅎㅎ

http://www.koreanrock.com/wiki.pl?SonDonPythonTips

http://del.icio.us/mwultong/python
크리에이티브 커먼즈 라이센스
Creative Commons License

Posted by gwlee

2008/08/25 01:27 2008/08/25 01:27
Response
0 Trackbacks , 0 Comments
RSS :
http://thegreatgoodplace.com/tt/study/rss/response/15

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

Leave a comment

블로그 이미지

gwLee's Study story

- gwlee



Site Stats

Total hits:
50158
Today:
82
Yesterday:
83