Main Content

fastawrite

Write to file using FASTA format

Syntax

fastawrite(File, Data)
fastawrite(File, Header, Sequence)

Arguments

File

Character vector or string specifying either a file name or a path and file name for saving the FASTA-formatted data. If you specify only a file name, fastawrite saves the file to the MATLAB® Current Folder. If you specify an existing file, fastawrite appends the data to the file, instead of overwriting the file.

Data

Any of the following:

  • Character vector or string containing a sequence

  • MATLAB structure containing the fields Header and Sequence

  • MATLAB structure containing sequence information from the GenBank® or GenPept database, such as returned by genbankread, getgenbank, genpeptread, or getgenpept.

  • Character array, where each row is a sequence.

HeaderCharacter vector or string containing header information about the sequence. This text appears in the header of the FASTA-formatted file, File.
Sequence

Character vector or string containing an amino acid or nucleotide sequence using the standard IUB/IUPAC letter or integer codes. For a list of valid characters, see Amino Acid Lookup or Nucleotide Lookup.

Description

fastawrite(File, Data) writes the contents of Data to File, a FASTA-formatted file. If you specify an existing FASTA-formatted file,fastawrite appends the data to the file, instead of overwriting the file. For the FASTA-format specifications, visit https://www.ncbi.nlm.nih.gov/BLAST/fasta.shtml.

fastawrite(File, Header, Sequence) writes the specified header and sequence information to File, a FASTA-formatted file.

Tip

To append FASTA-formatted data to an existing file, simply specify that file name. fastawrite adds the data to the end of the file.

If you are using fastawrite in a script, you can disable the append warning message by entering the following command lines before the fastawrite command:

warnState = warning %Save the current warning state
warning('off','Bioinfo:fastawrite:AppendToFile'); 
Then enter the following command line after the fastawrite command:
warning(warnState) %Reset warning state to previous settings

Examples

Example 6. Writing a Coding Region to a FASTA-Formatted File
  1. Retrieve the sequence for the human p53 gene from the GenBank database.

    seq = getgenbank('NM_000546');
  2. Read the coordinates of the coding region in the CDS line.

    start = seq.CDS.indices(1)
    
    start =
    
       198
    
    stop = seq.CDS.indices(2)
    
    stop =
    
       1379
  3. Extract the coding region.

    codingSeq = seq.Sequence(start:stop);
  4. Write the coding region to a FASTA-formatted file, specifying Coding region for p53 for the Header in the file, and p53coding.txt for the file name.

    fastawrite('p53coding.txt','Coding region for p53',codingSeq);
Example 7. Saving Multiple Sequences to a FASTA-Formatted File
  1. Write two nucleotide sequences to a MATLAB structure containing the fields Header and Sequence.

    data(1).Sequence = 'ACACAGGAAA';
    data(1).Header = 'First sequence';
    data(2).Sequence = 'ACGTCAGGTC';
    data(2).Header = 'Second sequence';
    
  2. Write the sequences to a FASTA-formatted file, specifying my_sequences.txt for the file name.

    fastawrite('my_sequences.txt', data)
    
  3. Display the FASTA-formatted file, my_sequences.txt.

    type('my_sequences.txt')
    
    >First sequence
    ACACAGGAAA
    
    >Second sequence
    ACGTCAGGTC
    
Example 8. Appending Sequences to a FASTA-Formatted File
  1. If you haven't already done so, create the FASTA-formatted file, my_sequences.txt, described previously.

  2. Append a third sequence to the file.

    fastawrite('my_sequences.txt','Third sequence','TACTGACTTC')
    
  3. Display the FASTA-formatted file, my_sequences.txt.

    type('my_sequences.txt')
    
    >First sequence
    ACACAGGAAA
    
    >Second sequence
    ACGTCAGGTC
    
    >Third sequence
    TACTGACTTC

Version History

Introduced before R2006a