[ RDocumentation ](/)

Moon  [ ](https://github.com/datacamp/rdocumentation-2.0) [ Learn R
](https://www.datacamp.com/learn/r)

Search all packages and functions

[ micropan  (version  1.0  ) ](/packages/micropan/versions/1.0)

#  readFasta and writeFasta: Read and write biological sequence files.

##  Description

Reads and writes biological sequences (DNA, RNA, protein) in the commonly used
FASTA format.

##  Usage

    
    
    readFasta(in.file)
    writeFasta(fdta,out.file,width=80)

##  Arguments

in.file

url/directory/name of FASTA file to read.

fdta

A  Fasta  object with the columns  Header  and  Sequence  , see ‘Details’
below.

out.file

Name of FASTA-file to create.

width

Number of characters per line in the FASTA format.

##  Value

` [ readFasta ](/link/readFasta?package=micropan&version=1.0) ` returns a `
Fasta ` object with the contents of the FASTA file. This is a small extension
to a ` data.frame ` and contains two columns of text. The first, named  Header
, contains the headerlines and the second, named  Sequence  , contains the
sequences. ` [ writeFasta ](/link/writeFasta?package=micropan&version=1.0) `
produces a FASTA file.

##  Details

These functions handle input/output of sequences in the commonly used FASTA
format. The sequences are stored in a ` Fasta ` object. This is a small (S3)
extension of a ` data.frame ` containing two text-columns named  Header  and
Sequence  . If other columns are present, these will be ignored by ` [
writeFasta ](/link/writeFasta?package=micropan&version=1.0) ` .

The ` Fasta ` object can be treated as a ` data.frame ` , but the generic
functions ` [ plot.Fasta ](/link/plot.Fasta?package=micropan&version=1.0) ` ,
` [ summary.Fasta ](/link/summary.Fasta?package=micropan&version=1.0) ` and `
[ str.Fasta ](/link/str.Fasta?package=micropan&version=1.0) ` are defined. The
` data.frame ` property makes it straightforward to manipulate all headers or
all sequences, or to extract or delete entries (rows), or to merge several
data sets using ` [ rbind ](/link/rbind?package=micropan&version=1.0) ` .

##  See Also

` [ plot.Fasta ](/link/plot.Fasta?package=micropan&version=1.0) ` , ` [
summary.Fasta ](/link/summary.Fasta?package=micropan&version=1.0) ` , ` [
str.Fasta ](/link/str.Fasta?package=micropan&version=1.0) ` .

##  Examples

[ Run this code
](https://www.datacamp.com/datalab/preview?_tag=rdocs&rdocsPath=packages%2Fmicropan%2Fversions%2F1.0%2Ftopics%2FreadFasta%20and%20writeFasta&utm_source=r-docs&utm_medium=docs&utm_term=readFasta
and writeFasta&utm_content=run_example_in_datalab)

    
    
    # Reading a FASTA-file from the package extdata/ folder
    # We need to uncompress it first...
    extdata.path <- file.path(path.package("micropan"),"extdata")
    filenames <- "Mpneumoniae_309_protein.fsa"
    pth <- lapply( file.path( extdata.path, paste( filenames, ".xz", sep="" ) ), xzuncompress )
    
    # Reading the file...
    fdta <- readFasta(in.file=file.path(extdata.path,filenames))
    
    # ...and compressing it again
    pth <- lapply( file.path( extdata.path, filenames ), xzcompress )
    
    # Plotting and printing the Fasta object
    plot(fdta)
    summary(fdta)
    str(fdta)
    
    # Writing the Fasta object to a new file
    writeFasta(fdta,out.file="Delete_me.fsa")
    

Run the code above in your browser using  [ DataLab
](https://www.datacamp.com/datalab/preview?_tag=rdocs&rdocsPath=packages%2Fmicropan%2Fversions%2F1.0%2Ftopics%2FreadFasta%20and%20writeFasta&utm_source=r-docs&utm_medium=docs&utm_term=readFasta
and writeFasta&utm_content=run_example_in_datalab)

Powered by  [ DataCamp ](https://www.datacamp.com/)