LoginSignup
1
4

More than 5 years have passed since last update.

Fasta配列を分割する

Last updated at Posted at 2017-05-11

blastでバカパラしたいときとか、でかいmulti fastaファイルを適当に分割したい時に使うやつ
絶対もっとシンプルな書き方ある

split.py

#!/usr/bin/env python
#coding: UTF-8
from Bio import SeqIO
#ファイル何個に分割したいか
num = 5
fastaname = "genes.faa"
#配列数の取得
seqnum = len(list(SeqIO.parse(open(fastaname), "fasta")))

#それぞれのファイルあたりの配列数
filenum = int((seqnum-seqnum%num)/num)
outList = []
#出力ファイル作成
for x in range(1,num+1):
    w = open(str(x) +".fasta","w")
    outList.append(w)
#ファイルの書き込み
for i,record in enumerate(SeqIO.parse(open(fastaname),"fasta")):
    #print (record)
    tmpnum = i+1
    if tmpnum >= filenum * num:#端数の扱い
        filenumber = int((tmpnum-tmpnum%filenum)/filenum)-1
    else:
        filenumber = int((tmpnum-tmpnum%filenum)/filenum)
    outfile = outList[filenumber]
    outfile.write(">" + str(record.id)+ "\n")
    outfile.write( str(record.seq) + "\n")
#出力ファイルのclose
for out in outList:
    out.close()
1
4
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
4