奋斗的青春分享 http://blog.sciencenet.cn/u/liufuyan 信息分析 脚本撰写 实验技术 计算机语言

博文

提取fasta特定位置的序列脚本

已有 6174 次阅读 2015-4-11 12:40 |个人分类:perl脚本|系统分类:科研笔记| 位置, fasta序列提取

将下列代码存入文件get_position_seq.pl,然后可以使用perl get_position_seq.pl -h获得帮助!

#! /usr/bin/perl

use Bio::SeqIO;

use Getopt::Long;

GetOptions(

        'Position|P=s'           => $inputfile1,

        'Seq|s=s'               => \$inputfile2,

        'O=s'                   => \$inputfile3,

        'help|h!'              => \$help,


     

);

my @good;

my $h=0;$k=0;

if($help){

print "###################################################################\n";

print "usage perl get_position_seq.pl -p position.file -s seq.file -o getseq.file\n";

print "######input position file is the format:\n" ;

print "######gene_ID    start(0 as start)    get_length\n";

print "Options:\n";

print "    -p:the input position file\n";

print "    -s:the fasta sequence file\n";

print "    -o:the output sequence file\n";



}else{

open (IN1, "<$inputfile1")|| die "cannot open $inputfile:$!";

open (OUT, ">$inputfile3")|| die "cannot open $inputfile:$!";


while (<IN1>){

@tmp=split;

       push @position, [@tmp];


}

my $ina = Bio::SeqIO->new(-file => $inputfile2, -format => 'fasta');

while(my $obj = $ina->next_seq()){

         my $id = $obj->id;

         my $seq = $obj->seq;

  $xulie[$k++]="$id $seq\n";

}

foreach(@xulie){

@tmp=split;

push @Mat, [@tmp];


}

for($i=0;$i<=($#position);$i++){

 for($j=0;$j<=($#Mat);$j++){

   if($position[$i][0] eq $Mat[$j][0]){

        $good[$h][0]=">$position[$i][0]";

      $good[$h][1]=substr($Mat[$j][1],$position[$i][1],$position[$i][2]);

print OUT "$good[$h][0]\n$good[$h][1]\n";

      $h++;

last;

}

}

}


close IN1;

close OUT;

}




https://blog.sciencenet.cn/blog-2521743-881546.html


下一篇:mysql数据表的导入————生信相关的
收藏 IP: 113.140.84.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-5-17 12:56

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部