Mao Jianfeng
2011-Apr-13 10:15 UTC
[R] strategy for writing out file with lines header initiated with comment sign
Dear all, I have data.frame object in R. I want to export it in tab-delimited file with several lines of header initiated with comment sign (#). I do not know how to do that in R. Could you please give helps on this problem? Thanks in advance. Best, Jian-Feng, ################################################################## The lines I want to write in the header lines look like, with words in the last line (here the line "#CHROM POS ID REF ALT QUAL FILTER INFO FORMAT NA00001") be separated by tab : ##fileformat=VCFv4.1 ##fileDate=20090805 ##source=myImputationProgramV3.1 ##reference=file:///seq/references/1000GenomesPilot-NCBI36.fasta ##contig=<ID=20,length=62435964,assembly=B36,md5=f126cdf8a6e0c7f379d618ff66beb2da,species="Homo sapiens",taxonomy=x> ##phasing=partial ##INFO=<ID=NS,Number=1,Type=Integer,Description="Number of Samples With Data"> ##INFO=<ID=DP,Number=1,Type=Integer,Description="Total Depth"> ##INFO=<ID=AF,Number=A,Type=Float,Description="Allele Frequency"> ##INFO=<ID=AA,Number=1,Type=String,Description="Ancestral Allele"> ##INFO=<ID=DB,Number=0,Type=Flag,Description="dbSNP membership, build 129"> ##INFO=<ID=H2,Number=0,Type=Flag,Description="HapMap2 membership"> ##FILTER=<ID=q10,Description="Quality below 10"> ##FILTER=<ID=s50,Description="Less than 50% of samples have data"> ##FORMAT=<ID=GT,Number=1,Type=String,Description="Genotype"> ##FORMAT=<ID=GQ,Number=1,Type=Integer,Description="Genotype Quality"> ##FORMAT=<ID=DP,Number=1,Type=Integer,Description="Read Depth"> ##FORMAT=<ID=HQ,Number=2,Type=Integer,Description="Haplotype Quality"> #CHROM POS ID REF ALT QUAL FILTER INFO FORMAT NA00001
jim holtman
2011-Apr-13 13:15 UTC
[R] strategy for writing out file with lines header initiated with comment sign
Here is an outline of how to do it using connections: con <- file('/temp/mytemp.txt', 'w') writeLines(c("#comment", "# lines", "# in the file"), con = con) # create some data to be output as 'tab' separated myData <- as.data.frame(matrix(letters[1:25], 5)) write.table(myData, file = con, sep = '\t') close(con) con <- file('/temp/mytemp.txt', 'w') writeLines(c("#comment", "# lines", "# in the file"), con = con) # create some data to be output as 'tab' separated myData <- as.data.frame(matrix(letters[1:25], 5)) write.table(myData, file = con, sep = '\t') close(con) On Wed, Apr 13, 2011 at 6:15 AM, Mao Jianfeng <jianfeng.mao at gmail.com> wrote:> Dear all, > > I have data.frame object in R. I want to export it in tab-delimited > file with several lines of header initiated with comment sign (#). I > do not know how to do that in R. Could you please give helps on this > problem? > > Thanks in advance. > > Best, > Jian-Feng, > > ################################################################## > The lines I want to write in the header lines look like, with words in > the last line (here the line "#CHROM POS ? ? ID ? ? ? ?REF ALT ? ?QUAL > FILTER INFO ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?FORMAT ? ? ?NA00001") be > separated by tab : > > > ##fileformat=VCFv4.1 > ##fileDate=20090805 > ##source=myImputationProgramV3.1 > ##reference=file:///seq/references/1000GenomesPilot-NCBI36.fasta > ##contig=<ID=20,length=62435964,assembly=B36,md5=f126cdf8a6e0c7f379d618ff66beb2da,species="Homo > sapiens",taxonomy=x> > ##phasing=partial > ##INFO=<ID=NS,Number=1,Type=Integer,Description="Number of Samples With Data"> > ##INFO=<ID=DP,Number=1,Type=Integer,Description="Total Depth"> > ##INFO=<ID=AF,Number=A,Type=Float,Description="Allele Frequency"> > ##INFO=<ID=AA,Number=1,Type=String,Description="Ancestral Allele"> > ##INFO=<ID=DB,Number=0,Type=Flag,Description="dbSNP membership, build 129"> > ##INFO=<ID=H2,Number=0,Type=Flag,Description="HapMap2 membership"> > ##FILTER=<ID=q10,Description="Quality below 10"> > ##FILTER=<ID=s50,Description="Less than 50% of samples have data"> > ##FORMAT=<ID=GT,Number=1,Type=String,Description="Genotype"> > ##FORMAT=<ID=GQ,Number=1,Type=Integer,Description="Genotype Quality"> > ##FORMAT=<ID=DP,Number=1,Type=Integer,Description="Read Depth"> > ##FORMAT=<ID=HQ,Number=2,Type=Integer,Description="Haplotype Quality"> > #CHROM POS ? ? ID ? ? ? ?REF ALT ? ?QUAL FILTER INFO > ? ? ? ? ? FORMAT ? ? ?NA00001 > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Jim Holtman Data Munger Guru What is the problem that you are trying to solve?