http://www.perlmonks.org?node_id=976580


in reply to Analyse an array and join only some elements of it

From your description, it sounds like you are parsing a 'fasta' format file line by line and, if it is evenly divisible by 3, join that to a string of sequences passing that test. I'm not sure, but if a single line of the sequence is divisible by 3, the entire sequence may not. The method below reads in an entire sequence at a time, and checks for mod 3 == 0, (evenly divisible by 3).

#!/usr/bin/perl use strict; use warnings; use Bio::SeqIO; my $in = Bio::SeqIO->new( -file => "input1.txt" , -format => 'fasta'); my ($string, $valid, $invalid) = ('', 0, 0); while ( my $seq = $in->next_seq() ) { if ($seq->length % 3 == 0) { $string .= $seq->seq; ++$valid; } else { ++$invalid; } } open LOGFILE, ">", 'somefile' or die "Unable to open 'somefile' for wr +iting. $!"; print LOGFILE "Found ", $valid + $invalid, " blocks, joined $valid of +them and left out $invalid.\n"; close LOGFILE or die $!;

Chris

Replies are listed 'Best First'.
Re^2: Analyse an array and join only some elements of it
by Istirion (Initiate) on Jun 18, 2012 at 16:02 UTC
    Hi again! Thank you very much for all of your help and really good ideas. I will try all of them and see which works best in my programm. Christoforo, you're completely right: the input file is an annotated fasta file, which I have to read line by line and analyse for Codon Usage and so on. I didn't use Bioperl for creating a seqobject but instead did all the work by myself. :-) Your wellthought objection can be clearly answered, I think. If all parts are divisible by three the entire sequenz should be also divisible by three, because it consists of parts which are divisible by three. Thanks again, Markus