-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsv2bed.pl
More file actions
executable file
·59 lines (45 loc) · 1.01 KB
/
csv2bed.pl
File metadata and controls
executable file
·59 lines (45 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/perl -w
# convert csv format to bed format
if((@ARGV) != 2 && scalar(@ARGV) != 1) {
die "Usage: ~ <data.csv> [<out.bed>]\n";
}
use Flat;
my($in) = Flat->new1($ARGV[0]);
my(@data) = $in->getDataArray();
my($numOfFlds) = $in->getNumOfFields();
my($out);
if(scalar(@ARGV) == 2) {
$out = $ARGV[1];
}
else {
$out = $ARGV[0];
$out =~ s/\.csv$/\.bed/;
}
open OUT, "+>$out" || die $!;
for(my($j) = 0; $j < scalar(@data); $j++) {
my $d = $data[$j];
if($d->[0] =~ /^chr/) {
# keep the original notation
}
elsif($d->[0] eq '23') {
$d->[0] = "chrX";
}
elsif($d->[0] eq '24') {
$d->[0] = "chrY";
}
else {
$d->[0] = "chr$d->[0]";
}
# order start < end
my($start, $end) = ($d->[1], $d->[2]);
if($start > $end) {
($start, $end) = ($end, $start);
}
# chr1 100 200 name score strand
print OUT "$d->[0]\t$start\t$end\tname$j\t1\t$d->[3]";
for(my($i) = 4; $i < $numOfFlds; $i++) {
print OUT "\t$d->[$i]";
}
print OUT "\n";
}
close OUT;