In this section we will be talking about the SAMtools command view which performs three different kinds of operations. The first one is to view alignment. To allow one to view alignment. The second one is to allow conversion between the BAM and the SAM format. And the third one is to allow extraction of alignments only within a range of the genome. So let's illustrate each one of these. The viewing operation. So if we just invoke SAMtools with the package with a command view. And we give it our example.bam file as input. So that's our file, that we are going to use to illustrate the operations. And we're typing that for more. We're going to see basically all the lines the alignment file in the bam file. So we're going to see the SAM format. So automatically, it converts in to a SAM format. But you might notice that there was no header information. So let's say that we wish to see the alignments and the header which would form the full SAM file. So we do so by adding the dash h lowercase. Let's see what we get. So indeed, we have the header line which specifies that the file is sorted by coordinates so this we do know. Then, it has one line for every sequence in the reference genome, one line for the program, followed by the actual alignment file. And this looks like a complete alignment file. So we are going to save this input file example.sam Now we will say that maybe under some circumstances we might only wish to see the header. Perhaps we can save it and we can we can prepend it to another file. So SAMtools view dash capital H example.bam will give us precisely the header of the file. So thats the viewing operation. A second type of function that SAMtools performs is the conversion and we've already seen that conversion from the BAM format to the SAM format. But how can you convert from SAM to BAM? So, we can always specify samtools1 with the name of the command, in order to obtain more information about the particular command line parameters for that command. So, just type in samtools1 view, it give us the general usage of samtools view options followed by the input bam file and then potentially by the region. Which we are going get to in our next session. If we wish even more information, more specific information. We can also type help. So in this file, I'm looking particularly at the SAM to BAM conversion. So we're going to use this particular line. We want to convert from the SAM format to the BAM format. So we need to say, samtools1, Am calling it samtools1 because I get multiple versions of the program and this is most recent. View dash b which means the output should be in binary. T which gives the reference, indicates there's going to be a reference file. Then the reference file which is in my genome file on the system, hg38, hg38c. And finally the BAM file to the SAM file, example.sam And we're going to send that in to a file. Thats example.sam.bam. So that we dont override the example of the bam. So that's a conversion operation. And to make sure that they're succeeding we're going to take a look at the file samtools view example.sam.bam. Or we could also run flag stats to make sure that we have the same number of records as in the original file. So, that was really the second type of function which was to convert from the BAM format and to SAM and conversely from the SAM format to the BAM format. And next I am going to illustrate how we can use SAMtools with the command view in order to extract alignments within a range. So let's say that we're interested in just looking at alignments on chromosome 22 between location 24 million and location 25 million. And how will we do that? We can actually do it two ways. So let me remind you of the command samtools1 view. You probably have noticed that we have sam tools view, options input bam. And then there's an optional parameter at the end that allows us to specify the region. So that's one way. But we can also specify especially if we have multiple search regions on the genome, we can also specify the input regions in a file in for instance in BED format. Dash L, so with the dash L only includes reads overlapping these BED file. which will contain intervals. So let's try both options. So in the first option, we just type samtools1 view, will give the alignment file example.bam and then the range chromosome 22, 24 million to 25 million. So, let's see what happens. So notice that 'm simplifying to use example.bam. Oh and it then tells me that my file has to be indexed. Now normally a file in order to be indexed, has to be sorted. But in this case if we're looking at our alignment file, samtools, view example.bam and put it only at the header line. We don't need more than that. You are going see that the file is already sorted by coordinates. So we can simply apply the index just like we have demonstrated in the previous section samtool1, index example.bam And this creating the file example.bam.bai And now we can proceed with our range extraction, samtools view example.bam and then we are giving it the range. Then now, we can actually apply the same set of parameters to specify the type of the output. In this case the output will be in the SAM format. So let's look at what it looks like. One option will be with head. So you will notice that this are alignments and the start position is following position 24 million and let's also use tail to look at the last lines and make sure that they are also within the range that we asked for. So indeed the start position for the last alignments in the file. All alignments being sorted by their coordinates. So the last one is 24 million, nine hundred seventy one and so on. So just a little bit shorter than 25 million boundary. So, this shows one of the algorithms which we can use samtools view to extract alignments within a range. And let's try the next one. For this purpose, I have created a file, that's example.bed. It contains just one line. You might recall from the BED format that it requires at least columns. The chromosome number, start position and then end position interval. And I only have one interval here but then we have multiple lines. So let's now perform the same operation, samtools1 view, dash Capital L which says, extract only alignments within a range. And the range is specified by the example.bed file and then we don't need this region in here. Then this would produce sam file. In the same way we can use head to look at the first 10 lines and look at the coordinates, its not shown me after the position 24 million But we are also going to look at the tail and the last alignment will stop just short of the 25 million boundary. So, this concludes the presentation of SAMtools in particular samtools view.