Answers.Body: <p>In case an answer would still be relevant after a year and a half, below is a solution with the <code>Biostrings</code> package.</p>
  
  <pre><code>library(Biostrings)
  
  # loading data.table for the fread() function.
  # fread() is not only fast but is quite good at guessing how
  # the file is formatted
  library(data.table)
  
  # readDNAStringSet() function creates a "DNAStringSet" object
  my_fasta &lt;- readDNAStringSet("fasta.fa")
  
  &gt; my_fasta
    A DNAStringSet instance of length 4
      width seq                                                                names               
  [1]    61 TTTCACCGTAAATTAACTTTGAGAGGAGCTAATTCCTAAAAGAATTATACCGGCCTATTTG      rnd-5_family-5445...
  [2]   485 AATCTGAGATTAGATTTATTTATCTGTTATCC...GGCCAATCATTCAAAAGTTATGAAAGTGATC rnd-5_family-2614...
  [3]   215 TTTCTAGTTTGTATTTGAGGAAAGAAAATCCT...TAGAAATTGTAATTTACAAAATGAATTACTT rnd-5_family-2614...
  [4]    87 ATGCTTAATTTGCTAAGCATTTAATAACCTTT...GTAGACATTAAAATAAACCTTATACTGCTAT rnd-4_family-798_...
  
  # the fasta header is stored at the @NAMES slot of the @ranges slot of the
  # DNAStringSet object created above
  &gt; my_fasta@ranges@NAMES
  [1] "rnd-5_family-5445_Unspecified(-)" "rnd-5_family-2614_Unspecified(-)"
  [3] "rnd-5_family-2614_Unspecified(-)" "rnd-4_family-798_Unspecified(-)"
  
  # removing the trailing "_Unpecified..."
  my_fasta@ranges@NAMES &lt;- sub("_Unspecified.*",
                                "",
                                my_fasta@ranges@NAMES)
  
  my_coord &lt;- fread("repeat_masker.txt")
  &gt; my_coord
             V1   V2   V3                            V4 V5 V6
  1: KB824701.1  417  478 rnd-5_family-5445_Unspecified  .  -
  2: KB824701.1  587 1072 rnd-5_family-2614_Unspecified  .  -
  3: KB824701.1  914 1129 rnd-5_family-2614_Unspecified  .  -
  4: KB824701.1 1138 1225  rnd-4_family-798_Unspecified  .  -
  #removing the "&gt;" sign
  
  my_coord<span class="math-container">$V1 &lt;- sub("&gt;",
                     "",
                     my_coord$</span>V1)
  
  # appending first 3 columns of the coordinate file to "NAMES" as requested by the OP
  my_fasta@ranges@NAMES &lt;- paste(my_fasta@ranges@NAMES,
                                 my_coord<span class="math-container">$V1,
                                 my_coord$</span>V2,
                                 my_coord$V3,
                                 sep = "_")
  
  # writing the modified DNAStringSet object into a fasta file
  writeXStringSet(my_fasta, "edited_fasta.fa")
  
  &gt;rnd-5_family-5445_KB824701.1_417_478
  TTTCACCGTAAATTAACTTTGAGAGGAGCTAATTCCTAAAAGAATTATACCGGCCTATTTG
  &gt;rnd-5_family-2614_KB824701.1_587_1072
  AATCTGAGATTAGATTTATTTATCTGTTATCCGCTCAGGTTGAAAAGTTTGCGCAATGTATAACTATAAAAATCTTTCTA
  CCACTTTTGATTCATTTTTTAAATCTGGGGTCACATTTTATTCACGGTAGAAATTGTAATTTACAAAATGAATTACTTGA
  AGGCAACACGAATCCAGAGTGATGCTTTACATAAATCTGCTTCTACCGATGCCAAAAATTGACGATATTCTATTATTTAA
  TCTAAATGTTAGTCTTTACATACCCTCCCCTAATTGTTAGAATTTTATGAAATTTGATTTCAGGGGTCAGTTTAGCATGC
  TAAATCTAATTCAATAGATTGATATTTTTCTTCAGGTAAAGAAAATTTTTGCGTCAAAGTAATCATATTCCTCCACGATT
  GCATATAACTATGGTATATAATTTAAAAGATTACACTTTACGTAATGAAAAATCGGCCAATCATTCAAAAGTTATGAAAG
  TGATC
  &gt;rnd-5_family-2614_KB824701.1_914_1129
  TTTCTAGTTTGTATTTGAGGAAAGAAAATCCTACTATCACTTTTTAAAAAATTCACCAATCTGAGATTAGATTTATTTAT
  CTGTTATCCGCTCAGGTTGAAAAGTTTGCGCAATGTATAACTATAAAAATCTTTCTACCACTTTTGATTCATTTTTTAAA
  TCTGGGGTCACATTTTATTCACGGTAGAAATTGTAATTTACAAAATGAATTACTT
  &gt;rnd-4_family-798_KB824701.1_1138_1225
  ATGCTTAATTTGCTAAGCATTTAATAACCTTTTTGGGTTTTGTGATAAAGGATGCTGTAGACATTAAAATAAACCTTATA
  CTGCTAT
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-09-09T18:57:40.833Z
Answers.LastActivityDate: 2019-09-09T18:57:40.833Z
Answers.UserDisplayName: haci
Body: <p>I have been asked to write a script in R to edit, using data.frame, this .fa file that is the result from extracting gene sequences from a repeat masker coordinate file on the genome I am working with. Each input looks as such:</p>
  
  <pre><code>&gt;rnd-5_family-5445_Unspecified(-)
  TTTCACCGTAAATTAACTTTGAGAGGAGCTAATTCCTAAAAGAATTATACCGGCCTATTTG
  &gt;rnd-5_family-2614_Unspecified(-)
  AATCTGAGATTAGATTTATTTATCTGTTATCCGCTCAGGTTGAAAAGTTTGCGCAATGTATAACTATA
  AAAATCTTTCTACCACTTTTGATTCATTTTTTAAATCTGGGGTCACATTTTATTCACGGTAGAAATTG
  TAATTTACAAAATGAATTACTTGAAGGCAACACGAATCCAGAGTGATGCTTTACATAAATCTGCTTCT
  ACCGATGCCAAAAATTGACGATATTCTATTATTTAATCTAAATGTTAGTCTTTACATACCCTCCCCTA
  ATTGTTAGAATTTTATGAAATTTGATTTCAGGGGTCAGTTTAGCATGCTAAATCTAATTCAATAGATT
  GATATTTTTCTTCAGGTAAAGAAAATTTTTGCGTCAAAGTAATCATATTCCTCCACGATTGCATATAA
  CTATGGTATATAATTTAAAAGATTACACTTTACGTAATGAAAAATCGGCCAATCATTCAAAAGTTATG
  AAAGTGATC 
  &gt;rnd-5_family-2614_Unspecified(-)
  TTTCTAGTTTGTATTTGAGGAAAGAAAATCCTACTATCACTTTTTAAAAAATTCACCAATCTGAGATT
  AGATTTATTTATCTGTTATCCGCTCAGGTTGAAAAGTTTGCGCAATGTATAACTATAAAAATCTTTCT
  ACCACTTTTGATTCATTTTTTAAATCTGGGGTCACATTTTATTCACGGTAGAAATTGTAATTTACAAA
  ATGAATTACTT 
  &gt;rnd-4_family-798_Unspecified(-)
  ATGCTTAATTTGCTAAGCATTTAATAACCTTTTTGGGTTTTGTGATAAAGGATGCTGTAGACATTAAA
  ATAAACCTTATACTGCTAT
  </code></pre>
  
  <p>This is the repeat masker coordinates that the above were derived from:</p>
  
  <pre><code>&gt;KB824701.1 417 478 rnd-5_family-5445_Unspecified . -
  &gt;KB824701.1 587 1072 rnd-5_family-2614_Unspecified . -
  &gt;KB824701.1 914 1129 rnd-5_family-2614_Unspecified . -
  &gt;KB824701.1 1138 1225 rnd-4_family-798_Unspecified . -
  </code></pre>
  
  <p>and ideally I would like it to be in the format of this, for example using the first one:</p>
  
  <pre><code>&gt;rnd-5_family-5445_KB824701.1_417_478
  </code></pre>
  
  <p>I was told to use data.frame in R but I am not sure exactly how this will work? Can someone help please</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2018-04-24T19:55:23.673Z
Comments.Text: Hi Noob, what have you done? Are you familiar with R or other shell programs? This seems as homework could you confirm it is so? I wouldn't like to post a solution to get you later accused to plagiarism... or worse, to provide an answer and you just used it without learning from it.
Comments.UserDisplayName: llrs
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2018-04-25T19:20:07.047Z
Comments.Text: It is not homework, it is actually a project I am being to asked to help work on while also learning to skills so I have a lot of trouble trying to work and learn at the same time. I used a bedtools command getfasta to extract the sequences from a genome I was given repeat masker coordinates for already
Comments.UserDisplayName: Noob
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2018-05-01T14:46:39.390Z
Comments.Text: Just checking that I understood correctly, you want to change from `>KB824701.1 417 478 rnd-5_family-5445_Unspecified . -` to `>rnd-5_family-5445_KB824701.1_417_478`? Or from  `>rnd-5_family-5445_Unspecified(-)` to `>rnd-5_family-5445_KB824701.1_417_478`. Are you comfortable with a other solutions not involving R ?
Comments.UserDisplayName: llrs
Tags: <r><ngs><phylogenetics><sequence-annotation>
Title: Help Adjusting Bedtools Output in R
ThreadText: # Help Adjusting Bedtools Output in R
  
  (Asked by: username_0 on 2018-04-24)
  
  I have been asked to write a script in R to edit, using data.frame, this .fa file that is the result from extracting gene sequences from a repeat masker coordinate file on the genome I am working with. Each input looks as such:
  
  ```
  >rnd-5_family-5445_Unspecified(-)
  TTTCACCGTAAATTAACTTTGAGAGGAGCTAATTCCTAAAAGAATTATACCGGCCTATTTG
  >rnd-5_family-2614_Unspecified(-)
  AATCTGAGATTAGATTTATTTATCTGTTATCCGCTCAGGTTGAAAAGTTTGCGCAATGTATAACTATA
  AAAATCTTTCTACCACTTTTGATTCATTTTTTAAATCTGGGGTCACATTTTATTCACGGTAGAAATTG
  TAATTTACAAAATGAATTACTTGAAGGCAACACGAATCCAGAGTGATGCTTTACATAAATCTGCTTCT
  ACCGATGCCAAAAATTGACGATATTCTATTATTTAATCTAAATGTTAGTCTTTACATACCCTCCCCTA
  ATTGTTAGAATTTTATGAAATTTGATTTCAGGGGTCAGTTTAGCATGCTAAATCTAATTCAATAGATT
  GATATTTTTCTTCAGGTAAAGAAAATTTTTGCGTCAAAGTAATCATATTCCTCCACGATTGCATATAA
  CTATGGTATATAATTTAAAAGATTACACTTTACGTAATGAAAAATCGGCCAATCATTCAAAAGTTATG
  AAAGTGATC 
  >rnd-5_family-2614_Unspecified(-)
  TTTCTAGTTTGTATTTGAGGAAAGAAAATCCTACTATCACTTTTTAAAAAATTCACCAATCTGAGATT
  AGATTTATTTATCTGTTATCCGCTCAGGTTGAAAAGTTTGCGCAATGTATAACTATAAAAATCTTTCT
  ACCACTTTTGATTCATTTTTTAAATCTGGGGTCACATTTTATTCACGGTAGAAATTGTAATTTACAAA
  ATGAATTACTT 
  >rnd-4_family-798_Unspecified(-)
  ATGCTTAATTTGCTAAGCATTTAATAACCTTTTTGGGTTTTGTGATAAAGGATGCTGTAGACATTAAA
  ATAAACCTTATACTGCTAT
  
  ```
  
  This is the repeat masker coordinates that the above were derived from:
  
  ```
  >KB824701.1 417 478 rnd-5_family-5445_Unspecified . -
  >KB824701.1 587 1072 rnd-5_family-2614_Unspecified . -
  >KB824701.1 914 1129 rnd-5_family-2614_Unspecified . -
  >KB824701.1 1138 1225 rnd-4_family-798_Unspecified . -
  
  ```
  
  and ideally I would like it to be in the format of this, for example using the first one:
  
  ```
  >rnd-5_family-5445_KB824701.1_417_478
  
  ```
  
  I was told to use data.frame in R but I am not sure exactly how this will work? Can someone help please
  
  **Tags:** r, ngs, phylogenetics, sequence-annotation
  
  > **Comments:**
  > **username_1**: Hi username_0, what have you done? Are you familiar with R or other shell programs? This seems as homework could you confirm it is so? I wouldn't like to post a solution to get you later accused to plagiarism... or worse, to provide an answer and you just used it without learning from it.  
  > **username_0**: It is not homework, it is actually a project I am being to asked to help work on while also learning to skills so I have a lot of trouble trying to work and learn at the same time. I used a bedtools command getfasta to extract the sequences from a genome I was given repeat masker coordinates for already  
  > **username_1**: Just checking that I understood correctly, you want to change from `>KB824701.1 417 478 rnd-5_family-5445_Unspecified . -` to `>rnd-5_family-5445_KB824701.1_417_478`? Or from  `>rnd-5_family-5445_Unspecified(-)` to `>rnd-5_family-5445_KB824701.1_417_478`. Are you comfortable with a other solutions not involving R ?  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2019-09-09. Score: 1
  
  In case an answer would still be relevant after a year and a half, below is a solution with the `Biostrings` package.
  
  ```
  library(Biostrings)
  
  # loading data.table for the fread() function.
  # fread() is not only fast but is quite good at guessing how
  # the file is formatted
  library(data.table)
  
  # readDNAStringSet() function creates a "DNAStringSet" object
  my_fasta <- readDNAStringSet("fasta.fa")
  
  > my_fasta
    A DNAStringSet instance of length 4
      width seq                                                                names               
  [1]    61 TTTCACCGTAAATTAACTTTGAGAGGAGCTAATTCCTAAAAGAATTATACCGGCCTATTTG      rnd-5_family-5445...
  [2]   485 AATCTGAGATTAGATTTATTTATCTGTTATCC...GGCCAATCATTCAAAAGTTATGAAAGTGATC rnd-5_family-2614...
  [3]   215 TTTCTAGTTTGTATTTGAGGAAAGAAAATCCT...TAGAAATTGTAATTTACAAAATGAATTACTT rnd-5_family-2614...
  [4]    87 ATGCTTAATTTGCTAAGCATTTAATAACCTTT...GTAGACATTAAAATAAACCTTATACTGCTAT rnd-4_family-798_...
  
  # the fasta header is stored at the @NAMES slot of the @ranges slot of the
  # DNAStringSet object created above
  > my_fasta@ranges@NAMES
  [1] "rnd-5_family-5445_Unspecified(-)" "rnd-5_family-2614_Unspecified(-)"
  [3] "rnd-5_family-2614_Unspecified(-)" "rnd-4_family-798_Unspecified(-)"
  
  # removing the trailing "_Unpecified..."
  my_fasta@ranges@NAMES <- sub("_Unspecified.*",
                                "",
                                my_fasta@ranges@NAMES)
  
  my_coord <- fread("repeat_masker.txt")
  > my_coord
             V1   V2   V3                            V4 V5 V6
  1: KB824701.1  417  478 rnd-5_family-5445_Unspecified  .  -
  2: KB824701.1  587 1072 rnd-5_family-2614_Unspecified  .  -
  3: KB824701.1  914 1129 rnd-5_family-2614_Unspecified  .  -
  4: KB824701.1 1138 1225  rnd-4_family-798_Unspecified  .  -
  #removing the ">" sign
  
  my_coord$V1 <- sub(">",
                     "",
                     my_coord$V1)
  
  # appending first 3 columns of the coordinate file to "NAMES" as requested by the OP
  my_fasta@ranges@NAMES <- paste(my_fasta@ranges@NAMES,
                                 my_coord$V1,
                                 my_coord$V2,
                                 my_coord$V3,
                                 sep = "_")
  
  # writing the modified DNAStringSet object into a fasta file
  writeXStringSet(my_fasta, "edited_fasta.fa")
  
  >rnd-5_family-5445_KB824701.1_417_478
  TTTCACCGTAAATTAACTTTGAGAGGAGCTAATTCCTAAAAGAATTATACCGGCCTATTTG
  >rnd-5_family-2614_KB824701.1_587_1072
  AATCTGAGATTAGATTTATTTATCTGTTATCCGCTCAGGTTGAAAAGTTTGCGCAATGTATAACTATAAAAATCTTTCTA
  CCACTTTTGATTCATTTTTTAAATCTGGGGTCACATTTTATTCACGGTAGAAATTGTAATTTACAAAATGAATTACTTGA
  AGGCAACACGAATCCAGAGTGATGCTTTACATAAATCTGCTTCTACCGATGCCAAAAATTGACGATATTCTATTATTTAA
  TCTAAATGTTAGTCTTTACATACCCTCCCCTAATTGTTAGAATTTTATGAAATTTGATTTCAGGGGTCAGTTTAGCATGC
  TAAATCTAATTCAATAGATTGATATTTTTCTTCAGGTAAAGAAAATTTTTGCGTCAAAGTAATCATATTCCTCCACGATT
  GCATATAACTATGGTATATAATTTAAAAGATTACACTTTACGTAATGAAAAATCGGCCAATCATTCAAAAGTTATGAAAG
  TGATC
  >rnd-5_family-2614_KB824701.1_914_1129
  TTTCTAGTTTGTATTTGAGGAAAGAAAATCCTACTATCACTTTTTAAAAAATTCACCAATCTGAGATTAGATTTATTTAT
  CTGTTATCCGCTCAGGTTGAAAAGTTTGCGCAATGTATAACTATAAAAATCTTTCTACCACTTTTGATTCATTTTTTAAA
  TCTGGGGTCACATTTTATTCACGGTAGAAATTGTAATTTACAAAATGAATTACTT
  >rnd-4_family-798_KB824701.1_1138_1225
  ATGCTTAATTTGCTAAGCATTTAATAACCTTTTTGGGTTTTGTGATAAAGGATGCTGTAGACATTAAAATAAACCTTATA
  CTGCTAT
  
  ```
  
  
  ---
  
  