---
title: "Create Library Demo"
author: "Witold Wolski"
date: "29 September 2015"
output: pdf_document
---
<!--
%\VignetteEngine{knitr::rmarkdown}
%\VignetteIndexEntry{Supplementary Materials}
-->


# Create decoy fasta

Create fasta file with FGCZ contaminants and decoys.


```{r}

rm(list=ls())
library(prozor)

file = file.path(path.package("prozor"),"extdata/uniprot_taxonomy_Oryctolagus_cuniculus.fasta.gz")
res <- createDecoyDB(c(file,file))

```

# Removing signal peptides
## Homo sapiens

```{r}
library(prozor)
hsfasta <- loadHomoSapiensFasta()
hssignal <- loadHomoSapiensSignalPeptides()
length(grep("SIGNAL",hssignal$Signal.peptide))/length(hssignal$Signal.peptide)*100

noSigFasta <- removeSignalPeptide(hsfasta, hssignal)

homoNoSigFile <-tempfile(pattern = "homo", tmpdir = tempdir(), fileext = "fasta")
homoNoSigFile

writeFasta(file = homoNoSigFile, noSigFasta)
resDB <- createDecoyDB(homoNoSigFile)

homoNoSigDecoyFile <-tempfile(pattern = "homo_d_", tmpdir = tempdir(), fileext = "fasta")

writeFasta(file=homoNoSigDecoyFile,res )


```


## Mus Musculus

```{r}
library(prozor)
library(seqinr)

hsfasta <- loadMusMusculusFasta()

annot <- "zz|uniprot_20160320|mus_musculus_10090_reviewed"
dummy <-as.SeqFastaAA("CRAPCRAPCRAP", Annot=annot, name= annot)

dbname <- tempfile(pattern = "mus_musculus", tmpdir = tempdir(), fileext = "fasta")

writeFasta(file =dbname, c(dummy,hsfasta))
resDB <- createDecoyDB(dbname)
writeFasta(file= "p1687_withSignalPeptides_d_20160405.fasta" , resDB)

```


```{r}

library(prozor)
hsfasta <- loadMusMusculusFasta()
hssignal <- loadMusMusculusSignalPeptides()


head(hssignal)

hssignal[grep("*GKN2_MOUSE*",hssignal$Entry.name),]

length(grep("SIGNAL",hssignal$Signal.peptide))/length(hssignal$Signal.peptide)*100


noSigFasta <- removeSignalPeptide(hsfasta, hssignal)

hsfasta[5627]
noSigFasta[5627]

musNoSigFile <-tempfile(pattern = "mus_musculus_nosig", tmpdir = tempdir(), fileext = "fasta")


writeFasta(file = musNoSigFile, noSigFasta)
resDB <- createDecoyDB(musNoSigFile)

writeFasta(file="p1687_noSignalPeptides_d_29032016.fasta",resDB )


```

# Strange bean with id rename example

```{r}
library(prozor)
fasta <- readPeptideFasta("d:/projects/p2147/NCBI_txid3821_20160410.fasta")

for(i in 1:length(fasta)){
    attributes(fasta[[i]])$name <- gsub("gi\\|[0-9]*\\|gb","gb",attributes(fasta[[i]])$name)
    attributes(fasta[[i]])$Annot <- gsub("gi\\|[0-9]*\\|gb","gb",attributes(fasta[[i]])$Annot)
}

writeFasta(file = "d:/projects/p2147/p2147_NCBI_txid3821_20160410.fasta", fasta )
writeFasta(file="d:/projects/p2147/p2147_db1_20160421.fasta", fasta,loadContaminantsFasta())
length(c(fasta,loadContaminantsFasta()))
resDB <- createDecoyDB("d:/projects/p2147/p2147_NCBI_txid3821_20160410.fasta")
length(resDB)
writeFasta(resDB, file="d:/projects/p2147/p2147_db1_d_20160421.fasta")

```


# Read table


```{r}
library(prozor)

resDB <- createDecoyDB("c:/temp/short_headers.all_concatenated.windows_linebreaks.fasta",annot = "zz|20160520PeterH|FromPeterHunziker")
writeFasta(resDB, file="c:/temp/p65_8283_carniovorus_swiss_20160520.fasta")

resDB <- createDecoyDB("c:/temp/Tryp_Leishmania_totalProt_nr_8521.fasta",annot = "zz|20160520PeterH|FromPeterHunziker")
writeFasta(resDB, file="c:/temp/p65_8521_tryp_leish_totalprot_20160520.fasta")

resDB <- createDecoyDB("Z:/p0/PH/griseus_panproteom_nr.fasta", annot="zz|20160623PeterH|FromPeterHunziker")
writeFasta(resDB, file="Z:/p0/PH/p65_griseus_panproteom_nr_d_20160623.fasta")

library(seqinr)
annot="zz|sourceOf|database"
dummy <-as.SeqFastaAA("CRAPCRAPCRAP", Annot=annot, name= annot)
dummy
```


# SRM Service

```{r}
xx <- readPeptideFasta("d:/projects/SRMServiceDB/20160804_targets.fasta")
xx_rev <- reverseSeq(xx)

all <- readPeptideFasta("d:/projects/SRMServiceDB/fgcz_srmService_db1_20160606.fasta")
sum(grepl("REV_",names(all)))
sum(!grepl("REV_",names(all)))

new <- c(all, xx, xx_rev)
length(new)
length(all)
writeFasta(new, file="d:/projects/fgcz_srmService_d_db1_20160707.fasta")

```

# p2135

```{r}
library(prozor)
resDB <- createDecoyDB("d:/projects/p2135_Hottiger/uniprot-all.fasta",annot = "zz|p2135_d_9606_20160928.fasta|Uniprot canonical 20,177 entries")
writeFasta(resDB, file="d:/projects/p2135_Hottiger/p2135_d_9606_20160928.fasta")

```



# p1511

```{r}
library(prozor)
dir<-"Z:/p1511/Proteomics/newFasta2016"
outfile <- "p1511_d_NEW_2016104.fasta"
file.remove(file.path(dir, outfile))

files<-dir(dir)
annot <- "zz|p1551_d_2016104.fasta|VariousOrg"

annot <- paste(c(annot, gsub(" ","_",files)),collapse = " ")
files <- file.path("Z:/p1511/Proteomics/newFasta2016", files )
resDB <- createDecoyDB(files, annot=paste("zz|p1551_d_NEW_2016104.fasta|VariousDatabasesfrom ",annot= annot ))

sum(duplicated(names(resDB)))
resDB <-resDB[!duplicated(names(resDB))]
length(resDB)

writeFasta(resDB, file=file.path(dir, outfile))


```

## big version

```{r}
library(prozor)
dir<-"Z:/p1511/Proteomics/newFasta2016_Large/"
outfile <- "p1511_d_LARGE_NOHUMAN_20161017.fasta"
file.remove(file.path(dir, outfile))

files <- list.files(dir)
files <- grep("fasta",files,value=TRUE)

annot <- "zz|p1511_d_LARGE_NOHUMAN_20161017.fasta|VariousOrg dbs from p1511/Proteomics/newFasta2016_Large"

annot <- paste(c(annot, gsub(" ","_",files)),collapse = " ")
files <- file.path(dir, files )

files
resDB <- createDecoyDB(files, annot=annot)
length(resDB)
writeFasta(resDB, file=file.path(dir, "output", outfile))

```


## contaminants


```{r}
cont <- loadContaminantsFasta()
cont[[1]]
tmp <- (lapply(cont, function(x){attributes(x)$Annot}))
tmp <- data.frame(oldID = names(tmp), annot= unlist(tmp))
tmp$annot <- gsub(">zz\\|ZZ_FGCZCont[0-9]+\\|","", tmp$annot)

quantable::write.tab(tmp,file="newSuggestedContaminants.txt")


```


# p1797

```{r}
library(prozor)
dir<-"d:/projects/p1797/"
#uniprot-proteomeAUP000008227.fasta"
outfile <- "p1797_d_UP000008227_20161010.fasta"

files<-dir(dir)
files <- grep("*.fasta",files,value = T)
files <- file.path(dir, files )
annot <- "zz|p1797_d_AUP000008227_20161010|UP000008227 http://www.uniprot.org/proteomes/UP000008227"
resDB <- createDecoyDB(files, annot= annot)

outputdir <- file.path(dir,"output")
dir.create(outputdir)

writeFasta(resDB, file=file.path(outputdir, outfile))

annot <- "zz|p1797_AUP000008227_20161010|UP000008227 http://www.uniprot.org/proteomes/UP000008227"
outfile <- "p1797_UP000008227_20161010.fasta"

resDB <- createDecoyDB(files, annot= annot, revLab=NULL)
length(resDB)

writeFasta(resDB, file=file.path(outputdir, outfile))
```


# p1000 triple proteome


```{r}

library(prozor)
dir<-"d:/projects/p1000/databases/"
#uniprot-proteomeAUP000008227.fasta"
outfile <- "p1000_d_tripleProteomeHYE_20161011.fasta"

files<-dir(dir)
files <- grep("*.fasta",files,value = T)
files <- file.path(dir, files )
annot <- "zz|p1000_d_tripleProteome|UP000000625UP000002311up000005640" 
#http://www.uniprot.org/uniprot/?query=proteome:UP000002311
#http://www.uniprot.org/uniprot/?query=proteome:up000005640
#http://www.uniprot.org/uniprot/?query=proteome:UP000000625

resDB <- createDecoyDB(files, annot= annot)

outputdir <- file.path(dir,"output")
dir.create(outputdir)

writeFasta(resDB, file=file.path(outputdir, outfile))

annot <- "zz|p1000_tripleProteome|UP000000625UP000002311up000005640" #http://www.uniprot.org/proteomes/UP000008227"
outfile <- "p1000_tripleProteomeHYE_20161011.fasta"

resDB <- createDecoyDB(files, annot= annot, revLab=NULL)
length(resDB)

writeFasta(resDB, file=file.path(outputdir, outfile))

```


# make human only database

```{r}
library(prozor)
#http://www.uniprot.org/uniprot/?query=proteome:up000005640

dir<-"d:/projects/p1000/databases/"
#uniprot-proteomeAUP000008227.fasta"

files<-dir(dir)

outfile <- "p1000_d_tripleP_HS_20161013.fasta"
files <- file.path(dir, files )
annot <- "zz|p1000_d_tripleProteome|up000005640 Homo Sapiens http://www.uniprot.org/uniprot/?query=proteome:UP000002311 http://www.uniprot.org/uniprot/?query=proteome:up000005640" 

resDB <- createDecoyDB(files[1], annot= annot)
outputdir <- file.path(dir,"output")
writeFasta(resDB, file=file.path(outputdir, outfile))

```

## make yeast only db

```{r}

#http://www.uniprot.org/uniprot/?query=proteome:UP000002311
library(prozor)
dir<-"d:/projects/p1000/databases/"
#uniprot-proteomeAUP000008227.fasta"

files<-dir(dir)

outfile <- "p1000_d_tripleP_Yest_20161013.fasta"
files <- file.path(dir, files )
annot <- "zz|p1000_d_tripleProteome|UP000002311 Yeast http://www.uniprot.org/uniprot/?query=proteome:UP000002311" 

resDB <- createDecoyDB(files[2], annot= annot)
outputdir <- file.path(dir,"output")
writeFasta(resDB, file=file.path(outputdir, outfile))

```

```{r}
#http://www.uniprot.org/uniprot/?query=proteome:UP000000625

library(prozor)
dir<-"d:/projects/p1000/databases/"
files<-dir(dir)

outfile <- "p1000_d_tripleP_EColi_20161013.fasta"
files <- file.path(dir, files )
annot <- "zz|p1000_d_tripleProteome|UP000000625 EColi http://www.uniprot.org/uniprot/?query=proteome:UP000000625" 

resDB <- createDecoyDB(files[3], annot= annot)
outputdir <- file.path(dir,"output")
writeFasta(resDB, file=file.path(outputdir, outfile))

```

# Simonas Savickas 

```{r}
library(prozor)



file <- "d:/projects/p2123/data/uniprot-proteomeUP000001075_Savitzkas.fasta"
annot <- "zz|p2123_d_UP000001075_CNL|UP000001075_CNL  UP000001075 unreviewed no isoforms"
outfile <- "p2123_d_UP000001075_CNL.fasta"

resDB <- createDecoyDB(file, annot= annot)
outputdir <- file.path(dir,"output")
writeFasta(resDB, file="d:/projects/p2123/data/p2123_d_UP000001075_CNL_13122016.fasta")

```

# p2095


```{r}
library(prozor)
files1 <- "z:/p2095/Proteomics/fasta_db/Mouse/uniprot-proteome_UP000000589_cnl.fasta"
files2 <- "z:/p2095/Proteomics/fasta_db/Mouse/irtfusion.fasta"
annot <- "zz|p2095_UP000000589_cnl|UP000000589_cnl irtfusion and contaminant 13122016"
outfile <- "p2095_UP000000589_cnl_20161213.fasta"
resDB<-createDecoyDB(c(files1,files2), revLab=NULL, useContaminants = FALSE, annot=annot)
writeFasta(resDB, file=file.path("z:/p2095/Proteomics/fasta_db/Mouse/",outfile))

files1 <- "z:/p2095/Proteomics/fasta_db/Mouse/uniprot-mouse_sp_cnl.fasta"
files2 <- "z:/p2095/Proteomics/fasta_db/Mouse/irtfusion.fasta"
annot <- "zz|p2095_uniprot-mouse_sp_cnl|uniprot-mouse_sp_cnl irtfusion and contaminant 13122016"
outfile <- "p2095_uniprot-mouse_sp_cnl_20161213.fasta"
resDB<-createDecoyDB(c(files1,files2), revLab=NULL, useContaminants = FALSE, annot=annot)
writeFasta(resDB, file=file.path("z:/p2095/Proteomics/fasta_db/Mouse/",outfile))
```

# p2242

```{r}
library(prozor)
files1 <- "z:/p2242/Proteomics/fasta_db/p2242_dog/uniprot_proteome_UP000002254_cnl.fasta"
annotation <- readLines("z:/p2242/Proteomics/fasta_db/p2242_dog/annotation.txt")
resDB <- createDecoyDB(files1,useContaminants = TRUE, annot=annotation)
outfile <- paste(dirname(files1),".fasta", sep="")
writeFasta(resDB, file=outfile)

```



```{r}
files1 <- "z:/p2242/Proteomics/fasta_db/p2242_dog_iso/uniprot_proteome_UP000002254_cnl_iso.fasta"
annotation <- readLines("z:/p2242/Proteomics/fasta_db/p2242_dog_iso/annotation.txt")
annotation[1]
resDB <- createDecoyDB(files1,useContaminants = TRUE, annot=annotation)
length(resDB)

outfile <- paste(dirname(files1),".fasta", sep="")
writeFasta(resDB, file=outfile)

```



