annotate pre_process_protein_name_set.R @ 11:7e371c979469 draft

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