File: write.pca.R

package info (click to toggle)
r-cran-bios2cor 2.2.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,520 kB
  • sloc: makefile: 5
file content (80 lines) | stat: -rw-r--r-- 2,194 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# Bios2cor is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# See the GNU General Public License at:
# http://www.gnu.org/licenses/
#
write.pca <- function(corr_pca, filepathroot, pc= NULL, entropy= NULL){
  
  if (missing(corr_pca)) {
      stop("A PCA object created by the centered_pca function is required")
  }

 if (is.null(filepathroot)) {
  filename <- paste(tempdir(), "/PCA_COORD.csv", sep="")
  }else{
  filename <-paste(filepathroot, "_PCA_COORD.csv", sep="")
  }


  pca_coord <- corr_pca$coord
  pca_positions <- rownames(pca_coord)
  pca_size <- length(pca_coord[,1])
  
  if (is.null(pc)) { 
  pca_dim <- length(pca_coord[1,])
  } else {
  pca_dim <- pc
  }

  head <- "position"
  if(!is.null(entropy)) {
    head <- paste(head, "entropy")
  }
  
  lapply(1:pca_dim, function(dim){
    head <<- paste(head, paste("PCA", dim, sep= ""))
  })
  
  write(head, file= filename, append= FALSE)
  
  if(!is.null(entropy)) {
    for(pos in 1:pca_size){
      pos_line <- pca_coord[pos,]
      position <- pca_positions[pos]
      entropy_val <- format(as.numeric(entropy[position]), digits=3, nsmall=3)
      
      #Ignoring possible NaN values
      if(sum(is.na(pos_line)) <= 0){
	coord_tmp <- paste(pca_coord[pos, 1:pca_dim], collapse= " ")
	current_line <- paste(position, entropy_val, coord_tmp)
	write(current_line, file= filename, append= TRUE)
      }
    }
  } else {
    for(pos in 1:pca_size){
      pos_line <- pca_coord[pos,]
      position <- pca_positions[pos]
      
      #Ignoring possible NaN values
      if(sum(is.na(pos_line)) <= 0){
	coords <- ""
	for(dim in 1:pca_dim){
	  coords <- paste(coords, pca_coord[pos,dim])
	}
	current_line <- paste(position, coords, sep= "")
	write(current_line, file= filename, append= TRUE)
      }
    }
  }
  
}