본문 바로가기

BRIC_Q&A

BRIC 연재 _31_ 예제 코드 예시

다음의 코드를 fasta2faa.py 와 같은 파이썬 실행 파일로 저장한 다음,

------

$ fasta2faa.py NP_000610.fasta IFNgamma.faa

------

등의 명령으로 실행해 본다. 이때, fasta2faa.py는 명령어 디렉터리에, NP_000610.fasta는 현재 디렉터리에 있어야 한다.


---

#!/usr/bin/env python3


import sys, string  #line 3


i_filename= sys.argv[1]  #line 5

o_filename= sys.argv[2]

i_handle = open(i_filename, 'r')

o_handle = open(o_filename, 'w')

i_cursor = i_handle.readlines()  #line 9


for line in i_cursor:

if line[0] == ">":

o_handle.write(">" + o_filename + " " + line[1:])

else:

o_handle.write(line.strip(string.whitespace))  # line 15


o_handle.close()

i_handle.close()

---


실행 후, 생성된 IFNgamma.faa 파일을 확인한다.


다음은 주요 내용에 관한 간단한 설명이다.


line 3: sys.argv와 string.whitespace 사용을 위한 sys 및 string 모듈 호출.

lines 5, 6: 입출력 파일명 지정.

lines 7, 8: 입출력 파일 객체.

line 9: 입력 파일의 내용을 readlines() 메서드를 이용하여 변수에 적재.

line 15: 입력 내용에서 ">"를 포함하고 있는 첫 줄을 제외한 내용에서 공백 문자 제거.

lines 17, 18: 파일 객체 닫음.


위의 코드는 하나의 예시로서 참고용이다.

'BRIC_Q&A' 카테고리의 다른 글

BLAST+ 테스트용 질의 서열  (1) 2015.03.01
Cygwin 설치  (3) 2014.10.24