2012-08-27 4 views
1

단백질 서열을 정렬하기 위해 Biopython과 Clustalw2를 사용하여 파이썬에서 약간의 생물 정보학 작업을하고 있습니다. 나는 이것 (상당히 2 개월의 경험)에 상당히 익숙하며, stdout을 사용하고 전체 디렉토리를 반복하는 데 문제가있다. 어떤 도움을 주시면 감사하겠습니다. Python을 사용하는 Bioinformatics 스크립트/Biopython/Clustalw stdout을 사용하여 단백질 디렉토리를 반복합니다.

그래서 나는

#!/usr/bin/python 

import Bio 
import os 
from Bio.Align.Applications import ClustalwCommandline 
from Bio import Seq 
from Bio import SeqIO 
from Bio import AlignIO 
from Bio.SeqRecord import SeqRecord 


clustal_loc=r"/Users/Wes/Desktop/eggNOG_files/clustalw-2.1-macosx/clustalw2" 
try: 
    f_in=raw_input("Enter the filepath of the FASTA to align: ") 
    f_out= raw_input("Enter the output filename: ") 
    fh= open(f_in) 
    fo=open(f_out,'w') 
    for record in SeqIO.parse(fh,"fasta"): 
      id = record.id 
      seq = record.seq 
      print("Name: %s, size: %s"%(id,len(seq))) 
    try: 
      cl = ClustalwCommandline(clustal_loc,infile=f_in, outfile=f_out, align=True, outorder="ALIGNED", convert=True, output="pir") 
      assert os.path.isfile(clustal_loc), "Clustal W not found" 
      stdout, stderr = cl() 
      print cl 
    except: 
      print("There was a problem aligning. Check ClustalW path and .fasta input.") 


    fh.close() 
    fo.close() 


except: 
     print("Could not parse. Check to make sure filepath is correct and that file is in FASTA format") 

한 번에 하나 개의 파일을 처리하고 원하는 결과를 생산하는이 ... 작성했습니다 ... 그리고 이것은 잘 작동하는 것 같다. 문제는 제가 전체 디렉토리 (1000 + 파일 정렬과 같은 파일을 통해 이것을 반복하려고 할 때 온다. 문제는 stdout 함께 알지만이 시점에서 너무 아마추어 그것을 수정하는 방법을 알고 알고 오전 .

/usr/bin/python 

import Bio 
import os 
from Bio.Align.Applications import ClustalwCommandline 
from Bio import Seq 
from Bio import SeqIO 
from Bio import AlignIO 
from Bio.SeqRecord import SeqRecord 
import subprocess 
from subprocess import Popen 
clustal_loc=r"/Users/Wes/Desktop/eggNOG_files/clustalw-2.1-macosx/clustalw2" 

try: 

    folder= raw_input("Enter the folder of .fasta files to iterate over and align: ") 
    listing = os.listdir(folder) 

    for infile in listing: 
     print folder+'/'+infile 
     f_in = open(folder+'/'+infile,'r') 

     f_out=open(folder+'/'+infile+".pir",'w') 


     for record in SeqIO.parse(f_in,"fasta"): 
       id = record.id 
       seq = record.seq 
       print("Name: %s, size: %s"%(id,len(seq))) 

     clustalw_cline= ClustalwCommandline(clustal_loc,infile=f_in, outfile=f_out, align=True, outorder="ALIGNED", convert=True, output="pir") 

     assert os.path.isfile(clustal_loc), "Clustal W not found" 
     saveout = sys.stdout 
     sys.stdout = clustalw_cline() 
     sys.stdout = saveout 






     f_in.close() 

     f_out.close() 
except: 
    print("There was a problem aligning. Check ClustalW path and .fasta folder format/location") 

당신은 내가 꽤 심하게이 일을 일 처리 한 덕분에 당신이 제공 할 수있는 모든 도움을 볼 수있는 바와 같이

+0

당신은 또한 BIOSTAR를 요청할 수있다 : - 오브젝트를 제출하지 http://www.biostars.org/ – Pierre

+0

을 당신이 ClustalwCommandline 객체를 생성 경우, INFILE과 OUTFILE 인수는 문자열로 파일 이름이어야합니다. 나는 여기에 더 쓸 것이지만 주석은 내가 코드 포맷팅을하도록 내버려 두지 않는다. ... – peterjc

답변

0

정확하게 당신이보고있는 오류는 무엇인가 - 여기 아래 깨진 코드입니다..? sys.sterr과 sys.stdout을 문자열 값으로 설정하면 안됩니다 (clustalw_cline() 함수는 클러스터 표준 오류와 stdout을 문자열로 반환합니다). 파이썬에서 stdout에 아무 것도 쓸 수 없으므로

아래 코드를 정리하고 수정하려고했습니다.

#!/usr/bin/env python 

import Bio 
import os 
from glob import glob 
from Bio.Align.Applications import ClustalwCommandline 
from Bio import Seq 
from Bio import SeqIO 
from Bio import AlignIO 
from Bio.SeqRecord import SeqRecord 
import subprocess 
from subprocess import Popen 
clustal_loc=r"/Users/Wes/Desktop/eggNOG_files/clustalw-2.1-macosx/clustalw2" 

try: 
    folder= raw_input("Enter the folder of .fasta files to iterate over and align: ") 
    listing = glob(os.path.join(folder, '*.fasta')) 
    for infile in listing: 
     print infile 
     with open(os.path.splitext(infile) + '.pir') as f_out: 
      with open(infile) as f_in: 
       for record in SeqIO.parse(infile,"fasta"): 
         id = record.id 
         seq = record.seq 
         print("Name: %s, size: %s"%(id,len(seq))) 
       assert os.path.isfile(clustal_loc), "Clustal W not found" 
       clustalw_cline= ClustalwCommandline(clustal_loc,infile=f_in, 
                outfile=f_out, align=True, 
                outorder="ALIGNED",convert=True, output="pir") 
       stdout, stderr = clustalw_cline() 
except Exception: 
    print("There was a problem aligning. Check ClustalW path and .fasta folder format/location") 
+0

글쎄, clustalw에 의해 생성 된 정렬을 .pir 파일에 쓰려고한다. 첫 번째 스크립트는이 파일을 하나의 파일로 처리하는 것으로 보인다. 내가 stdout과 stderr을 버려야한다고 말하는거야? 이렇게하면 단일 파일을 처리하는 원본 스크립트가 제대로 작동하지 않습니다. 정화 및 도움을 당신을 감사하십시오. –

+0

첫 번째 스크립트에서 stdout 및 stderr 변수는 clustal의 출력을 문자열로 catch합니다. 두 번째 스크립트에서는 파이썬 파일과 비슷한 객체 인 sys.stdout을 clustalw_cline()의 출력에 할당합니다.이 출력은 형식 (stdout, stderr)의 튜플인데, 거의 확실하게 원하지 않습니다. clustalw 출력을 sys.stdout에 쓰려면 print 함수를 사용하거나 sys.stdout.write (출력)를 사용해야합니다. – user747508

+0

clustalw_cline() 출력을 내가 연 파일에 쓰고 싶습니다. f_out ...하지만 이번에는 그렇게 할 수 없습니다. –