bioinfo_commons.R 1.95 KB
Newer Older
1
#library(BiocGenerics, quietly=T, warn_conflicts=F )
Holger Brandl's avatar
Holger Brandl committed
2
#library(Biostrings, quietly=T)
3
#require_auto(BiocGenerics )x
Holger Brandl's avatar
Holger Brandl committed
4

5
#require_auto(Biostrings)
Holger Brandl's avatar
Holger Brandl committed
6
7
8


read.fasta <- function(fileName){
9
    warning("Deprecated: use read_fasta instead!")
10
    read_fasta(fileName)
11
12
13
14
}


read_fasta <- function(fileName){
Holger Brandl's avatar
Holger Brandl committed
15
16
    ## read a fasta file as data.fram

17
    fastaData <- Biostrings::readBStringSet(fileName, "fasta")
Holger Brandl's avatar
Holger Brandl committed
18
19
20
21
22
23
24
    #	fastaDataDF <- as.data.frame(as.character(fastaData), stringsAsFactors=FALSE)
    fastaDataDF <- data.frame(Sequence=as.character(fastaData), GeneDesc=names(fastaData), stringsAsFactors=FALSE)
    rownames(fastaDataDF) <- NULL
    #	names(fastaDataDF)[1] <- "Sequence"
    return(fastaDataDF)
}

Holger Brandl's avatar
Holger Brandl committed
25

26
27
28

write.fasta <- function(fileName){
    warning("Deprecated: use write_fasta instead!")
29
    read_fasta(fileName)
30
31
32
33
34
}


write_fasta <- function(seq_names, sequences, file){
    fastaData <- Biostrings::AAStringSet(sequences)
Holger Brandl's avatar
Holger Brandl committed
35
    names(fastaData) <- seq_names;
36
    Biostrings::writeXStringSet(fastaData, file=file, format="fasta", width=80)
Holger Brandl's avatar
Holger Brandl committed
37
38
39
}


Holger Brandl's avatar
Holger Brandl committed
40
getSeqLengthFromFasta <- function(fileName){
41
    assembly <- mutate(read_fasta(fileName), seq_len=nchar(Sequence))
Holger Brandl's avatar
Holger Brandl committed
42
43
44
45
46
    assembly$Sequence = NULL
    return(assembly)
}


Holger Brandl's avatar
Holger Brandl committed
47
## necessary to disable scientific number formats for long integers
48
#options(scipen=100)
Holger Brandl's avatar
Holger Brandl committed
49
50
51
52
53

## writes a table in bed format expecting columns being ordered according to bed spec already
#write.bed <- function(bedData, file){
#    write.table(bedData, file=file, quote=FALSE, sep ="\t", na="NA", row.names=FALSE, col.names=FALSE)
#}
54

Holger Brandl's avatar
Holger Brandl committed
55
56
write_bed <- function(bedData, file) write.bed(bedData, file)

57
58
59
60
61
62
63
64
65
66
67
write.bed <- function(bedData, file){
    oldScipen<-getOption("scipen")

     ## necessary to disable scientific number formats for long integers
    options(scipen=100)

    write.table(bedData, file=file, quote=FALSE, sep ="\t", na="NA", row.names=FALSE, col.names=FALSE)

    ## restore old scipen value
    options(scipen=oldScipen)
}
68
69

## reload to fix rename overloading
70
#reload_dplyr()