From Genome Analysis Wiki
Jump to navigationJump to search
1,121 bytes added
, 13:46, 16 March 2010
Line 35: |
Line 35: |
| Returns true if successfully written, false if not. | | Returns true if successfully written, false if not. |
| |} | | |} |
| + | |
| + | === Usage Example === |
| + | The following example reads in a sam/bam file and writes it out as a sam/bam file. The file format of the input sam/bam is determined by the SamFile class based on reading the type from the file. The file format of the output sam/bam file is determined by the SamFile class based on the extension of the output file. A ".bam" extension indicates a BAM file. All other extensions indicate SAM files. |
| + | <pre> |
| + | int main(int argc, char ** argv) |
| + | { |
| + | if(argc != 3) |
| + | { |
| + | printf("./bam <inputFile> <outputFile.sam/bam>\n"); |
| + | exit(-1); |
| + | } |
| + | |
| + | |
| + | SamFile samIn; |
| + | |
| + | samIn.OpenForRead(argv[1]); |
| + | |
| + | SamFile samOut; |
| + | |
| + | samOut.OpenForWrite(argv[2]); |
| + | |
| + | // Read the sam header. |
| + | SamFileHeader samHeader; |
| + | samIn.ReadHeader(samHeader); |
| + | |
| + | samOut.WriteHeader(samHeader); |
| + | |
| + | // Read the first sam record. |
| + | SamRecord samRecord; |
| + | |
| + | // Keep reading records until it fails. |
| + | int recordCount = 0; |
| + | while (samIn.ReadRecord(samHeader, samRecord) == true) |
| + | { |
| + | recordCount++; |
| + | samOut.WriteRecord(samHeader, samRecord); |
| + | } |
| + | printf("RecordCount = %d\n", recordCount); |
| + | } |
| + | </pre> |