« CLIPSeqTools/PreprocessApp/cut_adaptor.pm
↑ Summary
CLIPSeqTools/PlotApp/cluster_size_and_score_distribution.pm »
| 1 | =head1 NAME | ||
| 2 | |||
| 3 | CLIPSeqTools::PreprocessApp::trim_fastq - Trim N nucleotides from the start | ||
| 4 | and/or end of FASTQ sequences. | ||
| 5 | |||
| 6 | =head1 SYNOPSIS | ||
| 7 | |||
| 8 | clipseqtools-preprocess trim_fastq [options/parameters] | ||
| 9 | |||
| 10 | =head1 DESCRIPTION | ||
| 11 | |||
| 12 | Trim the sequences in a fastq file. | ||
| 13 | Removes N nucleotides from the start and/or end of each sequence. | ||
| 14 | |||
| 15 | =head1 OPTIONS | ||
| 16 | |||
| 17 | Input. | ||
| 18 | --fastq <Str> FASTQ file with reads. | ||
| 19 | --N_from_start <Int> Number of nucleotides to trim from the start of | ||
| 20 | each sequence. [Default: 0] | ||
| 21 | --N_from_end <Int> Number of nucleotides to trim from the end of | ||
| 22 | each sequence. [Default: 0] | ||
| 23 | |||
| 24 | Output | ||
| 25 | --o_prefix <Str> output path prefix. Script will create and add | ||
| 26 | extension to path. [Default: ./] | ||
| 27 | |||
| 28 | Other options. | ||
| 29 | -v --verbose print progress lines and extra information. | ||
| 30 | -h -? --usage --help print help message | ||
| 31 | |||
| 32 | =cut | ||
| 33 | |||
| 34 | package CLIPSeqTools::PreprocessApp::trim_fastq; | ||
| 35 | $CLIPSeqTools::PreprocessApp::trim_fastq::VERSION = '1.0.0'; | ||
| 36 | |||
| 37 | # Make it an app command | ||
| 38 | use MooseX::App::Command; | ||
| 39 | use GenOO::Data::File::FASTQ; | ||
| 40 | extends 'CLIPSeqTools::PreprocessApp'; | ||
| 41 | |||
| 42 | |||
| 43 | ####################################################################### | ||
| 44 | ####################### Load External modules ##################### | ||
| 45 | ####################################################################### | ||
| 46 | use Modern::Perl; | ||
| 47 | use autodie; | ||
| 48 | use namespace::autoclean; | ||
| 49 | |||
| 50 | |||
| 51 | ####################################################################### | ||
| 52 | ####################### Command line options ###################### | ||
| 53 | ####################################################################### | ||
| 54 | option 'fastq' => ( | ||
| 55 | is => 'rw', | ||
| 56 | isa => 'Str', | ||
| 57 | required => 1, | ||
| 58 | documentation => 'FASTQ file with sequences.', | ||
| 59 | ); | ||
| 60 | |||
| 61 | option 'N_from_start' => ( | ||
| 62 | is => 'rw', | ||
| 63 | isa => 'Int', | ||
| 64 | required => 0, | ||
| 65 | default => 0, | ||
| 66 | documentation => 'Number of nucleotides to trim from the start of each sequence [Default: 0]', | ||
| 67 | ); | ||
| 68 | |||
| 69 | option 'N_from_end' => ( | ||
| 70 | is => 'rw', | ||
| 71 | isa => 'Int', | ||
| 72 | required => 0, | ||
| 73 | default => 0, | ||
| 74 | documentation => 'Number of nucleotides to trim from the end of each sequence [Default: 0]', | ||
| 75 | ); | ||
| 76 | |||
| 77 | ####################################################################### | ||
| 78 | ########################## Consume Roles ########################## | ||
| 79 | ####################################################################### | ||
| 80 | with | ||
| 81 | "CLIPSeqTools::Role::Option::OutputPrefix" => { | ||
| 82 | -alias => { validate_args => '_validate_args_for_output_prefix' }, | ||
| 83 | -excludes => 'validate_args', | ||
| 84 | }; | ||
| 85 | |||
| 86 | |||
| 87 | ####################################################################### | ||
| 88 | ######################## Interface Methods ######################## | ||
| 89 | ####################################################################### | ||
| 90 | sub validate_args { | ||
| 91 | my ($self) = @_; | ||
| 92 | |||
| 93 | $self->_validate_args_for_output_prefix; | ||
| 94 | } | ||
| 95 | |||
| 96 | sub run { | ||
| 97 | my ($self) = @_; | ||
| 98 | |||
| 99 | warn "Starting job: trim_fastq\n"; | ||
| 100 | |||
| 101 | warn "Validating arguments\n" if $self->verbose; | ||
| 102 | $self->validate_args(); | ||
| 103 | |||
| 104 | warn "Creating output path\n" if $self->verbose; | ||
| 105 | $self->make_path_for_output_prefix(); | ||
| 106 | |||
| 107 | warn "Trimming FASTQ\n" if $self->verbose; | ||
| 108 | $self->trim_fastq( | ||
| 109 | $self->fastq, $self->N_from_start, $self->N_from_end, | ||
| 110 | $self->o_prefix.'trimmed.fastq'); | ||
| 111 | } | ||
| 112 | |||
| 113 | sub trim_fastq { | ||
| 114 | my ($self, $fastq, $Nstart, $Nend, $out) = @_; | ||
| 115 | |||
| 116 | open (my $OUT, ">", $out); | ||
| 117 | |||
| 118 | my $class = "GenOO::Data::File::FASTQ"; | ||
| 119 | my $fp = $class->new(file => $fastq); | ||
| 120 | |||
| 121 | while (my $rec = $fp->next_record) { | ||
| 122 | my $seq = $rec->sequence; | ||
| 123 | my $qual = $rec->quality; | ||
| 124 | |||
| 125 | if ($Nstart >= length($seq)){next;} | ||
| 126 | if ($Nstart > 0){ | ||
| 127 | $seq = substr($seq, $Nstart); | ||
| 128 | $qual = substr($qual, $Nstart); | ||
| 129 | } | ||
| 130 | |||
| 131 | if ($Nend >= length($seq)){next;} | ||
| 132 | if ($Nend > 0){ | ||
| 133 | $seq = substr($seq, 0, (0 - $Nend)); | ||
| 134 | $qual = substr($qual, 0, (0 - $Nend)); | ||
| 135 | } | ||
| 136 | $rec->sequence($seq); | ||
| 137 | $rec->quality($qual); | ||
| 138 | print $OUT $rec->to_string."\n"; | ||
| 139 | } | ||
| 140 | |||
| 141 | } | ||
| 142 | |||
| 143 | |||
| 144 | ####################################################################### | ||
| 145 | ######################## Private Functions ######################## | ||
| 146 | ####################################################################### | ||
| 147 | |||
| 148 | 1; |