File: readsmj.R

package info (click to toggle)
r-cran-seqinr 3.3-3-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 5,844 kB
  • ctags: 69
  • sloc: ansic: 1,955; makefile: 13
file content (91 lines) | stat: -rw-r--r-- 3,438 bytes parent folder | download | duplicates (5)
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
81
82
83
84
85
86
87
88
89
90
91
###################################################################################################
#                                                                                                 #
#                                       readsmj                                                   #
#                                                                                                 #
#                                                                                                 #
# ==>   readsmj&num=xx&nl=xx                                                                      #
# <==  code=xx&nl=xx                                                                              #
#     recnum=xx&name="xx"&plong=xx{&libel="xx"}                                                   #
#     ... a series of nl lines like that ...                                                      #
# Returns data from nl consecutive records starting from rank num of file SMJYT including label   #
# if not empty.                                                                                   #
# Code != 0 indicates error.                                                                      #
#                                                                                                 #
###################################################################################################

readsmj <- function(socket = autosocket(), num = 2, nl = 10, recnum.add = FALSE, nature.add = TRUE,
plong.add = FALSE, libel.add = FALSE, sname.add = FALSE, all.add = FALSE)
{ 
  #
  # Turn all flags to TRUE when requested:
  #
  if(all.add) sname.add <- libel.add <- plong.add <- nature.add <- recnum.add <- TRUE

  
  #
  # Build the request:
  #
  request <- paste("readsmj&num=", num, "&nl=", nl, sep = "", collapse = "")
  
  #
  # Send request:
  #
  
  writeLines(request, socket, sep = "\n") 
  #
  # Read answer from server:
  #
  s <- readLines(socket, n = 1)
  rep <- parser.socket(s)

  #
  # check answer from server:
  #
  if(rep[1] != "0") stop("Error from server")

  #
  # read answer from server:
  #
  n <- as.numeric(rep[2])
  ans <- readLines(socket, n = n)
  
  #
  # Put answer into a data.frame:
  #
  name <- character(n) # the only mandatory one
  if(recnum.add) recnum <- numeric(n)
  if(nature.add) nature <- factor(character(n), levels = c("00","01","02","03","04","05","06","07"))
  if(plong.add) plong <- numeric(n)
  if(libel.add) libel <- character(n)
  if(sname.add) sname <- character(n)
  for(i in seq_len(n)){
    tmp <- parser.socket(ans[[i]])
    name[i] <- substr(tmp[2], 2, nchar(tmp[2]) - 1)
    if(name[i] == "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"){
      name[i] <- NA
      next
    }
    if(sname.add) sname[i] <- substr(name[i], 3, nchar(name[i]))
    if(recnum.add) recnum[i] <- tmp[1]
    if(nature.add) nature[i] <- substr(name[i], 1, 2)
    if(plong.add) plong[i] <- tmp[3]
    if(libel.add){
      if(length(tmp) == 4){
        libel[i] <- substr(tmp[4], 2, nchar(tmp[4]) - 1)
      } else {
        libel[i] <- NA
      }
    }
  }
  df <- data.frame(list(name = I(name)))
  if(sname.add)  df$sname <- sname
  if(recnum.add) df$recnum <- recnum
  if(nature.add){
    levels(nature) <- c("status", "molecule", "journal", "year", "type", "organelle", "division", "dbstrucinfo")
    df$nature <- nature
  }
  if(plong.add) df$plong <- plong
  if(libel.add) df$libel <- libel
  
  return(df)
}