| 1 | #!/usr/bin/env perl | ||
| 2 | # Last changed Time-stamp: <2016-04-21 15:52:48 mtw> | ||
| 3 | # | ||
| 4 | # *********************************************************************** | ||
| 5 | # * Copyright notice | ||
| 6 | # * | ||
| 7 | # * Copyright 2016 Michael T. Wolfinger <michael@wolfinger.eu> | ||
| 8 | # * All rights reserved | ||
| 9 | # * | ||
| 10 | # * This program is free software: you can redistribute it and/or modify | ||
| 11 | # * it under the terms of the GNU General Public License as published by | ||
| 12 | # * the Free Software Foundation, either version 3 of the License, or | ||
| 13 | # * (at your option) any later version. | ||
| 14 | # * | ||
| 15 | # * This program is distributed in the hope that it will be useful, | ||
| 16 | # * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 17 | # * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 18 | # * GNU General Public License for more details. | ||
| 19 | # * | ||
| 20 | # * You should have received a copy of the GNU General Public License}, | ||
| 21 | # * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 22 | # * | ||
| 23 | # * This copyright notice MUST APPEAR in all copies of the script! | ||
| 24 | # *********************************************************************** | ||
| 25 | |||
| 26 | use warnings; | ||
| 27 | use strict; | ||
| 28 | use PerlIO::gzip; | ||
| 29 | use Getopt::Long qw( :config posix_default bundling no_ignore_case ); | ||
| 30 | use Pod::Usage; | ||
| 31 | |||
| 32 | #^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^# | ||
| 33 | #^^^^^^^^^^ Variables ^^^^^^^^^^^# | ||
| 34 | #^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^# | ||
| 35 | |||
| 36 | my ($file, $five, $three, $a5, $str, $count, $last); | ||
| 37 | my $len = 0; | ||
| 38 | #^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^# | ||
| 39 | #^^^^^^^^^^^^^^ Main ^^^^^^^^^^^^^# | ||
| 40 | #^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^# | ||
| 41 | |||
| 42 | Getopt::Long::config('no_ignore_case'); | ||
| 43 | pod2usage(-verbose => 0) unless GetOptions( | ||
| 44 | "fastq|f=s" => \$file, | ||
| 45 | "up|u=s" => \$five, | ||
| 46 | "down|d=s" => \$three, | ||
| 47 | "a5=s" => \$a5, | ||
| 48 | "man" => sub{pod2usage(-verbose => 2)}, | ||
| 49 | "help|h" => sub{pod2usage(-verbose => 1)} | ||
| 50 | ); | ||
| 51 | |||
| 52 | unless ( $file ) { | ||
| 53 | warn "Please provide the name of the fastq via the -f option"; | ||
| 54 | pod2usage(-verbose => 0); | ||
| 55 | } | ||
| 56 | |||
| 57 | unless ($a5) { | ||
| 58 | unless ( $five && $three ){ | ||
| 59 | warn "Please define trim length via -u and -d"; | ||
| 60 | pod2usage(-verbose => 0); | ||
| 61 | } | ||
| 62 | } | ||
| 63 | else { | ||
| 64 | if ( $five && $three ){ | ||
| 65 | warn "The -a5 and -u/-d options are mutually exclusive. Please do not use -u/-d when trimming a 5' adapter sequence"; | ||
| 66 | pod2usage(-verbose => 0); | ||
| 67 | } | ||
| 68 | $len = length($a5); | ||
| 69 | } | ||
| 70 | |||
| 71 | unless ( -f $file) { | ||
| 72 | warn "Could not find input file $file given via -f option"; | ||
| 73 | pod2usage(-verbose => 0); | ||
| 74 | } | ||
| 75 | |||
| 76 | $count = 0; | ||
| 77 | $len = length($a5); | ||
| 78 | |||
| 79 | open(IN,"<:gzip(autopop)",$file)|| die "Can't open file: $!"; | ||
| 80 | my $x=1; | ||
| 81 | while(<IN>){ | ||
| 82 | my $string="$_"; | ||
| 83 | chomp($string); | ||
| 84 | $string =~ s/\cn/\\cn\cn/gs; | ||
| 85 | if( (++$x)%2 ){ | ||
| 86 | # print "> $x $string\n"; | ||
| 87 | if( $a5){ | ||
| 88 | my $pattern = qr/$a5/; | ||
| 89 | if ($string =~ m/^$pattern/){ | ||
| 90 | $count++; | ||
| 91 | $str = substr($string,$len); | ||
| 92 | $last = $x; | ||
| 93 | } | ||
| 94 | if ($x == $last + 2){ # trim quality string for previously trimmed read | ||
| 95 | $str = substr($string,$len); | ||
| 96 | $last = -1; | ||
| 97 | } | ||
| 98 | print "$x $str\n"; | ||
| 99 | } | ||
| 100 | else{ | ||
| 101 | my $i = substr($string,$three,-$five); | ||
| 102 | print "$i\n"; | ||
| 103 | } | ||
| 104 | } | ||
| 105 | else { | ||
| 106 | print "* $string\n"; | ||
| 107 | } | ||
| 108 | } | ||
| 109 | close(IN); | ||
| 110 | |||
| 111 | print "found adapter $a5 $count times\n" | ||
| 112 | |||
| 113 | __END__ | ||
| 114 | |||
| 115 | =head1 NAME | ||
| 116 | |||
| 117 | trim_fastq.pl - Trim sequence and quality string of fastq files on the fly | ||
| 118 | |||
| 119 | =head1 SYNOPSIS | ||
| 120 | |||
| 121 | trim_fastq.pl [-f I<FILE>] [-u I<INTEGER>] [-d I<INTEGER>] [-a5 I<STRING>] | ||
| 122 | [options] | ||
| 123 | |||
| 124 | =head1 DESCRIPTION | ||
| 125 | |||
| 126 | This program trims sequence and qualitystring fields from a fastq | ||
| 127 | file, either by a number of nucleotides, or by removing a user defined | ||
| 128 | 5' adapter sequence. The typical usage scenario is to allow re-mapping | ||
| 129 | if mapping quality is not sufficient without trimming. | ||
| 130 | |||
| 131 | |||
| 132 | =head1 OPTIONS | ||
| 133 | |||
| 134 | =over1 | ||
| 135 | |||
| 136 | =item B<--fastq -f> | ||
| 137 | |||
| 138 | Fastq file for trimming | ||
| 139 | |||
| 140 | =item B<--up -u> | ||
| 141 | |||
| 142 | Number of nucleotides to trim from read / quality string start | ||
| 143 | |||
| 144 | =item B<--down -d> | ||
| 145 | |||
| 146 | Number of nucleotides to trim from read / quality string end | ||
| 147 | |||
| 148 | =item B<--a5> | ||
| 149 | |||
| 150 | Sequence of 5' adapter to trim from the left side of the reads. This | ||
| 151 | option and -u / -d are mutually exclusive | ||
| 152 | |||
| 153 | =item B<--help -h> | ||
| 154 | |||
| 155 | Print short help | ||
| 156 | |||
| 157 | =item B<--man> | ||
| 158 | |||
| 159 | Prints the manual page and exits | ||
| 160 | |||
| 161 | =back | ||
| 162 | |||
| 163 | =head1 AUTHORS | ||
| 164 | |||
| 165 | Joerg Fallmann E<lt>joerg.fallmann@univie.ac.atE<gt> | ||
| 166 | Michael T. Wolfinger E<lt>michael@wolfinger.euE<gt> | ||
| 167 | |||
| 168 | =cut |