annotate baseline/Baseline_Functions.r @ 46:cfc9a442e59d draft

Uploaded
author davidvanzessen
date Wed, 12 Apr 2017 04:28:16 -0400
parents c33d93683a09
children ba33b94637ca
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1 #########################################################################################
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2 # License Agreement
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
3 #
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
4 # THIS WORK IS PROVIDED UNDER THE TERMS OF THIS CREATIVE COMMONS PUBLIC LICENSE
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
5 # ("CCPL" OR "LICENSE"). THE WORK IS PROTECTED BY COPYRIGHT AND/OR OTHER
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
6 # APPLICABLE LAW. ANY USE OF THE WORK OTHER THAN AS AUTHORIZED UNDER THIS LICENSE
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
7 # OR COPYRIGHT LAW IS PROHIBITED.
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
8 #
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
9 # BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND AGREE TO BE
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
10 # BOUND BY THE TERMS OF THIS LICENSE. TO THE EXTENT THIS LICENSE MAY BE CONSIDERED
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
11 # TO BE A CONTRACT, THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED HERE IN
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
12 # CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS.
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
13 #
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
14 # BASELIne: Bayesian Estimation of Antigen-Driven Selection in Immunoglobulin Sequences
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
15 # Coded by: Mohamed Uduman & Gur Yaari
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
16 # Copyright 2012 Kleinstein Lab
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
17 # Version: 1.3 (01/23/2014)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
18 #########################################################################################
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
19
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
20 # Global variables
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
21
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
22 FILTER_BY_MUTATIONS = 1000
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
23
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
24 # Nucleotides
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
25 NUCLEOTIDES = c("A","C","G","T")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
26
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
27 # Amino Acids
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
28 AMINO_ACIDS <- c("F", "F", "L", "L", "S", "S", "S", "S", "Y", "Y", "*", "*", "C", "C", "*", "W", "L", "L", "L", "L", "P", "P", "P", "P", "H", "H", "Q", "Q", "R", "R", "R", "R", "I", "I", "I", "M", "T", "T", "T", "T", "N", "N", "K", "K", "S", "S", "R", "R", "V", "V", "V", "V", "A", "A", "A", "A", "D", "D", "E", "E", "G", "G", "G", "G")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
29 names(AMINO_ACIDS) <- c("TTT", "TTC", "TTA", "TTG", "TCT", "TCC", "TCA", "TCG", "TAT", "TAC", "TAA", "TAG", "TGT", "TGC", "TGA", "TGG", "CTT", "CTC", "CTA", "CTG", "CCT", "CCC", "CCA", "CCG", "CAT", "CAC", "CAA", "CAG", "CGT", "CGC", "CGA", "CGG", "ATT", "ATC", "ATA", "ATG", "ACT", "ACC", "ACA", "ACG", "AAT", "AAC", "AAA", "AAG", "AGT", "AGC", "AGA", "AGG", "GTT", "GTC", "GTA", "GTG", "GCT", "GCC", "GCA", "GCG", "GAT", "GAC", "GAA", "GAG", "GGT", "GGC", "GGA", "GGG")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
30 names(AMINO_ACIDS) <- names(AMINO_ACIDS)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
31
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
32 #Amino Acid Traits
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
33 #"*" "A" "C" "D" "E" "F" "G" "H" "I" "K" "L" "M" "N" "P" "Q" "R" "S" "T" "V" "W" "Y"
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
34 #B = "Hydrophobic/Burried" N = "Intermediate/Neutral" S="Hydrophilic/Surface")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
35 TRAITS_AMINO_ACIDS_CHOTHIA98 <- c("*","N","B","S","S","B","N","N","B","S","B","B","S","N","S","S","N","N","B","B","N")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
36 names(TRAITS_AMINO_ACIDS_CHOTHIA98) <- sort(unique(AMINO_ACIDS))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
37 TRAITS_AMINO_ACIDS <- array(NA,21)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
38
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
39 # Codon Table
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
40 CODON_TABLE <- as.data.frame(matrix(NA,ncol=64,nrow=12))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
41
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
42 # Substitution Model: Smith DS et al. 1996
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
43 substitution_Literature_Mouse <- matrix(c(0, 0.156222928, 0.601501588, 0.242275484, 0.172506739, 0, 0.241239892, 0.586253369, 0.54636291, 0.255795364, 0, 0.197841727, 0.290240811, 0.467680608, 0.24207858, 0),nrow=4,byrow=T,dimnames=list(NUCLEOTIDES,NUCLEOTIDES))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
44 substitution_Flu_Human <- matrix(c(0,0.2795596,0.5026927,0.2177477,0.1693210,0,0.3264723,0.5042067,0.4983549,0.3328321,0,0.1688130,0.2021079,0.4696077,0.3282844,0),4,4,byrow=T,dimnames=list(NUCLEOTIDES,NUCLEOTIDES))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
45 substitution_Flu25_Human <- matrix(c(0,0.2580641,0.5163685,0.2255674,0.1541125,0,0.3210224,0.5248651,0.5239281,0.3101292,0,0.1659427,0.1997207,0.4579444,0.3423350,0),4,4,byrow=T,dimnames=list(NUCLEOTIDES,NUCLEOTIDES))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
46 load("FiveS_Substitution.RData")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
47
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
48 # Mutability Models: Shapiro GS et al. 2002
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
49 triMutability_Literature_Human <- matrix(c(0.24, 1.2, 0.96, 0.43, 2.14, 2, 1.11, 1.9, 0.85, 1.83, 2.36, 1.31, 0.82, 0.52, 0.89, 1.33, 1.4, 0.82, 1.83, 0.73, 1.83, 1.62, 1.53, 0.57, 0.92, 0.42, 0.42, 1.47, 3.44, 2.58, 1.18, 0.47, 0.39, 1.12, 1.8, 0.68, 0.47, 2.19, 2.35, 2.19, 1.05, 1.84, 1.26, 0.28, 0.98, 2.37, 0.66, 1.58, 0.67, 0.92, 1.76, 0.83, 0.97, 0.56, 0.75, 0.62, 2.26, 0.62, 0.74, 1.11, 1.16, 0.61, 0.88, 0.67, 0.37, 0.07, 1.08, 0.46, 0.31, 0.94, 0.62, 0.57, 0.29, NA, 1.44, 0.46, 0.69, 0.57, 0.24, 0.37, 1.1, 0.99, 1.39, 0.6, 2.26, 1.24, 1.36, 0.52, 0.33, 0.26, 1.25, 0.37, 0.58, 1.03, 1.2, 0.34, 0.49, 0.33, 2.62, 0.16, 0.4, 0.16, 0.35, 0.75, 1.85, 0.94, 1.61, 0.85, 2.09, 1.39, 0.3, 0.52, 1.33, 0.29, 0.51, 0.26, 0.51, 3.83, 2.01, 0.71, 0.58, 0.62, 1.07, 0.28, 1.2, 0.74, 0.25, 0.59, 1.09, 0.91, 1.36, 0.45, 2.89, 1.27, 3.7, 0.69, 0.28, 0.41, 1.17, 0.56, 0.93, 3.41, 1, 1, NA, 5.9, 0.74, 2.51, 2.24, 2.24, 1.95, 3.32, 2.34, 1.3, 2.3, 1, 0.66, 0.73, 0.93, 0.41, 0.65, 0.89, 0.65, 0.32, NA, 0.43, 0.85, 0.43, 0.31, 0.31, 0.23, 0.29, 0.57, 0.71, 0.48, 0.44, 0.76, 0.51, 1.7, 0.85, 0.74, 2.23, 2.08, 1.16, 0.51, 0.51, 1, 0.5, NA, NA, 0.71, 2.14), nrow=64,byrow=T)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
50 triMutability_Literature_Mouse <- matrix(c(1.31, 1.35, 1.42, 1.18, 2.02, 2.02, 1.02, 1.61, 1.99, 1.42, 2.01, 1.03, 2.02, 0.97, 0.53, 0.71, 1.19, 0.83, 0.96, 0.96, 0, 1.7, 2.22, 0.59, 1.24, 1.07, 0.51, 1.68, 3.36, 3.36, 1.14, 0.29, 0.33, 0.9, 1.11, 0.63, 1.08, 2.07, 2.27, 1.74, 0.22, 1.19, 2.37, 1.15, 1.15, 1.56, 0.81, 0.34, 0.87, 0.79, 2.13, 0.49, 0.85, 0.97, 0.36, 0.82, 0.66, 0.63, 1.15, 0.94, 0.85, 0.25, 0.93, 1.19, 0.4, 0.2, 0.44, 0.44, 0.88, 1.06, 0.77, 0.39, 0, 0, 0, 0, 0, 0, 0.43, 0.43, 0.86, 0.59, 0.59, 0, 1.18, 0.86, 2.9, 1.66, 0.4, 0.2, 1.54, 0.43, 0.69, 1.71, 0.68, 0.55, 0.91, 0.7, 1.71, 0.09, 0.27, 0.63, 0.2, 0.45, 1.01, 1.63, 0.96, 1.48, 2.18, 1.2, 1.31, 0.66, 2.13, 0.49, 0, 0, 0, 2.97, 2.8, 0.79, 0.4, 0.5, 0.4, 0.11, 1.68, 0.42, 0.13, 0.44, 0.93, 0.71, 1.11, 1.19, 2.71, 1.08, 3.43, 0.4, 0.67, 0.47, 1.02, 0.14, 1.56, 1.98, 0.53, 0.33, 0.63, 2.06, 1.77, 1.46, 3.74, 2.93, 2.1, 2.18, 0.78, 0.73, 2.93, 0.63, 0.57, 0.17, 0.85, 0.52, 0.31, 0.31, 0, 0, 0.51, 0.29, 0.83, 0.54, 0.28, 0.47, 0.9, 0.99, 1.24, 2.47, 0.73, 0.23, 1.13, 0.24, 2.12, 0.24, 0.33, 0.83, 1.41, 0.62, 0.28, 0.35, 0.77, 0.17, 0.72, 0.58, 0.45, 0.41), nrow=64,byrow=T)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
51 triMutability_Names <- c("AAA", "AAC", "AAG", "AAT", "ACA", "ACC", "ACG", "ACT", "AGA", "AGC", "AGG", "AGT", "ATA", "ATC", "ATG", "ATT", "CAA", "CAC", "CAG", "CAT", "CCA", "CCC", "CCG", "CCT", "CGA", "CGC", "CGG", "CGT", "CTA", "CTC", "CTG", "CTT", "GAA", "GAC", "GAG", "GAT", "GCA", "GCC", "GCG", "GCT", "GGA", "GGC", "GGG", "GGT", "GTA", "GTC", "GTG", "GTT", "TAA", "TAC", "TAG", "TAT", "TCA", "TCC", "TCG", "TCT", "TGA", "TGC", "TGG", "TGT", "TTA", "TTC", "TTG", "TTT")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
52 load("FiveS_Mutability.RData")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
53
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
54 # Functions
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
55
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
56 # Translate codon to amino acid
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
57 translateCodonToAminoAcid<-function(Codon){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
58 return(AMINO_ACIDS[Codon])
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
59 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
60
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
61 # Translate amino acid to trait change
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
62 translateAminoAcidToTraitChange<-function(AminoAcid){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
63 return(TRAITS_AMINO_ACIDS[AminoAcid])
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
64 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
65
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
66 # Initialize Amino Acid Trait Changes
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
67 initializeTraitChange <- function(traitChangeModel=1,species=1,traitChangeFileName=NULL){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
68 if(!is.null(traitChangeFileName)){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
69 tryCatch(
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
70 traitChange <- read.delim(traitChangeFileName,sep="\t",header=T)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
71 , error = function(ex){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
72 cat("Error|Error reading trait changes. Please check file name/path and format.\n")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
73 q()
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
74 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
75 )
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
76 }else{
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
77 traitChange <- TRAITS_AMINO_ACIDS_CHOTHIA98
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
78 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
79 TRAITS_AMINO_ACIDS <<- traitChange
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
80 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
81
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
82 # Read in formatted nucleotide substitution matrix
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
83 initializeSubstitutionMatrix <- function(substitutionModel,species,subsMatFileName=NULL){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
84 if(!is.null(subsMatFileName)){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
85 tryCatch(
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
86 subsMat <- read.delim(subsMatFileName,sep="\t",header=T)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
87 , error = function(ex){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
88 cat("Error|Error reading substitution matrix. Please check file name/path and format.\n")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
89 q()
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
90 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
91 )
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
92 if(sum(apply(subsMat,1,sum)==1)!=4) subsMat = t(apply(subsMat,1,function(x)x/sum(x)))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
93 }else{
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
94 if(substitutionModel==1)subsMat <- substitution_Literature_Mouse
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
95 if(substitutionModel==2)subsMat <- substitution_Flu_Human
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
96 if(substitutionModel==3)subsMat <- substitution_Flu25_Human
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
97
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
98 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
99
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
100 if(substitutionModel==0){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
101 subsMat <- matrix(1,4,4)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
102 subsMat[,] = 1/3
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
103 subsMat[1,1] = 0
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
104 subsMat[2,2] = 0
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
105 subsMat[3,3] = 0
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
106 subsMat[4,4] = 0
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
107 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
108
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
109
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
110 NUCLEOTIDESN = c(NUCLEOTIDES,"N", "-")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
111 if(substitutionModel==5){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
112 subsMat <- FiveS_Substitution
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
113 return(subsMat)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
114 }else{
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
115 subsMat <- rbind(subsMat,rep(NA,4),rep(NA,4))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
116 return( matrix(data.matrix(subsMat),6,4,dimnames=list(NUCLEOTIDESN,NUCLEOTIDES) ) )
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
117 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
118 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
119
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
120
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
121 # Read in formatted Mutability file
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
122 initializeMutabilityMatrix <- function(mutabilityModel=1, species=1,mutabilityMatFileName=NULL){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
123 if(!is.null(mutabilityMatFileName)){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
124 tryCatch(
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
125 mutabilityMat <- read.delim(mutabilityMatFileName,sep="\t",header=T)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
126 , error = function(ex){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
127 cat("Error|Error reading mutability matrix. Please check file name/path and format.\n")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
128 q()
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
129 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
130 )
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
131 }else{
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
132 mutabilityMat <- triMutability_Literature_Human
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
133 if(species==2) mutabilityMat <- triMutability_Literature_Mouse
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
134 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
135
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
136 if(mutabilityModel==0){ mutabilityMat <- matrix(1,64,3)}
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
137
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
138 if(mutabilityModel==5){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
139 mutabilityMat <- FiveS_Mutability
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
140 return(mutabilityMat)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
141 }else{
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
142 return( matrix( data.matrix(mutabilityMat), 64, 3, dimnames=list(triMutability_Names,1:3)) )
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
143 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
144 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
145
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
146 # Read FASTA file formats
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
147 # Modified from read.fasta from the seqinR package
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
148 baseline.read.fasta <-
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
149 function (file = system.file("sequences/sample.fasta", package = "seqinr"),
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
150 seqtype = c("DNA", "AA"), as.string = FALSE, forceDNAtolower = TRUE,
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
151 set.attributes = TRUE, legacy.mode = TRUE, seqonly = FALSE,
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
152 strip.desc = FALSE, sizeof.longlong = .Machine$sizeof.longlong,
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
153 endian = .Platform$endian, apply.mask = TRUE)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
154 {
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
155 seqtype <- match.arg(seqtype)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
156
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
157 lines <- readLines(file)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
158
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
159 if (legacy.mode) {
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
160 comments <- grep("^;", lines)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
161 if (length(comments) > 0)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
162 lines <- lines[-comments]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
163 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
164
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
165
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
166 ind_groups<-which(substr(lines, 1L, 3L) == ">>>")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
167 lines_mod<-lines
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
168
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
169 if(!length(ind_groups)){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
170 lines_mod<-c(">>>All sequences combined",lines)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
171 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
172
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
173 ind_groups<-which(substr(lines_mod, 1L, 3L) == ">>>")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
174
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
175 lines <- array("BLA",dim=(length(ind_groups)+length(lines_mod)))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
176 id<-sapply(1:length(ind_groups),function(i)ind_groups[i]+i-1)+1
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
177 lines[id] <- "THIS IS A FAKE SEQUENCE"
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
178 lines[-id] <- lines_mod
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
179 rm(lines_mod)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
180
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
181 ind <- which(substr(lines, 1L, 1L) == ">")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
182 nseq <- length(ind)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
183 if (nseq == 0) {
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
184 stop("no line starting with a > character found")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
185 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
186 start <- ind + 1
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
187 end <- ind - 1
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
188
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
189 while( any(which(ind%in%end)) ){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
190 ind=ind[-which(ind%in%end)]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
191 nseq <- length(ind)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
192 if (nseq == 0) {
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
193 stop("no line starting with a > character found")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
194 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
195 start <- ind + 1
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
196 end <- ind - 1
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
197 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
198
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
199 end <- c(end[-1], length(lines))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
200 sequences <- lapply(seq_len(nseq), function(i) paste(lines[start[i]:end[i]], collapse = ""))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
201 if (seqonly)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
202 return(sequences)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
203 nomseq <- lapply(seq_len(nseq), function(i) {
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
204
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
205 #firstword <- strsplit(lines[ind[i]], " ")[[1]][1]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
206 substr(lines[ind[i]], 2, nchar(lines[ind[i]]))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
207
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
208 })
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
209 if (seqtype == "DNA") {
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
210 if (forceDNAtolower) {
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
211 sequences <- as.list(tolower(chartr(".","-",sequences)))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
212 }else{
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
213 sequences <- as.list(toupper(chartr(".","-",sequences)))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
214 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
215 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
216 if (as.string == FALSE)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
217 sequences <- lapply(sequences, s2c)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
218 if (set.attributes) {
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
219 for (i in seq_len(nseq)) {
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
220 Annot <- lines[ind[i]]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
221 if (strip.desc)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
222 Annot <- substr(Annot, 2L, nchar(Annot))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
223 attributes(sequences[[i]]) <- list(name = nomseq[[i]],
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
224 Annot = Annot, class = switch(seqtype, AA = "SeqFastaAA",
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
225 DNA = "SeqFastadna"))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
226 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
227 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
228 names(sequences) <- nomseq
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
229 return(sequences)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
230 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
231
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
232
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
233 # Replaces non FASTA characters in input files with N
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
234 replaceNonFASTAChars <-function(inSeq="ACGTN-AApA"){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
235 gsub('[^ACGTNacgt[:punct:]-[:punct:].]','N',inSeq,perl=TRUE)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
236 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
237
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
238 # Find the germlines in the FASTA list
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
239 germlinesInFile <- function(seqIDs){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
240 firstChar = sapply(seqIDs,function(x){substr(x,1,1)})
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
241 secondChar = sapply(seqIDs,function(x){substr(x,2,2)})
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
242 return(firstChar==">" & secondChar!=">")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
243 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
244
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
245 # Find the groups in the FASTA list
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
246 groupsInFile <- function(seqIDs){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
247 sapply(seqIDs,function(x){substr(x,1,2)})==">>"
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
248 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
249
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
250 # In the process of finding germlines/groups, expand from the start to end of the group
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
251 expandTillNext <- function(vecPosToID){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
252 IDs = names(vecPosToID)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
253 posOfInterests = which(vecPosToID)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
254
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
255 expandedID = rep(NA,length(IDs))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
256 expandedIDNames = gsub(">","",IDs[posOfInterests])
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
257 startIndexes = c(1,posOfInterests[-1])
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
258 stopIndexes = c(posOfInterests[-1]-1,length(IDs))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
259 expandedID = unlist(sapply(1:length(startIndexes),function(i){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
260 rep(i,stopIndexes[i]-startIndexes[i]+1)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
261 }))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
262 names(expandedID) = unlist(sapply(1:length(startIndexes),function(i){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
263 rep(expandedIDNames[i],stopIndexes[i]-startIndexes[i]+1)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
264 }))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
265 return(expandedID)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
266 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
267
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
268 # Process FASTA (list) to return a matrix[input, germline)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
269 processInputAdvanced <- function(inputFASTA){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
270
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
271 seqIDs = names(inputFASTA)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
272 numbSeqs = length(seqIDs)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
273 posGermlines1 = germlinesInFile(seqIDs)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
274 numbGermlines = sum(posGermlines1)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
275 posGroups1 = groupsInFile(seqIDs)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
276 numbGroups = sum(posGroups1)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
277 consDef = NA
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
278
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
279 if(numbGermlines==0){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
280 posGermlines = 2
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
281 numbGermlines = 1
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
282 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
283
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
284 glPositionsSum = cumsum(posGermlines1)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
285 glPositions = table(glPositionsSum)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
286 #Find the position of the conservation row
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
287 consDefPos = as.numeric(names(glPositions[names(glPositions)!=0 & glPositions==1]))+1
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
288 if( length(consDefPos)> 0 ){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
289 consDefID = match(consDefPos, glPositionsSum)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
290 #The coservation rows need to be pulled out and stores seperately
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
291 consDef = inputFASTA[consDefID]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
292 inputFASTA = inputFASTA[-consDefID]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
293
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
294 seqIDs = names(inputFASTA)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
295 numbSeqs = length(seqIDs)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
296 posGermlines1 = germlinesInFile(seqIDs)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
297 numbGermlines = sum(posGermlines1)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
298 posGroups1 = groupsInFile(seqIDs)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
299 numbGroups = sum(posGroups1)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
300 if(numbGermlines==0){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
301 posGermlines = 2
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
302 numbGermlines = 1
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
303 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
304 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
305
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
306 posGroups <- expandTillNext(posGroups1)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
307 posGermlines <- expandTillNext(posGermlines1)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
308 posGermlines[posGroups1] = 0
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
309 names(posGermlines)[posGroups1] = names(posGroups)[posGroups1]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
310 posInput = rep(TRUE,numbSeqs)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
311 posInput[posGroups1 | posGermlines1] = FALSE
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
312
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
313 matInput = matrix(NA, nrow=sum(posInput), ncol=2)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
314 rownames(matInput) = seqIDs[posInput]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
315 colnames(matInput) = c("Input","Germline")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
316
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
317 vecInputFASTA = unlist(inputFASTA)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
318 matInput[,1] = vecInputFASTA[posInput]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
319 matInput[,2] = vecInputFASTA[ which( names(inputFASTA)%in%paste(">",names(posGermlines)[posInput],sep="") )[ posGermlines[posInput]] ]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
320
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
321 germlines = posGermlines[posInput]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
322 groups = posGroups[posInput]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
323
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
324 return( list("matInput"=matInput, "germlines"=germlines, "groups"=groups, "conservationDefinition"=consDef ))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
325 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
326
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
327
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
328 # Replace leading and trailing dashes in the sequence
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
329 replaceLeadingTrailingDashes <- function(x,readEnd){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
330 iiGap = unlist(gregexpr("-",x[1]))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
331 ggGap = unlist(gregexpr("-",x[2]))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
332 #posToChange = intersect(iiGap,ggGap)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
333
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
334
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
335 seqIn = replaceLeadingTrailingDashesHelper(x[1])
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
336 seqGL = replaceLeadingTrailingDashesHelper(x[2])
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
337 seqTemplate = rep('N',readEnd)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
338 seqIn <- c(seqIn,seqTemplate[(length(seqIn)+1):readEnd])
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
339 seqGL <- c(seqGL,seqTemplate[(length(seqGL)+1):readEnd])
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
340 # if(posToChange!=-1){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
341 # seqIn[posToChange] = "-"
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
342 # seqGL[posToChange] = "-"
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
343 # }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
344
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
345 seqIn = c2s(seqIn[1:readEnd])
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
346 seqGL = c2s(seqGL[1:readEnd])
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
347
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
348 lenGL = nchar(seqGL)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
349 if(lenGL<readEnd){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
350 seqGL = paste(seqGL,c2s(rep("N",readEnd-lenGL)),sep="")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
351 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
352
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
353 lenInput = nchar(seqIn)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
354 if(lenInput<readEnd){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
355 seqIn = paste(seqIn,c2s(rep("N",readEnd-lenInput)),sep="")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
356 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
357 return( c(seqIn,seqGL) )
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
358 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
359
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
360 replaceLeadingTrailingDashesHelper <- function(x){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
361 grepResults = gregexpr("-*",x)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
362 grepResultsPos = unlist(grepResults)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
363 grepResultsLen = attr(grepResults[[1]],"match.length")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
364 #print(paste("x = '", x, "'", sep=""))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
365 x = s2c(x)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
366 if(x[1]=="-"){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
367 x[1:grepResultsLen[1]] = "N"
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
368 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
369 if(x[length(x)]=="-"){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
370 x[(length(x)-grepResultsLen[length(grepResultsLen)]+1):length(x)] = "N"
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
371 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
372 return(x)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
373 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
374
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
375
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
376
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
377
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
378 # Check sequences for indels
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
379 checkForInDels <- function(matInputP){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
380 insPos <- checkInsertion(matInputP)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
381 delPos <- checkDeletions(matInputP)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
382 return(list("Insertions"=insPos, "Deletions"=delPos))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
383 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
384
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
385 # Check sequences for insertions
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
386 checkInsertion <- function(matInputP){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
387 insertionCheck = apply( matInputP,1, function(x){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
388 inputGaps <- as.vector( gregexpr("-",x[1])[[1]] )
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
389 glGaps <- as.vector( gregexpr("-",x[2])[[1]] )
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
390 return( is.finite( match(FALSE, glGaps%in%inputGaps ) ) )
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
391 })
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
392 return(as.vector(insertionCheck))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
393 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
394 # Fix inserstions
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
395 fixInsertions <- function(matInputP){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
396 insPos <- checkInsertion(matInputP)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
397 sapply((1:nrow(matInputP))[insPos],function(rowIndex){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
398 x <- matInputP[rowIndex,]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
399 inputGaps <- gregexpr("-",x[1])[[1]]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
400 glGaps <- gregexpr("-",x[2])[[1]]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
401 posInsertions <- glGaps[!(glGaps%in%inputGaps)]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
402 inputInsertionToN <- s2c(x[2])
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
403 inputInsertionToN[posInsertions]!="-"
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
404 inputInsertionToN[posInsertions] <- "N"
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
405 inputInsertionToN <- c2s(inputInsertionToN)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
406 matInput[rowIndex,2] <<- inputInsertionToN
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
407 })
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
408 return(insPos)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
409 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
410
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
411 # Check sequences for deletions
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
412 checkDeletions <-function(matInputP){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
413 deletionCheck = apply( matInputP,1, function(x){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
414 inputGaps <- as.vector( gregexpr("-",x[1])[[1]] )
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
415 glGaps <- as.vector( gregexpr("-",x[2])[[1]] )
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
416 return( is.finite( match(FALSE, inputGaps%in%glGaps ) ) )
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
417 })
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
418 return(as.vector(deletionCheck))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
419 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
420 # Fix sequences with deletions
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
421 fixDeletions <- function(matInputP){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
422 delPos <- checkDeletions(matInputP)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
423 sapply((1:nrow(matInputP))[delPos],function(rowIndex){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
424 x <- matInputP[rowIndex,]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
425 inputGaps <- gregexpr("-",x[1])[[1]]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
426 glGaps <- gregexpr("-",x[2])[[1]]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
427 posDeletions <- inputGaps[!(inputGaps%in%glGaps)]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
428 inputDeletionToN <- s2c(x[1])
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
429 inputDeletionToN[posDeletions] <- "N"
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
430 inputDeletionToN <- c2s(inputDeletionToN)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
431 matInput[rowIndex,1] <<- inputDeletionToN
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
432 })
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
433 return(delPos)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
434 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
435
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
436
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
437 # Trim DNA sequence to the last codon
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
438 trimToLastCodon <- function(seqToTrim){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
439 seqLen = nchar(seqToTrim)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
440 trimmedSeq = s2c(seqToTrim)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
441 poi = seqLen
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
442 tailLen = 0
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
443
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
444 while(trimmedSeq[poi]=="-" || trimmedSeq[poi]=="."){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
445 tailLen = tailLen + 1
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
446 poi = poi - 1
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
447 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
448
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
449 trimmedSeq = c2s(trimmedSeq[1:(seqLen-tailLen)])
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
450 seqLen = nchar(trimmedSeq)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
451 # Trim sequence to last codon
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
452 if( getCodonPos(seqLen)[3] > seqLen )
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
453 trimmedSeq = substr(seqToTrim,1, ( (getCodonPos(seqLen)[1])-1 ) )
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
454
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
455 return(trimmedSeq)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
456 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
457
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
458 # Given a nuclotide position, returns the pos of the 3 nucs that made the codon
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
459 # e.g. nuc 86 is part of nucs 85,86,87
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
460 getCodonPos <- function(nucPos){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
461 codonNum = (ceiling(nucPos/3))*3
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
462 return( (codonNum-2):codonNum)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
463 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
464
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
465 # Given a nuclotide position, returns the codon number
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
466 # e.g. nuc 86 = codon 29
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
467 getCodonNumb <- function(nucPos){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
468 return( ceiling(nucPos/3) )
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
469 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
470
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
471 # Given a codon, returns all the nuc positions that make the codon
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
472 getCodonNucs <- function(codonNumb){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
473 getCodonPos(codonNumb*3)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
474 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
475
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
476 computeCodonTable <- function(testID=1){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
477
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
478 if(testID<=4){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
479 # Pre-compute every codons
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
480 intCounter = 1
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
481 for(pOne in NUCLEOTIDES){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
482 for(pTwo in NUCLEOTIDES){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
483 for(pThree in NUCLEOTIDES){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
484 codon = paste(pOne,pTwo,pThree,sep="")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
485 colnames(CODON_TABLE)[intCounter] = codon
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
486 intCounter = intCounter + 1
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
487 CODON_TABLE[,codon] = mutationTypeOptimized(cbind(permutateAllCodon(codon),rep(codon,12)))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
488 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
489 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
490 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
491 chars = c("N","A","C","G","T", "-")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
492 for(a in chars){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
493 for(b in chars){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
494 for(c in chars){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
495 if(a=="N" | b=="N" | c=="N"){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
496 #cat(paste(a,b,c),sep="","\n")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
497 CODON_TABLE[,paste(a,b,c,sep="")] = rep(NA,12)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
498 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
499 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
500 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
501 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
502
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
503 chars = c("-","A","C","G","T")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
504 for(a in chars){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
505 for(b in chars){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
506 for(c in chars){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
507 if(a=="-" | b=="-" | c=="-"){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
508 #cat(paste(a,b,c),sep="","\n")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
509 CODON_TABLE[,paste(a,b,c,sep="")] = rep(NA,12)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
510 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
511 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
512 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
513 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
514 CODON_TABLE <<- as.matrix(CODON_TABLE)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
515 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
516 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
517
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
518 collapseClone <- function(vecInputSeqs,glSeq,readEnd,nonTerminalOnly=0){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
519 #print(length(vecInputSeqs))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
520 vecInputSeqs = unique(vecInputSeqs)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
521 if(length(vecInputSeqs)==1){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
522 return( list( c(vecInputSeqs,glSeq), F) )
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
523 }else{
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
524 charInputSeqs <- sapply(vecInputSeqs, function(x){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
525 s2c(x)[1:readEnd]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
526 })
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
527 charGLSeq <- s2c(glSeq)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
528 matClone <- sapply(1:readEnd, function(i){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
529 posNucs = unique(charInputSeqs[i,])
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
530 posGL = charGLSeq[i]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
531 error = FALSE
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
532 if(posGL=="-" & sum(!(posNucs%in%c("-","N")))==0 ){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
533 return(c("-",error))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
534 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
535 if(length(posNucs)==1)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
536 return(c(posNucs[1],error))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
537 else{
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
538 if("N"%in%posNucs){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
539 error=TRUE
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
540 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
541 if(sum(!posNucs[posNucs!="N"]%in%posGL)==0){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
542 return( c(posGL,error) )
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
543 }else{
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
544 #return( c(sample(posNucs[posNucs!="N"],1),error) )
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
545 if(nonTerminalOnly==0){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
546 return( c(sample(charInputSeqs[i,charInputSeqs[i,]!="N" & charInputSeqs[i,]!=posGL],1),error) )
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
547 }else{
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
548 posNucs = charInputSeqs[i,charInputSeqs[i,]!="N" & charInputSeqs[i,]!=posGL]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
549 posNucsTable = table(posNucs)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
550 if(sum(posNucsTable>1)==0){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
551 return( c(posGL,error) )
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
552 }else{
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
553 return( c(sample( posNucs[posNucs%in%names(posNucsTable)[posNucsTable>1]],1),error) )
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
554 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
555 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
556
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
557 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
558 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
559 })
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
560
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
561
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
562 #print(length(vecInputSeqs))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
563 return(list(c(c2s(matClone[1,]),glSeq),"TRUE"%in%matClone[2,]))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
564 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
565 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
566
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
567 # Compute the expected for each sequence-germline pair
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
568 getExpectedIndividual <- function(matInput){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
569 if( any(grep("multicore",search())) ){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
570 facGL <- factor(matInput[,2])
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
571 facLevels = levels(facGL)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
572 LisGLs_MutabilityU = mclapply(1:length(facLevels), function(x){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
573 computeMutabilities(facLevels[x])
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
574 })
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
575 facIndex = match(facGL,facLevels)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
576
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
577 LisGLs_Mutability = mclapply(1:nrow(matInput), function(x){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
578 cInput = rep(NA,nchar(matInput[x,1]))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
579 cInput[s2c(matInput[x,1])!="N"] = 1
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
580 LisGLs_MutabilityU[[facIndex[x]]] * cInput
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
581 })
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
582
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
583 LisGLs_Targeting = mclapply(1:dim(matInput)[1], function(x){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
584 computeTargeting(matInput[x,2],LisGLs_Mutability[[x]])
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
585 })
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
586
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
587 LisGLs_MutationTypes = mclapply(1:length(matInput[,2]),function(x){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
588 #print(x)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
589 computeMutationTypes(matInput[x,2])
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
590 })
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
591
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
592 LisGLs_Exp = mclapply(1:dim(matInput)[1], function(x){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
593 computeExpected(LisGLs_Targeting[[x]],LisGLs_MutationTypes[[x]])
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
594 })
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
595
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
596 ul_LisGLs_Exp = unlist(LisGLs_Exp)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
597 return(matrix(ul_LisGLs_Exp,ncol=4,nrow=(length(ul_LisGLs_Exp)/4),byrow=T))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
598 }else{
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
599 facGL <- factor(matInput[,2])
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
600 facLevels = levels(facGL)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
601 LisGLs_MutabilityU = lapply(1:length(facLevels), function(x){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
602 computeMutabilities(facLevels[x])
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
603 })
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
604 facIndex = match(facGL,facLevels)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
605
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
606 LisGLs_Mutability = lapply(1:nrow(matInput), function(x){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
607 cInput = rep(NA,nchar(matInput[x,1]))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
608 cInput[s2c(matInput[x,1])!="N"] = 1
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
609 LisGLs_MutabilityU[[facIndex[x]]] * cInput
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
610 })
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
611
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
612 LisGLs_Targeting = lapply(1:dim(matInput)[1], function(x){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
613 computeTargeting(matInput[x,2],LisGLs_Mutability[[x]])
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
614 })
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
615
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
616 LisGLs_MutationTypes = lapply(1:length(matInput[,2]),function(x){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
617 #print(x)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
618 computeMutationTypes(matInput[x,2])
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
619 })
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
620
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
621 LisGLs_Exp = lapply(1:dim(matInput)[1], function(x){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
622 computeExpected(LisGLs_Targeting[[x]],LisGLs_MutationTypes[[x]])
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
623 })
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
624
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
625 ul_LisGLs_Exp = unlist(LisGLs_Exp)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
626 return(matrix(ul_LisGLs_Exp,ncol=4,nrow=(length(ul_LisGLs_Exp)/4),byrow=T))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
627
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
628 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
629 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
630
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
631 # Compute mutabilities of sequence based on the tri-nucleotide model
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
632 computeMutabilities <- function(paramSeq){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
633 seqLen = nchar(paramSeq)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
634 seqMutabilites = rep(NA,seqLen)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
635
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
636 gaplessSeq = gsub("-", "", paramSeq)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
637 gaplessSeqLen = nchar(gaplessSeq)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
638 gaplessSeqMutabilites = rep(NA,gaplessSeqLen)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
639
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
640 if(mutabilityModel!=5){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
641 pos<- 3:(gaplessSeqLen)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
642 subSeq = substr(rep(gaplessSeq,gaplessSeqLen-2),(pos-2),(pos+2))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
643 gaplessSeqMutabilites[pos] =
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
644 tapply( c(
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
645 getMutability( substr(subSeq,1,3), 3) ,
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
646 getMutability( substr(subSeq,2,4), 2),
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
647 getMutability( substr(subSeq,3,5), 1)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
648 ),rep(1:(gaplessSeqLen-2),3),mean,na.rm=TRUE
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
649 )
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
650 #Pos 1
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
651 subSeq = substr(gaplessSeq,1,3)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
652 gaplessSeqMutabilites[1] = getMutability(subSeq , 1)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
653 #Pos 2
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
654 subSeq = substr(gaplessSeq,1,4)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
655 gaplessSeqMutabilites[2] = mean( c(
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
656 getMutability( substr(subSeq,1,3), 2) ,
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
657 getMutability( substr(subSeq,2,4), 1)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
658 ),na.rm=T
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
659 )
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
660 seqMutabilites[which(s2c(paramSeq)!="-")]<- gaplessSeqMutabilites
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
661 return(seqMutabilites)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
662 }else{
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
663
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
664 pos<- 3:(gaplessSeqLen)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
665 subSeq = substr(rep(gaplessSeq,gaplessSeqLen-2),(pos-2),(pos+2))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
666 gaplessSeqMutabilites[pos] = sapply(subSeq,function(x){ getMutability5(x) }, simplify=T)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
667 seqMutabilites[which(s2c(paramSeq)!="-")]<- gaplessSeqMutabilites
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
668 return(seqMutabilites)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
669 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
670
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
671 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
672
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
673 # Returns the mutability of a triplet at a given position
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
674 getMutability <- function(codon, pos=1:3){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
675 triplets <- rownames(mutability)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
676 mutability[ match(codon,triplets) ,pos]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
677 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
678
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
679 getMutability5 <- function(fivemer){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
680 return(mutability[fivemer])
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
681 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
682
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
683 # Returns the substitution probabilty
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
684 getTransistionProb <- function(nuc){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
685 substitution[nuc,]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
686 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
687
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
688 getTransistionProb5 <- function(fivemer){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
689 if(any(which(fivemer==colnames(substitution)))){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
690 return(substitution[,fivemer])
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
691 }else{
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
692 return(array(NA,4))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
693 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
694 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
695
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
696 # Given a nuc, returns the other 3 nucs it can mutate to
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
697 canMutateTo <- function(nuc){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
698 NUCLEOTIDES[- which(NUCLEOTIDES==nuc)]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
699 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
700
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
701 # Given a nucleotide, returns the probabilty of other nucleotide it can mutate to
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
702 canMutateToProb <- function(nuc){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
703 substitution[nuc,canMutateTo(nuc)]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
704 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
705
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
706 # Compute targeting, based on precomputed mutatbility & substitution
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
707 computeTargeting <- function(param_strSeq,param_vecMutabilities){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
708
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
709 if(substitutionModel!=5){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
710 vecSeq = s2c(param_strSeq)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
711 matTargeting = sapply( 1:length(vecSeq), function(x) { param_vecMutabilities[x] * getTransistionProb(vecSeq[x]) } )
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
712 #matTargeting = apply( rbind(vecSeq,param_vecMutabilities),2, function(x) { as.vector(as.numeric(x[2]) * getTransistionProb(x[1])) } )
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
713 dimnames( matTargeting ) = list(NUCLEOTIDES,1:(length(vecSeq)))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
714 return (matTargeting)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
715 }else{
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
716
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
717 seqLen = nchar(param_strSeq)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
718 seqsubstitution = matrix(NA,ncol=seqLen,nrow=4)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
719 paramSeq <- param_strSeq
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
720 gaplessSeq = gsub("-", "", paramSeq)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
721 gaplessSeqLen = nchar(gaplessSeq)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
722 gaplessSeqSubstitution = matrix(NA,ncol=gaplessSeqLen,nrow=4)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
723
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
724 pos<- 3:(gaplessSeqLen)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
725 subSeq = substr(rep(gaplessSeq,gaplessSeqLen-2),(pos-2),(pos+2))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
726 gaplessSeqSubstitution[,pos] = sapply(subSeq,function(x){ getTransistionProb5(x) }, simplify=T)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
727 seqsubstitution[,which(s2c(paramSeq)!="-")]<- gaplessSeqSubstitution
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
728 #matTargeting <- param_vecMutabilities %*% seqsubstitution
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
729 matTargeting <- sweep(seqsubstitution,2,param_vecMutabilities,`*`)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
730 dimnames( matTargeting ) = list(NUCLEOTIDES,1:(seqLen))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
731 return (matTargeting)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
732 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
733 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
734
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
735 # Compute the mutations types
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
736 computeMutationTypes <- function(param_strSeq){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
737 #cat(param_strSeq,"\n")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
738 #vecSeq = trimToLastCodon(param_strSeq)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
739 lenSeq = nchar(param_strSeq)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
740 vecCodons = sapply({1:(lenSeq/3)}*3-2,function(x){substr(param_strSeq,x,x+2)})
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
741 matMutationTypes = matrix( unlist(CODON_TABLE[,vecCodons]) ,ncol=lenSeq,nrow=4, byrow=F)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
742 dimnames( matMutationTypes ) = list(NUCLEOTIDES,1:(ncol(matMutationTypes)))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
743 return(matMutationTypes)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
744 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
745 computeMutationTypesFast <- function(param_strSeq){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
746 matMutationTypes = matrix( CODON_TABLE[,param_strSeq] ,ncol=3,nrow=4, byrow=F)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
747 #dimnames( matMutationTypes ) = list(NUCLEOTIDES,1:(length(vecSeq)))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
748 return(matMutationTypes)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
749 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
750 mutationTypeOptimized <- function( matOfCodons ){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
751 apply( matOfCodons,1,function(x){ mutationType(x[2],x[1]) } )
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
752 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
753
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
754 # Returns a vector of codons 1 mutation away from the given codon
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
755 permutateAllCodon <- function(codon){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
756 cCodon = s2c(codon)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
757 matCodons = t(array(cCodon,dim=c(3,12)))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
758 matCodons[1:4,1] = NUCLEOTIDES
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
759 matCodons[5:8,2] = NUCLEOTIDES
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
760 matCodons[9:12,3] = NUCLEOTIDES
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
761 apply(matCodons,1,c2s)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
762 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
763
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
764 # Given two codons, tells you if the mutation is R or S (based on your definition)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
765 mutationType <- function(codonFrom,codonTo){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
766 if(testID==4){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
767 if( is.na(codonFrom) | is.na(codonTo) | is.na(translateCodonToAminoAcid(codonFrom)) | is.na(translateCodonToAminoAcid(codonTo)) ){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
768 return(NA)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
769 }else{
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
770 mutationType = "S"
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
771 if( translateAminoAcidToTraitChange(translateCodonToAminoAcid(codonFrom)) != translateAminoAcidToTraitChange(translateCodonToAminoAcid(codonTo)) ){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
772 mutationType = "R"
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
773 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
774 if(translateCodonToAminoAcid(codonTo)=="*" | translateCodonToAminoAcid(codonFrom)=="*"){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
775 mutationType = "Stop"
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
776 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
777 return(mutationType)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
778 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
779 }else if(testID==5){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
780 if( is.na(codonFrom) | is.na(codonTo) | is.na(translateCodonToAminoAcid(codonFrom)) | is.na(translateCodonToAminoAcid(codonTo)) ){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
781 return(NA)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
782 }else{
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
783 if(codonFrom==codonTo){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
784 mutationType = "S"
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
785 }else{
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
786 codonFrom = s2c(codonFrom)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
787 codonTo = s2c(codonTo)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
788 mutationType = "Stop"
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
789 nucOfI = codonFrom[which(codonTo!=codonFrom)]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
790 if(nucOfI=="C"){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
791 mutationType = "R"
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
792 }else if(nucOfI=="G"){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
793 mutationType = "S"
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
794 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
795 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
796 return(mutationType)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
797 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
798 }else{
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
799 if( is.na(codonFrom) | is.na(codonTo) | is.na(translateCodonToAminoAcid(codonFrom)) | is.na(translateCodonToAminoAcid(codonTo)) ){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
800 return(NA)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
801 }else{
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
802 mutationType = "S"
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
803 if( translateCodonToAminoAcid(codonFrom) != translateCodonToAminoAcid(codonTo) ){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
804 mutationType = "R"
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
805 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
806 if(translateCodonToAminoAcid(codonTo)=="*" | translateCodonToAminoAcid(codonFrom)=="*"){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
807 mutationType = "Stop"
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
808 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
809 return(mutationType)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
810 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
811 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
812 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
813
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
814
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
815 #given a mat of targeting & it's corresponding mutationtypes returns
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
816 #a vector of Exp_RCDR,Exp_SCDR,Exp_RFWR,Exp_RFWR
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
817 computeExpected <- function(paramTargeting,paramMutationTypes){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
818 # Replacements
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
819 RPos = which(paramMutationTypes=="R")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
820 #FWR
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
821 Exp_R_FWR = sum(paramTargeting[ RPos[which(FWR_Nuc_Mat[RPos]==T)] ],na.rm=T)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
822 #CDR
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
823 Exp_R_CDR = sum(paramTargeting[ RPos[which(CDR_Nuc_Mat[RPos]==T)] ],na.rm=T)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
824 # Silents
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
825 SPos = which(paramMutationTypes=="S")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
826 #FWR
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
827 Exp_S_FWR = sum(paramTargeting[ SPos[which(FWR_Nuc_Mat[SPos]==T)] ],na.rm=T)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
828 #CDR
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
829 Exp_S_CDR = sum(paramTargeting[ SPos[which(CDR_Nuc_Mat[SPos]==T)] ],na.rm=T)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
830
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
831 return(c(Exp_R_CDR,Exp_S_CDR,Exp_R_FWR,Exp_S_FWR))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
832 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
833
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
834 # Count the mutations in a sequence
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
835 # each mutation is treated independently
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
836 analyzeMutations2NucUri_website <- function( rev_in_matrix ){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
837 paramGL = rev_in_matrix[2,]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
838 paramSeq = rev_in_matrix[1,]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
839
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
840 #Fill seq with GL seq if gapped
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
841 #if( any(paramSeq=="-") ){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
842 # gapPos_Seq = which(paramSeq=="-")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
843 # gapPos_Seq_ToReplace = gapPos_Seq[paramGL[gapPos_Seq] != "-"]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
844 # paramSeq[gapPos_Seq_ToReplace] = paramGL[gapPos_Seq_ToReplace]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
845 #}
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
846
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
847
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
848 #if( any(paramSeq=="N") ){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
849 # gapPos_Seq = which(paramSeq=="N")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
850 # gapPos_Seq_ToReplace = gapPos_Seq[paramGL[gapPos_Seq] != "N"]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
851 # paramSeq[gapPos_Seq_ToReplace] = paramGL[gapPos_Seq_ToReplace]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
852 #}
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
853
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
854 analyzeMutations2NucUri( matrix(c( paramGL, paramSeq ),2,length(paramGL),byrow=T) )
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
855
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
856 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
857
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
858 #1 = GL
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
859 #2 = Seq
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
860 analyzeMutations2NucUri <- function( in_matrix=matrix(c(c("A","A","A","C","C","C"),c("A","G","G","C","C","A")),2,6,byrow=T) ){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
861 paramGL = in_matrix[2,]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
862 paramSeq = in_matrix[1,]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
863 paramSeqUri = paramGL
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
864 #mutations = apply(rbind(paramGL,paramSeq), 2, function(x){!x[1]==x[2]})
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
865 mutations_val = paramGL != paramSeq
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
866 if(any(mutations_val)){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
867 mutationPos = {1:length(mutations_val)}[mutations_val]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
868 mutationPos = mutationPos[sapply(mutationPos, function(x){!any(paramSeq[getCodonPos(x)]=="N")})]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
869 length_mutations =length(mutationPos)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
870 mutationInfo = rep(NA,length_mutations)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
871 if(any(mutationPos)){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
872
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
873 pos<- mutationPos
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
874 pos_array<-array(sapply(pos,getCodonPos))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
875 codonGL = paramGL[pos_array]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
876
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
877 codonSeq = sapply(pos,function(x){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
878 seqP = paramGL[getCodonPos(x)]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
879 muCodonPos = {x-1}%%3+1
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
880 seqP[muCodonPos] = paramSeq[x]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
881 return(seqP)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
882 })
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
883 GLcodons = apply(matrix(codonGL,length_mutations,3,byrow=TRUE),1,c2s)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
884 Seqcodons = apply(codonSeq,2,c2s)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
885 mutationInfo = apply(rbind(GLcodons , Seqcodons),2,function(x){mutationType(c2s(x[1]),c2s(x[2]))})
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
886 names(mutationInfo) = mutationPos
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
887 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
888 if(any(!is.na(mutationInfo))){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
889 return(mutationInfo[!is.na(mutationInfo)])
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
890 }else{
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
891 return(NA)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
892 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
893
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
894
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
895 }else{
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
896 return (NA)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
897 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
898 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
899
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
900 processNucMutations2 <- function(mu){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
901 if(!is.na(mu)){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
902 #R
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
903 if(any(mu=="R")){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
904 Rs = mu[mu=="R"]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
905 nucNumbs = as.numeric(names(Rs))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
906 R_CDR = sum(as.integer(CDR_Nuc[nucNumbs]),na.rm=T)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
907 R_FWR = sum(as.integer(FWR_Nuc[nucNumbs]),na.rm=T)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
908 }else{
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
909 R_CDR = 0
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
910 R_FWR = 0
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
911 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
912
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
913 #S
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
914 if(any(mu=="S")){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
915 Ss = mu[mu=="S"]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
916 nucNumbs = as.numeric(names(Ss))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
917 S_CDR = sum(as.integer(CDR_Nuc[nucNumbs]),na.rm=T)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
918 S_FWR = sum(as.integer(FWR_Nuc[nucNumbs]),na.rm=T)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
919 }else{
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
920 S_CDR = 0
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
921 S_FWR = 0
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
922 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
923
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
924
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
925 retVec = c(R_CDR,S_CDR,R_FWR,S_FWR)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
926 retVec[is.na(retVec)]=0
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
927 return(retVec)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
928 }else{
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
929 return(rep(0,4))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
930 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
931 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
932
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
933
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
934 ## Z-score Test
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
935 computeZScore <- function(mat, test="Focused"){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
936 matRes <- matrix(NA,ncol=2,nrow=(nrow(mat)))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
937 if(test=="Focused"){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
938 #Z_Focused_CDR
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
939 #P_Denom = sum( mat[1,c(5,6,8)], na.rm=T )
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
940 P = apply(mat[,c(5,6,8)],1,function(x){(x[1]/sum(x))})
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
941 R_mean = apply(cbind(mat[,c(1,2,4)],P),1,function(x){x[4]*(sum(x[1:3]))})
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
942 R_sd=sqrt(R_mean*(1-P))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
943 matRes[,1] = (mat[,1]-R_mean)/R_sd
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
944
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
945 #Z_Focused_FWR
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
946 #P_Denom = sum( mat[1,c(7,6,8)], na.rm=T )
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
947 P = apply(mat[,c(7,6,8)],1,function(x){(x[1]/sum(x))})
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
948 R_mean = apply(cbind(mat[,c(3,2,4)],P),1,function(x){x[4]*(sum(x[1:3]))})
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
949 R_sd=sqrt(R_mean*(1-P))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
950 matRes[,2] = (mat[,3]-R_mean)/R_sd
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
951 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
952
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
953 if(test=="Local"){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
954 #Z_Focused_CDR
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
955 #P_Denom = sum( mat[1,c(5,6,8)], na.rm=T )
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
956 P = apply(mat[,c(5,6)],1,function(x){(x[1]/sum(x))})
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
957 R_mean = apply(cbind(mat[,c(1,2)],P),1,function(x){x[3]*(sum(x[1:2]))})
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
958 R_sd=sqrt(R_mean*(1-P))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
959 matRes[,1] = (mat[,1]-R_mean)/R_sd
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
960
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
961 #Z_Focused_FWR
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
962 #P_Denom = sum( mat[1,c(7,6,8)], na.rm=T )
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
963 P = apply(mat[,c(7,8)],1,function(x){(x[1]/sum(x))})
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
964 R_mean = apply(cbind(mat[,c(3,4)],P),1,function(x){x[3]*(sum(x[1:2]))})
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
965 R_sd=sqrt(R_mean*(1-P))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
966 matRes[,2] = (mat[,3]-R_mean)/R_sd
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
967 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
968
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
969 if(test=="Imbalanced"){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
970 #Z_Focused_CDR
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
971 #P_Denom = sum( mat[1,c(5,6,8)], na.rm=T )
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
972 P = apply(mat[,5:8],1,function(x){((x[1]+x[2])/sum(x))})
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
973 R_mean = apply(cbind(mat[,1:4],P),1,function(x){x[5]*(sum(x[1:4]))})
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
974 R_sd=sqrt(R_mean*(1-P))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
975 matRes[,1] = (mat[,1]-R_mean)/R_sd
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
976
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
977 #Z_Focused_FWR
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
978 #P_Denom = sum( mat[1,c(7,6,8)], na.rm=T )
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
979 P = apply(mat[,5:8],1,function(x){((x[3]+x[4])/sum(x))})
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
980 R_mean = apply(cbind(mat[,1:4],P),1,function(x){x[5]*(sum(x[1:4]))})
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
981 R_sd=sqrt(R_mean*(1-P))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
982 matRes[,2] = (mat[,3]-R_mean)/R_sd
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
983 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
984
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
985 matRes[is.nan(matRes)] = NA
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
986 return(matRes)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
987 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
988
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
989 # Return a p-value for a z-score
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
990 z2p <- function(z){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
991 p=NA
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
992 if( !is.nan(z) && !is.na(z)){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
993 if(z>0){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
994 p = (1 - pnorm(z,0,1))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
995 } else if(z<0){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
996 p = (-1 * pnorm(z,0,1))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
997 } else{
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
998 p = 0.5
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
999 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1000 }else{
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1001 p = NA
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1002 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1003 return(p)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1004 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1005
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1006
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1007 ## Bayesian Test
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1008
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1009 # Fitted parameter for the bayesian framework
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1010 BAYESIAN_FITTED<-c(0.407277142798302, 0.554007336744485, 0.63777155771234, 0.693989162719009, 0.735450014674917, 0.767972534429806, 0.794557287143399, 0.816906816601605, 0.83606796225341, 0.852729446430296, 0.867370424541641, 0.880339760590323, 0.891900995024999, 0.902259181289864, 0.911577919359,0.919990301665853, 0.927606458124537, 0.934518806350661, 0.940805863754375, 0.946534836475715, 0.951763691199255, 0.95654428191308, 0.960920179487397, 0.964930893680829, 0.968611312149038, 0.971992459313836, 0.975102110004818, 0.977964943023096, 0.980603428208439, 0.983037660179428, 0.985285800977406, 0.987364285326685, 0.989288037855441, 0.991070478823525, 0.992723699729969, 0.994259575477392, 0.995687688867975, 0.997017365051493, 0.998257085153047, 0.999414558305388, 1.00049681357804, 1.00151036237481, 1.00246080204981, 1.00335370751909, 1.0041939329768, 1.0049859393417, 1.00573382091263, 1.00644127217376, 1.00711179729107, 1.00774845526417, 1.00835412715854, 1.00893143010366, 1.00948275846309, 1.01001030293661, 1.01051606798079, 1.01100188771288, 1.01146944044216, 1.01192026195449, 1.01235575766094, 1.01277721370986)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1011 CONST_i <- sort(c(((2^(seq(-39,0,length.out=201)))/2)[1:200],(c(0:11,13:99)+0.5)/100,1-(2^(seq(-39,0,length.out=201)))/2))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1012
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1013 # Given x, M & p, returns a pdf
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1014 calculate_bayes <- function ( x=3, N=10, p=0.33,
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1015 i=CONST_i,
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1016 max_sigma=20,length_sigma=4001
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1017 ){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1018 if(!0%in%N){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1019 G <- max(length(x),length(N),length(p))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1020 x=array(x,dim=G)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1021 N=array(N,dim=G)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1022 p=array(p,dim=G)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1023 sigma_s<-seq(-max_sigma,max_sigma,length.out=length_sigma)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1024 sigma_1<-log({i/{1-i}}/{p/{1-p}})
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1025 index<-min(N,60)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1026 y<-dbeta(i,x+BAYESIAN_FITTED[index],N+BAYESIAN_FITTED[index]-x)*(1-p)*p*exp(sigma_1)/({1-p}^2+2*p*{1-p}*exp(sigma_1)+{p^2}*exp(2*sigma_1))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1027 if(!sum(is.na(y))){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1028 tmp<-approx(sigma_1,y,sigma_s)$y
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1029 tmp/sum(tmp)/{2*max_sigma/{length_sigma-1}}
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1030 }else{
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1031 return(NA)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1032 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1033 }else{
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1034 return(NA)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1035 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1036 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1037 # Given a mat of observed & expected, return a list of CDR & FWR pdf for selection
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1038 computeBayesianScore <- function(mat, test="Focused", max_sigma=20,length_sigma=4001){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1039 flagOneSeq = F
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1040 if(nrow(mat)==1){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1041 mat=rbind(mat,mat)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1042 flagOneSeq = T
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1043 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1044 if(test=="Focused"){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1045 #CDR
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1046 P = c(apply(mat[,c(5,6,8)],1,function(x){(x[1]/sum(x))}),0.5)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1047 N = c(apply(mat[,c(1,2,4)],1,function(x){(sum(x))}),0)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1048 X = c(mat[,1],0)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1049 bayesCDR = apply(cbind(X,N,P),1,function(x){calculate_bayes(x=x[1],N=x[2],p=x[3],max_sigma=max_sigma,length_sigma=length_sigma)})
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1050 bayesCDR = bayesCDR[-length(bayesCDR)]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1051
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1052 #FWR
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1053 P = c(apply(mat[,c(7,6,8)],1,function(x){(x[1]/sum(x))}),0.5)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1054 N = c(apply(mat[,c(3,2,4)],1,function(x){(sum(x))}),0)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1055 X = c(mat[,3],0)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1056 bayesFWR = apply(cbind(X,N,P),1,function(x){calculate_bayes(x=x[1],N=x[2],p=x[3],max_sigma=max_sigma,length_sigma=length_sigma)})
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1057 bayesFWR = bayesFWR[-length(bayesFWR)]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1058 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1059
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1060 if(test=="Local"){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1061 #CDR
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1062 P = c(apply(mat[,c(5,6)],1,function(x){(x[1]/sum(x))}),0.5)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1063 N = c(apply(mat[,c(1,2)],1,function(x){(sum(x))}),0)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1064 X = c(mat[,1],0)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1065 bayesCDR = apply(cbind(X,N,P),1,function(x){calculate_bayes(x=x[1],N=x[2],p=x[3],max_sigma=max_sigma,length_sigma=length_sigma)})
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1066 bayesCDR = bayesCDR[-length(bayesCDR)]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1067
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1068 #FWR
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1069 P = c(apply(mat[,c(7,8)],1,function(x){(x[1]/sum(x))}),0.5)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1070 N = c(apply(mat[,c(3,4)],1,function(x){(sum(x))}),0)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1071 X = c(mat[,3],0)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1072 bayesFWR = apply(cbind(X,N,P),1,function(x){calculate_bayes(x=x[1],N=x[2],p=x[3],max_sigma=max_sigma,length_sigma=length_sigma)})
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1073 bayesFWR = bayesFWR[-length(bayesFWR)]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1074 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1075
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1076 if(test=="Imbalanced"){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1077 #CDR
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1078 P = c(apply(mat[,c(5:8)],1,function(x){((x[1]+x[2])/sum(x))}),0.5)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1079 N = c(apply(mat[,c(1:4)],1,function(x){(sum(x))}),0)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1080 X = c(apply(mat[,c(1:2)],1,function(x){(sum(x))}),0)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1081 bayesCDR = apply(cbind(X,N,P),1,function(x){calculate_bayes(x=x[1],N=x[2],p=x[3],max_sigma=max_sigma,length_sigma=length_sigma)})
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1082 bayesCDR = bayesCDR[-length(bayesCDR)]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1083
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1084 #FWR
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1085 P = c(apply(mat[,c(5:8)],1,function(x){((x[3]+x[4])/sum(x))}),0.5)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1086 N = c(apply(mat[,c(1:4)],1,function(x){(sum(x))}),0)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1087 X = c(apply(mat[,c(3:4)],1,function(x){(sum(x))}),0)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1088 bayesFWR = apply(cbind(X,N,P),1,function(x){calculate_bayes(x=x[1],N=x[2],p=x[3],max_sigma=max_sigma,length_sigma=length_sigma)})
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1089 bayesFWR = bayesFWR[-length(bayesFWR)]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1090 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1091
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1092 if(test=="ImbalancedSilent"){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1093 #CDR
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1094 P = c(apply(mat[,c(6,8)],1,function(x){((x[1])/sum(x))}),0.5)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1095 N = c(apply(mat[,c(2,4)],1,function(x){(sum(x))}),0)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1096 X = c(apply(mat[,c(2,4)],1,function(x){(x[1])}),0)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1097 bayesCDR = apply(cbind(X,N,P),1,function(x){calculate_bayes(x=x[1],N=x[2],p=x[3],max_sigma=max_sigma,length_sigma=length_sigma)})
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1098 bayesCDR = bayesCDR[-length(bayesCDR)]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1099
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1100 #FWR
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1101 P = c(apply(mat[,c(6,8)],1,function(x){((x[2])/sum(x))}),0.5)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1102 N = c(apply(mat[,c(2,4)],1,function(x){(sum(x))}),0)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1103 X = c(apply(mat[,c(2,4)],1,function(x){(x[2])}),0)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1104 bayesFWR = apply(cbind(X,N,P),1,function(x){calculate_bayes(x=x[1],N=x[2],p=x[3],max_sigma=max_sigma,length_sigma=length_sigma)})
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1105 bayesFWR = bayesFWR[-length(bayesFWR)]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1106 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1107
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1108 if(flagOneSeq==T){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1109 bayesCDR = bayesCDR[1]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1110 bayesFWR = bayesFWR[1]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1111 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1112 return( list("CDR"=bayesCDR, "FWR"=bayesFWR) )
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1113 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1114
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1115 ##Covolution
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1116 break2chunks<-function(G=1000){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1117 base<-2^round(log(sqrt(G),2),0)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1118 return(c(rep(base,floor(G/base)-1),base+G-(floor(G/base)*base)))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1119 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1120
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1121 PowersOfTwo <- function(G=100){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1122 exponents <- array()
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1123 i = 0
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1124 while(G > 0){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1125 i=i+1
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1126 exponents[i] <- floor( log2(G) )
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1127 G <- G-2^exponents[i]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1128 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1129 return(exponents)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1130 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1131
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1132 convolutionPowersOfTwo <- function( cons, length_sigma=4001 ){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1133 G = ncol(cons)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1134 if(G>1){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1135 for(gen in log(G,2):1){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1136 ll<-seq(from=2,to=2^gen,by=2)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1137 sapply(ll,function(l){cons[,l/2]<<-weighted_conv(cons[,l],cons[,l-1],length_sigma=length_sigma)})
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1138 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1139 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1140 return( cons[,1] )
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1141 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1142
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1143 convolutionPowersOfTwoByTwos <- function( cons, length_sigma=4001,G=1 ){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1144 if(length(ncol(cons))) G<-ncol(cons)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1145 groups <- PowersOfTwo(G)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1146 matG <- matrix(NA, ncol=length(groups), nrow=length(cons)/G )
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1147 startIndex = 1
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1148 for( i in 1:length(groups) ){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1149 stopIndex <- 2^groups[i] + startIndex - 1
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1150 if(stopIndex!=startIndex){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1151 matG[,i] <- convolutionPowersOfTwo( cons[,startIndex:stopIndex], length_sigma=length_sigma )
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1152 startIndex = stopIndex + 1
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1153 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1154 else {
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1155 if(G>1) matG[,i] <- cons[,startIndex:stopIndex]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1156 else matG[,i] <- cons
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1157 #startIndex = stopIndex + 1
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1158 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1159 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1160 return( list( matG, groups ) )
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1161 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1162
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1163 weighted_conv<-function(x,y,w=1,m=100,length_sigma=4001){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1164 lx<-length(x)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1165 ly<-length(y)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1166 if({lx<m}| {{lx*w}<m}| {{ly}<m}| {{ly*w}<m}){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1167 if(w<1){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1168 y1<-approx(1:ly,y,seq(1,ly,length.out=m))$y
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1169 x1<-approx(1:lx,x,seq(1,lx,length.out=m/w))$y
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1170 lx<-length(x1)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1171 ly<-length(y1)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1172 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1173 else {
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1174 y1<-approx(1:ly,y,seq(1,ly,length.out=m*w))$y
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1175 x1<-approx(1:lx,x,seq(1,lx,length.out=m))$y
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1176 lx<-length(x1)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1177 ly<-length(y1)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1178 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1179 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1180 else{
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1181 x1<-x
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1182 y1<-approx(1:ly,y,seq(1,ly,length.out=floor(lx*w)))$y
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1183 ly<-length(y1)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1184 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1185 tmp<-approx(x=1:(lx+ly-1),y=convolve(x1,rev(y1),type="open"),xout=seq(1,lx+ly-1,length.out=length_sigma))$y
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1186 tmp[tmp<=0] = 0
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1187 return(tmp/sum(tmp))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1188 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1189
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1190 calculate_bayesGHelper <- function( listMatG,length_sigma=4001 ){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1191 matG <- listMatG[[1]]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1192 groups <- listMatG[[2]]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1193 i = 1
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1194 resConv <- matG[,i]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1195 denom <- 2^groups[i]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1196 if(length(groups)>1){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1197 while( i<length(groups) ){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1198 i = i + 1
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1199 resConv <- weighted_conv(resConv, matG[,i], w= {{2^groups[i]}/denom} ,length_sigma=length_sigma)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1200 #cat({{2^groups[i]}/denom},"\n")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1201 denom <- denom + 2^groups[i]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1202 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1203 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1204 return(resConv)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1205 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1206
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1207 # Given a list of PDFs, returns a convoluted PDF
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1208 groupPosteriors <- function( listPosteriors, max_sigma=20, length_sigma=4001 ,Threshold=2 ){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1209 listPosteriors = listPosteriors[ !is.na(listPosteriors) ]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1210 Length_Postrior<-length(listPosteriors)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1211 if(Length_Postrior>1 & Length_Postrior<=Threshold){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1212 cons = matrix(unlist(listPosteriors),length(listPosteriors[[1]]),length(listPosteriors))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1213 listMatG <- convolutionPowersOfTwoByTwos(cons,length_sigma=length_sigma)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1214 y<-calculate_bayesGHelper(listMatG,length_sigma=length_sigma)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1215 return( y/sum(y)/(2*max_sigma/(length_sigma-1)) )
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1216 }else if(Length_Postrior==1) return(listPosteriors[[1]])
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1217 else if(Length_Postrior==0) return(NA)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1218 else {
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1219 cons = matrix(unlist(listPosteriors),length(listPosteriors[[1]]),length(listPosteriors))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1220 y = fastConv(cons,max_sigma=max_sigma, length_sigma=length_sigma )
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1221 return( y/sum(y)/(2*max_sigma/(length_sigma-1)) )
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1222 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1223 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1224
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1225 fastConv<-function(cons, max_sigma=20, length_sigma=4001){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1226 chunks<-break2chunks(G=ncol(cons))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1227 if(ncol(cons)==3) chunks<-2:1
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1228 index_chunks_end <- cumsum(chunks)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1229 index_chunks_start <- c(1,index_chunks_end[-length(index_chunks_end)]+1)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1230 index_chunks <- cbind(index_chunks_start,index_chunks_end)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1231
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1232 case <- sum(chunks!=chunks[1])
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1233 if(case==1) End <- max(1,((length(index_chunks)/2)-1))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1234 else End <- max(1,((length(index_chunks)/2)))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1235
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1236 firsts <- sapply(1:End,function(i){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1237 indexes<-index_chunks[i,1]:index_chunks[i,2]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1238 convolutionPowersOfTwoByTwos(cons[ ,indexes])[[1]]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1239 })
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1240 if(case==0){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1241 result<-calculate_bayesGHelper( convolutionPowersOfTwoByTwos(firsts) )
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1242 }else if(case==1){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1243 last<-list(calculate_bayesGHelper(
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1244 convolutionPowersOfTwoByTwos( cons[ ,index_chunks[length(index_chunks)/2,1]:index_chunks[length(index_chunks)/2,2]] )
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1245 ),0)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1246 result_first<-calculate_bayesGHelper(convolutionPowersOfTwoByTwos(firsts))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1247 result<-calculate_bayesGHelper(
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1248 list(
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1249 cbind(
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1250 result_first,last[[1]]),
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1251 c(log(index_chunks_end[length(index_chunks)/2-1],2),log(index_chunks[length(index_chunks)/2,2]-index_chunks[length(index_chunks)/2,1]+1,2))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1252 )
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1253 )
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1254 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1255 return(as.vector(result))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1256 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1257
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1258 # Computes the 95% CI for a pdf
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1259 calcBayesCI <- function(Pdf,low=0.025,up=0.975,max_sigma=20, length_sigma=4001){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1260 if(length(Pdf)!=length_sigma) return(NA)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1261 sigma_s=seq(-max_sigma,max_sigma,length.out=length_sigma)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1262 cdf = cumsum(Pdf)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1263 cdf = cdf/cdf[length(cdf)]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1264 return( c(sigma_s[findInterval(low,cdf)-1] , sigma_s[findInterval(up,cdf)]) )
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1265 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1266
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1267 # Computes a mean for a pdf
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1268 calcBayesMean <- function(Pdf,max_sigma=20,length_sigma=4001){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1269 if(length(Pdf)!=length_sigma) return(NA)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1270 sigma_s=seq(-max_sigma,max_sigma,length.out=length_sigma)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1271 norm = {length_sigma-1}/2/max_sigma
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1272 return( (Pdf%*%sigma_s/norm) )
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1273 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1274
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1275 # Returns the mean, and the 95% CI for a pdf
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1276 calcBayesOutputInfo <- function(Pdf,low=0.025,up=0.975,max_sigma=20, length_sigma=4001){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1277 if(is.na(Pdf))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1278 return(rep(NA,3))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1279 bCI = calcBayesCI(Pdf=Pdf,low=low,up=up,max_sigma=max_sigma,length_sigma=length_sigma)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1280 bMean = calcBayesMean(Pdf=Pdf,max_sigma=max_sigma,length_sigma=length_sigma)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1281 return(c(bMean, bCI))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1282 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1283
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1284 # Computes the p-value of a pdf
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1285 computeSigmaP <- function(Pdf, length_sigma=4001, max_sigma=20){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1286 if(length(Pdf)>1){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1287 norm = {length_sigma-1}/2/max_sigma
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1288 pVal = {sum(Pdf[1:{{length_sigma-1}/2}]) + Pdf[{{length_sigma+1}/2}]/2}/norm
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1289 if(pVal>0.5){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1290 pVal = pVal-1
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1291 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1292 return(pVal)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1293 }else{
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1294 return(NA)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1295 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1296 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1297
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1298 # Compute p-value of two distributions
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1299 compareTwoDistsFaster <-function(sigma_S=seq(-20,20,length.out=4001), N=10000, dens1=runif(4001,0,1), dens2=runif(4001,0,1)){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1300 #print(c(length(dens1),length(dens2)))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1301 if(length(dens1)>1 & length(dens2)>1 ){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1302 dens1<-dens1/sum(dens1)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1303 dens2<-dens2/sum(dens2)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1304 cum2 <- cumsum(dens2)-dens2/2
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1305 tmp<- sum(sapply(1:length(dens1),function(i)return(dens1[i]*cum2[i])))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1306 #print(tmp)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1307 if(tmp>0.5)tmp<-tmp-1
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1308 return( tmp )
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1309 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1310 else {
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1311 return(NA)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1312 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1313 #return (sum(sapply(1:N,function(i)(sample(sigma_S,1,prob=dens1)>sample(sigma_S,1,prob=dens2))))/N)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1314 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1315
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1316 # get number of seqeunces contributing to the sigma (i.e. seqeunces with mutations)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1317 numberOfSeqsWithMutations <- function(matMutations,test=1){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1318 if(test==4)test=2
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1319 cdrSeqs <- 0
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1320 fwrSeqs <- 0
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1321 if(test==1){#focused
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1322 cdrMutations <- apply(matMutations, 1, function(x){ sum(x[c(1,2,4)]) })
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1323 fwrMutations <- apply(matMutations, 1, function(x){ sum(x[c(3,4,2)]) })
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1324 if( any(which(cdrMutations>0)) ) cdrSeqs <- sum(cdrMutations>0)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1325 if( any(which(fwrMutations>0)) ) fwrSeqs <- sum(fwrMutations>0)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1326 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1327 if(test==2){#local
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1328 cdrMutations <- apply(matMutations, 1, function(x){ sum(x[c(1,2)]) })
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1329 fwrMutations <- apply(matMutations, 1, function(x){ sum(x[c(3,4)]) })
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1330 if( any(which(cdrMutations>0)) ) cdrSeqs <- sum(cdrMutations>0)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1331 if( any(which(fwrMutations>0)) ) fwrSeqs <- sum(fwrMutations>0)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1332 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1333 return(c("CDR"=cdrSeqs, "FWR"=fwrSeqs))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1334 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1335
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1336
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1337
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1338 shadeColor <- function(sigmaVal=NA,pVal=NA){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1339 if(is.na(sigmaVal) & is.na(pVal)) return(NA)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1340 if(is.na(sigmaVal) & !is.na(pVal)) sigmaVal=sign(pVal)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1341 if(is.na(pVal) || pVal==1 || pVal==0){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1342 returnColor = "#FFFFFF";
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1343 }else{
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1344 colVal=abs(pVal);
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1345
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1346 if(sigmaVal<0){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1347 if(colVal>0.1)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1348 returnColor = "#CCFFCC";
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1349 if(colVal<=0.1)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1350 returnColor = "#99FF99";
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1351 if(colVal<=0.050)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1352 returnColor = "#66FF66";
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1353 if(colVal<=0.010)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1354 returnColor = "#33FF33";
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1355 if(colVal<=0.005)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1356 returnColor = "#00FF00";
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1357
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1358 }else{
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1359 if(colVal>0.1)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1360 returnColor = "#FFCCCC";
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1361 if(colVal<=0.1)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1362 returnColor = "#FF9999";
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1363 if(colVal<=0.05)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1364 returnColor = "#FF6666";
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1365 if(colVal<=0.01)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1366 returnColor = "#FF3333";
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1367 if(colVal<0.005)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1368 returnColor = "#FF0000";
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1369 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1370 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1371
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1372 return(returnColor)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1373 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1374
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1375
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1376
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1377 plotHelp <- function(xfrac=0.05,yfrac=0.05,log=FALSE){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1378 if(!log){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1379 x = par()$usr[1]-(par()$usr[2]-par()$usr[1])*xfrac
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1380 y = par()$usr[4]+(par()$usr[4]-par()$usr[3])*yfrac
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1381 }else {
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1382 if(log==2){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1383 x = par()$usr[1]-(par()$usr[2]-par()$usr[1])*xfrac
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1384 y = 10^((par()$usr[4])+((par()$usr[4])-(par()$usr[3]))*yfrac)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1385 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1386 if(log==1){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1387 x = 10^((par()$usr[1])-((par()$usr[2])-(par()$usr[1]))*xfrac)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1388 y = par()$usr[4]+(par()$usr[4]-par()$usr[3])*yfrac
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1389 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1390 if(log==3){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1391 x = 10^((par()$usr[1])-((par()$usr[2])-(par()$usr[1]))*xfrac)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1392 y = 10^((par()$usr[4])+((par()$usr[4])-(par()$usr[3]))*yfrac)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1393 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1394 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1395 return(c("x"=x,"y"=y))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1396 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1397
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1398 # SHMulation
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1399
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1400 # Based on targeting, introduce a single mutation & then update the targeting
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1401 oneMutation <- function(){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1402 # Pick a postion + mutation
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1403 posMutation = sample(1:(seqGermlineLen*4),1,replace=F,prob=as.vector(seqTargeting))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1404 posNucNumb = ceiling(posMutation/4) # Nucleotide number
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1405 posNucKind = 4 - ( (posNucNumb*4) - posMutation ) # Nuc the position mutates to
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1406
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1407 #mutate the simulation sequence
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1408 seqSimVec <- s2c(seqSim)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1409 seqSimVec[posNucNumb] <- NUCLEOTIDES[posNucKind]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1410 seqSim <<- c2s(seqSimVec)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1411
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1412 #update Mutability, Targeting & MutationsTypes
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1413 updateMutabilityNTargeting(posNucNumb)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1414
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1415 #return(c(posNucNumb,NUCLEOTIDES[posNucKind]))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1416 return(posNucNumb)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1417 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1418
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1419 updateMutabilityNTargeting <- function(position){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1420 min_i<-max((position-2),1)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1421 max_i<-min((position+2),nchar(seqSim))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1422 min_ii<-min(min_i,3)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1423
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1424 #mutability - update locally
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1425 seqMutability[(min_i):(max_i)] <<- computeMutabilities(substr(seqSim,position-4,position+4))[(min_ii):(max_i-min_i+min_ii)]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1426
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1427
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1428 #targeting - compute locally
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1429 seqTargeting[,min_i:max_i] <<- computeTargeting(substr(seqSim,min_i,max_i),seqMutability[min_i:max_i])
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1430 seqTargeting[is.na(seqTargeting)] <<- 0
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1431 #mutCodonPos = getCodonPos(position)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1432 mutCodonPos = seq(getCodonPos(min_i)[1],getCodonPos(max_i)[3])
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1433 #cat(mutCodonPos,"\n")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1434 mutTypeCodon = getCodonPos(position)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1435 seqMutationTypes[,mutTypeCodon] <<- computeMutationTypesFast( substr(seqSim,mutTypeCodon[1],mutTypeCodon[3]) )
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1436 # Stop = 0
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1437 if(any(seqMutationTypes[,mutCodonPos]=="Stop",na.rm=T )){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1438 seqTargeting[,mutCodonPos][seqMutationTypes[,mutCodonPos]=="Stop"] <<- 0
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1439 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1440
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1441
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1442 #Selection
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1443 selectedPos = (min_i*4-4)+(which(seqMutationTypes[,min_i:max_i]=="R"))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1444 # CDR
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1445 selectedCDR = selectedPos[which(matCDR[selectedPos]==T)]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1446 seqTargeting[selectedCDR] <<- seqTargeting[selectedCDR] * exp(selCDR)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1447 seqTargeting[selectedCDR] <<- seqTargeting[selectedCDR]/baseLineCDR_K
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1448
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1449 # FWR
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1450 selectedFWR = selectedPos[which(matFWR[selectedPos]==T)]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1451 seqTargeting[selectedFWR] <<- seqTargeting[selectedFWR] * exp(selFWR)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1452 seqTargeting[selectedFWR] <<- seqTargeting[selectedFWR]/baseLineFWR_K
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1453
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1454 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1455
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1456
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1457
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1458 # Validate the mutation: if the mutation has not been sampled before validate it, else discard it.
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1459 validateMutation <- function(){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1460 if( !(mutatedPos%in%mutatedPositions) ){ # if it's a new mutation
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1461 uniqueMutationsIntroduced <<- uniqueMutationsIntroduced + 1
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1462 mutatedPositions[uniqueMutationsIntroduced] <<- mutatedPos
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1463 }else{
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1464 if(substr(seqSim,mutatedPos,mutatedPos)==substr(seqGermline,mutatedPos,mutatedPos)){ # back to germline mutation
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1465 mutatedPositions <<- mutatedPositions[-which(mutatedPositions==mutatedPos)]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1466 uniqueMutationsIntroduced <<- uniqueMutationsIntroduced - 1
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1467 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1468 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1469 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1470
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1471
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1472
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1473 # Places text (labels) at normalized coordinates
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1474 myaxis <- function(xfrac=0.05,yfrac=0.05,log=FALSE,w="text",cex=1,adj=1,thecol="black"){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1475 par(xpd=TRUE)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1476 if(!log)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1477 text(par()$usr[1]-(par()$usr[2]-par()$usr[1])*xfrac,par()$usr[4]+(par()$usr[4]-par()$usr[3])*yfrac,w,cex=cex,adj=adj,col=thecol)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1478 else {
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1479 if(log==2)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1480 text(
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1481 par()$usr[1]-(par()$usr[2]-par()$usr[1])*xfrac,
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1482 10^((par()$usr[4])+((par()$usr[4])-(par()$usr[3]))*yfrac),
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1483 w,cex=cex,adj=adj,col=thecol)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1484 if(log==1)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1485 text(
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1486 10^((par()$usr[1])-((par()$usr[2])-(par()$usr[1]))*xfrac),
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1487 par()$usr[4]+(par()$usr[4]-par()$usr[3])*yfrac,
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1488 w,cex=cex,adj=adj,col=thecol)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1489 if(log==3)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1490 text(
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1491 10^((par()$usr[1])-((par()$usr[2])-(par()$usr[1]))*xfrac),
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1492 10^((par()$usr[4])+((par()$usr[4])-(par()$usr[3]))*yfrac),
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1493 w,cex=cex,adj=adj,col=thecol)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1494 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1495 par(xpd=FALSE)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1496 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1497
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1498
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1499
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1500 # Count the mutations in a sequence
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1501 analyzeMutations <- function( inputMatrixIndex, model = 0 , multipleMutation=0, seqWithStops=0){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1502
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1503 paramGL = s2c(matInput[inputMatrixIndex,2])
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1504 paramSeq = s2c(matInput[inputMatrixIndex,1])
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1505
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1506 #if( any(paramSeq=="N") ){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1507 # gapPos_Seq = which(paramSeq=="N")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1508 # gapPos_Seq_ToReplace = gapPos_Seq[paramGL[gapPos_Seq] != "N"]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1509 # paramSeq[gapPos_Seq_ToReplace] = paramGL[gapPos_Seq_ToReplace]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1510 #}
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1511 mutations_val = paramGL != paramSeq
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1512
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1513 if(any(mutations_val)){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1514 mutationPos = which(mutations_val)#{1:length(mutations_val)}[mutations_val]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1515 length_mutations =length(mutationPos)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1516 mutationInfo = rep(NA,length_mutations)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1517
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1518 pos<- mutationPos
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1519 pos_array<-array(sapply(pos,getCodonPos))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1520 codonGL = paramGL[pos_array]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1521 codonSeqWhole = paramSeq[pos_array]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1522 codonSeq = sapply(pos,function(x){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1523 seqP = paramGL[getCodonPos(x)]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1524 muCodonPos = {x-1}%%3+1
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1525 seqP[muCodonPos] = paramSeq[x]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1526 return(seqP)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1527 })
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1528 GLcodons = apply(matrix(codonGL,length_mutations,3,byrow=TRUE),1,c2s)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1529 SeqcodonsWhole = apply(matrix(codonSeqWhole,length_mutations,3,byrow=TRUE),1,c2s)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1530 Seqcodons = apply(codonSeq,2,c2s)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1531
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1532 mutationInfo = apply(rbind(GLcodons , Seqcodons),2,function(x){mutationType(c2s(x[1]),c2s(x[2]))})
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1533 names(mutationInfo) = mutationPos
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1534
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1535 mutationInfoWhole = apply(rbind(GLcodons , SeqcodonsWhole),2,function(x){mutationType(c2s(x[1]),c2s(x[2]))})
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1536 names(mutationInfoWhole) = mutationPos
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1537
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1538 mutationInfo <- mutationInfo[!is.na(mutationInfo)]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1539 mutationInfoWhole <- mutationInfoWhole[!is.na(mutationInfoWhole)]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1540
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1541 if(any(!is.na(mutationInfo))){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1542
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1543 #Filter based on Stop (at the codon level)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1544 if(seqWithStops==1){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1545 nucleotidesAtStopCodons = names(mutationInfoWhole[mutationInfoWhole!="Stop"])
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1546 mutationInfo = mutationInfo[nucleotidesAtStopCodons]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1547 mutationInfoWhole = mutationInfo[nucleotidesAtStopCodons]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1548 }else{
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1549 countStops = sum(mutationInfoWhole=="Stop")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1550 if(seqWithStops==2 & countStops==0) mutationInfo = NA
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1551 if(seqWithStops==3 & countStops>0) mutationInfo = NA
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1552 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1553
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1554 if(any(!is.na(mutationInfo))){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1555 #Filter mutations based on multipleMutation
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1556 if(multipleMutation==1 & !is.na(mutationInfo)){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1557 mutationCodons = getCodonNumb(as.numeric(names(mutationInfoWhole)))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1558 tableMutationCodons <- table(mutationCodons)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1559 codonsWithMultipleMutations <- as.numeric(names(tableMutationCodons[tableMutationCodons>1]))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1560 if(any(codonsWithMultipleMutations)){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1561 #remove the nucleotide mutations in the codons with multiple mutations
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1562 mutationInfo <- mutationInfo[!(mutationCodons %in% codonsWithMultipleMutations)]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1563 #replace those codons with Ns in the input sequence
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1564 paramSeq[unlist(lapply(codonsWithMultipleMutations, getCodonNucs))] = "N"
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1565 matInput[inputMatrixIndex,1] <<- c2s(paramSeq)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1566 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1567 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1568
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1569 #Filter mutations based on the model
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1570 if(any(mutationInfo)==T | is.na(any(mutationInfo))){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1571
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1572 if(model==1 & !is.na(mutationInfo)){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1573 mutationInfo <- mutationInfo[mutationInfo=="S"]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1574 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1575 if(any(mutationInfo)==T | is.na(any(mutationInfo))) return(mutationInfo)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1576 else return(NA)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1577 }else{
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1578 return(NA)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1579 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1580 }else{
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1581 return(NA)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1582 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1583
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1584
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1585 }else{
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1586 return(NA)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1587 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1588
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1589
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1590 }else{
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1591 return (NA)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1592 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1593 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1594
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1595 analyzeMutationsFixed <- function( inputArray, model = 0 , multipleMutation=0, seqWithStops=0){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1596
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1597 paramGL = s2c(inputArray[2])
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1598 paramSeq = s2c(inputArray[1])
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1599 inputSeq <- inputArray[1]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1600 #if( any(paramSeq=="N") ){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1601 # gapPos_Seq = which(paramSeq=="N")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1602 # gapPos_Seq_ToReplace = gapPos_Seq[paramGL[gapPos_Seq] != "N"]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1603 # paramSeq[gapPos_Seq_ToReplace] = paramGL[gapPos_Seq_ToReplace]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1604 #}
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1605 mutations_val = paramGL != paramSeq
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1606
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1607 if(any(mutations_val)){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1608 mutationPos = which(mutations_val)#{1:length(mutations_val)}[mutations_val]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1609 length_mutations =length(mutationPos)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1610 mutationInfo = rep(NA,length_mutations)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1611
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1612 pos<- mutationPos
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1613 pos_array<-array(sapply(pos,getCodonPos))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1614 codonGL = paramGL[pos_array]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1615 codonSeqWhole = paramSeq[pos_array]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1616 codonSeq = sapply(pos,function(x){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1617 seqP = paramGL[getCodonPos(x)]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1618 muCodonPos = {x-1}%%3+1
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1619 seqP[muCodonPos] = paramSeq[x]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1620 return(seqP)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1621 })
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1622 GLcodons = apply(matrix(codonGL,length_mutations,3,byrow=TRUE),1,c2s)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1623 SeqcodonsWhole = apply(matrix(codonSeqWhole,length_mutations,3,byrow=TRUE),1,c2s)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1624 Seqcodons = apply(codonSeq,2,c2s)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1625
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1626 mutationInfo = apply(rbind(GLcodons , Seqcodons),2,function(x){mutationType(c2s(x[1]),c2s(x[2]))})
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1627 names(mutationInfo) = mutationPos
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1628
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1629 mutationInfoWhole = apply(rbind(GLcodons , SeqcodonsWhole),2,function(x){mutationType(c2s(x[1]),c2s(x[2]))})
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1630 names(mutationInfoWhole) = mutationPos
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1631
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1632 mutationInfo <- mutationInfo[!is.na(mutationInfo)]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1633 mutationInfoWhole <- mutationInfoWhole[!is.na(mutationInfoWhole)]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1634
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1635 if(any(!is.na(mutationInfo))){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1636
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1637 #Filter based on Stop (at the codon level)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1638 if(seqWithStops==1){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1639 nucleotidesAtStopCodons = names(mutationInfoWhole[mutationInfoWhole!="Stop"])
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1640 mutationInfo = mutationInfo[nucleotidesAtStopCodons]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1641 mutationInfoWhole = mutationInfo[nucleotidesAtStopCodons]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1642 }else{
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1643 countStops = sum(mutationInfoWhole=="Stop")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1644 if(seqWithStops==2 & countStops==0) mutationInfo = NA
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1645 if(seqWithStops==3 & countStops>0) mutationInfo = NA
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1646 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1647
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1648 if(any(!is.na(mutationInfo))){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1649 #Filter mutations based on multipleMutation
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1650 if(multipleMutation==1 & !is.na(mutationInfo)){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1651 mutationCodons = getCodonNumb(as.numeric(names(mutationInfoWhole)))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1652 tableMutationCodons <- table(mutationCodons)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1653 codonsWithMultipleMutations <- as.numeric(names(tableMutationCodons[tableMutationCodons>1]))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1654 if(any(codonsWithMultipleMutations)){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1655 #remove the nucleotide mutations in the codons with multiple mutations
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1656 mutationInfo <- mutationInfo[!(mutationCodons %in% codonsWithMultipleMutations)]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1657 #replace those codons with Ns in the input sequence
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1658 paramSeq[unlist(lapply(codonsWithMultipleMutations, getCodonNucs))] = "N"
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1659 #matInput[inputMatrixIndex,1] <<- c2s(paramSeq)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1660 inputSeq <- c2s(paramSeq)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1661 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1662 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1663
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1664 #Filter mutations based on the model
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1665 if(any(mutationInfo)==T | is.na(any(mutationInfo))){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1666
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1667 if(model==1 & !is.na(mutationInfo)){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1668 mutationInfo <- mutationInfo[mutationInfo=="S"]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1669 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1670 if(any(mutationInfo)==T | is.na(any(mutationInfo))) return(list(mutationInfo,inputSeq))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1671 else return(list(NA,inputSeq))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1672 }else{
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1673 return(list(NA,inputSeq))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1674 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1675 }else{
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1676 return(list(NA,inputSeq))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1677 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1678
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1679
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1680 }else{
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1681 return(list(NA,inputSeq))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1682 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1683
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1684
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1685 }else{
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1686 return (list(NA,inputSeq))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1687 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1688 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1689
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1690 # triMutability Background Count
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1691 buildMutabilityModel <- function( inputMatrixIndex, model=0 , multipleMutation=0, seqWithStops=0, stopMutations=0){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1692
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1693 #rowOrigMatInput = matInput[inputMatrixIndex,]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1694 seqGL = gsub("-", "", matInput[inputMatrixIndex,2])
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1695 seqInput = gsub("-", "", matInput[inputMatrixIndex,1])
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1696 #matInput[inputMatrixIndex,] <<- cbind(seqInput,seqGL)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1697 tempInput <- cbind(seqInput,seqGL)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1698 seqLength = nchar(seqGL)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1699 list_analyzeMutationsFixed<- analyzeMutationsFixed(tempInput, model, multipleMutation, seqWithStops)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1700 mutationCount <- list_analyzeMutationsFixed[[1]]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1701 seqInput <- list_analyzeMutationsFixed[[2]]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1702 BackgroundMatrix = mutabilityMatrix
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1703 MutationMatrix = mutabilityMatrix
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1704 MutationCountMatrix = mutabilityMatrix
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1705 if(!is.na(mutationCount)){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1706 if((stopMutations==0 & model==0) | (stopMutations==1 & (sum(mutationCount=="Stop")<length(mutationCount))) | (model==1 & (sum(mutationCount=="S")>0)) ){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1707
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1708 fivermerStartPos = 1:(seqLength-4)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1709 fivemerLength <- length(fivermerStartPos)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1710 fivemerGL <- substr(rep(seqGL,length(fivermerStartPos)),(fivermerStartPos),(fivermerStartPos+4))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1711 fivemerSeq <- substr(rep(seqInput,length(fivermerStartPos)),(fivermerStartPos),(fivermerStartPos+4))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1712
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1713 #Background
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1714 for(fivemerIndex in 1:fivemerLength){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1715 fivemer = fivemerGL[fivemerIndex]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1716 if(!any(grep("N",fivemer))){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1717 fivemerCodonPos = fivemerCodon(fivemerIndex)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1718 fivemerReadingFrameCodon = substr(fivemer,fivemerCodonPos[1],fivemerCodonPos[3])
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1719 fivemerReadingFrameCodonInputSeq = substr(fivemerSeq[fivemerIndex],fivemerCodonPos[1],fivemerCodonPos[3])
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1720
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1721 # All mutations model
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1722 #if(!any(grep("N",fivemerReadingFrameCodon))){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1723 if(model==0){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1724 if(stopMutations==0){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1725 if(!any(grep("N",fivemerReadingFrameCodonInputSeq)))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1726 BackgroundMatrix[fivemer] <- (BackgroundMatrix[fivemer] + 1)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1727 }else{
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1728 if( !any(grep("N",fivemerReadingFrameCodonInputSeq)) & translateCodonToAminoAcid(fivemerReadingFrameCodon)!="*" ){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1729 positionWithinCodon = which(fivemerCodonPos==3)#positionsWithinCodon[(fivemerCodonPos[1]%%3)+1]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1730 BackgroundMatrix[fivemer] <- (BackgroundMatrix[fivemer] + probNonStopMutations[fivemerReadingFrameCodon,positionWithinCodon])
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1731 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1732 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1733 }else{ # Only silent mutations
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1734 if( !any(grep("N",fivemerReadingFrameCodonInputSeq)) & translateCodonToAminoAcid(fivemerReadingFrameCodon)!="*" & translateCodonToAminoAcid(fivemerReadingFrameCodonInputSeq)==translateCodonToAminoAcid(fivemerReadingFrameCodon)){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1735 positionWithinCodon = which(fivemerCodonPos==3)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1736 BackgroundMatrix[fivemer] <- (BackgroundMatrix[fivemer] + probSMutations[fivemerReadingFrameCodon,positionWithinCodon])
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1737 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1738 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1739 #}
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1740 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1741 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1742
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1743 #Mutations
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1744 if(stopMutations==1) mutationCount = mutationCount[mutationCount!="Stop"]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1745 if(model==1) mutationCount = mutationCount[mutationCount=="S"]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1746 mutationPositions = as.numeric(names(mutationCount))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1747 mutationCount = mutationCount[mutationPositions>2 & mutationPositions<(seqLength-1)]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1748 mutationPositions = mutationPositions[mutationPositions>2 & mutationPositions<(seqLength-1)]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1749 countMutations = 0
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1750 for(mutationPosition in mutationPositions){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1751 fivemerIndex = mutationPosition-2
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1752 fivemer = fivemerSeq[fivemerIndex]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1753 GLfivemer = fivemerGL[fivemerIndex]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1754 fivemerCodonPos = fivemerCodon(fivemerIndex)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1755 fivemerReadingFrameCodon = substr(fivemer,fivemerCodonPos[1],fivemerCodonPos[3])
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1756 fivemerReadingFrameCodonGL = substr(GLfivemer,fivemerCodonPos[1],fivemerCodonPos[3])
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1757 if(!any(grep("N",fivemer)) & !any(grep("N",GLfivemer))){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1758 if(model==0){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1759 countMutations = countMutations + 1
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1760 MutationMatrix[GLfivemer] <- (MutationMatrix[GLfivemer] + 1)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1761 MutationCountMatrix[GLfivemer] <- (MutationCountMatrix[GLfivemer] + 1)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1762 }else{
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1763 if( translateCodonToAminoAcid(fivemerReadingFrameCodonGL)!="*" ){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1764 countMutations = countMutations + 1
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1765 positionWithinCodon = which(fivemerCodonPos==3)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1766 glNuc = substr(fivemerReadingFrameCodonGL,positionWithinCodon,positionWithinCodon)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1767 inputNuc = substr(fivemerReadingFrameCodon,positionWithinCodon,positionWithinCodon)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1768 MutationMatrix[GLfivemer] <- (MutationMatrix[GLfivemer] + substitution[glNuc,inputNuc])
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1769 MutationCountMatrix[GLfivemer] <- (MutationCountMatrix[GLfivemer] + 1)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1770 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1771 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1772 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1773 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1774
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1775 seqMutability = MutationMatrix/BackgroundMatrix
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1776 seqMutability = seqMutability/sum(seqMutability,na.rm=TRUE)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1777 #cat(inputMatrixIndex,"\t",countMutations,"\n")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1778 return(list("seqMutability" = seqMutability,"numbMutations" = countMutations,"seqMutabilityCount" = MutationCountMatrix, "BackgroundMatrix"=BackgroundMatrix))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1779
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1780 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1781 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1782
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1783 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1784
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1785 #Returns the codon position containing the middle nucleotide
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1786 fivemerCodon <- function(fivemerIndex){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1787 codonPos = list(2:4,1:3,3:5)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1788 fivemerType = fivemerIndex%%3
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1789 return(codonPos[[fivemerType+1]])
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1790 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1791
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1792 #returns probability values for one mutation in codons resulting in R, S or Stop
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1793 probMutations <- function(typeOfMutation){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1794 matMutationProb <- matrix(0,ncol=3,nrow=125,dimnames=list(words(alphabet = c(NUCLEOTIDES,"N"), length=3),c(1:3)))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1795 for(codon in rownames(matMutationProb)){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1796 if( !any(grep("N",codon)) ){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1797 for(muPos in 1:3){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1798 matCodon = matrix(rep(s2c(codon),3),nrow=3,ncol=3,byrow=T)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1799 glNuc = matCodon[1,muPos]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1800 matCodon[,muPos] = canMutateTo(glNuc)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1801 substitutionRate = substitution[glNuc,matCodon[,muPos]]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1802 typeOfMutations = apply(rbind(rep(codon,3),apply(matCodon,1,c2s)),2,function(x){mutationType(c2s(x[1]),c2s(x[2]))})
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1803 matMutationProb[codon,muPos] <- sum(substitutionRate[typeOfMutations==typeOfMutation])
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1804 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1805 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1806 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1807
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1808 return(matMutationProb)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1809 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1810
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1811
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1812
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1813
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1814 #Mapping Trinucleotides to fivemers
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1815 mapTriToFivemer <- function(triMutability=triMutability_Literature_Human){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1816 rownames(triMutability) <- triMutability_Names
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1817 Fivemer<-rep(NA,1024)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1818 names(Fivemer)<-words(alphabet=NUCLEOTIDES,length=5)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1819 Fivemer<-sapply(names(Fivemer),function(Word)return(sum( c(triMutability[substring(Word,3,5),1],triMutability[substring(Word,2,4),2],triMutability[substring(Word,1,3),3]),na.rm=TRUE)))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1820 Fivemer<-Fivemer/sum(Fivemer)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1821 return(Fivemer)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1822 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1823
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1824 collapseFivemerToTri<-function(Fivemer,Weights=MutabilityWeights,position=1,NUC="A"){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1825 Indices<-substring(names(Fivemer),3,3)==NUC
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1826 Factors<-substring(names(Fivemer[Indices]),(4-position),(6-position))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1827 tapply(which(Indices),Factors,function(i)weighted.mean(Fivemer[i],Weights[i],na.rm=TRUE))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1828 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1829
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1830
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1831
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1832 CountFivemerToTri<-function(Fivemer,Weights=MutabilityWeights,position=1,NUC="A"){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1833 Indices<-substring(names(Fivemer),3,3)==NUC
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1834 Factors<-substring(names(Fivemer[Indices]),(4-position),(6-position))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1835 tapply(which(Indices),Factors,function(i)sum(Weights[i],na.rm=TRUE))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1836 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1837
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1838 #Uses the real counts of the mutated fivemers
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1839 CountFivemerToTri2<-function(Fivemer,Counts=MutabilityCounts,position=1,NUC="A"){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1840 Indices<-substring(names(Fivemer),3,3)==NUC
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1841 Factors<-substring(names(Fivemer[Indices]),(4-position),(6-position))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1842 tapply(which(Indices),Factors,function(i)sum(Counts[i],na.rm=TRUE))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1843 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1844
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1845 bootstrap<-function(x=c(33,12,21),M=10000,alpha=0.05){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1846 N<-sum(x)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1847 if(N){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1848 p<-x/N
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1849 k<-length(x)-1
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1850 tmp<-rmultinom(M, size = N, prob=p)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1851 tmp_p<-apply(tmp,2,function(y)y/N)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1852 (apply(tmp_p,1,function(y)quantile(y,c(alpha/2/k,1-alpha/2/k))))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1853 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1854 else return(matrix(0,2,length(x)))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1855 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1856
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1857
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1858
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1859
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1860 bootstrap2<-function(x=c(33,12,21),n=10,M=10000,alpha=0.05){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1861
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1862 N<-sum(x)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1863 k<-length(x)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1864 y<-rep(1:k,x)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1865 tmp<-sapply(1:M,function(i)sample(y,n))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1866 if(n>1)tmp_p<-sapply(1:M,function(j)sapply(1:k,function(i)sum(tmp[,j]==i)))/n
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1867 if(n==1)tmp_p<-sapply(1:M,function(j)sapply(1:k,function(i)sum(tmp[j]==i)))/n
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1868 (apply(tmp_p,1,function(z)quantile(z,c(alpha/2/(k-1),1-alpha/2/(k-1)))))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1869 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1870
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1871
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1872
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1873 p_value<-function(x=c(33,12,21),M=100000,x_obs=c(2,5,3)){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1874 n=sum(x_obs)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1875 N<-sum(x)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1876 k<-length(x)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1877 y<-rep(1:k,x)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1878 tmp<-sapply(1:M,function(i)sample(y,n))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1879 if(n>1)tmp_p<-sapply(1:M,function(j)sapply(1:k,function(i)sum(tmp[,j]==i)))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1880 if(n==1)tmp_p<-sapply(1:M,function(j)sapply(1:k,function(i)sum(tmp[j]==i)))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1881 tmp<-rbind(sapply(1:3,function(i)sum(tmp_p[i,]>=x_obs[i])/M),
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1882 sapply(1:3,function(i)sum(tmp_p[i,]<=x_obs[i])/M))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1883 sapply(1:3,function(i){if(tmp[1,i]>=tmp[2,i])return(-tmp[2,i])else return(tmp[1,i])})
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1884 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1885
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1886 #"D:\\Sequences\\IMGT Germlines\\Human_SNPless_IGHJ.FASTA"
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1887 # Remove SNPs from IMGT germline segment alleles
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1888 generateUnambiguousRepertoire <- function(repertoireInFile,repertoireOutFile){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1889 repertoireIn <- read.fasta(repertoireInFile, seqtype="DNA",as.string=T,set.attributes=F,forceDNAtolower=F)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1890 alleleNames <- sapply(names(repertoireIn),function(x)strsplit(x,"|",fixed=TRUE)[[1]][2])
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1891 SNPs <- tapply(repertoireIn,sapply(alleleNames,function(x)strsplit(x,"*",fixed=TRUE)[[1]][1]),function(x){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1892 Indices<-NULL
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1893 for(i in 1:length(x)){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1894 firstSeq = s2c(x[[1]])
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1895 iSeq = s2c(x[[i]])
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1896 Indices<-c(Indices,which(firstSeq[1:320]!=iSeq[1:320] & firstSeq[1:320]!="." & iSeq[1:320]!="." ))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1897 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1898 return(sort(unique(Indices)))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1899 })
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1900 repertoireOut <- repertoireIn
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1901 repertoireOut <- lapply(names(repertoireOut), function(repertoireName){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1902 alleleName <- strsplit(repertoireName,"|",fixed=TRUE)[[1]][2]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1903 geneSegmentName <- strsplit(alleleName,"*",fixed=TRUE)[[1]][1]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1904 alleleSeq <- s2c(repertoireOut[[repertoireName]])
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1905 alleleSeq[as.numeric(unlist(SNPs[geneSegmentName]))] <- "N"
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1906 alleleSeq <- c2s(alleleSeq)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1907 repertoireOut[[repertoireName]] <- alleleSeq
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1908 })
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1909 names(repertoireOut) <- names(repertoireIn)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1910 write.fasta(repertoireOut,names(repertoireOut),file.out=repertoireOutFile)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1911
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1912 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1913
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1914
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1915
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1916
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1917
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1918
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1919 ############
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1920 groupBayes2 = function(indexes, param_resultMat){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1921
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1922 BayesGDist_Focused_CDR = calculate_bayesG( x=param_resultMat[indexes,1], N=apply(param_resultMat[indexes,c(1,2,4)],1,sum,na.rm=T), p=apply(param_resultMat[indexes,5:8],1,function(x){x[1]/(x[1]+x[2]+x[4])}))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1923 BayesGDist_Focused_FWR = calculate_bayesG( x=param_resultMat[indexes,3], N=apply(param_resultMat[indexes,c(3,2,4)],1,sum,na.rm=T), p=apply(param_resultMat[indexes,5:8],1,function(x){x[3]/(x[3]+x[2]+x[4])}))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1924 #BayesGDist_Local_CDR = calculate_bayesG( x=param_resultMat[indexes,1], N=apply(param_resultMat[indexes,c(1,2)],1,sum,na.rm=T), p=apply(param_resultMat[indexes,5:8],1,function(x){x[1]/(x[1]+x[2])}))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1925 #BayesGDist_Local_FWR = calculate_bayesG( x=param_resultMat[indexes,3], N=apply(param_resultMat[indexes,c(3,4)],1,sum,na.rm=T), p=apply(param_resultMat[indexes,5:8],1,function(x){x[3]/(x[3]+x[4])}))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1926 #BayesGDist_Global_CDR = calculate_bayesG( x=param_resultMat[indexes,1], N=apply(param_resultMat[indexes,c(1,2,3,4)],1,sum,na.rm=T), p=apply(param_resultMat[indexes,5:8],1,function(x){x[1]/(x[1]+x[2]+x[3]+x[4])}))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1927 #BayesGDist_Global_FWR = calculate_bayesG( x=param_resultMat[indexes,3], N=apply(param_resultMat[indexes,c(1,2,3,4)],1,sum,na.rm=T), p=apply(param_resultMat[indexes,5:8],1,function(x){x[3]/(x[1]+x[2]+x[3]+x[4])}))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1928 return ( list("BayesGDist_Focused_CDR"=BayesGDist_Focused_CDR,
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1929 "BayesGDist_Focused_FWR"=BayesGDist_Focused_FWR) )
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1930 #"BayesGDist_Local_CDR"=BayesGDist_Local_CDR,
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1931 #"BayesGDist_Local_FWR" = BayesGDist_Local_FWR))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1932 # "BayesGDist_Global_CDR" = BayesGDist_Global_CDR,
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1933 # "BayesGDist_Global_FWR" = BayesGDist_Global_FWR) )
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1934
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1935
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1936 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1937
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1938
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1939 calculate_bayesG <- function( x=array(), N=array(), p=array(), max_sigma=20, length_sigma=4001){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1940 G <- max(length(x),length(N),length(p))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1941 x=array(x,dim=G)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1942 N=array(N,dim=G)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1943 p=array(p,dim=G)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1944
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1945 indexOfZero = N>0 & p>0
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1946 N = N[indexOfZero]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1947 x = x[indexOfZero]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1948 p = p[indexOfZero]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1949 G <- length(x)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1950
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1951 if(G){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1952
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1953 cons<-array( dim=c(length_sigma,G) )
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1954 if(G==1) {
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1955 return(calculate_bayes(x=x[G],N=N[G],p=p[G],max_sigma=max_sigma,length_sigma=length_sigma))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1956 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1957 else {
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1958 for(g in 1:G) cons[,g] <- calculate_bayes(x=x[g],N=N[g],p=p[g],max_sigma=max_sigma,length_sigma=length_sigma)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1959 listMatG <- convolutionPowersOfTwoByTwos(cons,length_sigma=length_sigma)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1960 y<-calculate_bayesGHelper(listMatG,length_sigma=length_sigma)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1961 return( y/sum(y)/(2*max_sigma/(length_sigma-1)) )
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1962 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1963 }else{
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1964 return(NA)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1965 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1966 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1967
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1968
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1969 calculate_bayesGHelper <- function( listMatG,length_sigma=4001 ){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1970 matG <- listMatG[[1]]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1971 groups <- listMatG[[2]]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1972 i = 1
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1973 resConv <- matG[,i]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1974 denom <- 2^groups[i]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1975 if(length(groups)>1){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1976 while( i<length(groups) ){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1977 i = i + 1
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1978 resConv <- weighted_conv(resConv, matG[,i], w= {{2^groups[i]}/denom} ,length_sigma=length_sigma)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1979 #cat({{2^groups[i]}/denom},"\n")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1980 denom <- denom + 2^groups[i]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1981 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1982 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1983 return(resConv)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1984 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1985
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1986 weighted_conv<-function(x,y,w=1,m=100,length_sigma=4001){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1987 lx<-length(x)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1988 ly<-length(y)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1989 if({lx<m}| {{lx*w}<m}| {{ly}<m}| {{ly*w}<m}){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1990 if(w<1){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1991 y1<-approx(1:ly,y,seq(1,ly,length.out=m))$y
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1992 x1<-approx(1:lx,x,seq(1,lx,length.out=m/w))$y
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1993 lx<-length(x1)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1994 ly<-length(y1)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1995 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1996 else {
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1997 y1<-approx(1:ly,y,seq(1,ly,length.out=m*w))$y
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1998 x1<-approx(1:lx,x,seq(1,lx,length.out=m))$y
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1999 lx<-length(x1)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2000 ly<-length(y1)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2001 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2002 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2003 else{
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2004 x1<-x
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2005 y1<-approx(1:ly,y,seq(1,ly,length.out=floor(lx*w)))$y
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2006 ly<-length(y1)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2007 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2008 tmp<-approx(x=1:(lx+ly-1),y=convolve(x1,rev(y1),type="open"),xout=seq(1,lx+ly-1,length.out=length_sigma))$y
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2009 tmp[tmp<=0] = 0
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2010 return(tmp/sum(tmp))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2011 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2012
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2013 ########################
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2014
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2015
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2016
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2017
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2018 mutabilityMatrixONE<-rep(0,4)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2019 names(mutabilityMatrixONE)<-NUCLEOTIDES
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2020
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2021 # triMutability Background Count
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2022 buildMutabilityModelONE <- function( inputMatrixIndex, model=0 , multipleMutation=0, seqWithStops=0, stopMutations=0){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2023
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2024 #rowOrigMatInput = matInput[inputMatrixIndex,]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2025 seqGL = gsub("-", "", matInput[inputMatrixIndex,2])
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2026 seqInput = gsub("-", "", matInput[inputMatrixIndex,1])
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2027 matInput[inputMatrixIndex,] <<- c(seqInput,seqGL)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2028 seqLength = nchar(seqGL)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2029 mutationCount <- analyzeMutations(inputMatrixIndex, model, multipleMutation, seqWithStops)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2030 BackgroundMatrix = mutabilityMatrixONE
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2031 MutationMatrix = mutabilityMatrixONE
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2032 MutationCountMatrix = mutabilityMatrixONE
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2033 if(!is.na(mutationCount)){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2034 if((stopMutations==0 & model==0) | (stopMutations==1 & (sum(mutationCount=="Stop")<length(mutationCount))) | (model==1 & (sum(mutationCount=="S")>0)) ){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2035
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2036 # ONEmerStartPos = 1:(seqLength)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2037 # ONEmerLength <- length(ONEmerStartPos)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2038 ONEmerGL <- s2c(seqGL)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2039 ONEmerSeq <- s2c(seqInput)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2040
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2041 #Background
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2042 for(ONEmerIndex in 1:seqLength){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2043 ONEmer = ONEmerGL[ONEmerIndex]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2044 if(ONEmer!="N"){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2045 ONEmerCodonPos = getCodonPos(ONEmerIndex)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2046 ONEmerReadingFrameCodon = c2s(ONEmerGL[ONEmerCodonPos])
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2047 ONEmerReadingFrameCodonInputSeq = c2s(ONEmerSeq[ONEmerCodonPos] )
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2048
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2049 # All mutations model
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2050 #if(!any(grep("N",ONEmerReadingFrameCodon))){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2051 if(model==0){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2052 if(stopMutations==0){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2053 if(!any(grep("N",ONEmerReadingFrameCodonInputSeq)))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2054 BackgroundMatrix[ONEmer] <- (BackgroundMatrix[ONEmer] + 1)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2055 }else{
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2056 if( !any(grep("N",ONEmerReadingFrameCodonInputSeq)) & translateCodonToAminoAcid(ONEmerReadingFrameCodonInputSeq)!="*"){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2057 positionWithinCodon = which(ONEmerCodonPos==ONEmerIndex)#positionsWithinCodon[(ONEmerCodonPos[1]%%3)+1]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2058 BackgroundMatrix[ONEmer] <- (BackgroundMatrix[ONEmer] + probNonStopMutations[ONEmerReadingFrameCodon,positionWithinCodon])
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2059 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2060 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2061 }else{ # Only silent mutations
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2062 if( !any(grep("N",ONEmerReadingFrameCodonInputSeq)) & translateCodonToAminoAcid(ONEmerReadingFrameCodonInputSeq)!="*" & translateCodonToAminoAcid(ONEmerReadingFrameCodonInputSeq)==translateCodonToAminoAcid(ONEmerReadingFrameCodon) ){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2063 positionWithinCodon = which(ONEmerCodonPos==ONEmerIndex)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2064 BackgroundMatrix[ONEmer] <- (BackgroundMatrix[ONEmer] + probSMutations[ONEmerReadingFrameCodon,positionWithinCodon])
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2065 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2066 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2067 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2068 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2069 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2070
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2071 #Mutations
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2072 if(stopMutations==1) mutationCount = mutationCount[mutationCount!="Stop"]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2073 if(model==1) mutationCount = mutationCount[mutationCount=="S"]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2074 mutationPositions = as.numeric(names(mutationCount))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2075 mutationCount = mutationCount[mutationPositions>2 & mutationPositions<(seqLength-1)]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2076 mutationPositions = mutationPositions[mutationPositions>2 & mutationPositions<(seqLength-1)]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2077 countMutations = 0
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2078 for(mutationPosition in mutationPositions){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2079 ONEmerIndex = mutationPosition
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2080 ONEmer = ONEmerSeq[ONEmerIndex]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2081 GLONEmer = ONEmerGL[ONEmerIndex]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2082 ONEmerCodonPos = getCodonPos(ONEmerIndex)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2083 ONEmerReadingFrameCodon = c2s(ONEmerSeq[ONEmerCodonPos])
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2084 ONEmerReadingFrameCodonGL =c2s(ONEmerGL[ONEmerCodonPos])
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2085 if(!any(grep("N",ONEmer)) & !any(grep("N",GLONEmer))){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2086 if(model==0){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2087 countMutations = countMutations + 1
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2088 MutationMatrix[GLONEmer] <- (MutationMatrix[GLONEmer] + 1)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2089 MutationCountMatrix[GLONEmer] <- (MutationCountMatrix[GLONEmer] + 1)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2090 }else{
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2091 if( translateCodonToAminoAcid(ONEmerReadingFrameCodonGL)!="*" ){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2092 countMutations = countMutations + 1
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2093 positionWithinCodon = which(ONEmerCodonPos==ONEmerIndex)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2094 glNuc = substr(ONEmerReadingFrameCodonGL,positionWithinCodon,positionWithinCodon)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2095 inputNuc = substr(ONEmerReadingFrameCodon,positionWithinCodon,positionWithinCodon)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2096 MutationMatrix[GLONEmer] <- (MutationMatrix[GLONEmer] + substitution[glNuc,inputNuc])
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2097 MutationCountMatrix[GLONEmer] <- (MutationCountMatrix[GLONEmer] + 1)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2098 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2099 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2100 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2101 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2102
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2103 seqMutability = MutationMatrix/BackgroundMatrix
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2104 seqMutability = seqMutability/sum(seqMutability,na.rm=TRUE)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2105 #cat(inputMatrixIndex,"\t",countMutations,"\n")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2106 return(list("seqMutability" = seqMutability,"numbMutations" = countMutations,"seqMutabilityCount" = MutationCountMatrix, "BackgroundMatrix"=BackgroundMatrix))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2107 # tmp<-list("seqMutability" = seqMutability,"numbMutations" = countMutations,"seqMutabilityCount" = MutationCountMatrix)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2108 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2109 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2110
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2111 ################
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2112 # $Id: trim.R 989 2006-10-29 15:28:26Z ggorjan $
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2113
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2114 trim <- function(s, recode.factor=TRUE, ...)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2115 UseMethod("trim", s)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2116
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2117 trim.default <- function(s, recode.factor=TRUE, ...)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2118 s
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2119
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2120 trim.character <- function(s, recode.factor=TRUE, ...)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2121 {
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2122 s <- sub(pattern="^ +", replacement="", x=s)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2123 s <- sub(pattern=" +$", replacement="", x=s)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2124 s
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2125 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2126
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2127 trim.factor <- function(s, recode.factor=TRUE, ...)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2128 {
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2129 levels(s) <- trim(levels(s))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2130 if(recode.factor) {
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2131 dots <- list(x=s, ...)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2132 if(is.null(dots$sort)) dots$sort <- sort
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2133 s <- do.call(what=reorder.factor, args=dots)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2134 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2135 s
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2136 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2137
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2138 trim.list <- function(s, recode.factor=TRUE, ...)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2139 lapply(s, trim, recode.factor=recode.factor, ...)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2140
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2141 trim.data.frame <- function(s, recode.factor=TRUE, ...)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2142 {
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2143 s[] <- trim.list(s, recode.factor=recode.factor, ...)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2144 s
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2145 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2146 #######################################
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2147 # Compute the expected for each sequence-germline pair by codon
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2148 getExpectedIndividualByCodon <- function(matInput){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2149 if( any(grep("multicore",search())) ){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2150 facGL <- factor(matInput[,2])
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2151 facLevels = levels(facGL)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2152 LisGLs_MutabilityU = mclapply(1:length(facLevels), function(x){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2153 computeMutabilities(facLevels[x])
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2154 })
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2155 facIndex = match(facGL,facLevels)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2156
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2157 LisGLs_Mutability = mclapply(1:nrow(matInput), function(x){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2158 cInput = rep(NA,nchar(matInput[x,1]))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2159 cInput[s2c(matInput[x,1])!="N"] = 1
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2160 LisGLs_MutabilityU[[facIndex[x]]] * cInput
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2161 })
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2162
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2163 LisGLs_Targeting = mclapply(1:dim(matInput)[1], function(x){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2164 computeTargeting(matInput[x,2],LisGLs_Mutability[[x]])
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2165 })
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2166
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2167 LisGLs_MutationTypes = mclapply(1:length(matInput[,2]),function(x){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2168 #print(x)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2169 computeMutationTypes(matInput[x,2])
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2170 })
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2171
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2172 LisGLs_R_Exp = mclapply(1:nrow(matInput), function(x){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2173 Exp_R <- rollapply(as.zoo(1:readEnd),width=3,by=3,
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2174 function(codonNucs){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2175 RPos = which(LisGLs_MutationTypes[[x]][,codonNucs]=="R")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2176 sum( LisGLs_Targeting[[x]][,codonNucs][RPos], na.rm=T )
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2177 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2178 )
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2179 })
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2180
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2181 LisGLs_S_Exp = mclapply(1:nrow(matInput), function(x){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2182 Exp_S <- rollapply(as.zoo(1:readEnd),width=3,by=3,
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2183 function(codonNucs){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2184 SPos = which(LisGLs_MutationTypes[[x]][,codonNucs]=="S")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2185 sum( LisGLs_Targeting[[x]][,codonNucs][SPos], na.rm=T )
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2186 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2187 )
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2188 })
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2189
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2190 Exp_R = matrix(unlist(LisGLs_R_Exp),nrow=nrow(matInput),ncol=readEnd/3,T)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2191 Exp_S = matrix(unlist(LisGLs_S_Exp),nrow=nrow(matInput),ncol=readEnd/3,T)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2192 return( list( "Expected_R"=Exp_R, "Expected_S"=Exp_S) )
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2193 }else{
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2194 facGL <- factor(matInput[,2])
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2195 facLevels = levels(facGL)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2196 LisGLs_MutabilityU = lapply(1:length(facLevels), function(x){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2197 computeMutabilities(facLevels[x])
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2198 })
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2199 facIndex = match(facGL,facLevels)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2200
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2201 LisGLs_Mutability = lapply(1:nrow(matInput), function(x){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2202 cInput = rep(NA,nchar(matInput[x,1]))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2203 cInput[s2c(matInput[x,1])!="N"] = 1
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2204 LisGLs_MutabilityU[[facIndex[x]]] * cInput
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2205 })
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2206
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2207 LisGLs_Targeting = lapply(1:dim(matInput)[1], function(x){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2208 computeTargeting(matInput[x,2],LisGLs_Mutability[[x]])
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2209 })
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2210
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2211 LisGLs_MutationTypes = lapply(1:length(matInput[,2]),function(x){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2212 #print(x)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2213 computeMutationTypes(matInput[x,2])
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2214 })
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2215
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2216 LisGLs_R_Exp = lapply(1:nrow(matInput), function(x){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2217 Exp_R <- rollapply(as.zoo(1:readEnd),width=3,by=3,
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2218 function(codonNucs){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2219 RPos = which(LisGLs_MutationTypes[[x]][,codonNucs]=="R")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2220 sum( LisGLs_Targeting[[x]][,codonNucs][RPos], na.rm=T )
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2221 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2222 )
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2223 })
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2224
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2225 LisGLs_S_Exp = lapply(1:nrow(matInput), function(x){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2226 Exp_S <- rollapply(as.zoo(1:readEnd),width=3,by=3,
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2227 function(codonNucs){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2228 SPos = which(LisGLs_MutationTypes[[x]][,codonNucs]=="S")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2229 sum( LisGLs_Targeting[[x]][,codonNucs][SPos], na.rm=T )
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2230 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2231 )
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2232 })
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2233
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2234 Exp_R = matrix(unlist(LisGLs_R_Exp),nrow=nrow(matInput),ncol=readEnd/3,T)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2235 Exp_S = matrix(unlist(LisGLs_S_Exp),nrow=nrow(matInput),ncol=readEnd/3,T)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2236 return( list( "Expected_R"=Exp_R, "Expected_S"=Exp_S) )
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2237 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2238 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2239
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2240 # getObservedMutationsByCodon <- function(listMutations){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2241 # numbSeqs <- length(listMutations)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2242 # obsMu_R <- matrix(0,nrow=numbSeqs,ncol=readEnd/3,dimnames=list(c(1:numbSeqs),c(1:(readEnd/3))))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2243 # obsMu_S <- obsMu_R
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2244 # temp <- mclapply(1:length(listMutations), function(i){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2245 # arrMutations = listMutations[[i]]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2246 # RPos = as.numeric(names(arrMutations)[arrMutations=="R"])
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2247 # RPos <- sapply(RPos,getCodonNumb)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2248 # if(any(RPos)){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2249 # tabR <- table(RPos)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2250 # obsMu_R[i,as.numeric(names(tabR))] <<- tabR
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2251 # }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2252 #
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2253 # SPos = as.numeric(names(arrMutations)[arrMutations=="S"])
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2254 # SPos <- sapply(SPos,getCodonNumb)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2255 # if(any(SPos)){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2256 # tabS <- table(SPos)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2257 # obsMu_S[i,names(tabS)] <<- tabS
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2258 # }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2259 # }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2260 # )
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2261 # return( list( "Observed_R"=obsMu_R, "Observed_S"=obsMu_S) )
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2262 # }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2263
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2264 getObservedMutationsByCodon <- function(listMutations){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2265 numbSeqs <- length(listMutations)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2266 obsMu_R <- matrix(0,nrow=numbSeqs,ncol=readEnd/3,dimnames=list(c(1:numbSeqs),c(1:(readEnd/3))))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2267 obsMu_S <- obsMu_R
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2268 temp <- lapply(1:length(listMutations), function(i){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2269 arrMutations = listMutations[[i]]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2270 RPos = as.numeric(names(arrMutations)[arrMutations=="R"])
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2271 RPos <- sapply(RPos,getCodonNumb)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2272 if(any(RPos)){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2273 tabR <- table(RPos)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2274 obsMu_R[i,as.numeric(names(tabR))] <<- tabR
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2275 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2276
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2277 SPos = as.numeric(names(arrMutations)[arrMutations=="S"])
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2278 SPos <- sapply(SPos,getCodonNumb)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2279 if(any(SPos)){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2280 tabS <- table(SPos)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2281 obsMu_S[i,names(tabS)] <<- tabS
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2282 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2283 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2284 )
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2285 return( list( "Observed_R"=obsMu_R, "Observed_S"=obsMu_S) )
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2286 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2287