| 1 | =head1 NAME | ||
| 2 | |||
| 3 | CLIPSeqTools::PreprocessApp::collapse_fastq - Keep a single record for | ||
| 4 | identical sequences | ||
| 5 | |||
| 6 | =head1 SYNOPSIS | ||
| 7 | |||
| 8 | clipseqtools-preprocess collapse_fastq [options/parameters] | ||
| 9 | |||
| 10 | =head1 DESCRIPTION | ||
| 11 | |||
| 12 | Collapse a fastq file. Will keep only a single record for each group of | ||
| 13 | identical sequences. | ||
| 14 | |||
| 15 | =head1 OPTIONS | ||
| 16 | |||
| 17 | Input. | ||
| 18 | --fastq <Str> FASTQ file with sequences | ||
| 19 | --memsave <Int> Memory saving factor. 0 is the fastest but uses the | ||
| 20 | most memory. 1 uses ~1/4 of memory and ~4x time. | ||
| 21 | Each step (2, 3, 4 etc) changes memory and time | ||
| 22 | by a factor of 4. If this option is used the output | ||
| 23 | will be grouped by first N nucleotides. | ||
| 24 | [Default: 0] | ||
| 25 | |||
| 26 | Output | ||
| 27 | --o_prefix <Str> output path prefix. Script will create and add | ||
| 28 | extension to path. [Default: ./] | ||
| 29 | |||
| 30 | Other options. | ||
| 31 | -v --verbose print progress lines and extra information. | ||
| 32 | -h -? --usage --help print help message | ||
| 33 | |||
| 34 | =cut | ||
| 35 | |||
| 36 | package CLIPSeqTools::PreprocessApp::collapse_fastq; | ||
| 37 | $CLIPSeqTools::PreprocessApp::collapse_fastq::VERSION = '1.0.0'; | ||
| 38 | |||
| 39 | # Make it an app command | ||
| 40 | use MooseX::App::Command; | ||
| 41 | use GenOO::Data::File::FASTQ; | ||
| 42 | extends 'CLIPSeqTools::PreprocessApp'; | ||
| 43 | |||
| 44 | |||
| 45 | ####################################################################### | ||
| 46 | ####################### Load External modules ##################### | ||
| 47 | ####################################################################### | ||
| 48 | use Modern::Perl; | ||
| 49 | use autodie; | ||
| 50 | use namespace::autoclean; | ||
| 51 | |||
| 52 | |||
| 53 | ####################################################################### | ||
| 54 | ####################### Command line options ###################### | ||
| 55 | ####################################################################### | ||
| 56 | option 'fastq' => ( | ||
| 57 | is => 'rw', | ||
| 58 | isa => 'Str', | ||
| 59 | required => 1, | ||
| 60 | documentation => 'FASTQ file with sequences.', | ||
| 61 | ); | ||
| 62 | |||
| 63 | option 'memsave' => ( | ||
| 64 | is => 'rw', | ||
| 65 | isa => 'Int', | ||
| 66 | required => 0, | ||
| 67 | default => 0, | ||
| 68 | documentation => 'Memory saving factor. 0 is the fastest but uses the most memory. 1 uses ~1/4 of memory and ~4x time. Each step (2, 3, 4 etc) changes memory and time by a factor of 4. If this option is used the output will be grouped by first N nucleotides.', | ||
| 69 | ); | ||
| 70 | |||
| 71 | ####################################################################### | ||
| 72 | ########################## Consume Roles ########################## | ||
| 73 | ####################################################################### | ||
| 74 | with | ||
| 75 | "CLIPSeqTools::Role::Option::OutputPrefix" => { | ||
| 76 | -alias => { validate_args => '_validate_args_for_output_prefix' }, | ||
| 77 | -excludes => 'validate_args', | ||
| 78 | }; | ||
| 79 | |||
| 80 | |||
| 81 | ####################################################################### | ||
| 82 | ######################## Interface Methods ######################## | ||
| 83 | ####################################################################### | ||
| 84 | sub validate_args { | ||
| 85 | my ($self) = @_; | ||
| 86 | |||
| 87 | $self->_validate_args_for_output_prefix; | ||
| 88 | } | ||
| 89 | |||
| 90 | sub run { | ||
| 91 | my ($self) = @_; | ||
| 92 | |||
| 93 | warn "Starting job: collapse_fastq\n"; | ||
| 94 | |||
| 95 | warn "Validating arguments\n" if $self->verbose; | ||
| 96 | $self->validate_args(); | ||
| 97 | |||
| 98 | warn "Creating output path\n" if $self->verbose; | ||
| 99 | $self->make_path_for_output_prefix(); | ||
| 100 | |||
| 101 | warn "Collapsing FASTQ\n" if $self->verbose; | ||
| 102 | $self->collapse_fastq($self->fastq, $self->memsave, $self->o_prefix.'collapsed.fastq'); | ||
| 103 | } | ||
| 104 | |||
| 105 | sub collapse_fastq { | ||
| 106 | my ($self, $fastq, $k, $out) = @_; | ||
| 107 | my @kmers; | ||
| 108 | open (OUT, ">", $out); | ||
| 109 | |||
| 110 | if ($k == 0){ | ||
| 111 | push @kmers, '.'; | ||
| 112 | } | ||
| 113 | else { | ||
| 114 | @kmers = _permuteK($k); | ||
| 115 | } | ||
| 116 | |||
| 117 | my $count = 0; | ||
| 118 | foreach my $kmer (@kmers){ | ||
| 119 | my $fp = GenOO::Data::File::FASTQ->new(file => $fastq); | ||
| 120 | my %already_found = (); | ||
| 121 | while (my $rec = $fp->next_record) { | ||
| 122 | my $seq = $rec->sequence; | ||
| 123 | if ($seq =~ /![ATGC]/){ | ||
| 124 | next; | ||
| 125 | } | ||
| 126 | if ($seq !~ /^$kmer/){ | ||
| 127 | next; | ||
| 128 | } | ||
| 129 | if (exists $already_found{$seq}){ | ||
| 130 | next; | ||
| 131 | } | ||
| 132 | else { | ||
| 133 | $already_found{$seq} = 1; | ||
| 134 | print OUT $rec->to_string."\n"; | ||
| 135 | } | ||
| 136 | } | ||
| 137 | } | ||
| 138 | |||
| 139 | #for non ATGC nucleotide reads | ||
| 140 | my $fp = GenOO::Data::File::FASTQ->new(file => $fastq); | ||
| 141 | my %already_found = (); | ||
| 142 | while (my $rec = $fp->next_record) { | ||
| 143 | my $seq = $rec->sequence; | ||
| 144 | unless ($seq =~ /![ATGC]/){ | ||
| 145 | next; | ||
| 146 | } | ||
| 147 | if (exists $already_found{$seq}){ | ||
| 148 | next; | ||
| 149 | } | ||
| 150 | else { | ||
| 151 | $already_found{$seq} = 1; | ||
| 152 | print OUT $rec->to_string."\n"; | ||
| 153 | } | ||
| 154 | } | ||
| 155 | |||
| 156 | close OUT; | ||
| 157 | } | ||
| 158 | |||
| 159 | |||
| 160 | ####################################################################### | ||
| 161 | ######################## Private Functions ######################## | ||
| 162 | ####################################################################### | ||
| 163 | sub _permuteK { | ||
| 164 | my $k = shift; | ||
| 165 | my $alphabet = shift || [ qw( A T G C ) ]; | ||
| 166 | my @bases = @$alphabet; | ||
| 167 | my @words = @bases; | ||
| 168 | for ( 1 .. --$k ) { | ||
| 169 | my @newwords; | ||
| 170 | foreach my $w (@words) { | ||
| 171 | foreach my $b (@bases) { | ||
| 172 | push (@newwords, $w.$b); | ||
| 173 | } | ||
| 174 | } | ||
| 175 | @words = @newwords; | ||
| 176 | } | ||
| 177 | return @words; | ||
| 178 | } | ||
| 179 | |||
| 180 | |||
| 181 | 1; |