| 1 | #!/usr/bin/env perl | ||||||||||||||||||||||||||||
| 2 | package Bio::App::SELEX::AdapterTrim; | ||||||||||||||||||||||||||||
| 3 | # ABSTRACT: Trim adapters from FASTQ files. Works for 5' or 3' adapters. | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | ############################################################################# | ||||||||||||||||||||||||||||
| 6 | # Author: Scott Givan with some modifications by Christopher Bottoms. | ||||||||||||||||||||||||||||
| 7 | ############################################################################# | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use Getopt::Long; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 2 | my ($infile, $outfile, $overwrite, $adapterseq, $help, $verbose, $debug, $fastq,$idlist,$idlistfile,$printall,$notwoadapters,$id2adapters); | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 2 | my $adapter3prime; | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 2 | my $options = GetOptions( | |||||||||||||||||||||||||||
| 18 | "infile=s" => \$infile, | ||||||||||||||||||||||||||||
| 19 | "outfile=s" => \$outfile, | ||||||||||||||||||||||||||||
| 20 | "overwrite" => \$overwrite, | ||||||||||||||||||||||||||||
| 21 | "adapterseq=s" => \$adapterseq, | ||||||||||||||||||||||||||||
| 22 | "help" => \$help, | ||||||||||||||||||||||||||||
| 23 | "verbose" => \$verbose, | ||||||||||||||||||||||||||||
| 24 | "debug" => \$debug, | ||||||||||||||||||||||||||||
| 25 | "fastq" => \$fastq, | ||||||||||||||||||||||||||||
| 26 | "idlist" => \$idlist, | ||||||||||||||||||||||||||||
| 27 | "idlistfile=s" => \$idlistfile, | ||||||||||||||||||||||||||||
| 28 | "printall" => \$printall, | ||||||||||||||||||||||||||||
| 29 | "notwoadapters" => \$notwoadapters, | ||||||||||||||||||||||||||||
| 30 | "id2adapters" => \$id2adapters, | ||||||||||||||||||||||||||||
| 31 | "adapter3prime=s" => \$adapter3prime, | ||||||||||||||||||||||||||||
| 32 | ); | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 2 | ▶ | if (!$options) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | 0 | print "couldn't parse options\n"; | |||||||||||||||||||||||||||
| 36 | 0 | exit(); | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 2 | ▶ | if ($help) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | 0 | print <<HELP; | |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | "infile=s" => \$infile, | ||||||||||||||||||||||||||||
| 43 | "outfile=s" => \$outfile, | ||||||||||||||||||||||||||||
| 44 | "overwrite" => \$overwrite, | ||||||||||||||||||||||||||||
| 45 | "adapterseq=s" => \$adapterseq, | ||||||||||||||||||||||||||||
| 46 | "help" => \$help, | ||||||||||||||||||||||||||||
| 47 | "verbose" => \$verbose, | ||||||||||||||||||||||||||||
| 48 | "debug" => \$debug, | ||||||||||||||||||||||||||||
| 49 | "fastq" => \$fastq, | ||||||||||||||||||||||||||||
| 50 | "idlist" => \$idlist, | ||||||||||||||||||||||||||||
| 51 | "idlistfile=s" => \$idlistfile, | ||||||||||||||||||||||||||||
| 52 | "printall" => \$printall, # print all sequences, even if they don't contain adapter sequence | ||||||||||||||||||||||||||||
| 53 | "notwoadapters" => \$notwoadapters, # skip sequences that contain > 1 adapter sequence, even if --printall | ||||||||||||||||||||||||||||
| 54 | "id2adapters" => \$id2adapters, # output sequences with >1 adapter to STDERR, requires --notwoadapters | ||||||||||||||||||||||||||||
| 55 | "adapter3prime=s" => \$adapter3prime, | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | notes: | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | --notwoadapters filters out sequences with more than one adapter sequence. Typically, these reads are composed | ||||||||||||||||||||||||||||
| 60 | almost exclusively of adapter sequence, so "two" usually means "at least two". You can see the sequences that | ||||||||||||||||||||||||||||
| 61 | are filtered using --id2adapters, which directs those sequences to STDERR. | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | ie: adapter_trim.pl --infile set1.fq --fastq --outfile trimmed1.fq --adapterseq AAGCAGTGGTATCAACGCAGAGTAC --notwoadapters --id2adapters >& 2adapters.txt | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | HELP | ||||||||||||||||||||||||||||
| 66 | 0 | exit; | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 2 | ▶ | $infile = 'infile' unless ($infile); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 2 | ▶ | $outfile = 'outfile' unless ($outfile); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | 2 | ▶ | $idlistfile = 'idlist' unless ($idlistfile); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 2 | ▶ | if (-e $outfile && !$overwrite) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
-e $outfile and not $overwrite
MC/DC0%0/2
-e $outfile and not $overwrite
-e $outfile
not $overwrite
Truth table33%1/3
A and not(B)
A= -e $outfileB= not $overwrite
| |||||||||||||||||||||||||||||
| 74 | 0 | print "$outfile already exists and you didn't specify to overwrite\n"; | |||||||||||||||||||||||||||
| 75 | 0 | exit(); | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 2 | ▶ | if (!$adapterseq && !$adapter3prime) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$adapterseq or $adapter3prime
MC/DC0%0/2
$adapterseq or $adapter3prime
$adapterseq
$adapter3prime
Truth table33%1/3
A or B
A= $adapterseqB= $adapter3prime
| |||||||||||||||||||||||||||||
| 79 | 0 | print "you must enter a adapter sequence using either the --adapterseq argument or the --adapter3prime argument\n"; | |||||||||||||||||||||||||||
| 80 | 0 | exit(); | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 2 | ▶ | open(IN,$infile) or die "can't open '$infile': $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 2 | ▶ | open(OUT,">$outfile") or die "can't open '$outfile': $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 2 | ▶ | if ($idlist) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | 0 | ▶ | open(ID,">$idlistfile") or die "can't open 'idlist': $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 2 | my ($seqname, $seq, $parsed); | |||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 2 | ▶ | if (!$fastq) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | 0 | ▶ | while (<IN>) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | 0 | ▶ | print $_ if ($debug); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 0 | ▶ | if ($_ =~ /^>(.+)\n/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | 0 | $seqname = $1; | |||||||||||||||||||||||||||
| 98 | 0 | next; | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 0 | chomp($_); | |||||||||||||||||||||||||||
| 102 | 0 | $seq = \$_; | |||||||||||||||||||||||||||
| 103 | # print "seq '$seqname' = '" . $$seq . "'\n" if ($debug); | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 0 | ▶ | if ($$seq =~ /^$adapterseq/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | 0 | ▶ | print "parsed read:\n" if ($debug); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | 0 | $parsed = $$seq; | |||||||||||||||||||||||||||
| 108 | 0 | $parsed =~ s/^$adapterseq//; | |||||||||||||||||||||||||||
| 109 | 0 | ▶ | print "\t>$seqname\n\t$parsed\n" if ($debug); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | 0 | print OUT ">$seqname\n$parsed\n"; | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | } else { | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 2 | my ($quality,$sequence,$qualname); | |||||||||||||||||||||||||||
| 117 | # | ||||||||||||||||||||||||||||
| 118 | # loop through file, collect sequence ID, sequence and quality string | ||||||||||||||||||||||||||||
| 119 | # trim sequence if necessary | ||||||||||||||||||||||||||||
| 120 | # if sequence gets trimmed, trim same number of characters from quality string | ||||||||||||||||||||||||||||
| 121 | # print trimmed fastq sequence and quality string | ||||||||||||||||||||||||||||
| 122 | # | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 2 | ▶ | while (<IN>) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | 12 | ▶ | if ($_ =~ /^\@(.+)\n/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | 12 | $seqname = $1; | |||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 12 | $sequence = <IN>; | |||||||||||||||||||||||||||
| 129 | 12 | chomp($sequence); | |||||||||||||||||||||||||||
| 130 | 12 | $qualname = <IN>; | |||||||||||||||||||||||||||
| 131 | 12 | $qualname =~ s/[\+\n]//g; | |||||||||||||||||||||||||||
| 132 | 12 | $quality = <IN>; | |||||||||||||||||||||||||||
| 133 | 12 | chomp($quality); | |||||||||||||||||||||||||||
| 134 | 12 | ▶ | if ($debug) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | 0 | print "sequence: '$sequence'\n"; | |||||||||||||||||||||||||||
| 136 | 0 | print "quality: '$quality'\n"; | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 12 | ▶ | if ($sequence =~ /^$adapterseq/) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | # If debug ... | ||||||||||||||||||||||||||||
| 143 | 8 | ▶ | print "seqname:\t'$seqname'\nsequence:\t'$sequence'\nqualname:\t'$qualname'\nquality:\t'$quality'\n\n" if ($debug); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | # Remove 5' adapter sequence | ||||||||||||||||||||||||||||
| 146 | 8 | $sequence =~ s/$adapterseq//; | |||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | # Remove quality scores for the adapter sequence | ||||||||||||||||||||||||||||
| 149 | 8 | $quality = substr($quality,length($adapterseq)); | |||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | # If 3' adapter is defined, then check for it | ||||||||||||||||||||||||||||
| 152 | 8 | ▶ | if ($adapter3prime) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 153 | 4 | ▶ | if ( $sequence =~ /$adapter3prime$/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | # Calculate length of sequence sans the adapter | ||||||||||||||||||||||||||||
| 156 | 2 | my $insert_length = length($sequence) - length($adapter3prime); | |||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | # Remove sequence and quality for the adapter | ||||||||||||||||||||||||||||
| 159 | 2 | $sequence = substr( $sequence, 0, $insert_length ); | |||||||||||||||||||||||||||
| 160 | 2 | $quality = substr( $quality, 0, $insert_length ); | |||||||||||||||||||||||||||
| 161 | }else{ | ||||||||||||||||||||||||||||
| 162 | # Skip sequence since 3' adapter is not found | ||||||||||||||||||||||||||||
| 163 | 2 | next; | |||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | 6 | ▶ | if ($notwoadapters) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 168 | 0 | ▶ | if (index($sequence,$adapterseq) >= 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 169 | 0 | ▶ | print STDERR "\@$seqname\n$sequence\n\+$seqname\n$quality\n" if ($id2adapters); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 170 | 0 | next; | |||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | 6 | ▶ | print "seqname:\t'$seqname'\nsequence:\t'$sequence'\nqualname:\t'$qualname'\nquality:\t'$quality'\n\n" if ($debug); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | 6 | print OUT "\@$seqname\n$sequence\n\+$seqname\n$quality\n"; | |||||||||||||||||||||||||||
| 177 | 6 | ▶ | print ID "$seqname\n" if ($idlist); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | } elsif ($printall) { | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | 0 | print OUT "\@$seqname\n$sequence\n\+$seqname\n$quality\n"; | |||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | 2 | close(IN); | |||||||||||||||||||||||||||
| 190 | 2 | close(OUT); | |||||||||||||||||||||||||||
| 191 | 2 | ▶ | close(ID) if ($idlist); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||