我需要比較以下三個文本文件。我有一個Perl腳本來比較兩個文件,我如何修改下面的腳本來比較三個示例文本文件。比較三個文本文件
文件1: AT4G01510.1 1 6993 7241 AT1G01020.2 1 7320 8668 AT1G01050.1 1 31388 32672 AT1G01060.1 1 33997 36923 AT1G01070.1 1 38904 40879
文件2: AT2G40310.1 1 16223981 16225810 AT1G02980.1 1 16232948 16236108 AT1G50180.1 1 16271231 16272957 AT1G43722.1 1 16355439 16355906 AT3G32904.1 1 16357277 16358327 AT2G34120.1 1 16457818 16457919 AT2G34110.1 1 16459685 16459879 AT5G36228.1 1 16472204 16473265 AT3G30520.1 1 16494448 16496240
文件3
AT1G01010.1 1 3760 5627 AT1G01020.1 1 6918 7232 AT1G01020.1 1 8236 8666 AT1G01030.1 1 11867 12940 AT1G01040.1 1 23519 30817 AT1G01050.1 1 31385 32670 AT1G01060.1 1 33995 35993 AT1G01060.1 1 36811 36921 AT1G01070.1 1 38901 40877 AT1G01090.1 1 47708 49166 的Perl腳本用於比較兩個文件:
#!/usr/bin/perl -w
use strict;
use Getopt::Std;
use vars qw ($opt_s $opt_t);
getopts ('s:t:');
if(! $opt_s || !$opt_t){
print "Usage: $0\n";
print "-s file1 converted output file \n";
print "-t file2 converted output file \n";
exit;
}
my $tablefile1 = $opt_s;
my $tablefile2 = $opt_t;
if(!$tablefile1){
die "Invalid data file name $tablefile1.\n";
}
if(!$tablefile2){
die "Invalid data file name $tablefile2.\n";
}
open(IN,$tablefile1) || die "Can't open $tablefile1..exiting.\n";
my %hash =();
my @coord_arr =();
my @chrs =();
while(<IN>){
chomp;
next if (/^\s*$/);
my @cols = split(/\t/, $_);
my $id = $cols[0];
my $chr = $cols[1];
my $s = $cols[2];
my $e = $cols[3];
if (exists($hash{$chr})) {
my $str = $hash{$chr};
$hash{$chr} = "$str/$id:$s:$e";
} else {
$hash{$chr} = "$id:$s:$e";
}
}
close IN;
open(IN,$tablefile2) || die "Can't open $tablefile2..exiting.\n";
my $found = 0;
print "Chr\tfile1-ID\tStart\tEnd\tfile2-ID\tStart\tEnd\tStart diff\tEnd diff\n";
while(<IN>){
chomp;
next if (/^\s*$/);
my @cols = split(/\t/, $_);
my $id = $cols[0];
my $chr = $cols[1];
my $s = $cols[2];
my $e = $cols[3];
my $info_str = $hash{$chr};
next if (!$info_str);
my @pseudos = split(/\//, $info_str);
for (my $i = 0; $i < @pseudos; $i++) {
my ($id1, $s1, $e1) = split(/:/, $pseudos[$i]);
if (&is_overlap($s, $e, $s1, $e1)) {
print "$chr\t$id\t$s\t$e\t$id1\t$s1\t$e1\t";
my $d1 = $s1-$s;
my $d2 = $e1-$e;
print "$d1\t$d2\n";
}
}
}
close IN;
sub is_overlap {
my ($s, $e, $s1, $e1) = @_;
my $found = 0;
if ($s >= $s1 && $s < $e1 ||
$s1 >= $s && $s1 < $e ||
$s1 >= $s && $e1 <= $e ||
$s1 <= $s && $e1 >= $e) {
$found = 1;
}
return $found;
}
這將有助於解釋你需要比較什麼。 –