より長続きするブログ

続けていきたい気持ち。

Accession No. のリストを作って実行するとfastaファイルを作ってくれるRubyスクリプト

欲しかったので作った。

メインになるサーバアクセスはbioRubyでやってもらって、取ってきたファイルを整形しているだけ。
作業自動化を色々やりたいのだけど、とりあえずその一歩。保存も兼ねて置いてみる。

require 'bio'

readfile = open("no.ini", "r")
while number = readfile.gets do
number.chomp!
txtfile = open("data/" << number << ".txt", "a+")
fasfile = open("data/" << number << ".fas", "a+")
serv = Bio::Fetch.new("http://www.ebi.ac.uk/Tools/dbfetch/dbfetch")
entry = serv.fetch("genbank", number)
txtfile.puts entry
txtfile.close

### txtfileから情報取得
txtfile = open("data/" << number << ".txt", "r")
while line = txtfile.gets do
if line.include?("ID ")
line.sub!("ID ", ">")
line.sub!(/;.*/, "")
fasfile.puts line

### その他大文字アルファベットから始まる行を削除
elsif /[A-Z][A-Z].*/ =~ line

### 最後の//を削除
elsif /.*\/\/.*/ =~ line
line.sub!(/.*\/\/.*/, "")
fasfile.puts line

### それ以外の行(atgc...)をアウトプット
else
line.sub!(/[0-9].*/, "")
line.strip!
fasfile.puts line

### if終わり
end

### 行抜き出しの繰り返し終わり
end
txtfile.close
fasfile.close

### 一つのアセッションナンバー終わり
end

readfile.close