title: Extract sequence fragments from FASTA file using coordinates on a GRanges object
body: <p>I need to extract the intergenic sequences of Bacillus Subtilis.</p>
  
  <p>I have the full DNA sequence of B.subtilis in R, imported with seqinr.
  I imported the dna sequence into R using the function read.fasta of the package seqinr;</p>
  
  <p>I then created a GRanges object from Bacillus subtilis genbank file using the package "genbankr" to extract intergenic coordinates.</p>
  
  <p>This is the format of my GRanges Object with intergenic coordinates:</p>
  
  <pre><code>GRanges object with 3841 ranges and 1 metadata column:
    seqnames             ranges strand |             intergenic_id
       &lt;Rle&gt;          &lt;IRanges&gt;  &lt;Rle&gt; |               &lt;character&gt;
    168      168       [   1,  409]      * | intergenic_SEQ-BEGIN_dnaA
    168      168       [1751, 1938]      * |      intergenic_dnaA_dnaN
    168      168       [3076, 3205]      * |      intergenic_dnaN_yaaA
    168      168       [3422, 3436]      * |      intergenic_yaaA_recF
    168      168       [4550, 4566]      * |      intergenic_recF_yaaB
    ...      ...                ...    ... .                       ...
  </code></pre>
  
  <p>Therefore in R I have:
   "x" (the full DNA sequence imported with seqinr) and "intergenic" (the GRanges Object with coordinates)</p>
  
  <p>I have seen people asking similar question in this forum, I have tried to follow those answers unsuccessfully by using a variety of packages but I can't figure it out. I am hopeful there is a easy solution for this; any help is appreciated.</p>
  
  <p>My desired output would be to produce a fasta file with the intergenic sequences in the following format:</p>
  
  <pre><code>&gt;intergenic_SEQ-BEGIN_dnaA
  ATATATATATTATTTATTTTTTTTTTTTATTATAT
  &gt;intergenic_dnaA_dnaN
  ATATATCGCGTCGATCTAGACTCAGGCATG
  etc.
  </code></pre>
  
  <p>Basically a line with the name of the intergenic sequence taken from the intergenic_id name column of my GRanges object followed by the sequence extracted from the fasta using the coordinates in the GRanges.</p>
  
  <p>NOTE: in the desired output i just typed some random sequences, just as example.</p>