Unconfigured Ad

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • NicoBxl
    not just another member
    • Aug 2010
    • 264

    #1

    filter sequence by length

    Hi,

    To filter sequencesby length from a fasta file, I'm using now a bioperl script. But is there an another method (faster) ?

    here's my little script


    Code:
    #!/usr/bin/perl
    
    use warnings;
    use strict;
    use Bio::SeqIO;
    
    
    my $file = $ARGV[0]; 
    my $min = $ARGV[1];
    my $max = $ARGV[2];
    my $out = $ARGV[3];
    
    open (FILE, ">>$out") or die ("Error : Cannot open file $out for writing..!\n");
    
    my $seq_in  = Bio::SeqIO->new( -format => 'fasta',-file => $file);
    
    while( my $seq1 = $seq_in->next_seq() ) {	
    	
    	my $id  = $seq1->primary_id;
    	chomp $id;
    	my $seq = $seq1->seq;
    	chomp $seq;
    	my $lseq = length($seq);
    	if($lseq>=$min && $lseq <=$max){
    		print FILE ">",$id,"\n",$seq,"\n";	
    	}
    }
  • Giorgio C
    Member
    • Oct 2010
    • 89

    #2
    You could try Galaxy:

    Galaxy is a community-driven web-based analysis platform for life science research.


    It's very fast and you can do different type of analysis.

    Comment

    • NicoBxl
      not just another member
      • Aug 2010
      • 264

      #3
      Thanks,

      is it possible to have this tool in command line ?

      Comment

      • Giorgio C
        Member
        • Oct 2010
        • 89

        #4
        From Galaxy i don't think is possible. But probably if you search you may find a similar script.

        Comment

        • maasha
          Senior Member
          • Apr 2009
          • 153

          #5
          With Biopieces (www.biopieces.org) you can do:

          Code:
          read_fasta -i test.fna | grab -e 'SEQ_LEN > 10' | grab -e 'SEQ_LEN <= 100' | write_fasta -x
          Cheers,


          Martin

          Comment

          • tnabtaf
            Member
            • Jan 2011
            • 53

            #6
            Originally posted by Giorgio C View Post
            From Galaxy i don't think is possible. But probably if you search you may find a similar script.
            This is a command line tool that is included in the Galaxy distribution. I've included the code below.

            To get any tool that is included with Galaxy, follow the instructions at http://getgalaxy.org/. Tools are defined in the tools directory. Many will have dependencies (although the one below does not).

            Code:
            #!/usr/bin/env python
            """
            Input: fasta, minimal length, maximal length
            Output: fasta
            Return sequences whose lengths are within the range.
            """
            
            import sys, os
            
            assert sys.version_info[:2] >= ( 2, 4 )
            
            def stop_err( msg ):
                sys.stderr.write( msg )
                sys.exit()
            
            def __main__():
                input_filename = sys.argv[1]
                try:
                    min_length = int( sys.argv[2] )
                except:
                    stop_err( "Minimal length of the return sequence requires a numerical value." )
                try:
                    max_length = int( sys.argv[3] )
                except:
                    stop_err( "Maximum length of the return sequence requires a numerical value." )
                output_filename = sys.argv[4]
                output_handle = open( output_filename, 'w' )
                tmp_size = 0 #-1
                tmp_buf = ''
                at_least_one = 0
                for line in file(input_filename):
                    if not line or line.startswith('#'):
                        continue
                    if line[0] == '>':
                        if min_length <= tmp_size <= max_length or (min_length <= tmp_size and max_length == 0):
                            output_handle.write(tmp_buf)
                            at_least_one = 1
                        tmp_buf = line
                        tmp_size = 0                                                       
                    else:
                        if max_length == 0 or tmp_size < max_length:
                            tmp_size += len(line.rstrip('\r\n'))
                            tmp_buf += line
                # final flush of buffer
                if min_length <= tmp_size <= max_length or (min_length <= tmp_size and max_length == 0):
                    output_handle.write(tmp_buf.rstrip('\r\n'))
                    at_least_one = 1
                output_handle.close()
                if at_least_one == 0:
                    print "There is no sequence that falls within your range."
            
            if __name__ == "__main__" : __main__()

            Comment

            • Giorgio C
              Member
              • Oct 2010
              • 89

              #7
              Very useful. Thanks

              Comment

              Latest Articles

              Collapse

              • SEQadmin2
                Beyond CRISPR/Cas9: Understand, Choose, and Use the Right Genome Editing Tool
                by SEQadmin2



                CRISPR/Cas9 sparked the gene editing revolution for both research and therapeutics.1 But this system still showed severe issues that limited its applications. The most prominent were the heavy reliance on PAM sequences, delivery limitations, double-stranded breaks that prompt unintended edits and cell death, and editing inefficiency (both in targeting and in knock-in reliability).

                Despite this, “CRISPR helped turn genome editing from a specialized technique into
                ...
                07-31-2026, 11:01 AM
              • SEQadmin2
                Proteomic Platforms: How to Choose the Right Analytical Strategy to Improve Detection and Clinical Applications
                by SEQadmin2


                Proteomics platforms are evolving rapidly, with advances in mass spectrometry and affinity-based approaches expanding what researchers can detect and at what scale. As the field moves toward deeper proteome coverage and clinical applications, scientists face an increasingly complex landscape of tools. This article will explore how researchers are navigating these choices to find the right platform for their work.

                The systematic characterization of the human proteome has
                ...
                07-20-2026, 11:48 AM

              ad_right_rmr

              Collapse

              News

              Collapse

              Topics Statistics Last Post
              Started by SEQadmin2, 08-06-2026, 07:41 AM
              0 responses
              17 views
              0 reactions
              Last Post SEQadmin2  
              Started by SEQadmin2, 08-03-2026, 10:13 AM
              0 responses
              33 views
              0 reactions
              Last Post SEQadmin2  
              Started by SEQadmin2, 07-31-2026, 02:55 AM
              0 responses
              43 views
              0 reactions
              Last Post SEQadmin2  
              Started by SEQadmin2, 07-24-2026, 12:17 PM
              0 responses
              26 views
              0 reactions
              Last Post SEQadmin2  
              Working...