PesKchan
PesKchan

Reputation: 978

Accessing a list and saving them into files

The object im trying to save is this which is generated after annotating the region with chipseeker library

class(peakAnnoList)
[1] "list"

So if i try to see the data inside the object i get like this

peakAnnoList
$DOWN
Annotated peaks generated by ChIPseeker
9458/9458  peaks were annotated
Genomic Annotation Summary:
             Feature Frequency
5           Promoter 12.359907
1           1st Exon  1.776274
4         Other Exon  5.783464
3 Downstream (<=300)  1.733982
2  Distal Intergenic 78.346373

$High
Annotated peaks generated by ChIPseeker
15395/15395  peaks were annotated
Genomic Annotation Summary:
             Feature Frequency
5           Promoter  7.392010
1           1st Exon  1.779799
4         Other Exon  7.417993
3 Downstream (<=300)  2.949009
2  Distal Intergenic 80.461189

$Low
Annotated peaks generated by ChIPseeker
6043/6043  peaks were annotated
Genomic Annotation Summary:
             Feature Frequency
5           Promoter  8.406421
1           1st Exon  1.472778
4         Other Exon  4.517624
3 Downstream (<=300)  1.373490
2  Distal Intergenic 84.229687

$UP
Annotated peaks generated by ChIPseeker
16628/16628  peaks were annotated
Genomic Annotation Summary:
             Feature Frequency
5           Promoter  9.123166
1           1st Exon  1.677893
4         Other Exon  7.373106
3 Downstream (<=300)  3.103199
2  Distal Intergenic 78.722637

So these are like each categories which i have annotated.

Now to access further information and save them into a dataframe I have to do like this

UP <- as.data.frame(peakAnnoList[["UP"]]@anno) 
DOWN <- as.data.frame(peakAnnoList[["DOWN"]]@anno)
High <-  as.data.frame(peakAnnoList[["High"]]@anno)
Low <- as.data.frame(peakAnnoList[["Low"]]@anno)

How to save this into individual files

Any suggestion or help would be really helpful

Upvotes: 0

Views: 63

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 389265

You can any of the apply family of functions to write csv for each list item in peakAnnoList.

Here's an example with Map.

Map(function(x, y) write.csv(x@anno, y, row.names = FALSE), 
    peakAnnoList, paste0(names(peakAnnoList), '.csv'))

This should create new csv files named UP.csv, DOWN.csv etc in the working directory.

The same can be achieved with purrr::imap.

purrr::imap(peakAnnoList, ~readr::write_csv(.x@anno, paste0(.y, '.csv')))

Upvotes: 1

Related Questions