# Creating Target Decoy databases

#### Witek Wolski

#### 2018-07-26

Using the package prozor for creating standarized fasta. prozor can be used to:

     * merge several fasta files into a single fasta file
     * add reverse sequences to the fasta file.
     * add contaminants to the fasta file

# Contaminants

This package provides two sets of typical contaminant proteins and peptides, one with and one without contaminants of human origin, which can be accessed by the functions `loadContaminantsFasta` and `loadContaminantsNoHumanFasta`. At the FGCZ we always are adding one of those two contaminant files to the database. To databases already containing human proteins, we will add the `ContaminantsNoHumanFasta`. The contaminants are easy to distinguish from other entries thanks to the `zz|FGCZCont` prefix.

    library(prozor)
    head(names(loadContaminantsFasta()))

    ## [1] "zz|FGCZCont0000_P61626|LYSC_HUMAN"
    ## [2] "zz|FGCZCont0001_P02534|K1M1_SHEEP"
    ## [3] "zz|FGCZCont0002_P08729|K2C7_HUMAN"
    ## [4] "zz|FGCZCont0003_P97861|KRT86_MOUSE"
    ## [5] "zz|FGCZCont0004_Q148H4|KRT81_BOVIN"
    ## [6] "zz|FGCZCont0005_Q15323|K1H1_HUMAN"

    length(loadContaminantsFasta())

    ## [1] 261

    length(loadContaminantsNoHumanFasta())

    ## [1] 113

# Creating a fasta protein amino acid sequence database for searching.

To merge several fasta databases into a single file place them into a single folder and give the folder the name of the database. At the FGCZ the database name starts with the project number e.g. `p1000` a consecutive number e.g. `db1` and descriptive name `example` i.e. `p1000_db1_example`.

Add to the folder also an annotation.txt file. The annotation file should contain a single line formatted like a fasta entry header with the following conent: `aa|p<project_number>_<database_name>|<YYYYMMDD> <detailed_description>`.

Example : `AA|p1000_db1_example|20180119_Example https://github.com/protViz/prozor`

The package provides an example of such a folder with the fasta files. Based on this folder a database can be created.

    databasedirectory = system.file("p1000_db1_example",package = "prozor")
    #databasedirectory <- file.path(find.package("prozor"), "p1000_db1_example")
    dbname <- basename(databasedirectory)
    fasta <- grep("fasta", dir(databasedirectory),value = T)
    files1 <- file.path(databasedirectory,fasta)
    annot <- grep("annotation",dir(databasedirectory), value=T)
    annotation <- readLines(file.path(databasedirectory,annot))
    annotation

    ## [1] "AA|p2569_db1_mouse_phosPXS|20171029_UNIPROT http://www.uniprot.org/proteomes/UP000000589"

## Create non decoy database

    resDB <- createDecoyDB(files1, useContaminants = loadContaminantsFasta(), annot=annotation, revLab = NULL)

    ## reading db :C:/Users/wolski/AppData/Local/Temp/RtmpCAVINT/Rinst4245ad43432/prozor/p1000_db1_example/shortfasta.fasta.gz

    ## reading db :C:/Users/wolski/AppData/Local/Temp/RtmpCAVINT/Rinst4245ad43432/prozor/p1000_db1_example/uniprot_taxonomy_Oryctolagus_cuniculus.fasta.gz

    length(resDB)

    ## [1] 2610

Based on the directory name we build the name of the fasta file adding the current date.

    dirname(databasedirectory)

    ## [1] "C:/Users/wolski/AppData/Local/Temp/RtmpCAVINT/Rinst4245ad43432/prozor"

    xx <- file.path(dirname(databasedirectory), paste(dbname,"_",format(Sys.time(), "%Y%m%d"),".fasta" ,sep=""))
    print(xx)

    ## [1] "C:/Users/wolski/AppData/Local/Temp/RtmpCAVINT/Rinst4245ad43432/prozor/p1000_db1_example_20180726.fasta"

    writeFasta(resDB, file=xx)

## Create decoy database

To add a decoy database, using reverse sequences specify the `revLab` parameter in the `createDecoyDB` function. The resulting database will be twice as long as the non-decoy database.

    resDBDecoy <- createDecoyDB(files1,
                                useContaminants = loadContaminantsFasta(),
                                annot=annotation,
                                revLab = "REV_")

    ## reading db :C:/Users/wolski/AppData/Local/Temp/RtmpCAVINT/Rinst4245ad43432/prozor/p1000_db1_example/shortfasta.fasta.gz

    ## reading db :C:/Users/wolski/AppData/Local/Temp/RtmpCAVINT/Rinst4245ad43432/prozor/p1000_db1_example/uniprot_taxonomy_Oryctolagus_cuniculus.fasta.gz

    resDBDecoy[[length(resDBDecoy) - 1]]

    ## [1] "IHCQEELQTPNFSLRIHMPLAKLAKDFKELAEDDVEPTRVLCQCALSQEPEASNEMCFLLYKKYDTDLVLVKNENLADIKFVAPIKTKEAIIKKQACEGNEWKQLLIELDGEPTPKLEEVYVRLPASQADLLSIDSAAMALSYWTGAVKQIDLGKMTQTVILAQAGCTLALALLLCKM"
    ## attr(,"name")
    ## [1] "REV_zz|FGCZCont0259_P02754|LACB_BOVIN"
    ## attr(,"Annot")
    ## [1] "REV_>zz|FGCZCont0259_P02754|LACB_BOVIN blastpHomologue_5.0e-129"
    ## attr(,"class")
    ## [1] "SeqFastaAA"

    length(resDBDecoy)

    ## [1] 5219

    sum(duplicated(names(resDBDecoy)))

    ## [1] 0

    sum(duplicated(resDBDecoy))

    ## [1] 0

    dbname_decoy <- unlist(strsplit(dbname,"_"))
    dbname_decoy <- paste(c(dbname_decoy[1],"d",dbname_decoy[2:length(dbname_decoy)]),collapse="_")
    dbname_decoy

    ## [1] "p1000_d_db1_example"

    xx <- file.path(dirname(databasedirectory), paste(dbname_decoy,"_",format(Sys.time(), "%Y%m%d"),".fasta" ,sep=""))
    print(xx)

    ## [1] "C:/Users/wolski/AppData/Local/Temp/RtmpCAVINT/Rinst4245ad43432/prozor/p1000_d_db1_example_20180726.fasta"

    writeFasta(resDBDecoy, file=xx)