#!/usr/bin/perl

use strict;
use warnings;

# usage: split large VCF file into individual chromosomes

my $vcf = $ARGV[0];

my $oname = "";
my $ofs;
open (my $ifs, $vcf) or die $!;
while (my $line = <$ifs>) {
  next if (substr ($line, 0, 1) eq "#");
  my ($chr, $rest) = split ("\t", $line);
  if ($chr ne $oname) {
    if ($oname ne "") {
      close ($ofs);
    }
    $oname = $chr;
    printf (STDERR "Chromosome %s\n", $chr);
    open ($ofs, ">$oname.vcf") or die $!;
  }
  printf ($ofs "%s", $line);
}
close ($ofs);
close ($ifs);
