美文网首页
2019-07-21perl(1)

2019-07-21perl(1)

作者: Bio小盼 | 来源:发表于2019-07-21 14:00 被阅读0次

整理代码,方便自己查看复习,无参考价值,勿喷

通用代码

  • 定义外部参数
my ($infile,$outfile);
 GetOptions(
     "i|infile:s"  => \$infile,
     "o|outfile:s"  => \$outfile
  );
  • 打开关闭文件
open F,"$infile"or die $!;
close F;
  • 打开关闭写入文件
open OUT,">$outfile" or die $!;
close OUT;
  • 注意while,if,foreach使用:
  • 子程序建立哈希(ex2)
  • 调用子程序
my %hash = read_lis($infile);

常看的四题代码

  • 提取文件三列
      my ($infile,$outfile);
 33 GetOptions(
 34     "i|infile:s"  => \$infile,
 35     "o|outfile:s"  => \$outfile
 36 );
 37 
 38 open F,"$infile"or die $!;
 39 open OUT,">$outfile" or die $!;
 40 while (my $line=<F>){
 41     chomp $line;
 42     my @a=split("\t",$line);
 43     if ($a[2] eq "transcript"){
 44         my $length =abs($a[4]-$a[3])+1;
 45         my $id0 =$a[8];
 46         my @id1 = split(";",$id0);
 47         my $id =$id1[0];
 48         my $chr=$a[0];
 49         print OUT "$chr\t$length\t$id\n";
 50 
 51     }
 52 }
 53 close F;
 54 close OUT;
32 my $infile;
 33 GetOptions(
 34     "i|infile:s"=> \$infile,
 35 #    "o|outfile"=> \$outfile,
 36 );
 37 
 38 #建立哈希
 39 my %hash = read_lis($infile);
 40 #计算所有value的sum;
 41 use List::Util qw/sum/;
 42 my @c = values %hash;
 43 my $sum = sum @c;
 44 #计算特定key对应value的sum1,if循环;
 45 my $sum1=0;
 46 foreach (keys %hash){
 47     if (($_>=18)and($_<=30)){
 48         $sum1 += $hash{$_};
 49         print "$sum1\n";
  • ex1
my ($infile1,$infile2,$outfile);
 33 GetOptions(
 34     "ifa|infile1:s" => \$infile1,
 35     "id|infile2:s"  => \$infile2,
 36     "o|outfile:s"  => \$outfile,
 37 
 38 );
 39 
 40 my $testid =read_id ($infile2);
 41 my @testid2=split /-/,$testid;
 42 my %hash2 = read_fa($infile1);
 43 #主程序:判断并输出
 44 open OUT,">$outfile" or die $!;
 45 my $seq2;
46 foreach (@testid2){
 47        if (exists $hash2{$_}){
 48           $seq2 =$hash2{$_};
 49           print OUT "$_\n$seq2\n";
 50       }
 51 }
 52 #close F;
 53 close OUT;
 54 
 55 #将test.fa创建为哈希
 56 sub read_fa{
 57 my %hash;
 58 open F1,$infile1 or die $!;
#这个子程序已经定义了参数,也没有必要写为子程序
 59 my ($id,$gene, $seq);
 60 while(my $line=<F1>){
 61 
 62       if ($line =~/^>/){
 63             ($id,$gene)=split" ",$line;
 64             $id=~ s/>//;
65             $seq='';
 66         }
 67         else{
 68         $seq.=$line;
 69         }
 70          $hash{$id}=$seq;
 71     }
 72 close F1;
 73 return %hash;
 74 }
 75 
 76 #读取文件test.id,将其转变为数组
 77 sub read_id{
 78 open F,$infile2 or die $!;
 79 my ($a,$testid);
 80 while(my $line1=<F>){
 81     chomp $line1;
 82     $a=$line1;
 83     $testid .="$a-";
 84   }
 85       close F;
 86      return $testid;
 87 }

  • ex2
 33 my ($infile,$inchr,$sta_end,$outfile);
 34 GetOptions(
 35            "it|infile:s"=>\$infile,
 36            "chr|inchr:i"=>\$inchr,
 37            "se|sta_end:s"=>\$sta_end,
 38            "o|outfile:s"=>\$outfile,             
 39               );
40 #定义输入文件chr-seq为哈希%testhash,需要调用>    子程序并输入参数
 41 my %testhash=read_fafile($infile);
 42 #赋值$defseq为%testhash中key为输入参数(inchr    )的value值
 43 my $defseq=$testhash{$inchr};
 44 
 45 #主程序
 46 #调用外部输入参数$sta_end,定义其差值,若大于0    输出%defhash,小于0,输出反向互补的%defseq,
 47 open OUT,">$outfile"or die "$outfile\n";
 48 my ($s,$e) = split "-",$sta_end;
49 my $dse=$e-$s;
 50 if ($dse>0){
 51         my $locseq=substr($defseq,$s-1,$e-$s+    1);
 52         print OUT "$inchr\n $locseq\n";
 53     }else{
 54         my $cseq=reverse $defseq;
 55         $cseq =~ tr/ATCGacgt/TAGCtgca/;
 56         my $relocseq=substr($cseq,$e-1,$s-$e+    1);
 57         print OUT "$inchr\n$relocseq\n";
 58 }
 59 close OUT;
 60 #子程序1:读取文件,将fa文件chr-seq建立哈希,>    返回哈希,
 61 sub read_fafile{
 62     my $filename = shift;
 63     open F,$filename or die $!;
 64     #my $filename = $(@_)[0];还没有见过这种写
    法,不知道对不对
 65     #my $filename = $_[0]; 
 66     my (%hash,$chr,$seq);
 67     while (my $line=<F>){
 68         #       my ($chr,$seq);
 69         $line =~s/[\r\n]//g;
 70         if ($line=~/^>/){
 71              $chr = $line;
 72              $chr =~s/^>//;
 73              $seq ='';
 74          # print "$chr\n";
 75         }
 76         else{
 77             $seq .=$line;
 78         }
 79         #print "err-$seq\n";
 80         $hash{$chr}=$seq;
 81     }
 82     return %hash;
 83 }

  • 第三题
  • 按照序列数分fq1与fq2文件
my ($infile1,$infile2,$defseqc);
GetOptions(
        "ifq1|infile1:s"=>\ $infile1,
        "ifq2|infile2:s"=> \$infile2,
        "c|defseqc:i"=> \$defseqc,
 );
cut_seqcount($infile1);
cut_seqcount($infile2);
sub cut_seqcount{
$/ = "@";
my ($b,$a);
$a =0; 
$b=0;
my $filena =shift;
my $filename=$filena.$b;
open OUT,">$filename" or die $!; 
print OUT "@";
open F,$filena or die $!; 
my $line;
while (my $file =<F>){
    #chomp $file;
    my $line = $file;
    if ($a< $defseqc){
        print OUT $line or die $!; 
        $a =$a+1;
     }   
    else {
          close OUT;
          $a = 0;
          $b=$b+1;
          $filename = $filena.$b;
          open OUT,">$filename" or die $!; 
          #my $line2 = $_
          print OUT "@";
          print OUT "$line" or die $!; 
          $a =$a+1;
    }   
} 
close OUT;
close F;
}

  • 按照文件数目分fq1,fq2文件
my ($infile1,$infile2,$deffilec);
GetOptions(
       "ifq1|infile1:s"=>\ $infile1,
       "ifq2|infile2:s"=>\ $infile2,
       "c|deffilec:i"=> \$deffilec,
);
cut_fileco($infile1);
cut_fileco($infile2);
sub cut_fileco{
$/ = "@";
my $filenam =shift;
open F,$filenam or die $!;
##创建并切换目录:
#my $di = "outfile_$b";
#my $dir ="/($di)";
#mkdir ($dir) ,0755 or die "无法创建目录,$!";
#chdir ($dir) or die "无法切换目录,$!";
#open F,$infile1 or die $!;

my ($b,$a);
$a =0;#对应外部定义序列数的计数器
$b=0;#对应文件名的计数器
my $cseq =0;#对应读取文件line的计数器
my $all_line =`wc -l $infile1`;
my $all_seq_count = ($all_line)/4;
my $filename=$filenam.$b;
open OUT,">$filename" or die $!;
print OUT "@";
my $line;
my $defseqc=int($all_seq_count/$deffilec);
print $defseqc;
while (my $file =<F>){
    #chomp $file;
    $cseq +=1;
    # my $defseqc=int($all_seq_count/$deffilec);    
#my $defseqc =("%.0f",$all_seq_count/$deffilec );
    #print $defseqc;
    my $line = $file;
    #print "$file-";
    if ($a< $defseqc or $cseq > $deffilec * $defseqc ){
        #$line .=$_;
        print OUT $line or die $!;
        $a =$a+1;
     }
    else {
          close OUT;
          $a = 0;
          $b=$b+1;
          $filename =$filenam.$b;
          open OUT,">$filename" or die $!;
          #my $line2 = $_
          print OUT "@";
          print OUT "$line" or die $!;
          $a =$a+1;
    }
}
close OUT;
close F;
}

相关文章

  • 2019-07-21perl(1)

    整理代码,方便自己查看复习,无参考价值,勿喷 通用代码 定义外部参数 打开关闭文件 打开关闭写入文件 注意whil...

  • 1▪1▪1▪1▪1

    今天是国际劳动节,出门看人头,上路遇堵车,处处挤破头,急哭也停不下车。 不如歇了吧 ...

  • 1+1+1…+1=1

    对“一”的理解: 赠人玫瑰,不仅仅是手留余香。 利益他人,实际上也疗愈了自己。 利他、利己,如此往复循环, 最终利...

  • (-1)×(-1)= 1

    数学家经过很长一段时间才认识到(-1)×(-1)= 1是不能被证明的(即使大数学家欧拉曾给出不能令人信服的...

  • 1-2-1-1-1

    【下马请罪】 子龙下马,向张飞跪地请罪道:“张将军,一时失手……”话未停,便被张飞一矛刺了个透心凉。子龙堵着胸口汩...

  • 1 1:1 1(原创小说)

    闪回:那天她…… 当时,我确实听到了那个声音,可如今却怎么也记不清了。 掉下来了。 我觉得,那一刻...

  • 《1+1=1-1》

    十一月十一日晚,致X小姐。 十月初九, 一个人购物的孤独, 你谈起, 月光下轧过的马路, 金钱带不来满足, 忙忙碌...

  • 1+1=-1

    结婚育子这几年,在磕磕碰碰中一路走来,才恍然大悟,自己真正的成长,始于育儿。 婚前是父母的公主,虽说家境贫困,却得...

  • 1+1<1

    也许有人看到我的标题就会来质疑我,说我怎么连最简单的数学都不会。1+1=2>1啊,这么简单的算数题,我怎会不知?但...

  • 1+1=-1

    看到他人发表文章,我也有点手痒痒了~这是接着上回文章的下半部分,有点长,没人看到就好了︿︿ 这个第二件小事的主题就...

网友评论

      本文标题:2019-07-21perl(1)

      本文链接:https://www.haomeiwen.com/subject/njimlctx.html