1

次の内容の巨大なファイルがあります。

ファイル名:input.txt

>chr1
jdlfnhl
dh,ndh
dnh.

dhjl

>chr2
dhfl
dhl
dh;l

>chr3

shgl
sgl

>chr2_random
dgld

このファイルを、次のように4つの別々のファイルを取得するように分割する必要があります。

ファイル1:chr1.fa

>chr1
jdlfnhl
dh,ndh
dnh.

dhjl

ファイル2:chr2.fa

>chr2
dhfl
dhl
dh;l

ファイル3:chr3.fa

>chr3

shgl
sgl

ファイル4:chr2_random.fa

>chr2_random
dgld

Linuxでcsplitを試しましたが、「>」の直後のテキストで名前を変更できませんでした。

csplit -z input.txt '/>/' '{*}'
4

8 に答える 8

9

Linuxボックスを使用していることを示しているので、「awk」がこの作業に適したツールのようです。

利用方法:
./foo.awk your_input_file

foo.awk:

#!/usr/bin/awk -f

/^>chr/ {
    OUT=substr($0,2) ".fa"
}

OUT {
    print >OUT
}

これも1行で実行できます。

awk '/^>chr/ {OUT=substr($0,2) ".fa"}; OUT {print >OUT}' your_input
于 2012-08-05T17:38:53.343 に答える
2

FASTA / FASTQファイルでもっと複雑なことをしたい場合は、Biopythonを検討する必要があります。

FASTQファイルの変更と再書き込みに関する投稿は次のとおりです。http://news.open-bio.org/news/2009/09/biopython-fast-fastq/

そして、FASTAファイルの分割についてのもう1つ:http://lists.open-bio.org/pipermail/biopython/2012-July/008102.html

于 2012-08-05T18:27:25.693 に答える
1

少し面倒なスクリプトですが、一度に1行しか読み取れないため、大きなファイルで機能するはずです。

実行するには、実行しますpython thescript.py input.txt(または、stdinから読み取りますcat input.txt | python thescript.py

import sys
import fileinput

in_file = False

for line in fileinput.input():
    if line.startswith(">"):
        # Close current file
        if in_file:
            f.close()

        # Make new filename
        fname = line.rstrip().partition(">")[2]
        fname = "%s.fa" % fname

        # Open new file
        f = open(fname, "w")
        in_file = True

        # Write current line
        f.write(line)

    elif in_file:
        # Write line to currently open file
        f.write(line)

    else:
        # Something went wrong, no ">chr1" found yet
        print >>sys.stderr, "Line %r encountered, but no preceeding > line found"
于 2012-08-05T17:38:22.893 に答える
1

あなたの最善の策は、exonerateスイートのfastaexplodeプログラムを使用することです:

$ fastaexplode -h
fastaexplode from exonerate version 2.2.0
Using glib version 2.30.2
Built on Jan 12 2012
Branch: unnamed branch

fastaexplode: Split a fasta file up into individual sequences
Guy St.C. Slater. guy@ebi.ac.uk. 2000-2003.

Synopsis:
--------
fastaexplode <path>

General Options:
---------------
-h --shorthelp [FALSE] <TRUE>
   --help [FALSE] 
-v --version [FALSE] 

Sequence Input Options:
----------------------
-f --fasta [mandatory]  <*** not set ***>
-d --directory [.] 

--
于 2012-08-14T02:18:28.220 に答える
0
with open('data.txt') as f:
    lines=f.read()
    lines=lines.split('>')
    lines=['>'+x for x in lines[1:]]
    for x in lines:
        file_name=x.split('\n')[0][1:]  #use this variable to create the new file
        fil=open(file_name+'.fa','w')
        fil.write(x)
        fil.close()
于 2012-08-05T17:32:16.813 に答える
0

特にPythonでこれを試したい場合は、このコードを使用できます

f2 = open("/dev/null", "r")
f = open("input.txt", "r")
for line in f:
    if ">" in line:
        f2.close()
        f2 = open(line.split(">")[1]),"w")
    else:
        f2.write(line)

f.close()
于 2012-08-05T17:36:20.733 に答える
0

あるいは、BioPythonを使用することもできます。virtualenvへのインストールは簡単です。

virtualenv biopython_env
source biopython_env/bin/activate
pip install numpy
pip install biopython

そして、これが行われると、fastaファイルを分割するのは簡単です。変数にfastaファイルへのパスがあると仮定しましょうfasta_file

from Bio import SeqIO

parser = SeqIO.parse(fasta_file, "fasta")
for entry in parser:
   SeqIO.write(entry, "chr{}.fa".format(entry.id), "fasta")

このバージョンのフォーマットはPython2.7で機能しますが、古いバージョンでは機能しない可能性があることに注意してください。

パフォーマンスに関しては、これを使用して1000人ゲノムプロジェクトからのヒトゲノム参照を無視できる時間で分割しましたが、より大きなファイルでどのように機能するかわかりません。

于 2013-08-22T18:56:41.417 に答える
0
#!/usr/bin/perl-w
use strict;
use warnings;


my %hash =();
my $key = '';
open F, "input.txt", or die $!;
while(<F>){
    chomp;
    if($_ =~ /^(>.+)/){
        $key = $1;
    }else{
       push @{$hash{$key}}, $_ ;
    }   
}

foreach(keys %hash){
    my $key1 = $_;
    my $key2 ='';
    if($key1 =~ /^>(.+)/){
        $key2 = $1;
    }
    open MYOUTPUT, ">","$key2.fa", or die $!;
    print MYOUTPUT join("\n",$_,@{$hash{$_}}),"\n";
    close MYOUTPUT;
}
于 2014-01-02T18:23:07.613 に答える