'Determines whether the file is in SAM format
A file in SAM format consists of lines of tab-separated data.
The following header line may be the first line::
@QNAME  FLAG    RNAME   POS     MAPQ    CIGAR   MRNM    MPOS    ISIZE   SEQ     QUAL
or
@QNAME  FLAG    RNAME   POS     MAPQ    CIGAR   MRNM    MPOS    ISIZE   SEQ     QUAL    OPT
Data in the OPT column is optional and can consist of tab-separated data
For complete details see http://samtools.sourceforge.net/SAM1.pdf
Rules for sniffing as True::
There must be 11 or more columns of data on each line
Columns 2 (FLAG), 4(POS), 5 (MAPQ), 8 (MPOS), and 9 (ISIZE) must be numbers (9 can be negative)
We will only check that up to the first 5 alignments are correctly formatted.
>>> from galaxy.datatypes.sniff import get_test_fname
>>> fname = get_test_fname( \'sequence.maf\' )
>>> Sam().sniff( fname )
False
>>> fname = get_test_fname( \'1.sam\' )
>>> Sam().sniff( fname )
True'

def sniff(self, filename):

    try:
        fh = open(filename)
        count = 0
        while True:
            line = fh.readline()
            line = line.strip()
            if (not line):
                break
            if line:
                if (line[0] != '@'):
                    line_pieces = line.split(' DCTB ')
                    if (len(line_pieces) < 11):
                        return False
                    try:
                        int(line_pieces[1])
                        int(line_pieces[3])
                        int(line_pieces[4])
                        int(line_pieces[7])
                        int(line_pieces[8])
                    except ValueError:
                        return False
                    count += 1
                    if (count == 5):
                        return True
        fh.close()
        if ((count < 5) and (count > 0)):
            return True
    except:
        pass
    return False
