Answers.Body: <p>An <code>awk</code> approach (assuming your input "csv" file is space-separated):</p>
  
  <pre><code>$ awk '(NR==FNR){a["&gt;"$1]=$2; next;} (/&gt;/){$1=$1" "a[$1]}1;' foo.csv foo.fa 
  &gt;Contig11 ref|XM_021487230.1|_PREDICTED:_Mizuhopecten_yessoensis_UNC93-like_protein_MFSD11_(LOC110443183),_mRNA ref|XM_021487230.1|_PREDICTED:_Mizuhopecten_yessoensis_UNC93-like_protein_MFSD11_(LOC110443183),_mRNA
  GACTCTCTTAAGGTAGCCAAATGCCTCGTCATCTAATTAGTGACGCGCATGAATGGATTA
  GACTCTCTTAAGGTAGCCAAATGCCTCGTCATCTAATTAGTGACGCGCATGAATGGATTA
  </code></pre>
  
  <h3>Explanation</h3>
  
  <ul>
  <li><code>NR==FNR</code> : <code>FNR</code> is the line number of the current input file and <code>NR</code> is the line number of all input. When processing more than one file, the two will only be equal while reading the first file. </li>
  <li><code>(NR==FNR){a["&gt;"$1]=$2; next;}</code> : if we're reading the first file, the csv, save the second field as the value in the associative array <code>a</code>, whose indices are a <code>&gt;</code> followed by the 1st field. I use the <code>&gt;</code> only to simplify matching it later in the fasta file. The <code>next</code> moves on to the next line so the rest of the command isn't run for the 1st file.</li>
  <li><code>(/&gt;/){$1=$1" "a[$1]}</code>: Now we're in the 2nd file. If the line starts with a <code>&gt;</code>, then set the 1st field to be itself, a space and then whatever was stored in the array <code>a</code> for this 1st field.</li>
  <li><code>1;</code> : this is awk shorthand for "print this line". </li>
  </ul>
  
  <p>You could expand the above into:</p>
  
  <pre><code>awk '{
          if(NR==FNR){
              a["&gt;"$1]=$2; 
          }
          else{
              if(/&gt;/){
                  $1=$1" "a[$1]
              }
          }
          print;
        }' foo.csv foo.fa 
  </code></pre>
  
  <hr>
  
  <p>If your files are small enough to be sorted easily, you can use the <code>FastaToTbl</code> and <code>TblToFasta</code> scripts <a href="https://bioinformatics.stackexchange.com/a/2651/298">I have posted before</a> and then <code>join</code>:</p>
  
  <pre><code>$ join -o 1.1,2.2,1.2 &lt;(FastaToTbl foo.fa | sort ) &lt;(sort  foo.csv ) | TblToFasta 
  &gt;Contig11 ref|XM_021487230.1|_PREDICTED:_Mizuhopecten_yessoensis_UNC93-like_protein_MFSD11_(LOC110443183),_mRNA 
  GACTCTCTTAAGGTAGCCAAATGCCTCGTCATCTAATTAGTGACGCGCATGAATGGATTA
  GACTCTCTTAAGGTAGCCAAATGCCTCGTCATCTAATTAGTGACGCGCATGAATGGATTA
  &gt;Contig12 mamamama|_PREDICTED:_Mizuhopecten_yessoensis_UNC93-like_protein_MFSD11_(LOC110443183),_mRNA 
  AKHSALKJHDLKAJBDLKAJDBADADKJALDKJBAD
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-07-09T15:56:27.467Z
Answers.LastActivityDate: 2018-07-09T16:24:50.520Z
Answers.LastEditDate: 2018-07-09T16:24:50.520Z
Answers.UserDisplayName: terdon
Body: <p>I've got a fasta file with some RNA seq data and another csv file with the output from plast where I've aligned it to a reference using plastn. I'm struggling with figuring out a command to append my original RNA seq data fasta headers with the annotations.</p>
  
  <p>I've looked at awk and sed but can't wrap my head around how to use them in this instance.</p>
  
  <p>CSV output example:</p>
  
  <pre><code>Contig11    ref|XM_021487230.1|_PREDICTED:_Mizuhopecten_yessoensis_UNC93-like_protein_MFSD11_(LOC110443183),_mRNA   78.95   95  20  0   371 465 4627    4721    1e-14   82.4    75  625 1   0   15.2    0   5692    1   0   15.2    0
  </code></pre>
  
  <p>And example fasta contig:</p>
  
  <pre><code>&gt;Contig11
  GACTCTCTTAAGGTAGCCAAATGCCTCGTCATCTAATTAGTGACGCGCATGAATGGATTA
  </code></pre>
  
  <p>How would I be able to parse through the csv to find a matching contig with the fasta file, and append the second csv column to the fasta header. Ideally to produce:</p>
  
  <pre><code>&gt;Contig11 ref|XM_021487230.1|_PREDICTED:_Mizuhopecten_yessoensis_UNC93-like_protein_MFSD11_(LOC110443183),_mRNA
  GACTCTCTTAAGGTAGCCAAATGCCTCGTCATCTAATTAGTGACGCGCATGAATGGATTA....    
  </code></pre>
  
  <p>Cheers for any help!</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-07-09T15:54:05.690Z
Comments.Text: What format is your "csv" in? It isn't a "real" csv (that means "comma separated file" where columns are defined by commas), so what is the column separator? Is it a space or a tab?
Comments.UserDisplayName: terdon
Tags: <rna-seq><sequence-annotation><awk><functional-annotation>
Title: RNA seq fasta file annotation from alignment to reference matches
ThreadText: # RNA seq fasta file annotation from alignment to reference matches
  
  (Asked by: username_0 on 2018-07-09)
  
  I've got a fasta file with some RNA seq data and another csv file with the output from plast where I've aligned it to a reference using plastn. I'm struggling with figuring out a command to append my original RNA seq data fasta headers with the annotations.
  
  I've looked at awk and sed but can't wrap my head around how to use them in this instance.
  
  CSV output example:
  
  ```
  Contig11    ref|XM_021487230.1|_PREDICTED:_Mizuhopecten_yessoensis_UNC93-like_protein_MFSD11_(LOC110443183),_mRNA   78.95   95  20  0   371 465 4627    4721    1e-14   82.4    75  625 1   0   15.2    0   5692    1   0   15.2    0
  
  ```
  
  And example fasta contig:
  
  ```
  >Contig11
  GACTCTCTTAAGGTAGCCAAATGCCTCGTCATCTAATTAGTGACGCGCATGAATGGATTA
  
  ```
  
  How would I be able to parse through the csv to find a matching contig with the fasta file, and append the second csv column to the fasta header. Ideally to produce:
  
  ```
  >Contig11 ref|XM_021487230.1|_PREDICTED:_Mizuhopecten_yessoensis_UNC93-like_protein_MFSD11_(LOC110443183),_mRNA
  GACTCTCTTAAGGTAGCCAAATGCCTCGTCATCTAATTAGTGACGCGCATGAATGGATTA....    
  
  ```
  
  Cheers for any help!
  
  **Tags:** rna-seq, sequence-annotation, awk, functional-annotation
  
  > **Comments:**
  > **username_1**: What format is your "csv" in? It isn't a "real" csv (that means "comma separated file" where columns are defined by commas), so what is the column separator? Is it a space or a tab?  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2018-07-09. Score: 1 | Accepted answer
  
  An `awk` approach (assuming your input "csv" file is space-separated):
  
  ```
  $ awk '(NR==FNR){a[">"$1]=$2; next;} (/>/){$1=$1" "a[$1]}1;' foo.csv foo.fa 
  >Contig11 ref|XM_021487230.1|_PREDICTED:_Mizuhopecten_yessoensis_UNC93-like_protein_MFSD11_(LOC110443183),_mRNA ref|XM_021487230.1|_PREDICTED:_Mizuhopecten_yessoensis_UNC93-like_protein_MFSD11_(LOC110443183),_mRNA
  GACTCTCTTAAGGTAGCCAAATGCCTCGTCATCTAATTAGTGACGCGCATGAATGGATTA
  GACTCTCTTAAGGTAGCCAAATGCCTCGTCATCTAATTAGTGACGCGCATGAATGGATTA
  
  ```
  
  ### Explanation
  
  * `NR==FNR` : `FNR` is the line number of the current input file and `NR` is the line number of all input. When processing more than one file, the two will only be equal while reading the first file.
  * `(NR==FNR){a[">"$1]=$2; next;}` : if we're reading the first file, the csv, save the second field as the value in the associative array `a`, whose indices are a `>` followed by the 1st field. I use the `>` only to simplify matching it later in the fasta file. The `next` moves on to the next line so the rest of the command isn't run for the 1st file.
  * `(/>/){$1=$1" "a[$1]}`: Now we're in the 2nd file. If the line starts with a `>`, then set the 1st field to be itself, a space and then whatever was stored in the array `a` for this 1st field.
  * `1;` : this is awk shorthand for "print this line".
  
  You could expand the above into:
  
  ```
  awk '{
          if(NR==FNR){
              a[">"$1]=$2; 
          }
          else{
              if(/>/){
                  $1=$1" "a[$1]
              }
          }
          print;
        }' foo.csv foo.fa 
  
  ```
  
  ---
  
  If your files are small enough to be sorted easily, you can use the `FastaToTbl` and `TblToFasta` scripts [I have posted before](https://bioinformatics.stackexchange.com/a/2651/298) and then `join`:
  
  ```
  $ join -o 1.1,2.2,1.2 <(FastaToTbl foo.fa | sort ) <(sort  foo.csv ) | TblToFasta 
  >Contig11 ref|XM_021487230.1|_PREDICTED:_Mizuhopecten_yessoensis_UNC93-like_protein_MFSD11_(LOC110443183),_mRNA 
  GACTCTCTTAAGGTAGCCAAATGCCTCGTCATCTAATTAGTGACGCGCATGAATGGATTA
  GACTCTCTTAAGGTAGCCAAATGCCTCGTCATCTAATTAGTGACGCGCATGAATGGATTA
  >Contig12 mamamama|_PREDICTED:_Mizuhopecten_yessoensis_UNC93-like_protein_MFSD11_(LOC110443183),_mRNA 
  AKHSALKJHDLKAJBDLKAJDBADADKJALDKJBAD
  
  ```
  
  
  ---
  
  