2013-01-11 117 views
0

我在此标记了python和perl,这是因为这是我迄今使用过的。如果有人知道更好的方式去做这件事,我当然愿意尝试一下。不管怎么说,我的问题:从文件中提取特定数据并将其写入另一个文件

我需要创建遵循以下格式的基因预测程序的输入文件:

seq1 5 15 
seq1 20 34 

seq2 50 48 
seq2 45 36 

seq3 17 20 

其中SEQ#是geneID和编号,右边是的位置在开放阅读框内的外显子。现在我有了一个.gff3文件中的这个信息,它有很多其他的信息。我可以用excel打开它,并轻松删除不相关数据的列。下面是它的现在安排:

PITG_00002 . gene 2 397 . + . ID=g.1;Name=ORF% 
PITG_00002 . mRNA 2 397 . + . ID=m.1; 
**PITG_00002** . exon **2 397** . + . ID=m.1.exon1; 
PITG_00002 . CDS 2 397 . + . ID=cds.m.1; 

PITG_00004 . gene 1 1275 . + . ID=g.3;Name=ORF%20g 
PITG_00004 . mRNA 1 1275 . + . ID=m.3; 
**PITG_00004** . exon **1 1275** . + . ID=m.3.exon1;P 
PITG_00004 . CDS 1 1275 . + . ID=cds.m.3;P 

PITG_00004 . gene 1397 1969 . + . ID=g.4;Name= 
PITG_00004 . mRNA 1397 1969 . + . ID=m.4; 
**PITG_00004** . exon **1397 1969** . + . ID=m.4.exon1; 
PITG_00004 . CDS 1397 1969 . + . ID=cds.m.4; 

所以我只需要那就是大胆的数据。例如,

PITG_0002 2 397 

PITG_00004 1 1275 
PITG_00004 1397 1969 

任何帮助你可以给予将不胜感激,谢谢!

编辑:好吧,我搞砸了格式。任何介于**之间的东西都是我需要的。

回答

1

它看起来像你的数据是制表符分隔的。

这Perl程序将从那些在第三列exon所有记录打印列1,4和5。您需要将open语句中的文件名更改为您的实际文件名。

use strict; 
use warnings; 

open my $fh, '<', 'genes.gff3' or die $!; 

while (<$fh>) { 
    chomp; 
    my @fields = split /\t/; 
    next unless @fields >= 5 and $fields[2] eq 'exon'; 
    print join("\t", @fields[0,3,4]), "\n"; 
} 

输出

PITG_00002 2 397 
PITG_00004 1 1275 
PITG_00004 1397 1969 
+0

谢谢大家对你的答案。这一个特别成功。我想知道,是否有办法在不同基因的外显子之间放置一个空间?这样PITG_00004将被组合在一起而没有多余的线,但是那么例如在PITG_00002和PITG_00004之间会有一个额外的线? – user1784467

1

行人:

(这是蟒蛇)

with open(data_file) as f: 
    for line in f: 
     tokens = line.split() 
     if len(tokens) > 3 and tokens[2] == 'exon': 
      print tokens[0], tokens[3], tokens[4] 

它打印

PITG_00002 2 397 
PITG_00004 1 1275 
PITG_00004 1397 1969 
2

在Unix中:

grep <file.gff3 " exon " | 
    sed "s/^\([^ ]+\) +[.] +exon +\([0-9]+\) \([0-9]+\).*$/\1 \2 \3/" 
0

这里是一个Perl脚本选项perl scriptName.pl file.gff3

use strict; 
use warnings; 

while (<>) { 
    print "@{ [ (split)[ 0, 3, 4 ] ] }\n" if /exon/; 
} 

输出:

PITG_00002 2 397 
PITG_00004 1 1275 
PITG_00004 1397 1969 

或者您也可以做到以下几点:

perl -n -e 'print "@{ [ (split)[ 0, 3, 4 ] ] }\n" if /exon/' file.gff3 

将数据保存到一个文件:

use strict; 
use warnings; 

open my $inFH, '<', 'file.gff3' or die $!; 
open my $outFH, '>>', 'data.txt' or die $!; 

while (<$inFH>) { 
    print $outFH "@{ [ (split)[ 0, 3, 4 ] ] }\n" if /exon/; 
} 
相关问题