20
|
1 ins_check_run <- function() {
|
|
2 if ("affy" %in% rownames(installed.packages())){}
|
|
3 else {
|
|
4 source("https://bioconductor.org/biocLite.R")
|
|
5 biocLite(c('mygene','affy'))
|
|
6 }
|
|
7 if ('data.table' %in% rownames(installed.packages())){}
|
|
8 else {
|
|
9 install.packages('data.table', repos='http://cran.us.r-project.org')
|
|
10 }
|
|
11 if ('stringr' %in% rownames(installed.packages())){}
|
|
12 else {
|
|
13 install.packages('stringr', repos='http://cran.us.r-project.org')
|
|
14 }
|
|
15 if ('VennDiagram' %in% rownames(installed.packages())){}
|
|
16 else {
|
|
17 install.packages('VennDiagram', repos='http://cran.us.r-project.org')
|
|
18 }
|
|
19 }
|
|
20
|
19
|
21 ins_check_run()
|
3
|
22 library(data.table)
|
|
23 library(affy)
|
|
24 library(stringr)
|
|
25 library(mygene)
|
|
26 library(VennDiagram)
|
|
27 #####
|
|
28 #data
|
21
|
29 main <- function(peptides_file, db_path) {
|
3
|
30 peptides_file = read.delim(peptides_file,header=TRUE,stringsAsFactors=FALSE,fill=TRUE)
|
|
31 peptides_txt = peptides_file
|
|
32 intensity_columns = names(peptides_txt[,str_detect(names(peptides_txt),"Intensity\\.*")]) #Pulls out all lines with Intensity in them.
|
|
33 intensity_columns = intensity_columns[2:length(intensity_columns)] #Removes the first column that does not have a bait.
|
|
34 peptides_txt_mapped = as.data.frame(map_peptides_proteins(peptides_txt)) #This function as below sets every line to a 1 to 1 intensity to each possible protein.
|
|
35 peptides_txt_mapped$Uniprot = str_extract(peptides_txt_mapped$mapped_protein, "[OPQ][0-9][A-Z0-9]{3}[0-9]|[A-NR-Z][0-9]([A-Z][A-Z0-9]{2}[0-9]){1,2}") #Pulls out just Uniprot id from the script.
|
|
36 peptides_txt_mapped = subset(peptides_txt_mapped,!is.na(Uniprot)) #removes reverse sequences and any that didn't match a uniprot accession
|
|
37 columns_comb = c("Uniprot", intensity_columns)
|
|
38 peptides_mapped_intensity = subset(peptides_txt_mapped, select = columns_comb) #Subsets out only the needed cloumns for Tukeys (Uniprot IDS and baited intensities)
|
21
|
39 swissprot_fasta = scan(db_path, what="character")
|
3
|
40 peptides_txt_mapped_log2 = peptides_mapped_intensity
|
|
41 # Takes the log2 of the intensities.
|
|
42 for (i in intensity_columns) {
|
|
43 peptides_txt_mapped_log2[,i] = log2(subset(peptides_txt_mapped_log2, select = i))
|
|
44 }
|
|
45 #get the minimum from each column while ignoring the -Inf; get the min of these mins for the global min; breaks when there's only one intensity column
|
|
46 global_min = min(apply(peptides_txt_mapped_log2[,2:ncol(peptides_txt_mapped_log2)],2,function(x) {
|
|
47 min(x[x != -Inf])
|
|
48 }))
|
|
49 peptides_txt_mapped_log2[peptides_txt_mapped_log2 == -Inf] <- 0
|
|
50 #uniprot accessions WITHOUT isoforms; it looks like only contaminants contain isoforms anyways
|
|
51 mapped_protein_uniprotonly = str_extract(peptides_txt_mapped_log2$Uniprot,"[OPQ][0-9][A-Z0-9]{3}[0-9]|[A-NR-Z][0-9]([A-Z][A-Z0-9]{2}[0-9]){1,2}")
|
|
52 mapped_protein_uniprot_accession = str_extract(peptides_txt_mapped_log2$Uniprot,"[OPQ][0-9][A-Z0-9]{3}[0-9](-[0-9]+)?|[A-NR-Z][0-9]([A-Z][A-Z0-9]{2}[0-9]){1,2}(-[0-9]+)?|[OPQ][0-9][A-Z0-9]{3}[0-9]|[A-NR-Z][0-9]([A-Z][A-Z0-9]{2}[0-9]){1,2}")
|
|
53 peptides_txt_mapped_log2$mapped_protein = mapped_protein_uniprotonly
|
|
54 # Runs the Tukey function returning completed table
|
|
55 peptides_txt_mapped_log2 = subset(peptides_txt_mapped_log2,mapped_protein %in% swissprot_fasta)
|
|
56 protein_intensities_tukeys = get_protein_values(peptides_txt_mapped_log2,intensity_columns)
|
|
57 protein_intensities_tukeys[protein_intensities_tukeys == 1] <- 0
|
|
58 write.table(protein_intensities_tukeys, "./tukeys_output.txt", row.names = FALSE, col.names = TRUE, quote = FALSE, sep = "\t")
|
|
59
|
|
60 }
|
|
61
|
|
62 map_peptides_proteins = function(peptides_in) {
|
|
63 #reverse sequences are blank but have a razor protein indicating that they are reverse; exclude these for now
|
|
64 peptides_in = subset(peptides_in,peptides_in$Proteins != "")
|
|
65 results_list = list()
|
|
66 k = 1
|
|
67 for (i in 1:nrow(peptides_in)) {
|
|
68 protein_names = peptides_in[i,"Proteins"]
|
|
69 protein_names_split = unlist(strsplit(protein_names,";"))
|
|
70 for (j in 1:length(protein_names_split)) {
|
|
71 peptides_mapped_proteins = data.frame(peptides_in[i,],mapped_protein=protein_names_split[j],stringsAsFactors=FALSE)
|
|
72 results_list[[k]] = peptides_mapped_proteins
|
|
73 k = k+1
|
|
74
|
|
75 }
|
|
76 }
|
|
77 return(rbindlist(results_list))
|
|
78 }
|
|
79
|
|
80 get_protein_values = function(mapped_peptides_in,intensity_columns_list) {
|
|
81 unique_mapped_proteins_list = unique(mapped_peptides_in$mapped_protein) # Gets list of all peptides listed.
|
|
82 # Generates a blank data frame with clomns of Intensities and rows of Uniprots.
|
|
83 Tukeys_df = data.frame(mapped_protein = unique_mapped_proteins_list, stringsAsFactors = FALSE )
|
|
84 for (q in intensity_columns_list) {Tukeys_df[,q] = NA}
|
|
85 for (i in 1:length(unique_mapped_proteins_list)) {
|
|
86 mapped_peptides_unique_subset = subset(mapped_peptides_in, mapped_protein == unique_mapped_proteins_list[i])
|
|
87 #calculate Tukey's Biweight from library(affy); returns a single numeric
|
|
88 #results_list[[i]] = data.frame(Protein=unique_mapped_proteins_list[i],Peptides_per_protein=nrow(mapped_peptides_unique_subset))
|
|
89 for (j in intensity_columns_list) {
|
|
90 #Populates with new Tukeys values.
|
|
91 Tukeys_df[i,j] = 2^(tukey.biweight(mapped_peptides_unique_subset[,j]))
|
|
92 }
|
|
93 }
|
|
94 return(Tukeys_df)
|
|
95 }
|
|
96
|
|
97 args <- commandArgs(trailingOnly = TRUE)
|
21
|
98 main(args[1], args[2])
|