Hi Jim I am trying to prepare a bed file to load as accustom track on the UCSC genome browser. I have a data frame that looks like the one below.> xV1 V2 V3 1 chr1 11255 55 2 chr1 11320 29 3 chr1 11400 45 4 chr2 21680 35 5 chr2 21750 84 6 chr2 21820 29 7 chr2 31890 46 8 chr3 32100 29 9 chr3 52380 29 10 chr3 66450 46 I would like to insert the following 4 lines at the beginning: browser position chr1:1-10000 browser hide all track type=wiggle_0 name=sample description=chr1_sample visibility=full variableStep chrom=chr1 span=1 and then insert 2 lines before each chromosome: track type=wiggle_0 name=sample description=chr2_sample visibility=full vriableStep chrom=chr2 span=1 The final result should be tab delimited file that looks like this: browser position chr1:1-10000 browser hide all track type=wiggle_0 name=sample description=chr1_sample visibility=full variableStep chrom=chr1 span=1 chr1 11255 55 chr1 11320 29 chr1 11400 45 track type=wiggle_0 name=sample description=chr2_sample visibility=full variableStep chrom=chr2 span=1 chr2 21680 35 chr2 21750 84 chr2 21820 29 track type=wiggle_0 name=sample description=chr3_sample visibility=full variableStep chrom=chr3 span=1 chr3 32100 29 chr3 32170 45 chr3 32240 45 Any kind of help or guidance will be much appreciated. Joseph ____________________________________________________________________________________ Be a better friend, newshound, and [[alternative HTML version deleted]]
Richard.Cotton at hsl.gov.uk
2008-Feb-06 10:22 UTC
[R] inserting text lines in a dat frame
> I am trying to prepare a bed file to load as accustom track on the > UCSC genome browser. > I have a data frame that looks like the one below. > > x > V1 V2 V3 > 1 chr1 11255 55 > 2 chr1 11320 29 > 3 chr1 11400 45 > 4 chr2 21680 35 > 5 chr2 21750 84 > 6 chr2 21820 29 > 7 chr2 31890 46 > 8 chr3 32100 29 > 9 chr3 52380 29 > 10 chr3 66450 46 > I would like to insert the following 4 lines at the beginning: > browser position chr1:1-10000 > browser hide all > track type=wiggle_0 name=sample description=chr1_sample visibility=full > variableStep chrom=chr1 span=1 > and then insert 2 lines before each chromosome: > track type=wiggle_0 name=sample description=chr2_sample visibility=full > vriableStep chrom=chr2 span=1 > The final result should be tab delimited file that looks like this: > browser position chr1:1-10000 > browser hide all > track type=wiggle_0 name=sample description=chr1_sample visibility=full > variableStep chrom=chr1 span=1 > chr1 11255 55 > chr1 11320 29 > chr1 11400 45 > track type=wiggle_0 name=sample description=chr2_sample visibility=full > variableStep chrom=chr2 span=1 > chr2 21680 35 > chr2 21750 84 > chr2 21820 29 > track type=wiggle_0 name=sample description=chr3_sample visibility=full > variableStep chrom=chr3 span=1 > chr3 32100 29 > chr3 32170 45 > chr3 32240 45#First write your preamble text to a file preamble <- c("browser position chr1:1-10000", "browser hide all", "track type=wiggle_0 name=sample description=chr1_sample visibility=full", "variableStep chrom=chr1 span=1") write(preamble, "myfile.txt") #Now split your data frame up by the values in V1 x <- data.frame(V1=rep(c("chr1", "chr2", "chr3"),times=c(3,4,3)), V2=c(11255,11320,11400,21680,21750,21820,21890,32100,52380,66450),V3=c(55,29,45,35,84,29,46,29,29,46)) spx <- split(x, x$V1) #Create lines of text to go before each piece of data frame lV1 <- levels(x$V1) maintext <- paste("track type=wiggle_0 name=sample description=", lV1, "_sample visibility=full\nvariableStep chrom=", lV1, " span=1", sep="") #Use a loop to write the pieces to the file for(i in 1: nlevels(x$V1)) { write(maintext[i], "myfile.txt", append=TRUE) write.table(spx[[i]], "myfile.txt", append=TRUE, sep="\t", quote=FALSE, col.names=FALSE, row.names=FALSE) } Regards, Richie. Mathematical Sciences Unit HSL ------------------------------------------------------------------------ ATTENTION: This message contains privileged and confidential inform...{{dropped:20}}
Try this and see if it is what you want:
x <- read.table(textConnection(" V1 V2 V3
1 chr1 11255 55
2 chr1 11320 29
3 chr1 11400 45
4 chr2 21680 35
5 chr2 21750 84
6 chr2 21820 29
7 chr2 31890 46
8 chr3 32100 29
9 chr3 52380 29
10 chr3 66450 46" ), header=TRUE)
cat("browser position chr1:1-10000\nrowser hide all\n",
file='tempxx.txt')
result <- lapply(split(x, x$V1), function(.chro){
cat(sprintf("track type=wiggle_0 name=sample description=%s_sample
visibility=full\nvariableStep chrom=%s span=1\n",
as.character(.chro$V1[1]), as.character(.chro$V1[1])),
file="tempxx.txt", append=TRUE)
write.table(.chro, sep="\t", file="tempxx.txt",
append=TRUE,
col.names=FALSE, row.names=FALSE)
})
On Feb 5, 2008 11:22 PM, joseph <jdsandjd at yahoo.com>
wrote:>
>
>
>
>
> Hi Jim
> I am trying to prepare a bed file to load as accustom track on the UCSC
> genome browser.
> I have a data frame that looks like the one below.
> > x
> V1 V2 V3
> 1 chr1 11255 55
> 2 chr1 11320 29
> 3 chr1 11400 45
> 4 chr2 21680 35
> 5 chr2 21750 84
> 6 chr2 21820 29
> 7 chr2 31890 46
> 8 chr3 32100 29
> 9 chr3 52380
> 29
> 10 chr3 66450 46
> I would like to insert the following 4 lines at the beginning:
> browser position chr1:1-10000
> browser hide all
> track type=wiggle_0 name=sample description=chr1_sample visibility=full
> variableStep chrom=chr1 span=1
> and then insert 2 lines before each chromosome:
> track type=wiggle_0 name=sample description=chr2_sample visibility=full
> vriableStep chrom=chr2 span=1
> The final result should be tab delimited file that looks like this:
> browser position chr1:1-10000
> browser hide all
> track type=wiggle_0 name=sample description=chr1_sample visibility=full
> variableStep chrom=chr1 span=1
> chr1 11255 55
> chr1 11320 29
> chr1 11400 45
> track type=wiggle_0 name=sample description=chr2_sample visibility=full
> variableStep chrom=chr2 span=1
> chr2 21680 35
> chr2 21750 84
> chr2 21820 29
> track type=wiggle_0 name=sample description=chr3_sample visibility=full
> variableStep chrom=chr3
> span=1
> chr3 32100 29
> chr3 32170 45
> chr3 32240 45
> Any kind of help or guidance will be much appreciated.
> Joseph
>
> ________________________________
> Be a better friend, newshound, and know-it-all with Yahoo! Mobile. Try it
> now.
--
Jim Holtman
Cincinnati, OH
+1 513 646 9390
What is the problem you are trying to solve?
Hi Jim
yes, this exactly want I want. However, I need one more step to get
rid of the first column so that the final file looks like this:
browser
position
chr1:1-10000
browser
hide
all
track
type=wiggle_0
name=sample
description=chr1_sample
visibility=full
variableStep
chrom=chr1
span=1
11255 55
11320
29
11400
45
track
type=wiggle_0
name=sample
description=chr2_sample
visibility=full
variableStep
chrom=chr2
span=1
21680
35
21750
84
21820
29
track
type=wiggle_0
name=sample
description=chr3_sample
visibility=full
variableStep
chrom=chr3
span=1
32100
29
32170
45
32240
45
Thank you very much
----- Original Message ----
From: jim holtman <jholtman@gmail.com>
To: joseph <jdsandjd@yahoo.com>
Cc: r-help@r-project.org
Sent: Wednesday, February 6, 2008 2:37:38 AM
Subject: Re: inserting text lines in a dat frame
Try
this
and
see
if
it
is
what
you
want:
x
<-
read.table(textConnection("
V1
V2
V3
1
chr1
11255
55
2
chr1
11320
29
3
chr1
11400
45
4
chr2
21680
35
5
chr2
21750
84
6
chr2
21820
29
7
chr2
31890
46
8
chr3
32100
29
9
chr3
52380
29
10
chr3
66450
46"
),
header=TRUE)
cat("browser
position
chr1:1-10000\nrowser
hide
all\n",
file='tempxx.txt')
result
<-
lapply(split(x,
x$V1),
function(.chro){
cat(sprintf("track
type=wiggle_0
name=sample
description=%s_sample
visibility=full\nvariableStep
chrom=%s
span=1\n",
as.character(.chro$V1[1]),
as.character(.chro$V1[1])),
file="tempxx.txt",
append=TRUE)
write.table(.chro,
sep="\t",
file="tempxx.txt",
append=TRUE,
col.names=FALSE,
row.names=FALSE)
})
On
Feb
5,
2008
11:22
PM,
joseph
<jdsandjd@yahoo.com>
wrote:>
>
>
>
>
>
Hi
Jim>
I
am
trying
to
prepare
a
bed
file
to
load
as
accustom
track
on
the
UCSC>
genome
browser.>
I
have
a
data
frame
that
looks
like
the
one
below.>
>
x>
V1
V2
V3>
1
chr1
11255
55>
2
chr1
11320
29>
3
chr1
11400
45>
4
chr2
21680
35>
5
chr2
21750
84>
6
chr2
21820
29>
7
chr2
31890
46>
8
chr3
32100
29>
9
chr3
52380>
29>
10
chr3
66450
46>
I
would
like
to
insert
the
following
4
lines
at
the
beginning:>
browser
position
chr1:1-10000>
browser
hide
all>
track
type=wiggle_0
name=sample
description=chr1_sample
visibility=full>
variableStep
chrom=chr1
span=1>
and
then
insert
2
lines
before
each
chromosome:>
track
type=wiggle_0
name=sample
description=chr2_sample
visibility=full>
vriableStep
chrom=chr2
span=1>
The
final
result
should
be
tab
delimited
file
that
looks
like
this:>
browser
position
chr1:1-10000>
browser
hide
all>
track
type=wiggle_0
name=sample
description=chr1_sample
visibility=full>
variableStep
chrom=chr1
span=1>
chr1
11255
55>
chr1
11320
29>
chr1
11400
45>
track
type=wiggle_0
name=sample
description=chr2_sample
visibility=full>
variableStep
chrom=chr2
span=1>
chr2
21680
35>
chr2
21750
84>
chr2
21820
29>
track
type=wiggle_0
name=sample
description=chr3_sample
visibility=full>
variableStep
chrom=chr3>
span=1>
chr3
32100
29>
chr3
32170
45>
chr3
32240
45>
Any
kind
of
help
or
guidance
will
be
much
appreciated.>
Joseph>
>
________________________________>
Be
a
better
friend,
newshound,
and
know-it-all
with
Mobile.
Try
it>
now.
--
Jim
Holtman
Cincinnati,
OH
+1
513
646
9390
What
is
the
problem
you
are
trying
to
solve?
____________________________________________________________________________________
Be a better friend, newshound, and
[[alternative HTML version deleted]]
thanks Richie. It woks perfectly. ----- Original Message ---- From: "Richard.Cotton@hsl.gov.uk" <Richard.Cotton@hsl.gov.uk> To: joseph <jdsandjd@yahoo.com> Cc: r-help@r-project.org; r-help-bounces@r-project.org Sent: Wednesday, February 6, 2008 2:22:16 AM Subject: Re: [R] inserting text lines in a dat frame>I am trying to prepare a bed file to load as accustom track on the>UCSC genome browser.>I have a data frame that looks like the one below.> >x>V1 V2 V3>1 chr1 11255 55>2 chr1 11320 29>3 chr1 11400 45>4 chr2 21680 35>5 chr2 21750 84>6 chr2 21820 29>7 chr2 31890 46>8 chr3 32100 29>9 chr3 52380 29>10 chr3 66450 46>I would like to insert the following 4 lines at the beginning:>browser position chr1:1-10000>browser hide all>track type=wiggle_0 name=sample description=chr1_sample visibility=full>variableStep chrom=chr1 span=1>and then insert 2 lines before each chromosome:>track type=wiggle_0 name=sample description=chr2_sample visibility=full>vriableStep chrom=chr2 span=1>The final result should be tab delimited file that looks like this:>browser position chr1:1-10000>browser hide all>track type=wiggle_0 name=sample description=chr1_sample visibility=full>variableStep chrom=chr1 span=1>chr1 11255 55>chr1 11320 29>chr1 11400 45>track type=wiggle_0 name=sample description=chr2_sample visibility=full>variableStep chrom=chr2 span=1>chr2 21680 35>chr2 21750 84>chr2 21820 29>track type=wiggle_0 name=sample description=chr3_sample visibility=full>variableStep chrom=chr3 span=1>chr3 32100 29>chr3 32170 45>chr3 32240 45 #First write your preamble text to a file preamble <- c("browser position chr1:1-10000", "browser hide all", "track type=wiggle_0 name=sample description=chr1_sample visibility=full", "variableStep chrom=chr1 span=1") write(preamble, "myfile.txt") #Now split your data frame up by the values in V1 x <- data.frame(V1=rep(c("chr1", "chr2", "chr3"),times=c(3,4,3)), V2=c(11255,11320,11400,21680,21750,21820,21890,32100,52380,66450),V3=c(55,29,45,35,84,29,46,29,29,46)) spx <- split(x, x$V1) #Create lines of text to go before each piece of data frame lV1 <- levels(x$V1) maintext <- paste("track type=wiggle_0 name=sample description=", lV1, "_sample visibility=full\nvariableStep chrom=", lV1, " span=1", sep="") #Use a loop to write the pieces to the file for(i in 1: nlevels(x$V1)) { write(maintext[i], "myfile.txt", append=TRUE) write.table(spx[[i]], "myfile.txt", append=TRUE, sep="\t", quote=FALSE, col.names=FALSE, row.names=FALSE) } Regards, Richie. Mathematical Sciences Unit HSL ------------------------------------------------------------------------ ATTENTION: This message contains privileged and confidential information intended for the addressee(s) only. If this message was sent to you in error, you must not disseminate, copy or take any action in reliance on it and we request that you notify the sender immediately by return email. Opinions expressed in this message and any attachments are not necessarily those held by the Health and Safety Laboratory or any person connected with the organisation, save those by whom the opinions were expressed. Please note that any messages sent or received by the Health and Safety Laboratory email system may be monitored and stored in an information retrieval system. ------------------------------------------------------------------------ ------------------------------------------------------------------------ This e-mail message has been scanned for Viruses and Content and cleared by NetIQ MailMarshal ------------------------------------------------------------------------ ____________________________________________________________________________________ Be a better friend, newshound, and [[alternative HTML version deleted]]