Created
January 21, 2011 20:14
-
-
Save mlawson/790326 to your computer and use it in GitHub Desktop.
Converts a vcf file (typically of indels) into a bed file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
use strict; | |
if (scalar @ARGV != 1) { | |
print "vcf2bed.pl <vcf_file>\n"; | |
exit 1; | |
} | |
my $vcfFile = $ARGV[0]; | |
open VCF, $vcfFile | |
or die "Cannot open $vcfFile"; | |
while (<VCF>) { | |
if (index($_, '#') == 0) { | |
next; | |
} | |
my @fields = split /\t/, $_; | |
my $chrom = $fields[0]; | |
my $startPos = $fields[1]; | |
my $ref = $fields[3]; | |
my @alts = split (/,/, $fields[4]); | |
my $endPos = $startPos; | |
my $indelCall = ""; | |
foreach my $alt (@alts) { | |
if (length($ref) > length($alt)) { | |
# deletion event | |
my $diff = substr($ref, length($alt)); | |
$endPos = $startPos + length($diff); | |
$indelCall = '-' . $diff; | |
} | |
elsif (length($alt) > length($ref)) { | |
# insertion event | |
my $diff = substr($alt, length($ref)); | |
$indelCall = '+' . $diff; | |
} | |
# print out the line | |
print "$chrom\t$startPos\t$endPos\t$indelCall\n"; | |
} | |
} | |
close VCF; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment