library(bio3d)
load(file="xrayPCA_EQ.RData")

pdb.closed <- read.pdb("1XCK_chainA_noWAT_noH.pdb")
##inds <- atom.select(pdb, "///500:515///CA/")
ca.inds <- atom.select(pdb.closed, "calpha")

pdb.e434k <- read.pdb("E434K_apo_chainA_noWAT_noH.pdb")
pdb.e461k <- read.pdb("E461K_apo_chainA_noWAT_noH.pdb")
ca.inds.e434k <- atom.select(pdb.e434k, "calpha")
ca.inds.e461k <- atom.select(pdb.e461k, "calpha")


e1 <- gaps.pos$f.inds[atom2xyz(1:134)] ## from Ala3
e2 <- gaps.pos$f.inds[atom2xyz(408:522)] ## to Leu524
eq <- c(e1,e2)

e1 <- atom2xyz(2:135)
e2 <- atom2xyz(409:523)
eq.md <- c(e1,e2)




trj.inds <- seq(1, 1500, by=1)

sim161 <- NULL
prefix161 <- "/net/lutefisk/slars/groel_md/mutants/161_E434K_apo/results/traj_monomer/"
prefix <- "20-50ns_noWAT_1500frames_noH_chain_"



for ( i in 1:7 ) {
  tmptrj <- read.ncdf(paste(prefix161, prefix, i , ".nc", sep=""))
  sim161$xyzfit[[i]] <- fit.xyz(fixed = pdbs$xyz[1, ],
                                mobile = tmptrj[trj.inds, ca.inds.e434k$xyz],
                                fixed.inds = core$c1A.xyz,
                                mobile.inds = core$c1A.xyz,
                                full.pdbs = FALSE)
}


for ( i in 8:14 ) {
  tmptrj <- read.ncdf(paste(prefix161, prefix, i , ".nc", sep=""))
  sim161$xyzfit[[i]] <- fit.xyz(fixed = pdbs$xyz[1,gaps.pos$f.inds],
                                mobile = tmptrj[trj.inds, ca.inds.e434k$xyz],
                                fixed.inds = core$c1A.xyz,
                                mobile.inds = core$c1A.xyz,
                                full.pdbs = FALSE)
}


sim162 <- NULL
prefix162 <- "/net/lutefisk/slars/groel_md/mutants/162_E434K_MGATP/results/traj_monomer/"
prefix <- "20-50ns_noWAT_1500frames_noH_chain_"
trj.inds <- seq(1, 1500, by=1)

for ( i in 1:7 ) {
  tmptrj <- read.ncdf(paste(prefix162, prefix, i , ".nc", sep=""))
  sim162$xyzfit[[i]] <- fit.xyz(fixed = pdbs$xyz[1,gaps.pos$f.inds],
                                mobile = tmptrj[trj.inds, ca.inds.e434k$xyz],
                                fixed.inds = core$c1A.xyz,
                                mobile.inds = core$c1A.xyz,
                                full.pdbs = FALSE)
}

for ( i in 8:14 ) {
  tmptrj <- read.ncdf(paste(prefix162, prefix, i , ".nc", sep=""))
  sim162$xyzfit[[i]] <- fit.xyz(fixed = pdbs$xyz[1,gaps.pos$f.inds],
                                mobile = tmptrj[trj.inds, ca.inds.e434k$xyz],
                                fixed.inds = core$c1A.xyz,
                                mobile.inds = core$c1A.xyz,
                                full.pdbs = FALSE)
}





tmptrj <- NULL
for ( i in 1:7 ) {
  tmptrj=rbind(tmptrj, sim162$xyzfit[[i]])
}
sim162$cis$proj <- pca.project(tmptrj[,eq.md], pc.xray)


tmptrj <- NULL
for ( i in 8:14 ) {
  tmptrj=rbind(tmptrj, sim162$xyzfit[[i]])
}
sim162$trans$proj <- pca.project(tmptrj[,eq.md], pc.xray)


tmptrj <- NULL
for ( i in 1:7 ) {
  tmptrj=rbind(tmptrj, sim161$xyzfit[[i]])
}
sim161$cis$proj <- pca.project(tmptrj[,eq.md], pc.xray)


tmptrj <- NULL
for ( i in 8:14 ) {
  tmptrj=rbind(tmptrj, sim161$xyzfit[[i]])
}
sim161$trans$proj <- pca.project(tmptrj[,eq.md], pc.xray)

sim161$xyzfit <- NULL
sim162$xyzfit <- NULL

save(sim161, sim162, file="projected_E434K.RData")









