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+2010140=2012203, ) 和 $5 列 (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

相关内容