2015-09-20 176 views
-2

我需要比較以下三個文本文件。我有一個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; 
} 
+1

這將有助於解釋你需要比較什麼。 –

回答

0

我想你應該提到你期望從比較中得到什麼樣的輸出。如果不知道這一點,很難提供解決方案。不過我會提供一個通用的解決方案。

你可以做什麼它比較文件單獨。像比較1和2;然後是1和3,然後是2和3.這將根據每次比較給出三種不同的輸出。我相信你會需要所有各種比較輸出,因爲這是獨特的數據。但是,如果您需要合併數據,那麼你需要添加更多的代碼,你可以操縱這些數據。

即使您決定比較一個函數中的所有內容,那麼操作次數也會相同。您需要爲每個文件的每一行分別進行比較。更好地重複使用此代碼分別進行比較

+0

我已經比較了1到2,1到3和2到3,但最後我需要知道所有三個之間的所有常見結果。 –

+0

我改進了我的輸入文件。作爲輸出,我關心的是列[1],[2],[3],輸出文件應該包含列的重疊數[2],[3],如上面腳本中提到的兩個文件比較.... –

+0

能否詳細說明多一點,因爲我無法達到最終目標。你想要重疊文件1,2和3;你想要更多東西。如果你只想要3個文件的重疊,那麼你可以比較1和2;然後從類似於您的輸入文件的比較中生成並輸出文件。最後將它與文件3進行比較。這應該給你重疊 – Sharad