annotate pre_process_protein_name_set.R @ 19:c56ebf254e89 draft

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