目录

脚本用途

该脚本主要用于解决一类相同的问题,例如以基因ID为中介将转录分析结果中的GO数据库富集的注释信息抓取到gene_fpkm表格中,方便老师同时查看基因的丰度信息及功能注释结果,以此类推,此类结果整理的需求都可以使用下面的脚本实现。

脚本代码

#!/usr/bin/perl
use strict;
use warnings;

# 检查参数是否正确
if (@ARGV != 3) {
    die "用法: $0 <input1> <input2> <output>\n";
}

# 获取输入参数
my $input1 = $ARGV[0];
my $input2 = $ARGV[1];
my $output = $ARGV[2];

# 打开输入文件A和文件B
open my $fileA, '<', $input1 or die "无法打开文件$input1: $!";
open my $fileB, '<', $input2 or die "无法打开文件$input2: $!";

my @paths = split /\//,$input1;
my $name = pop @paths;

open my $outfile, '>', $output."/".$name.".new" or die "无法打开文件$output: $!";
#open my $outfile, '>', $output or die "无法打开文件$output: $!";
# 读取文件B的键值对
my %dataB;
while (<$fileB>) {
    chomp;
    my @fields = split /\t/;
     $dataB{$fields[0]} = join("\t",@fields[1..$#fields]);
}

# 处理文件A的每一行,将"gene_name"添加到"ANNOTATION"列之后
while (my $line = <$fileA>) {
    chomp $line;
    my @fields = split /\t/,$line;
    my $anno = $fields[0];
    if(exists $dataB{$anno}){
        $line .= "\t";
        $line .= $dataB{$anno};
    }
    print $outfile "$line\n";

}

# 关闭文件句柄
close $fileA;
close $fileB;
close $outfile;

print "处理完成,结果已保存到". $output."/".$name.".new\n";

使用方法

perl anno.pl input1 input2