-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFastaUtils.R.txt
executable file
·83 lines (72 loc) · 2.24 KB
/
FastaUtils.R.txt
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
#' @title read sequences from a fasta file
#'
#' @description
#'
#' @param infile input filename of a standard
#' fasta file
#' ...
#' @export
read.fasta <- function (filename,n=-1,id='') {
fasta=list()
fin = file(filename,'r')
i=0
while(length((line=readLines(fin,n=1L)))>0) {
if (grepl('^>',line)) {
i=i+1
if (n != - 1 & i > n) {
break
}
cid = gsub('^>([^ ]+).+','\\1',line)
if(id !='' & cid == id)
{
fasta[[cid]]=''
}
else if (id=='') {
fasta[[cid]]=''
}
else if (id != '' & cid != id & length(names(fasta)) > 0) {
break
}
} else if (grepl ('^[A-Za-z]',line)) {
seq=gsub('[^A-Za-z]','',line)
fasta[[cid]]=paste(fasta[[cid]],seq,sep='')
}
}
close(fin)
class(fasta)="FastaUtils"
return(fasta)
}
print.FastaUtils <- function (x) {
for (nm in names (x)) {
cat(paste(nm,"\t",nchar(x[[nm]]),"\n",sep=''))
}
}
#creating the data frames with elements ( coloumns id and length)
summary.FastaUtils <- function (x) {
df=data.frame(id=names(x),length=rep(0,length(names(x))))
#TODO: create a data frame with coloumns id and length and return it
i=1
for (n in names(x)) {
df[i,'length']= nchar(x[n])
i=i+1
}
return(df)
}
#fu=read.fasta(commandArgs(trailingOnly=TRUE)[1],n=5)
Main <- function () {
args=commandArgs(trailingOnly=TRUE)
if (length(args) == 1) {
fu=read.fasta(args[1])
} else if (length(args) == 2 & grepl('^[0-9]+$',args[2])) {
fu=read.fasta(args[1],n=as.integer(args[2]))
} else if (length(args) == 2) {
fu=read.fasta(args[1],id=args[2])
} else {
print("Usage ...")
q()
}
print(fu)
}
if (sys.nframe() == 0 & !interactive()) {
Main()
}