消除perl中子例程中的空文件

时间:2017-01-24 20:25:04

标签: perl bioinformatics fasta fastq sequencing

我想在下一个脚本中添加一个代码来消除那些空输出文件。

该脚本将单个fastq文件或文件夹中的所有fastq文件转换为fasta格式,所有输出的fasta文件都与fastq文件保持相同的名称;该脚本提供了一个选项来排除所有呈现确定数量的NNN重复的序列(NNNNNNNNNNNNNNNNNNATAGTGAAGAATGCGACGTACAGGATCATCTA),我添加了此选项,因为一些序列仅在序列中出现NNNNN,例如:如果-n选项等于15(-n) 15)它将排除所有呈现15个或更多N个重复序列的序列,此时代码运行良好,但它生成一个空文件(在那些fastq文件中,所有序列呈现15个或更多N个重复被排除)。我想删除所有空文件(没有序列)并添加一个计数消除的数量,因为它是空的。

代码:

#!/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;
            }
        }

我刚刚在子程序中尝试了类似的东西,但最后一个代码不起作用!!!!

任何建议!!!! ???

非常感谢!!!

2 个答案:

答案 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; 
        }
    }
}

答案 1 :(得分:0)

如果在0子例程结束时将新内容写入新文件,则应检查。只需将代码放在fastq_fasta语句之后:

close OUTFILE

此外,最好将close OUTFILE; close LINE; my $outfile = $file_name.".fasta"; if (-z $outfile) { unlink $outfile || die "Error while deleting '$outfile': $!"; } 语句也添加到其他unlink行。应删除空文件。

如果您没有修复perl,但允许使用sed和bash循环,可能是另一种解决方案:

die/warn

希望有所帮助!

最佳弗兰克