perl Fasta文件格式化-每行固定数目碱基输出

2017-05-29 19:54:45来源:CSDN作者:niuhuihui_fei人点击

编程要求如下:

1) 程序采用getopt从命令行输入,参数共有4个,-infile,-outfile,-width,-h;

2) 其中-infile用于接收输入的FASTA文件名;

3) -outfile用于格式化的FASTA结果文件名;

4) –width用于接收每一行输出多少个碱基,要求只能从10到200,否则就报错;

5) -h用于给出程序的使用说明;


命令行命令:

perl   thir_test.pl   -infile   in.fa   -outfile   out.fa  -width  20


脚本 thir_test.pl

#!/usr/bin/perl -w
use strict;
use Getopt::Long;
my ($chr,$help);
my $width="0";
my $infile="";
my $outfile="";

GetOptions(
                "help|h"     => /&USAGE,
                "infile:s"   => /$infile,
                "outfile:s"  => /$outfile,
                "width=i"    => /$width,
);

die "please give me a number between 10 and 200/n" unless 10<= $width and $width <=200;
open IN,"<$infile"||die;
open OUT,">$outfile";


$/=">";<IN>;$/="/n";
while(<IN>){
    my $chr=$1 if /^(/S+)/;
    $/=">";
    chomp(my $seq=<IN>);
    $/="/n";
    $seq=~s//n+//g;
    print OUT ">$chr/n";
    my $start=0;
    my $len=length($seq);
    while($start+$width<$len){
    $b=substr($seq,$start,$width);
    print OUT "$b/n";
    $start=$start+$width;}
    while($start+$width>=$len){
    $b=substr($seq,$start,$width);
    print OUT "$b/n";
    last;}
}
sub USAGE{
                my $usage=<<"USAGE";

------------------------------------------------------------------------------------
       Program: thir_test.pl
       Date:2017-05-19
       Usage:
                 -infile    <fasta format> infile
                 -outfile   <fasta format> outfile
                 -width     <int>  seq length
                 -h          help


       Example1: perl thir_test.pl -infile data.fa -outfile out.fa -width 10
       Example2: perl thir_test.pl -h
------------------------------------------------------------------------------------
USAGE
        print $usage;
        exit;
}


最新文章

123

最新摄影

闪念基因

微信扫一扫

第七城市微信公众平台