File: prettyseq.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 (47 lines) | stat: -rw-r--r-- 1,315 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
#==>  prettyseq&num=xx{&bpl=xx}{&translate=[T|F]}
#<==   code=0\n
#line1
#line2
#...
#prettyseq END.\n
#To get a text representation of sequence of rank num and of its subsequences,
#with bpl bases per line (default = 60), and with optional translation of 
#protein-coding subsequences.

prettyseq <- function(num, bpl = 60, translate = TRUE, socket = autosocket()){
  #
  # Build request:
  #
  request <- paste("prettyseq&num=", formatC(num, format = "d"),
                   "&bpl=", bpl, "&translate=", ifelse(translate,"T", "F"), sep = "")
  writeLines(request, socket, sep = "\n")
  answerFromServer <- readLines(socket, n = 1)
  #
  # Check that there is an answer from server:
  #
  if(length(answerFromServer) == 0){
    warning("Empty answer from server")
    return(NA)
  }
  #
  # Check that no error code is returned by server:
  #
  if( !substr(x = answerFromServer, start = 6, stop = 6) == "0"){
    warning(paste("Server returned error code:", answerFromServer))
    return(NA)
  }
  #
  # Extract sequence from server:
  #
  result <- readLines(socket)
  #
  # Check the end:
  #
  if( result[length(result)] != "prettyseq END."){
    warning("Incomplete answer from server")
    return(NA)
  }
  result <- result[-length(result)] # remove last line
  cat(result, sep = "\n")
  invisible(result)
}