【发布时间】:2018-05-17 05:20:35
【问题描述】:
我需要用另一个文件中的信息/正则表达式替换一个文件中的变量。例如,我需要从 file1 的每一行中获取第一个字符串/变量(以 MSTRG 开头):
MSTRG.5734 MSTRG.5734 509 -4 0 -14 0 0
MSTRG.19266 MSTRG.19266 842 -4 0 -12 0 0
MSTRG.26588 MSTRG.26588 196 5 0 12 0 0
并使用它在 file2 中进行搜索,看起来像这样:
Chr1 StringTie transcript 24039360 24041181 1000 - . gene_id "MSTRG.5734"; transcript_id "MSTRG.5734.1";
Chr1 StringTie transcript 24039810 24040595 1000 - . gene_id "MSTRG.5734"; transcript_id "Transcript:AT1G64700.1"; ref_gene_id "Gene:AT1G64700"
Chr1 StringTie exon 24040560 24041181 1000 - . gene_id "MSTRG.19266"; transcript_id "MSTRG.19266.1"; exon_number "2";
Chr1 StringTie exon 24040560 24041181 1000 - . gene_id "MSTRG.26588"; transcript_id "MSTRG.26588.1"; exon_number "2";
Chr1 StringTie transcript 24039810 24040595 1000 - . gene_id "MSTRG.26588"; transcript_id "Transcript:AT5G41000.1"; ref_gene_id "Gene:AT5G41000";
理想情况下,例如MSTRG.5734 位于 file2 的一行中,该行还包含字符串,例如Gene:AT1G64700,它将获取信息Gene:AT1G64700并替换file1中的MSTRG.5734。因此,file1 中每一行上的每个MSTRG 都是唯一的,理论上它将匹配 file2 中的唯一基因。如果它与基因不匹配,那么我需要维护 file1 中的原始行。
File1 输出应如下所示:
Gene:AT1G64700 MSTRG.5734 509 -4 0 -14 0 0
MSTRG.19266 MSTRG.19266 842 -4 0 -12 0 0
Gene:AT5G41000 MSTRG.26588 196 5 0 12 0 0
我当前的 perl 代码是:
use strict;
use warnings;
use vars qw($outfile @id $mstrg $gene);
open(SEARCH, $ARGV[0]) or die "Couldn't open $ARGV[0]: $!";
open(FILE, $ARGV[1]) or die "Couldn't open $ARGV[1]: $!";
$outfile = "testout.txt";
open (OUT, ">$outfile") || die "Can't open $outfile for creation: $!\n";
my %mstrg;
while (<SEARCH>) {
chomp;
if (/^MSTRG/) {
chomp $_;
@id = split (/\t/, $_);
$mstrg{$id[1]}.="$id[1]";
}
}
while (<FILE>) # {@ffn=<FILE>};
{
chomp ($gene=$_);
if ($mstrg =~ /$gene/) {
print OUT "$id[1]\t$id[2]";}
else {
#print OUT "$_\n";
}
next;
}
close FILE;
不幸的是,这就是我卡住的地方,不知道如何继续? 感谢您为之前的帖子提供的任何帮助、感谢和道歉,我没有向看到的人提供任何代码。
【问题讨论】: