I have been working on a script for identifying frequent 'end points' of paired end reads from a SAM file on a genome for determining uneven cut frequencies, which I need to integrate the orientation and 1st/2nd read information into. The 2nd column of a SAM file gives the detail on the orientation with a number (as described [here][1] and [here][2]).

I hadn't used a bioperl import method because all I was doing was pulling the genomic start position of the read and building a hash by frequency, but I need to now tune it for directionality and mate pair.

My question is whether there is a better way to do this than:

```
## Numbers are random examples, not correctly done at the moment.
if ($line[1] == 99){
    $coordinate = $start
}elsif($line[1] == 147){
    $coordinate = $start - $readlength
}elsif($line[1] == 83){
    $coordinate = $start + $readlength
}.........etc
```

Given that a picture paints a thousand words, basically what I want is to pull out the coordinates of the red circles from my SAM file. It's part of a bigger perl script, so I'd prefer to avoid other tools, but if they exist I should look at them.

[<img alt="" src="http://i.imgur.com/EWT7mFA.png" style="height:155px; width:700px" />][3]

 [1]: http://bowtie-bio.sourceforge.net/manual.shtml#sam-bowtie-output
 [2]: http://http://picard.sourceforge.net/explain-flags.html
 [3]: http://i.imgur.com/EWT7mFA.png
You can parse the bitwise flags with the AND operator in perl. For example, the 5th bit in the bitwise flag denotes orientation:

```
if ($flag & 16) { 
   return 'negative strand';
} else {
   return 'positive strand';
}
```

The 5th bit is 2^4 = 16. 1st bit is 2^0, 2nd bit is 2^1...