Unconfigured Ad

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts
  • ekg
    Member
    • Apr 2010
    • 36

    FastaHack - FASTA file manipulation and subsequence extraction utilities

    I recently completed a C++ library and command-line utility which can be used to index and rapidly extract sequences and subsequences from FASTA files.

    I wanted to publicize the work here as it has been quite useful to me and a number of my fellow lab members.

    The utilities can be obtained using git via the project repository on github:



    Please let me know if obtaining the repository via git is a problem, and I will make a tarball release.

    What follows is the FastaHack README:



    fastahack --- *fast* FASTA file indexing, subsequence and sequence extraction

    Author: Erik Garrison <[email protected]>, Marth Lab, Boston College
    Date: May 7, 2010


    Overview:

    fastahack is a small application for indexing and extracting sequences and
    subsequences from FASTA files. The included Fasta.cpp library provides a FASTA
    reader and indexer that can be embedded into applications which would benefit
    from directly reading subsequences from FASTA files. The library automatically
    handles index file generation and use.


    Features:

    - FASTA index (.fai) generation for FASTA files
    - Sequence extraction
    - Subsequence extraction
    - Sequence statistics (TODO: currently only length is provided)

    Sequence and subsequence extraction use fseek64 to provide fastest-possible
    extraction without RAM-intensive file loading operations. This makes fastahack
    a useful tool for bioinformaticists who need to quickly extract many
    subsequences from a reference FASTA sequence.


    Notes:

    The index files generated by this system should be numerically equivalent to
    those generated by samtools (http://samtools.sourceforge.net/). However, while
    samtools truncates sequence names in the index file, fastahack provides them
    completely.

    To simplify use, sequences can be addressed by first whitespace-separated
    field; e.g. "8 SN(Homo sapiens) GA(HG18) URI(NC_000008.9)" can be addressed
    simply as "8", provided "8" is a unique first-field name in the FASTA file.
    Thus, to extract 20bp starting at position 323202 in chromosome 8 from the
    human reference:

    % fastahack subsequence h.sapiens.fasta 8 323202 20
    ACATTGTAATAGATCTCAGA

    Usage information is provided by running fastahack with no arguments:

    % fastahack
    usage: fastahack <command> [options]
    actions:
    index <fasta reference>
    sequence <fasta reference> <sequence name>
    subsequence <fasta reference> <sequence name> <0-based start> <length>
    stats <fasta reference> <sequence name> (returns sequence length)


    Limitations:

    fastahack will only generate indexes for FASTA files in which the sequences
    have self-consistent line lengths. Trailing whitespace is allowed at the end
    of sequences, but not embedded within the sequence. These limitations are
    necessitated by the complexity of indexing sequences whose lines change in
    length--- the use of indexes is frustrated by such inconsistencies; each change
    in line length would require a new entry in the index file.
  • ShellfishGene
    Member
    • Mar 2009
    • 14

    #2
    Personally I would find you program even more useful if there was an option to pipe sequence IDs and get the output on stdout!
    I've been using bioperl in a simple script to do that, but a C++ program with more features would be nice, too!

    Code:
    #!/usr/bin/perl
    use strict;
    use warnings;
    use Bio::DB::Fasta;
    
    my $file = shift;
    
    unless ( $file && -e $file ) { print "Usage: echo 'seq1:5..15' | get_seq.pl sequences.fasta\n      echo 'seq1' | get_seq.pl sequences.fasta\n"; exit; }
    
    my $db = Bio::DB::Fasta->new( $file );
    
    while (<>){
      my $query = $_;
      chomp $query;
    
      my $sequence;
      if ( $query =~ /:/ ) {
        $query =~ /^(.+):(\d+)\.\.(\d+)/;
        unless ( $1 && $2 && $3 ) {
          die "problem parsing request string.\n";
        }
    
        $sequence = $db->seq($1, $2 => $3);
      }
      else {
        $sequence = $db->seq($query);
      }
    
      unless ( $sequence ) { die "Sequence $query not found. \n" }
      print ">$query\n", "$sequence\n";
    
    }

    Comment

    • ekg
      Member
      • Apr 2010
      • 36

      #3
      Originally posted by ShellfishGene View Post
      Personally I would find you program even more useful if there was an option to pipe sequence IDs and get the output on stdout!
      I've been using bioperl in a simple script to do that, but a C++ program with more features would be nice, too!
      That's a great idea. Presently you can get the same effect by using xargs, but it would be quite a bit more cumbersome. I've thought of using a BED file as input to similar effect.

      I like the sequence and subsequence specification syntax that the utility you posted uses. I will probably adapt that into FastaHack. Does it use 0 or 1-based coordinates?

      Comment

      • ShellfishGene
        Member
        • Mar 2009
        • 14

        #4
        Originally posted by ekg View Post
        I like the sequence and subsequence specification syntax that the utility you posted uses. I will probably adapt that into FastaHack. Does it use 0 or 1-based coordinates?
        The syntax is actually quite widespread I think, Ensembl uses it for example. It is 1-based. One variation, used on the UCSC browser pages, is to use - instead of '..'. You might want to support both.

        Comment

        • mslider
          Junior Member
          • Sep 2010
          • 25

          #5
          another option

          maybe usefull to add a parameter to only count the number of sequence...
          when you have million of sequence, grep -c "^>" is very low !

          Comment

          • SES
            Senior Member
            • Mar 2010
            • 275

            #6
            Originally posted by mslider View Post
            maybe usefull to add a parameter to only count the number of sequence...
            when you have million of sequence, grep -c "^>" is very low !
            I disagree with part of this statement. There are myriad ways to index a fasta and these usually take a few seconds to a few minutes for millions of sequences. Then counting can take seconds. I just used grep to count 2.2 million 454 sequences and it took 13 seconds and did not create any huge index files. I would argue that grep is probably faster than creating an index then counting (in terms of overall time spent), but others may not agree. I agree that returning sequence stats from an index seems natural if you already have the index and it looks like this is on the author's to do list.

            Comment

            • Lee Sam
              Member
              • Oct 2008
              • 57

              #7
              Thanks for contributing this! It's literally exactly what I need.

              Comment

              • avilella
                Member
                • Mar 2009
                • 34

                #8
                There are two utilities that I am missing in current methods: I am using cdbfasta/cdbyank to index fastq files, but I would like to be able to compress the fastq file so that it takes up less space, even if it means a slower retrieval time. I would also like to be able to send a large number of ids, and retrieve the complement from them: the list of ids in the fastq file but not in the id list.

                Comment

                • mslider
                  Junior Member
                  • Sep 2010
                  • 25

                  #9
                  extract just subsequence

                  If you just want to extract a subsequence from a big sequence like a chromosome,
                  the program below is more faster and without creating index file:


                  Code:
                  #include<iostream>
                  #include<string.h>
                  #include<fstream>
                  #include <stdlib.h>
                  using namespace std;
                  
                   /* Steps:-
                  1- Download FASTA file and then remove the header. (>asdasfdasfassa)
                  2- Remove new lines from FASTA file. (using sed or perl)
                  3- Then you can use the C++  program like this in linux:
                  
                  ./ExtractSequence inputfilename start stop
                  */
                  
                    int GetIntVal(string strConvert) {
                                int intReturn;
                                intReturn = atoi(strConvert.c_str());
                                return(intReturn);
                    }
                  
                  int main(int argc ,char* argv[]){
                  
                         string line1;
                  	   ifstream myFile(argv[1]);
                  	   if(! myFile){
                  	      cout << "Error opening file" << endl;
                  		  return -1;
                  	   }
                  	   while(! myFile.eof()){
                  	       getline(myFile, line1);
                  
                  			 string r1 = argv[2];
                  			 string r2 = argv[3];
                  			 int range1 = GetIntVal(r1);
                  			 int range2 =  GetIntVal(r2)- range1;
                  			 cout << ">Sample Sequence" << endl;
                  			 cout << line1.substr(range1,range2) << endl;
                  	   }
                  	   myFile.close();
                      return 0;
                  }

                  Comment

                  • Thomas Doktor
                    Senior Member
                    • Apr 2009
                    • 105

                    #10
                    BEDTools' fastaFromBed utility allows you to extract (sub)sequences from a FASTA file using a BED/GFF/VCF file with intervals as input. It also supports strand specific sequence queries so you can extract strand specific features, such as exons.
                    BEDTools: http://code.google.com/p/bedtools/

                    Comment

                    • shaldenby
                      Junior Member
                      • Mar 2012
                      • 1

                      #11
                      This is a really useful little tool. Thanks very much!

                      Comment

                      • mattanswers
                        Member
                        • Oct 2009
                        • 65

                        #12
                        Thank you ekg for your fastahack tool. The tool seems to extract the sequence by its position in the fasta file. I was wondering if it can extract a provided subsequence from the fasta file, and if so, what if the provided subsequence occurs multiple times in the fasta file ?

                        Comment

                        • ekg
                          Member
                          • Apr 2010
                          • 36

                          #13
                          @mattanswers This sounds like a job for your favorite aligner. For short sequences, you can use smith-waterman (https://github.com/ekg/smithwaterman) but for bigger stuff I'd use something like blat or encode your sequences in FASTA and align them.

                          As for multiple mappings, you'll have to find a mapper that generates them. MOSAIK does, and I believe so does MrsFast.

                          Comment

                          • mattanswers
                            Member
                            • Oct 2009
                            • 65

                            #14
                            Thank you for your help, and also for writing and sharing FastaHack.

                            Comment

                            Latest Articles

                            Collapse

                            ad_right_rmr

                            Collapse

                            News

                            Collapse

                            Topics Statistics Last Post
                            Started by SEQadmin2, 06-09-2026, 11:58 AM
                            0 responses
                            24 views
                            0 reactions
                            Last Post SEQadmin2  
                            Started by SEQadmin2, 06-05-2026, 10:09 AM
                            0 responses
                            29 views
                            0 reactions
                            Last Post SEQadmin2  
                            Started by SEQadmin2, 06-04-2026, 08:59 AM
                            0 responses
                            39 views
                            0 reactions
                            Last Post SEQadmin2  
                            Started by SEQadmin2, 06-02-2026, 12:03 PM
                            0 responses
                            61 views
                            0 reactions
                            Last Post SEQadmin2  
                            Working...