| 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__ |