2015-09-04 49 views
-1
#! /usr/bin/perl 
use strict; 

my (@data,$data,@data1,@diff,$diff,$tempS,$tempE, @ID,@Seq,@Start,@End, @data2); 
#my $file=<>; 
open(FILE, "< ./out.txt"); 
while (<FILE>){ 
     chomp $_; 
    #next if ($line =~/Measurement count:/ or $line =~/^\s+/) ; 
     #push @data, [split ("\t", $line)] ; 
    my @data = split('\t'); 


      push(@ID, $data[0]); 
      push(@Seq, $data[1]); 
      push(@Start, $data[2]); 
      push(@End, $data[3]);     

#  push @$data, [split ("\t", $line)] ; 

} 
close(FILE); 
my %hash = map { my $key = "$ID[$_]"; $key => [ $Start[$_], $End[$_] ] } (0..$#ID); 

for my $key ( %hash) { 
    print "Key: $key contains: "; 
    for my $value ($hash{$key}) { 
     print " $hash{$key}[0] "; 
    } 
    print "\n"; 
} 

for (my $j=0; $j <=$#Start ; $j++) 
{ 
    if ($Start[$j] > $End[$j]) 
    { 
     $tempS=$Start[$j]; 
     $Start[$j]=$End[$j]; 
     $End[$j]=$tempS; 
    } 
     print"$tempS\t$Start[$j]\t$End[$j]\n"; 

} 
my @sortStart = sort { $a <=> $b } @Start; 
my @sortEnd = sort { $a <=> $b } @End; 

#open(OUT,">>./trial.txt"); 
for(my $i=1521;$i>=0;$i--) 
{ 
    print "hey"; 
    my $diff = $sortStart[$i] - $sortStart[$i-1]; 
    print "$ID[$i]\t$diff\n"; 
} 

排序其他兩個陣列我有相同的長度的三個陣列,ID用的ID(字符串),StartEnd與整數值(從文件讀取)。ID跟蹤,同時交換和在Perl

我想遍歷所有這些數組,並且還想跟蹤ID。首先將Start中的元素與End對換,如果開始>結束,那麼我必須對這兩個數組進行排序以供進一步應用(因爲我對Start中的每個項目否定Start[0]-Start[1])。排序時,Id值可能會更改,並且由於我的ID對於每個Start和End元素都是唯一的,因此如何在排序時跟蹤我的ID?

三個陣列,IDStartEnd,都在我的考慮之下。

這是我的輸入數據的小塊:

DQ704383 191990066 191990037 
DQ698580 191911184 191911214 
DQ724878 191905507 191905532 
DQ715191 191822657 191822686 
DQ722467 191653368 191653339 
DQ707634 191622552 191622581 
DQ715636 191539187 191539157 
DQ692360 191388765 191388796 
DQ722377 191083572 191083599 
DQ697520 189463214 189463185 
DQ709562 187245165 187245192 
DQ540163 182491372 182491400 
DQ720940 180753033 180753060 
DQ707760 178340696 178340726 
DQ725442 178286164 178286134 
DQ711885 178250090 178250119 
DQ718075 171329314 171329344 
DQ705091 171062479 171062503 

上述ID,開始,結束分別。如果開始>結束,我只在這兩個數組之間交換它們。但交換後,降序可能會改變,但我希望它們的降序也是它們對應的否定ID,如上所述。

+2

請添加您的代碼。 – serenesat

+0

請忽略我的代碼中的哈希創建。 – Kanhu

+0

添加輸入數據和預期輸出會更有幫助。 – serenesat

回答

3

不要使用不同的數組,請使用散列來將相關的信息保存在一起。

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

use enum qw(START END); 

my %hash; 
while (<>) { 
    my ($id, $start, $end) = split; 
    $hash{$id} = [ $start < $end ? ($start, $end) 
           : ($end, $start) ]; 
} 

my @by_start = sort { $hash{$a}[START] <=> $hash{$b}[START] } keys %hash; 
my @by_end = sort { $hash{$a}[END] <=> $hash{$b}[END] } keys %hash; 

use Test::More; 
is_deeply(\@by_start, \@by_end, 'same'); 

done_testing(); 

此外,在您提供的數據示例中,無論您按什麼排序,id的順序都是相同的。

+0

謝謝你,我提供的數據集是按照開始和結束的降序排列的,在這裏你會發現一些開始大於結束,反之亦然。所以我只希望開始時比對應的最終值小,所以如果在開始>結束時換掉這兩個值,我的下一個數據處理步驟就完成了。就像這之後,我想要否定從之前的開始點開始的下一個數據,但是如果沒有對它們進行排序,那麼在某些點上只會得到負值。全過程也是如此。 – Kanhu

+0

@Kanhu:您是否注意到我的代碼在需要時會交換開始和結束? – choroba

+0

是的,我注意到這裏。但我不太清楚哈希中的排序過程。好像它會照顧我進一步加工的目的!我正在嘗試這個,會在這裏更新。非常感謝你。 :-) – Kanhu