perl-get 輸入,檢查條件

perl-get 輸入,檢查條件

貓輸入1.txt

##gff-version 2
##source-version geneious 5.6.4
Xm_ABL1 Geneious        CDS     1       168     .       +       .       Name=Xm_ABL1;created by=User;modified by=User;ID=w0IVHutPuN4H4FVDCg4sFVRaJjQ.1340919460469.4
Xm_ABL1 Geneious        CDS     169     334     .       +       .       Name=Xm_ABL1;created by=User;modified by=User;ID=w0IVHutPuN4H4FVDCg4sFVRaJjQ.1340919460469.4
Xm_ABL1 Geneious        CDS     335     628     .       +       .       Name=Xm_ABL1;created by=User;modified by=User;ID=w0IVHutPuN4H4FVDCg4sFVRaJjQ.1340919460469.4
Xm_ABL1 Geneious        CDS     629     901     .       +       .       Name=Xm_ABL1;created by=User;modified by=User;ID=w0IVHutPuN4H4FVDCg4sFVRaJjQ.1340919460469.4
Xm_ABL1 Geneious        CDS     902     985     .       +       .       Name=Xm_ABL1;created by=User;modified by=User;ID=w0IVHutPuN4H4FVDCg4sFVRaJjQ.1340919460469.4
Xm_ABL1 Geneious        CDS     986     1165    .       +       .       Name=Xm_ABL1;created by=User;modified by=User;ID=w0IVHutPuN4H4FVDCg4sFVRaJjQ.1340919460469.4
Xm_ABL1 Geneious        CDS     1166    1350    .       +       .       Name=Xm_ABL1;created by=User;modified by=User;ID=w0IVHutPuN4H4FVDCg4sFVRaJjQ.1340919460469.4
Xm_ABL1 Geneious        CDS     1351    1504    .       +       .       Name=Xm_ABL1;created by=User;modified by=User;ID=w0IVHutPuN4H4FVDCg4sFVRaJjQ.1340919460469.4
Xm_ABL1 Geneious        BLAST Hit       169     334     .       +       .       
Xm_ABL1 Geneious        extracted region        1       168     .       +       .       Name=Extracted region from gi|371443098|gb|JH556762.1|;Extracted interval="351297 -> 351464"
Xm_ABL1 Geneious        extracted region        169     334     .       +       .       Name=Extracted region from gi|371443098|gb|JH556762.1|;Extracted interval="371785 -> 371950"
Xm_ABL1 Geneious        extracted region        335     628     .       +       .       Name=Extracted region from gi|371443098|gb|JH556762.1|;Extracted interval="372554 -> 372847"
Xm_ABL1 Geneious        extracted region        629     901     .       +       .       Name=Extracted region from gi|371443098|gb|JH556762.1|;Extracted interval="374760 -> 375032"
Xm_ABL1 Geneious        extracted region        902     985     .       +       .       Name=Extracted region from gi|371443098|gb|JH556762.1|;Extracted interval="375230 -> 375313"
Xm_ABL1 Geneious        extracted region        986     1165    .       +       .       Name=Extracted region from gi|371443098|gb|JH556762.1|;Extracted interval="375992 -> 376171"
Xm_ABL1 Geneious        extracted region        1166    1350    .       +       .       Name=Extracted region from gi|371443098|gb|JH556762.1|;Extracted interval="376575 -> 376759"
Xm_ABL1 Geneious        extracted region        1351    1504    .       +       .       Name=Extracted region from gi|371443098|gb|JH556762.1|;Extracted interval="376914 -> 377067"

檢查 input1.txt,列 CDS 和提取的區域具有相同的行數。如果它們相同,則從提取的區域$14 列(351297、351464、371785、371950)中獲取值,並將這些值替換為CDS 行$4 和$5 中的值(例如,在CDS 第1 行1 中替換為351297,將168 替換為351464,將169 替換為371785 、334 和 371950 等等)。列印唯一被替換的 CDS 行,如下圖所示

貓輸出1.txt

##gff-version 2
##source-version geneious 5.6.4
Xm_ABL1 Geneious        CDS     351297     351464     .       +       .       Name=Xm_ABL1;created by=User;modified by=User;ID=w0IVHutPuN4H4FVDCg4sFVRaJjQ.1340919460469.4
Xm_ABL1 Geneious        CDS     371785     371950     .       +       .       Name=Xm_ABL1;created by=User;modified by=User;ID=w0IVHutPuN4H4FVDCg4sFVRaJjQ.1340919460469.4
Xm_ABL1 Geneious        CDS     372554     372847     .       +       .       Name=Xm_ABL1;created by=User;modified by=User;ID=w0IVHutPuN4H4FVDCg4sFVRaJjQ.1340919460469.4
Xm_ABL1 Geneious        CDS     374760     375032     .       +       .       Name=Xm_ABL1;created by=User;modified by=User;ID=w0IVHutPuN4H4FVDCg4sFVRaJjQ.1340919460469.4
Xm_ABL1 Geneious        CDS     375230     375313     .       +       .       Name=Xm_ABL1;created by=User;modified by=User;ID=w0IVHutPuN4H4FVDCg4sFVRaJjQ.1340919460469.4
Xm_ABL1 Geneious        CDS     375992     376171    .       +       .       Name=Xm_ABL1;created by=User;modified by=User;ID=w0IVHutPuN4H4FVDCg4sFVRaJjQ.1340919460469.4
Xm_ABL1 Geneious        CDS     376575     376759    .       +       .       Name=Xm_ABL1;created by=User;modified by=User;ID=w0IVHutPuN4H4FVDCg4sFVRaJjQ.1340919460469.4
Xm_ABL1 Geneious        CDS     376914     377067    .       +       .       Name=Xm_ABL1;created by=User;modified by=User;ID=w0IVHutPuN4H4FVDCg4sFVRaJjQ.1340919460469.4

我有另一個輸入檔 input2.txt

貓輸入2.txt

##gff-version 2
##source-version geneious 5.6.3
gi371443188gbJH5566721_extraction_reversed      Geneious        CDS     1043    1132    .       +       .       Name=Xm ITGB3;created by=User;modified by=User;ID=Pa0FVoXpt/GgL1I/VO7LY0UlFAc.1341246976743.1
gi371443188gbJH5566721_extraction_reversed      Geneious        CDS     2063    2260    .       +       .       Name=Xm ITGB3;created by=User;modified by=User;ID=Pa0FVoXpt/GgL1I/VO7LY0UlFAc.1341246976743.1
gi371443188gbJH5566721_extraction_reversed      Geneious        CDS     2336    2593    .       +       .       Name=Xm ITGB3;created by=User;modified by=User;ID=Pa0FVoXpt/GgL1I/VO7LY0UlFAc.1341246976743.1
gi371443188gbJH5566721_extraction_reversed      Geneious        CDS     3474    3633    .       +       .       Name=Xm ITGB3;created by=User;modified by=User;ID=Pa0FVoXpt/GgL1I/VO7LY0UlFAc.1341246976743.1
gi371443188gbJH5566721_extraction_reversed      Geneious        extracted region        1       13933   .       +       .       Name=Extracted region from gi|371443188|gb|JH556672.1|;Extracted interval="2010140 <- 2024072"

我想將最後一行的 $14 (interval="1960862) 僅數字 (2010140) 加到 $4 列 (1043,1132,2063..3633) 即 (1043+2010140=2011183, 2063,+20630列(1132+2010140=2011272,2260+2010140=2012400 ),忽略最後一行。

輸出應如下所示:

貓輸出2.txt

##gff-version 2
##source-version geneious 5.6.3
gi371443188gbJH5566721_extraction_reversed      Geneious        CDS     2011183    2011272    .       +       .       Name=Xm ITGB3;created by=User;modified by=User;ID=Pa0FVoXpt/GgL1I/VO7LY0UlFAc.1341246976743.1
gi371443188gbJH5566721_extraction_reversed      Geneious        CDS     2012203    2012400    .       +       .       Name=Xm ITGB3;created by=User;modified by=User;ID=Pa0FVoXpt/GgL1I/VO7LY0UlFAc.1341246976743.1
gi371443188gbJH5566721_extraction_reversed      Geneious        CDS     2012476    2012733    .       +       .       Name=Xm ITGB3;created by=User;modified by=User;ID=Pa0FVoXpt/GgL1I/VO7LY0UlFAc.1341246976743.1
gi371443188gbJH5566721_extraction_reversed      Geneious        CDS     2013614    2013773    .       +       .       Name=Xm ITGB3;created by=User;modified by=User;ID=Pa0FVoXpt/GgL1I/VO7LY0UlFAc.1341246976743.1

但我需要在一個perl腳本中,根據使用者輸入(可能是input1.txt或input2.txt)檢查條件給出output1.txt或output2.txt

答案1

我假設提取的區域線遵循每個比對的 CDS 線。

將此程式碼複製到 script.pl 中:

use strict;
use warnings;

my $input = 1;
my @field = ('CDS','extracted region');
my (%data);
my (%counter);
&zero;

while ( <> ) { 

    ## Omit header.
    next if $. == 1;
    next if $. == 2;

    ## Remove last '\n'.
    chomp;

    ## Split line in tabs.
    my @f = split /\t/;

    ## Is loop over?
    if ( $f[2] =~ /$field[0]/ && $counter{$field[1]} > 1 )
    {
    &comparing;
    &zero;
    }

    ## Count number of $field[0] and $field[1] line
    $counter{$f[2]}++;

    ## Storing data
    @{$data{$f[2]}[$counter{$f[2]}]} = @f;
}

&comparing;

sub zero {
    $data{$field[0]} = [];
    $data{$field[1]} = [];
    $counter{$field[0]} = 0;
    $counter{$field[1]} = 0;
}

sub comparing {
    ## Is same line ($field[0] and $field[1])? if ( $input == 1 )
    if ( $counter{$field[0]} ==  $counter{$field[1]} || $input == 2 )
    {
    &recover;
    &stamp;
    }
}

sub recover {
    my $pos = &input2(0,0)  if ( $input == 2 );
    for my $i ( 1 .. $#{ $data{$field[0]} } ) {
    &input1($i) if ( $input == 1 );
    &input2($i,$pos) if ( $input == 2 );
    }
}

sub input1 {
    #;Extracted interval="376914 -> 377067"
    $data{$field[1]}[$_[0]][8] =~ m/;Extracted interval="(\d+) /;
    $data{$field[0]}[$_[0]][3] = $1;
    $data{$field[1]}[$_[0]][8] =~ m/;Extracted interval="\d+ -> (\d+)"/;
    $data{$field[0]}[$_[0]][4] = $1;
}

sub input2 {
    if ( $_[0] == 0 )
    {
    #;Extracted interval="2010140 <- 2024072"
    $data{$field[1]}[1][8] =~ m/;Extracted interval="(\d+) /;
    $1;
    }
    else
    {
    $data{$field[0]}[$_[0]][3] = $_[1] + $data{$field[0]}[$_[0]][3];
    $data{$field[0]}[$_[0]][4] = $_[1] + $data{$field[0]}[$_[0]][4];
    }
}

sub stamp {
    for my $i ( 1 .. $#{ $data{$field[0]} } ) {
    for my $j ( 0 .. $#{ $data{$field[0]}[$i] } ) {
        print "$data{$field[0]}[$i][$j]\t";
    }
    print "\n";
    }
}

您可以使用 input1.txt 執行 perl 腳本:

perl script.pl input1.txt > output1.txt

如果你修改行:

my $input = 1;

my $input = 2;

你可以使用 input2.txt 來執行 perl 腳本:

perl script.pl input2.txt > output2.txt

Perl 腳本還可以取得兩個參數:輸入檔案和類型 [12]。

編輯

https://stackoverflow.com/questions/1730333/how-do-i-use-getoptions-to-get-the-default-argument有一些方法可以獲得參數。

如果修改行:

my $input = 1;

my $input = 1;
$input = $ARGV[1] if defined $ARGV[1];

你可以使用 input1.txt 來執行 perl 腳本:

perl script.pl input1.txt > output1.txt

或者

perl script.pl input1.txt 1 > output1.txt

您可以使用 input2.txt 執行 perl 腳本:

perl script.pl input2.txt 2 > output2.txt

相關內容