2016-12-05 52 views
-1

我試圖通過啓動子程序將幾個腳本合併到1個腳本中。問題在於我無法將輸入從1個下標指向另一個下標。這需要爲多個腳本完成。這些是他們整個列表中的前兩個腳本。使用子程序perl將腳本組合成1個腳本

代碼1生成的數據需要提交給代碼2等。但是在代碼2中,還有一個將生成的文件與原始文件進行比較的額外步驟。

Code 1: 
subst_head_1($infile); 


sub subst_head_1 
{ 
    ##this code helps organise the file in a way that it makes it more convenient for the file to be pushed into a hash for later analysis 

    ##opening file 
    my $i = $_[0]; 

    open(IN, "<$i") || die "\n Error: Cannot open the infile: $infile\n"; 
    # open(OUT, ">op.fa"); 

    ##giving all the headers in the original file line numbers 
    my $lineno = 1; 

    while(<IN>) 
    { 
    chomp; 
    if ($_ =~ />/) 
    { 

     $_ = $lineno++,"\t", $_ ,"\n"; 

     subst_head_2($_); 

    } 
    } 

} 
    ##file organised in the following format; eg., "2>CBB_deg7180000000601_1100_2101_3" 

sub subst_head_2 
{ 

    ##opening files with header information(result of head-subs-1) and the original sequence(submitted query file) file for further info 
    my $i = $_[0]; 
    #print $i; 
    my $i_1 = $_[1]; 


    ##pushing file(headerinfo.txt) with the header information into a hash 
    open(IN, "<$i"); 
    my @file = <IN>; 

    my $file2 = join('', @file); 

    my %hash = split(/[\t\n]/, $file2); 

    ##opening the original file with the sequence information into an array 
    open(IN1, "<$i_1"); 
    my @fila = <IN1>; 


    ##foreach of the sequnces in the sequence file 
    foreach my $fila(@fila) 
    { 

    ##Substituting any "*" in the file, if any, especially at the end of some of sequnces which were present in the file 

    $fila =~ s/\*//g; 

    ##regex for matching with the header information in the file with all the query information 

    if($fila =~ /^\>(\S+).*/) 
    { 

     ##putting info(eg., CBB_deg7180000000601_1100_2101_3) into a variable $user 

     my $user = $1; 

     foreach my $has(sort keys %hash) 
     { 

     ##regex for the values in the key-value relationship in the headerinfo file 
     if($hash{$has} =~ /^\>(\S+).*/) 
     { 

      ##putting info(eg., CBB_deg7180000000601_1100_2101_3) into a variable $user1 
      my $user1 = $1; 

      ##is the info the same?; if it is, then substitute it in the original with key from headerinfo.txt 

      if($user eq $user1) 
      { 

      ##substitute header in the original file with the unique number; 

      $fila =~ s/^\>(\S+).*\n/>$has\n/; 

      } 
     } 
     } 
    } 
    } 

    print @fila; 
} 
+0

全局變量呢?或者將結果從一個子傳遞給另一個? – Robert

+0

嗨,這是問題..我無法弄清楚如何將結果從一個子節點傳遞到另一個子節點。 –

+0

你有沒有試過'''subst_head_2(subst_head_1(file))''' – tomc

回答

0

我的答案將解釋我在代碼中看到的最直接的問題。

代替此行

$_ = $lineno++,"\t", $_ ,"\n"; 

你可能有這個你改變程序之前的。

print $lineno++,"\t", $_ ,"\n"; 

所有你所做的是改變的賦值,把所有這一切都爲$_。但是該變量是標量。這意味着它是一個單一的價值。它不能列表。您的任務將=右側的第一件物品放入$_。這是$lineno++的結果,即$lineno$var++增量,但返回舊值)。其餘的都被丟棄。

所以現在你的電話subst_head_2($_)只有行號。但是在子程序中你需要兩個參數。

my $i = $_[0]; 
my $i_1 = $_[1]; 

第二個,$i_1(這是一個可怕的一個變量)是undef,所以你不能把它作爲一個文件名來打開一個文件。

但不幸的是,您的描述缺乏大量信息,所以我無法告訴您您實際需要做什麼。請提供樣本輸入數據和輸出數據,並考慮您希望代碼執行的操作。