Skip to content

Instantly share code, notes, and snippets.

@cth
Forked from mlawson/vcf2bed.pl
Last active November 11, 2015 12:21
Show Gist options
  • Save cth/25d6414ca67600c89e23 to your computer and use it in GitHub Desktop.
Save cth/25d6414ca67600c89e23 to your computer and use it in GitHub Desktop.
Converts a vcf file (typically of indels) into a bed file
#!/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));
$endPos = $startPos + length($alt);
$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