annotate pre_process_protein_name_set.R @ 20:e21be0412789 draft

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