2017-01-24 5 views
0

空の出力ファイルを削除するために、次のスクリプトでコードを追加します。perlのサブルーチンで空のファイルを削除する

1つのfastqファイルまたはフォルダ内のすべてのfastqファイルをfasta形式に変換すると、すべての出力fastaファイルはfastqファイルと同じ名前になります。 (NNNNNNNNNNNNNNNNNNATAGTGAAGAATGCGACGTACAGGATCATCTA)を指定したすべてのシーケンスを除外するオプションがあるので、このオプションを追加しました。シーケンス内にNNNNNのみが存在するためです。例:-nオプションが15(-n 15)それ以上のN個のリピートが15個以上存在するすべてのシーケンスを除外します。この時点でコードは正常に動作しますが、空のファイルが生成されます。私はすべての空のファイル(シーケンスなし)を削除し、それが空だったために削除されたファイルの数を追加したいと思います。

コード:

#!/usr/bin/env perl 
use strict; 
use warnings; 
use Getopt::Long; 

my ($infile, $file_name, $file_format, $N_repeat, $help, $help_descp, 
    $options, $options_descrp, $nofile, $new_file, $count); 

my $fastq_extension = "\\.fastq"; 

GetOptions (
    'in=s'  => \$infile, 
    'N|n=i'  =>\$N_repeat, 
    'h|help' =>\$help, 
    'op'  =>\$options 
); 

# Help 

$help_descp =(qq(    
       Ussaje: 
       fastQF -in fastq_folder/ -n 15 
         or 
       fastQF -in file.fastq -n 15 
      )); 

$options_descrp =(qq(

      -in  infile.fastq or fastq_folder/     required 
      -n  exclude sequences with more than N repeat  optional 
      -h  Help description        optional 
      -op  option section         optional 
        )); 

$nofile =(qq(
      ERROR: "No File or Folder Were Chosen !" 

       Usage: 
        fastQF -in folder/ 

       Or See -help or -op section 
      )); 

# Check Files 

    if ($help){ 
     print "$help_descp\n"; 
     exit; 
    } 
    elsif ($options){ 
     print "$options_descrp\n"; 
     exit; 
    } 

    elsif (!$infile){ 
     print "$nofile\n"; 
     exit; 
    } 


#Subroutine to convert from fastq to fasta 

    sub fastq_fasta { 

     my $file = shift; 
     ($file_name = $file) =~ s/(.*)$fastq_extension.*/$1/; 

# eliminate old files 

     my $oldfiles= $file_name.".fasta"; 

     if ($oldfiles){ 
      unlink $oldfiles; 
     } 

     open LINE, '<', $file    or die "can't read or open $file\n"; 
     open OUTFILE, '>>', "$file_name.fasta" or die "can't write $file_name\n"; 

     while (
      defined(my $head = <LINE>)  && 
      defined(my $seq  = <LINE>)  && 
      defined(my $qhead = <LINE>)  && 
      defined(my $quality = <LINE>) 
     ) { 
       substr($head, 0, 1, '>'); 


       if (!$N_repeat){ 
        print OUTFILE $head, $seq; 


       } 

       elsif ($N_repeat){ 

         my $number_n=$N_repeat-1; 

        if ($seq=~ m/(n)\1{$number_n}/ig){ 
         next; 
        } 
        else{ 
         print OUTFILE $head, $seq; 
        } 
       } 
     } 

     close OUTFILE; 
     close LINE; 
    } 

# execute the subrutine to extract the sequences 

    if (-f $infile) {   # -f es para folder !! 
     fastq_fasta($infile); 
    } 
    else { 
     foreach my $file (glob("$infile/*.fastq")) { 
     fastq_fasta($file); 
     } 
    } 

exit; 

私は(出口前)サブルーチンの外に次のコードを使用しようとしましたが、それはちょうど最後のファイルのために働く:

$new_file =$file_name.".fasta"; 
     foreach ($new_file){ 

      if (-z $new_file){ 
       $count++; 
       if ($count==1){ 
        print "\n\"The choosen File present not sequences\"\n"; 
        print " \"or was excluded due to -n $N_repeat\"\n\n"; 

       } 
       elsif ($count >=1){ 
        print "\n\"$count Files present not sequences\"\n"; 
        print " \" or were excluded due to -n $N_repeat\"\n\n"; 

       } 

       unlink $new_file; 
      } 
     } 

と私は試してみましたサブルーチンの中でも似たようなものですが、この最後のコードはうまくいきません!!!!

任意のアドバイス!!!! ???

ありがとうございます!

+4

の$ NEW_FILEは、単一のスカラー値ではなく、値のリストであるため、あなたのforeachループは一度だけ実行されます。 – toolic

答えて

0

fastq_fastaサブルーチンの最後に新しいファイルに何かが書き込まれているかどうかを確認する必要があります。ただ、close OUTFILE文の後にコードを置く:

close OUTFILE; 
close LINE; 

my $outfile = $file_name.".fasta"; 
if (-z $outfile) 
{ 
    unlink $outfile || die "Error while deleting '$outfile': $!"; 
} 

また、他のリンク解除ラインにもdie/warnステートメントを追加する方がよいでしょう。空のファイルは削除する必要があります。

たぶん、あなたはperlのに固定されますが、SEDとbashのループを使用することが許可されていない場合は、別の解決策:助け

for i in *.fastq 
do 
    out=$(dirname "$i")/$(basename "$i" .fastq).fasta 
    sed -n '1~4{s/^@/>/;N;p}' "$i" > "$out" 
    if [ -z $out ] 
    then 
     echo "Empty output file $out" 
     rm "$out" 
    fi 
done 

希望を!

ベストフランク

+0

ありがとう! –

0

一番簡単な方法は、OUTFILEにおける配列の数を追跡するためにあなたのサブルーチンにカウンターを追加するために、おそらくです:

sub fastq_fasta { 
    my $counter1 = 0; 
    my $file = shift; 
    ($file_name = $file) =~ s/(.*)$fastq_extension.*/$1/; 

# eliminate old files 

    my $oldfiles= $file_name.".fasta"; 

    if ($oldfiles){ 
     unlink $oldfiles; 
    } 

    open LINE, '<', $file    or die "can't read or open $file\n"; 
    open OUTFILE, '>>', "$file_name.fasta" or die "can't write $file_name\n"; 

    while (
     defined(my $head = <LINE>)  && 
     defined(my $seq  = <LINE>)  && 
     defined(my $qhead = <LINE>)  && 
     defined(my $quality = <LINE>) 
    ) { 
      $counter1 ++; 
      substr($head, 0, 1, '>'); 


      if (!$N_repeat){ 
       print OUTFILE $head, $seq; 


      } 

      elsif ($N_repeat){ 

        my $number_n=$N_repeat-1; 

       if ($seq=~ m/(n)\1{$number_n}/ig){ 
        $counter1 --; 
        next; 
       } 
       else{ 
        print OUTFILE $head, $seq; 
       } 
      } 
    } 

    close OUTFILE; 
    close LINE; 
    return $counter1; 
} 

返さカウントがゼロであるときは、その後、ファイルを削除することができます。

if (-f $infile) {   # -f es para folder !! 
    fastq_fasta($infile); 
} 
else { 
    foreach my $file (glob("$infile/*.fastq")) { 
     if (fastq_fasta($file) == 0) { 
      $file =~ s/(.*)$fastq_extension.*/$1.fasta/; 
      unlink $file; 
     } 
    } 
} 
+0

ありがとう! –

関連する問題