get_vaf.pl 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use File::Basename;
  5. my $usage="\n\tperl $0 vcf_file
  6. \tscript to get chrM/MT VAF infomation.\n\n";
  7. die $usage unless @ARGV;
  8. my $vfile=shift;
  9. my $out=basename($vfile);
  10. $out=~s/.vcf.*//;
  11. my $fh;
  12. if(-T $vfile){
  13. open $fh,'<',$vfile or die "Cannot open file:$!" ;
  14. }else{
  15. open $fh, "gzip -dc $vfile |" or die "Cannot open pipe:$! "
  16. }
  17. open my $wfh, '>', $out.'.MT.vaf.xls' or die "Cannot write file: $!\n";
  18. while(<$fh>){
  19. next if /^#/;
  20. my ($chr, $pos, $ref, $alt, $format, $call)=(split /\t/)[0,1,3, 4, 8,9];
  21. # $chr=~s/chr//;
  22. next unless $chr=~/M/;
  23. next unless $format=~/AD/;
  24. next if $call eq '.' or $call eq './.';
  25. my ($ad_col);
  26. my @formats=split /:/,$format;
  27. for my $i (0.. $#formats){
  28. $ad_col=$i if $formats[$i] eq 'AD';
  29. # $dp_col=$i if $formats[$i] eq 'DP';
  30. }
  31. my ($ad)=(split /:/,$call)[$ad_col];
  32. if($alt=~/,/){
  33. my @alts=split /,/, $alt;
  34. my @ads=split /,/, $ad;
  35. my $tot=0;
  36. for my $cnt(@ads){
  37. $tot+=$cnt;
  38. }
  39. #my $nalt=0;
  40. next unless $tot>0;
  41. for my $i(0..$#alts){
  42. my $base=$alts[$i];
  43. my $nbase=(split /,/, $ad )[$i+1];
  44. next if $base=~/\*/;
  45. print $wfh join("\t", $chr.'-'.$pos, $ref, $base ,100*sprintf("%.2f",$nbase/$tot).'%' ), "\n";
  46. }
  47. }else{
  48. my($nref, $nalt)=split /,/, $ad;
  49. print $wfh join("\t", $chr.'-'.$pos, $ref, $alt ,100*sprintf("%.2f",$nalt/($nref+$nalt) ).'%' ), "\n" if $nref+$nalt>0 ;
  50. }
  51. }