Extracting subsets of data
this seems to be an easy task really, but being completely new to the world of programming, I have problems with the following task: I have a huge file which has t开发者_运维问答he following format:
track type= wiggle name09
variableStep chrom=chr1
34 5
36 7
54 8
variableStep chrom=chr2
33 4
35 2
78 7
this is text with the word random in it# this we need to remove
82 4
88 6
variableStep chrom=chr3
78 5
89 4
56 7
now what I would like as an out put is just
one file called 1 and containing only
34 5
36 7
54 8
a second file called 2
33 4
35 2
78 7
82 4
88 6
a third file
78 5
89 4
56 7
It would be great to get some help on this... If any knows how to do it in R... that would be even better
Does the following help?
#!/usr/bin/env perl
use strict;
use warnings;
my $filename = 1;
my $flag;
my $fh;
while (<>) {
if (/^\d+\s+\d+\s*$/) {
if ( $flag == 1 ) {
$flag = 0;
open $fh, '>', $filename;
$filename++;
}
print $fh $_;
}
elsif (/random/) {
next;
}
else {
$flag = 1;
}
}
Usage:
Save the above as extract
(or any other name, if that matters).
Assuming that the file with data is named file
.
perl extract /path/to/file
Here's a solution in R.
Load your data:
a <- readLines(textConnection("track type= wiggle name09
variableStep chrom=chr1
34 5
36 7
54 8
variableStep chrom=chr2
33 4
35 2
78 7
this is text with the word random in it# this we need to remove
82 4
88 6
variableStep chrom=chr3
78 5
89 4
56 7"))
Process it by finding the break points and only keeping rows with number space number format:
idx <- grep("=", a)
idx <- idx[c(which((idx[-1]-idx[-length(idx)])>1),length(idx))]
idx <- cbind(idx+1,c(idx[-1]-1,length(a)))
sapply(1:nrow(idx), function(i) {
x <- a[idx[i,1]:idx[i,2]]
write.table(x[grep("^\\d+\\s+\\d+\\s*", x, perl=TRUE)], file=as.character(i), row.names=FALSE, col.names=FALSE, quote=FALSE)
})
精彩评论