| 1 | #!/usr/local/bin/perl -w | ||
| 2 | # | ||
| 3 | # sam2bed.pl - Convert SAM format to BED6 format. | ||
| 4 | # assume all chromosome names are in the format of | ||
| 5 | # '1','2',...,'X','Y','M'. All the others are considered as non-chromosome. | ||
| 6 | # | ||
| 7 | # History: | ||
| 8 | # | ||
| 9 | # Added an option to output only uniquely mapped reads. 07/11/2010. | ||
| 10 | # | ||
| 11 | |||
| 12 | # Program arguments. | ||
| 13 | use strict; | ||
| 14 | if(@ARGV < 2){ | ||
| 15 | print "Usage: $0 input.sam output.bed [-u]\n\n"; | ||
| 16 | print "-u Option to keep only uniquely aligned reads.\n"; | ||
| 17 | print " For reads with multiple aligned locations, only the top hit will be reported.\n"; | ||
| 18 | exit 0; | ||
| 19 | } | ||
| 20 | if($ARGV[0] eq '-') {*HSAM=*STDIN;} | ||
| 21 | else {open HSAM, "<", $ARGV[0] or die "Open input file error: $!\n";} | ||
| 22 | if($ARGV[1] eq '-') {*HBED=*STDOUT;} | ||
| 23 | else {open HBED, ">", $ARGV[1] or die "Open output file error: $!\n";} | ||
| 24 | |||
| 25 | my $utag = 0; # unique mapping tag. | ||
| 26 | $utag = 1 if defined $ARGV[2] and $ARGV[2] eq '-u'; | ||
| 27 | |||
| 28 | |||
| 29 | # Process the rest of the SAM. | ||
| 30 | while(<HSAM>){ | ||
| 31 | next if /^\@/; # skip SAM header lines. | ||
| 32 | next if /^\s*\#/; # skip comments. | ||
| 33 | chomp; | ||
| 34 | my($chrom,$pos,$len1,$len2,$nskip,$strand,$uniq) = parseln($_); | ||
| 35 | if($chrom eq '') { die "Encountered lines with incomplete information. Exit.\n";} | ||
| 36 | elsif($chrom eq 'BBB') { next;} # skip reads mapped to non-standard chromosomes. | ||
| 37 | if(($utag and $uniq) or !$utag){ | ||
| 38 | # pos is already 0-based. | ||
| 39 | printf HBED "%s\t%d\t%d\t%s\t%d\t%s\n", $chrom,$pos,$pos+$len1,'N','0',$strand; | ||
| 40 | if($nskip > 0){ # 2nd half of the spliced read. | ||
| 41 | printf HBED "%s\t%d\t%d\t%s\t%d\t%s\n", $chrom,$pos+$len1+$nskip,$pos+$len1+$nskip+$len2,'N','0',$strand; | ||
| 42 | } | ||
| 43 | } | ||
| 44 | } | ||
| 45 | close HSAM; | ||
| 46 | close HBED; | ||
| 47 | |||
| 48 | |||
| 49 | |||
| 50 | ################################### | ||
| 51 | # Function to parse one SAM line. | ||
| 52 | sub parseln{ | ||
| 53 | my @cells = split /\t/, $_[0]; | ||
| 54 | return('',-1,-1,'','') if @cells < 11; # lines with incomplete information. | ||
| 55 | #### Strand #### | ||
| 56 | my $strand = $cells[1] & 16? '-' : '+'; # Strand. 16 is the tag for reverse strand. | ||
| 57 | #### Chromosome name #### | ||
| 58 | # my $chrom; | ||
| 59 | # if($cells[2] =~ /(chr)?([0-9]+|[XYM]|MT)(\.fa)?/i){ | ||
| 60 | # $chrom = 'chr' . $2; # Extract chromosome name. | ||
| 61 | # }else{ | ||
| 62 | # return('BBB',-1,-1,'','') unless defined $chrom; # reads mapped to non-chromosome locations. | ||
| 63 | # } | ||
| 64 | $cells[2] =~ s/^chr//; | ||
| 65 | $cells[2] =~ s/\.fa$//; | ||
| 66 | my $chrom = 'chr' . $cells[2]; # chromosome name. | ||
| 67 | #### Mapped location #### | ||
| 68 | my $pos = $cells[3]-1; # SAM: 1-based, left-most; BED: 0-based. | ||
| 69 | # Determine matched and skipped lengths. | ||
| 70 | my($len1,$len2,$nskip); | ||
| 71 | my $cigar = $cells[5]; | ||
| 72 | if($cigar =~ /^([0-9]+)M(([0-9]+)N([0-9]+)M)?$/){ # Bowtie format. | ||
| 73 | $len1 = $1; | ||
| 74 | if(defined $2){ # the read is spliced. | ||
| 75 | $len2 = $4; | ||
| 76 | $nskip = $3; | ||
| 77 | } else{ | ||
| 78 | $len2 = 0; | ||
| 79 | $nskip = 0; | ||
| 80 | } | ||
| 81 | } else{ # Ignore other formats and set to seq length. | ||
| 82 | $len1 = length($cells[9]); | ||
| 83 | $len2 = 0; | ||
| 84 | $nskip = 0; | ||
| 85 | } | ||
| 86 | # Determine uniqueness of mapping. | ||
| 87 | my $uniq = 1; | ||
| 88 | if(@cells > 11){ # processing tags. | ||
| 89 | my @tags = splice @cells, 11; | ||
| 90 | my %tag_tbl; | ||
| 91 | foreach my $t(@tags){ | ||
| 92 | my @e = split /\:/, $t; | ||
| 93 | $tag_tbl{$e[0]} = {'type' => $e[1], 'value' => $e[2]}; | ||
| 94 | } | ||
| 95 | if(exists $tag_tbl{'XT'}){ # BWA read type tag. | ||
| 96 | if($tag_tbl{'XT'}->{'value'} ne 'U') {$uniq = 0;} | ||
| 97 | } elsif(exists $tag_tbl{'NH'}){ # SAM tag for number of alignments: Bowtie and others. | ||
| 98 | if($tag_tbl{'NH'}->{'value'} > 1) {$uniq = 0;} | ||
| 99 | } | ||
| 100 | } | ||
| 101 | return ($chrom,$pos,$len1,$len2,$nskip,$strand,$uniq); | ||
| 102 | } | ||
| 103 |