独钓寒江分享 http://blog.sciencenet.cn/u/yuyin110 当霓虹灯不再转动时 我提着空瓶 请自己喝酒

博文

perl提取序列信息写入Excel表

已有 9276 次阅读 2011-8-30 21:22 |个人分类:程序设计|系统分类:科研笔记

    perl一个强大的地方,就是有很多别人写好的模块,可以直接搬过来用,就好像提供了很多现在的工具,找到合适的工具就能解决相应的问题,这里用了一个很强大的专门操作Excel的模块Win32::OLE,可以方便的用perl读写Excel数据,用perl提取genebank格式的序列文件中mRNA信息,然后将筛选结果输出到Excel中,为以后数据库的建立打下基础,程序运行结果如下(有些序列中没有gene_name,所以为空的):

程序如下:
#!/usr/bin/perl
use strict;
use warnings;
use Win32::OLE;#这个模块可以对Excel进行读写操作
my $dir='F:/sequence/';
###########################################################################################
#####中的内容基本为昨天提取序列信息的程序
open F,"$dir".'a20001-22000.gb';#此文件中装有二千条拟南芥genebank格式的基因序列
my @a=<F>;
my $i=0;
my $temp;
my $temp1;
my $temp2;
my $temp3;
my $genename;
my $locus_tag_name;
my @b;
my @accession;
my @mRNA_length;
my @description;
my @CDS_length;
my @product;
my @protein_id;
my @AA_seq;
my @CDS_seq;
my @mRNA_seq;
my @gene_name;
my @tag_name;
my @Gene_id;
my $start;
my $gene_id;
while($i<=$#a)
{
if($a[$i]=~/^LOCUS(\s){7}/)
{
$a[$i]=~s/.*NM_(\d)+//;
$a[$i]=~s/(\s)+//g;
$a[$i]=~s/bp.*//s;
push @mRNA_length,$a[$i];$i++;
}elsif($a[$i]=~/DEFINITION(\s)+/)
{
$a[$i]=~s/DEFINITION  //;
$a[$i]=~s/\n//;
$temp1=$a[$i];$i++;
unless($a[$i]=~/ACCESSION(\s)+/)
{
$a[$i]=~s/(\s){11}//;
$a[$i]=~s/(\s)+$//;
$a[$i]=~s/\n//;
$temp2=$a[$i];
     $temp1=$temp1.$temp2;
    push @description,$temp1;$i++;
}else
{
push @description,$temp1;$i++;
}
$temp1='';$temp2='';
}
elsif($a[$i]=~/VERSION(\s){5}/)
{
$a[$i]=~s/VERSION//;
$a[$i]=~s/GI.*//s;
       $a[$i]=~s/(\s)+//g;
push @accession,$a[$i];$i++;
}
 elsif($a[$i]=~/\/gene=\"/)
  {
 $a[$i]=~s/.*=//;
  $a[$i]=~s/\"//g;
  $a[$i]=~s/(\s)+//g;
  $genename=$a[$i];$i++;
     }elsif($a[$i]=~/\/locus_tag=/)
{
        $a[$i]=~s/.*=//;
  $a[$i]=~s/\"//g;
  $a[$i]=~s/(\s)+//g;
  $locus_tag_name=$a[$i];$i++;
}elsif($a[$i]=~/\/db_xref=\"GeneID:/)
{
$a[$i]=~s/\/db_xref=\"GeneID://;
$a[$i]=~s/\"//g;
$a[$i]=~s/(\s)+//g;
$gene_id=$a[$i];$i++;
}
elsif($a[$i]=~/^(\s)+CDS(\s)+(\d)+/)
{
  $a[$i]=~s/CDS//;
         $a[$i]=~s/(\s)+//g;      
        $a[$i]=~s/\n//;
  @b=split/\.\./,$a[$i];
  $temp=int($b[1])-int($b[0])+1;
  $start=int($b[0]);
      push @CDS_length,$temp;
  @b=();$i++;
}
elsif($a[$i]=~/\/product=\"/)
 {
$a[$i]=~s/\/product=//;
$a[$i]=~s/\"//g;
$a[$i]=~s/^(\s)+//;
$a[$i]=~s/(\s)+$//;
$a[$i]=~s/\n//;
$temp1=$a[$i];$i++;
while(!($a[$i]=~/\/protein_id=/))
{
$a[$i]=~s/^(\s){21}//;
$a[$i]=~s/(\s)+$//;
$a[$i]=~s/\n//;
$temp2=$a[$i];
   $temp1=$temp1.$temp2;
  $i++;
}
push @product,$temp1;
         
$a[$i]=~s/\/protein_id=//;
      $a[$i]=~s/\"//g;
    $a[$i]=~s/(\s)+//g;
    $a[$i]=~s/\n//;
push @protein_id,$a[$i];$i++;
    $temp1='';$temp2='';
 }
 elsif($a[$i]=~/\/translation=/)
{
        $a[$i]=~s/\/translation=//;
$a[$i]=~s/\"//g;
$a[$i]=~s/^(\s)+//;
$a[$i]=~s/(\s)+$//;
$a[$i]=~s/\n//;
$temp1=$a[$i];$i++;
while(!($a[$i]=~/ORIGIN/))
{
$a[$i]=~s/(\s)+//g;
$a[$i]=~s/\n//;
$a[$i]=~s/\"//;
$temp2=$a[$i];
   $temp1=$temp1.$temp2;
   $i++;
}
push @AA_seq,$temp1;     
      $temp1='';$temp2='';
}
elsif($a[$i]=~/^ORIGIN(\s){6}/)
{
$i++;
    while(!($a[$i]=~/\/\//))
{
$a[$i]=~s/(\s)+//g;
$a[$i]=~s/(\d)+//g;
$a[$i]=~s/\n//;
   $temp2=$a[$i];
   $temp1=$temp1.$temp2;
   $i++;
}
$temp3=substr($temp1,$start-1,$temp);
push @CDS_seq,$temp3;
push @mRNA_seq,$temp1;
$temp1='';$temp2='';
 }
elsif($a[$i]=~/\/\//)
{
push @gene_name,$genename;
push @tag_name,$locus_tag_name;
push @Gene_id,$gene_id;
$genename='';$locus_tag_name='';$gene_id='';       
$i++;
}
else
{
$a[$i]=~s/.*//sg;$i++;
}
}
#################################################################################
#将结果输入到Excel表中
my $app_xls=Win32::OLE->new('Excel.Application',sub{$_[0]->Quit}) or die "Excel初始化失败,你可能没有安装Excel!";
$app_xls->{DisplayAlerts}='False';
my $book=$app_xls->WorkBooks->Add;
$book->SaveAs($dir.'A20001-22000.xls')or die "Save failer";#新建一个Excel表
undef $book;
my $src_book=$app_xls->WorkBooks->Open($dir.'A20001-22000.xls');
my $src_sheet=$src_book->Worksheets(1);#当前工作薄为sheet1
 
 
#输入表头信息
$src_sheet->Range("A1:L1")->{Value}=['Accession','mRNA_length','Gene_id','Gene_name','Locus_tag','CDS_length','Protein_id','Description','Product','CDS_seq','AA_seq','mRNA_seq'];
 
 
my $n=2;#因为第一行输入了表头,所以从第二行开始输出数据
for($i=0;$i<=$#accession;$i++)
{
$src_sheet->Cells($n,1)->{Value}=$accession[$i];#在第一列输出accession信息
$src_sheet->Cells($n,2)->{Value}=$mRNA_length[$i];
$src_sheet->Cells($n,3)->{Value}=$description[$i];
$src_sheet->Cells($n,4)->{Value}=$Gene_id[$i];
$src_sheet->Cells($n,5)->{Value}=$gene_name[$i];
$src_sheet->Cells($n,6)->{Value}=$tag_name[$i];
$src_sheet->Cells($n,7)->{Value}=$CDS_length[$i];
$src_sheet->Cells($n,8)->{Value}=$protein_id[$i];
$src_sheet->Cells($n,9)->{Value}=$product[$i];
$src_sheet->Cells($n,10)->{Value}=$CDS_seq[$i];
$src_sheet->Cells($n,11)->{Value}=$AA_seq[$i];
$src_sheet->Cells($n,12)->{Value}=$mRNA_seq[$i];
$n++;
}
$src_book->Save;#保存退出
$app_xls->{DisplayAlerts}='True';
undef $src_sheet;#消除变量
undef $src_book;
##############################################################################
 



https://blog.sciencenet.cn/blog-247480-481247.html

上一篇:perl批量提取genebank序列文件中编码区、CDS长度、氨基酸等信息
下一篇:mRNA序列、cDNA序列、ORF序列、CDS序列、Promoter、STS、ETS
收藏 IP: 218.20.26.*| 热度|

0

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

数据加载中...

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

GMT+8, 2024-5-22 06:33

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部