annotate pre_process_protein_name_set.R @ 24:85c19ee579d6 draft

Uploaded
author bornea
date Mon, 18 Apr 2016 10:17:48 -0400
parents 945f600f34cb
children dbd1af88f060
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3
945f600f34cb Uploaded
bornea
parents:
diff changeset
1 #######################################################################################
945f600f34cb Uploaded
bornea
parents:
diff changeset
2 # R-code: Protein Name and Tukey's Normalization
945f600f34cb Uploaded
bornea
parents:
diff changeset
3 # Author: Adam L Borne
945f600f34cb Uploaded
bornea
parents:
diff changeset
4 # Contributers: Paul A Stewart, Brent Kuenzi
945f600f34cb Uploaded
bornea
parents:
diff changeset
5 #######################################################################################
945f600f34cb Uploaded
bornea
parents:
diff changeset
6 # Assigns uniprot id from MaxQuant peptides file. Filters and normalizes the
945f600f34cb Uploaded
bornea
parents:
diff changeset
7 # intensities of each proteins. Resulting in a one to one list of intensities to
945f600f34cb Uploaded
bornea
parents:
diff changeset
8 # uniprot id.
945f600f34cb Uploaded
bornea
parents:
diff changeset
9 #######################################################################################
945f600f34cb Uploaded
bornea
parents:
diff changeset
10 # Copyright (C) Adam Borne.
945f600f34cb Uploaded
bornea
parents:
diff changeset
11 # Permission is granted to copy, distribute and/or modify this document
945f600f34cb Uploaded
bornea
parents:
diff changeset
12 # under the terms of the GNU Free Documentation License, Version 1.3
945f600f34cb Uploaded
bornea
parents:
diff changeset
13 # or any later version published by the Free Software Foundation;
945f600f34cb Uploaded
bornea
parents:
diff changeset
14 # with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
945f600f34cb Uploaded
bornea
parents:
diff changeset
15 # A copy of the license is included in the section entitled "GNU
945f600f34cb Uploaded
bornea
parents:
diff changeset
16 # Free Documentation License".
945f600f34cb Uploaded
bornea
parents:
diff changeset
17 #######################################################################################
945f600f34cb Uploaded
bornea
parents:
diff changeset
18 ## REQUIRED INPUT ##
945f600f34cb Uploaded
bornea
parents:
diff changeset
19
945f600f34cb Uploaded
bornea
parents:
diff changeset
20 # 1) peptides_file: MaxQuant peptides file.
945f600f34cb Uploaded
bornea
parents:
diff changeset
21 #######################################################################################
945f600f34cb Uploaded
bornea
parents:
diff changeset
22
945f600f34cb Uploaded
bornea
parents:
diff changeset
23
945f600f34cb Uploaded
bornea
parents:
diff changeset
24 ins_check_run <- function() {
945f600f34cb Uploaded
bornea
parents:
diff changeset
25 if ("affy" %in% rownames(installed.packages())){}
945f600f34cb Uploaded
bornea
parents:
diff changeset
26 else {
945f600f34cb Uploaded
bornea
parents:
diff changeset
27 source("https://bioconductor.org/biocLite.R")
945f600f34cb Uploaded
bornea
parents:
diff changeset
28 biocLite(c('mygene','affy'))
945f600f34cb Uploaded
bornea
parents:
diff changeset
29 }
945f600f34cb Uploaded
bornea
parents:
diff changeset
30 if ('data.table' %in% rownames(installed.packages())){}
945f600f34cb Uploaded
bornea
parents:
diff changeset
31 else {
945f600f34cb Uploaded
bornea
parents:
diff changeset
32 install.packages('data.table', repos='http://cran.us.r-project.org')
945f600f34cb Uploaded
bornea
parents:
diff changeset
33 }
945f600f34cb Uploaded
bornea
parents:
diff changeset
34 if ('stringr' %in% rownames(installed.packages())){}
945f600f34cb Uploaded
bornea
parents:
diff changeset
35 else {
945f600f34cb Uploaded
bornea
parents:
diff changeset
36 install.packages('stringr', repos='http://cran.us.r-project.org')
945f600f34cb Uploaded
bornea
parents:
diff changeset
37 }
945f600f34cb Uploaded
bornea
parents:
diff changeset
38 if ('VennDiagram' %in% rownames(installed.packages())){}
945f600f34cb Uploaded
bornea
parents:
diff changeset
39 else {
945f600f34cb Uploaded
bornea
parents:
diff changeset
40 install.packages('VennDiagram', repos='http://cran.us.r-project.org')
945f600f34cb Uploaded
bornea
parents:
diff changeset
41 }
945f600f34cb Uploaded
bornea
parents:
diff changeset
42 }
945f600f34cb Uploaded
bornea
parents:
diff changeset
43
945f600f34cb Uploaded
bornea
parents:
diff changeset
44 ins_check_run()
945f600f34cb Uploaded
bornea
parents:
diff changeset
45 library(data.table)
945f600f34cb Uploaded
bornea
parents:
diff changeset
46 library(affy)
945f600f34cb Uploaded
bornea
parents:
diff changeset
47 library(stringr)
945f600f34cb Uploaded
bornea
parents:
diff changeset
48 library(mygene)
945f600f34cb Uploaded
bornea
parents:
diff changeset
49 library(VennDiagram)
945f600f34cb Uploaded
bornea
parents:
diff changeset
50
945f600f34cb Uploaded
bornea
parents:
diff changeset
51
945f600f34cb Uploaded
bornea
parents:
diff changeset
52 main <- function(peptides_file, db_path) {
945f600f34cb Uploaded
bornea
parents:
diff changeset
53 peptides_file = read.delim(peptides_file,header=TRUE,stringsAsFactors=FALSE,fill=TRUE)
945f600f34cb Uploaded
bornea
parents:
diff changeset
54 peptides_txt = peptides_file
945f600f34cb Uploaded
bornea
parents:
diff changeset
55 intensity_columns = names(peptides_txt[,str_detect(names(peptides_txt),"Intensity\\.*")])
945f600f34cb Uploaded
bornea
parents:
diff changeset
56 # Pulls out all lines with Intensity in them.
945f600f34cb Uploaded
bornea
parents:
diff changeset
57 intensity_columns = intensity_columns[2:length(intensity_columns)]
945f600f34cb Uploaded
bornea
parents:
diff changeset
58 # Removes the first column that does not have a bait.
945f600f34cb Uploaded
bornea
parents:
diff changeset
59 peptides_txt_mapped = as.data.frame(map_peptides_proteins(peptides_txt))
945f600f34cb Uploaded
bornea
parents:
diff changeset
60 # This function as below sets every line to a 1 to 1 intensity to each possible protein.
945f600f34cb Uploaded
bornea
parents:
diff changeset
61 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.
945f600f34cb Uploaded
bornea
parents:
diff changeset
62 peptides_txt_mapped = subset(peptides_txt_mapped,!is.na(Uniprot))
945f600f34cb Uploaded
bornea
parents:
diff changeset
63 # Removes reverse sequences and any that didn't match a uniprot accession.
945f600f34cb Uploaded
bornea
parents:
diff changeset
64 columns_comb = c("Uniprot", intensity_columns)
945f600f34cb Uploaded
bornea
parents:
diff changeset
65 peptides_mapped_intensity = subset(peptides_txt_mapped, select = columns_comb)
945f600f34cb Uploaded
bornea
parents:
diff changeset
66 # Subsets out only the needed cloumns for Tukeys (Uniprot IDS and baited intensities)/
945f600f34cb Uploaded
bornea
parents:
diff changeset
67 swissprot_fasta = scan(db_path, what="character")
945f600f34cb Uploaded
bornea
parents:
diff changeset
68 peptides_txt_mapped_log2 = peptides_mapped_intensity
945f600f34cb Uploaded
bornea
parents:
diff changeset
69 # Takes the log2 of the intensities.
945f600f34cb Uploaded
bornea
parents:
diff changeset
70 for (i in intensity_columns) {
945f600f34cb Uploaded
bornea
parents:
diff changeset
71 peptides_txt_mapped_log2[,i] = log2(subset(peptides_txt_mapped_log2, select = i))
945f600f34cb Uploaded
bornea
parents:
diff changeset
72 }
945f600f34cb Uploaded
bornea
parents:
diff changeset
73 # Get the minimum from each column while ignoring the -Inf; get the min of these mins for the
945f600f34cb Uploaded
bornea
parents:
diff changeset
74 # global min; breaks when there's only one intensity column.
945f600f34cb Uploaded
bornea
parents:
diff changeset
75 global_min = min(apply(peptides_txt_mapped_log2[,2:ncol(peptides_txt_mapped_log2)],2,function(x) {
945f600f34cb Uploaded
bornea
parents:
diff changeset
76 min(x[x != -Inf])
945f600f34cb Uploaded
bornea
parents:
diff changeset
77 }))
945f600f34cb Uploaded
bornea
parents:
diff changeset
78 peptides_txt_mapped_log2[peptides_txt_mapped_log2 == -Inf] <- 0
945f600f34cb Uploaded
bornea
parents:
diff changeset
79 #uniprot accessions WITHOUT isoforms; it looks like only contaminants contain isoforms anyways.
945f600f34cb Uploaded
bornea
parents:
diff changeset
80 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}")
945f600f34cb Uploaded
bornea
parents:
diff changeset
81 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}")
945f600f34cb Uploaded
bornea
parents:
diff changeset
82 peptides_txt_mapped_log2$mapped_protein = mapped_protein_uniprotonly
945f600f34cb Uploaded
bornea
parents:
diff changeset
83 # Runs the Tukey function returning completed table.
945f600f34cb Uploaded
bornea
parents:
diff changeset
84 peptides_txt_mapped_log2 = subset(peptides_txt_mapped_log2,mapped_protein %in% swissprot_fasta)
945f600f34cb Uploaded
bornea
parents:
diff changeset
85 protein_intensities_tukeys = get_protein_values(peptides_txt_mapped_log2,intensity_columns)
945f600f34cb Uploaded
bornea
parents:
diff changeset
86 protein_intensities_tukeys[protein_intensities_tukeys == 1] <- 0
945f600f34cb Uploaded
bornea
parents:
diff changeset
87 write.table(protein_intensities_tukeys, "./tukeys_output.txt", row.names = FALSE, col.names = TRUE, quote = FALSE, sep = "\t")
945f600f34cb Uploaded
bornea
parents:
diff changeset
88
945f600f34cb Uploaded
bornea
parents:
diff changeset
89 }
945f600f34cb Uploaded
bornea
parents:
diff changeset
90
945f600f34cb Uploaded
bornea
parents:
diff changeset
91 map_peptides_proteins = function(peptides_in) {
945f600f34cb Uploaded
bornea
parents:
diff changeset
92 peptides_in = subset(peptides_in,peptides_in$Proteins != "")
945f600f34cb Uploaded
bornea
parents:
diff changeset
93 results_list = list()
945f600f34cb Uploaded
bornea
parents:
diff changeset
94 k = 1
945f600f34cb Uploaded
bornea
parents:
diff changeset
95 for (i in 1:nrow(peptides_in)) {
945f600f34cb Uploaded
bornea
parents:
diff changeset
96 protein_names = peptides_in[i,"Proteins"]
945f600f34cb Uploaded
bornea
parents:
diff changeset
97 protein_names_split = unlist(strsplit(protein_names,";"))
945f600f34cb Uploaded
bornea
parents:
diff changeset
98 for (j in 1:length(protein_names_split)) {
945f600f34cb Uploaded
bornea
parents:
diff changeset
99 peptides_mapped_proteins = data.frame(peptides_in[i,],mapped_protein=protein_names_split[j],stringsAsFactors=FALSE)
945f600f34cb Uploaded
bornea
parents:
diff changeset
100 results_list[[k]] = peptides_mapped_proteins
945f600f34cb Uploaded
bornea
parents:
diff changeset
101 k = k+1
945f600f34cb Uploaded
bornea
parents:
diff changeset
102
945f600f34cb Uploaded
bornea
parents:
diff changeset
103 }
945f600f34cb Uploaded
bornea
parents:
diff changeset
104 }
945f600f34cb Uploaded
bornea
parents:
diff changeset
105 return(rbindlist(results_list))
945f600f34cb Uploaded
bornea
parents:
diff changeset
106 }
945f600f34cb Uploaded
bornea
parents:
diff changeset
107
945f600f34cb Uploaded
bornea
parents:
diff changeset
108 get_protein_values = function(mapped_peptides_in,intensity_columns_list) {
945f600f34cb Uploaded
bornea
parents:
diff changeset
109 unique_mapped_proteins_list = unique(mapped_peptides_in$mapped_protein)
945f600f34cb Uploaded
bornea
parents:
diff changeset
110 # Gets list of all peptides listed.
945f600f34cb Uploaded
bornea
parents:
diff changeset
111 # Generates a blank data frame with clomns of Intensities and rows of Uniprots.
945f600f34cb Uploaded
bornea
parents:
diff changeset
112 Tukeys_df = data.frame(mapped_protein = unique_mapped_proteins_list, stringsAsFactors = FALSE )
945f600f34cb Uploaded
bornea
parents:
diff changeset
113 for (q in intensity_columns_list) {Tukeys_df[,q] = NA}
945f600f34cb Uploaded
bornea
parents:
diff changeset
114 for (i in 1:length(unique_mapped_proteins_list)) {
945f600f34cb Uploaded
bornea
parents:
diff changeset
115 mapped_peptides_unique_subset = subset(mapped_peptides_in, mapped_protein == unique_mapped_proteins_list[i])
945f600f34cb Uploaded
bornea
parents:
diff changeset
116 # Calculate Tukey's Biweight from library(affy); returns a single numeric.
945f600f34cb Uploaded
bornea
parents:
diff changeset
117 # Results_list[[i]] = data.frame(Protein=unique_mapped_proteins_list[i],Peptides_per_protein=nrow(mapped_peptides_unique_subset)).
945f600f34cb Uploaded
bornea
parents:
diff changeset
118 for (j in intensity_columns_list) {
945f600f34cb Uploaded
bornea
parents:
diff changeset
119 # Populates with new Tukeys values.
945f600f34cb Uploaded
bornea
parents:
diff changeset
120 Tukeys_df[i,j] = 2^(tukey.biweight(mapped_peptides_unique_subset[,j]))
945f600f34cb Uploaded
bornea
parents:
diff changeset
121 }
945f600f34cb Uploaded
bornea
parents:
diff changeset
122 }
945f600f34cb Uploaded
bornea
parents:
diff changeset
123 return(Tukeys_df)
945f600f34cb Uploaded
bornea
parents:
diff changeset
124 }
945f600f34cb Uploaded
bornea
parents:
diff changeset
125
945f600f34cb Uploaded
bornea
parents:
diff changeset
126
945f600f34cb Uploaded
bornea
parents:
diff changeset
127 args <- commandArgs(trailingOnly = TRUE)
945f600f34cb Uploaded
bornea
parents:
diff changeset
128 main(args[1], args[2])