<?xml version='1.0'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:georss="http://www.georss.org/georss" xmlns:atom="http://www.w3.org/2005/Atom" >
<channel>
	<title><![CDATA[BOL: Extract fasta sequence from a multifasta file with coordinates]]></title>
	<link>https://bioinformaticsonline.com/snippets/view/33316/extract-fasta-sequence-from-a-multifasta-file-with-coordinates?</link>
	<atom:link href="https://bioinformaticsonline.com/snippets/view/33316/extract-fasta-sequence-from-a-multifasta-file-with-coordinates?" rel="self" type="application/rss+xml" />
	<description><![CDATA[]]></description>
	
	<item>
	<guid isPermaLink="true">https://bioinformaticsonline.com/snippets/view/33316/extract-fasta-sequence-from-a-multifasta-file-with-coordinates</guid>
	<pubDate>Tue, 30 May 2017 05:35:07 -0500</pubDate>
	<link>https://bioinformaticsonline.com/snippets/view/33316/extract-fasta-sequence-from-a-multifasta-file-with-coordinates</link>
	<title><![CDATA[Extract fasta sequence from a multifasta file with coordinates]]></title>
	<description><![CDATA[<code>#!/usr/bin/perl

use Bio::DB::Fasta;

#USAGE
perl extractFASTAwithSIZE.pl finalSample_filtered.fa 0 1000 &gt; aaaaaa.fa

my $fastaFile = shift;
my $querySizeST = shift;
my $querySizeED = shift;

my $db = Bio::DB::Fasta-&gt;new( $fastaFile );
my @ids = $db-&gt;get_all_primary_ids;

foreach my $id (@ids) {
    my $sequence = $db-&gt;seq($id, $querySizeST =&gt; $querySizeED);
    if  (!defined( $sequence )) {
            die &quot;Sequence $seq not found. \n&quot; 
    }   
    print &quot;&gt;$id\n&quot;, &quot;$sequence\n&quot;;
}

__END__

use Bio::DB::Fasta;

  # Create database from a directory of Fasta files
  my $db       = Bio::DB::Fasta-&gt;new(&#039;/path/to/fasta/files/&#039;);
  my @ids      = $db-&gt;get_all_primary_ids;

  # Simple access
  my $seqstr   = $db-&gt;seq(&#039;CHROMOSOME_I&#039;, 4_000_000 =&gt; 4_100_000);
  my $revseq   = $db-&gt;seq(&#039;CHROMOSOME_I&#039;, 4_100_000 =&gt; 4_000_000);
  my $length   = $db-&gt;length(&#039;CHROMOSOME_I&#039;);
  my $header   = $db-&gt;header(&#039;CHROMOSOME_I&#039;);
  my $alphabet = $db-&gt;alphabet(&#039;CHROMOSOME_I&#039;);

  # Access to sequence objects. See Bio::PrimarySeqI.
  my $seq     = $db-&gt;get_Seq_by_id(&#039;CHROMOSOME_I&#039;);
  my $seqstr  = $seq-&gt;seq;
  my $subseq  = $seq-&gt;subseq(4_000_000 =&gt; 4_100_000);
  my $trunc   = $seq-&gt;trunc(4_000_000 =&gt; 4_100_000);
  my $length  = $seq-&gt;length;

  # Loop through sequence objects
  my $stream  = $db-&gt;get_PrimarySeq_stream;
  while (my $seq = $stream-&gt;next_seq) {
    # Bio::PrimarySeqI stuff
  }

  # Filehandle access
  my $fh = Bio::DB::Fasta-&gt;newFh(&#039;/path/to/fasta/files/&#039;);
  while (my $seq = &lt;$fh&gt;) {
    # Bio::PrimarySeqI stuff
  }

  # Tied hash access
  tie %sequences,&#039;Bio::DB::Fasta&#039;,&#039;/path/to/fasta/files/&#039;;
  print $sequences{&#039;CHROMOSOME_I:1,20000&#039;};</code>]]></description>
	<dc:creator>Jit</dc:creator>
</item>

</channel>
</rss>