def loadFile(self,filename,sigset_dir=None):
        '''
        Loads a BEE matrix from a file.
        '''
        self.filename = filename
        self.shortname = os.path.basename(filename)
        
        # open the file for reading
        f = open(filename,'rb')
        
        #read the distance matrix header (first four lines of the file)
        
        line = f.readline()
        # Test line endings
        if len(line) != 3 or line[-1] != "\x0a":
            # Note: \x0a is the "official" line ending char as of 
            #       \x0d is also supported in the Java and C++ tools but it will cause a failure in this implementation.
            #       see IARPA BEST - Challenge Problem Specification and Executable Application Program Interface
            #       thanks to Todd Scruggs
            raise ValueError("Unsupported line ending.  Should two characters followed by LF (0x0A).")
        # Check Format
        line = line.strip()
        if line not in ['D2','S2','M2']:
            raise ValueError('Unknown matrix Format "%s".  Should be D2, S2, or M2.'%line)
        
        self.is_distance = True
        if line[0][0] == 'S':
            self.is_distance = False

        # read and process line 2 (target sigset)
        line = f.readline().split()
        self.target_filename = os.path.basename(line[0])

        # read and process line 3 (query sigset)
        line = f.readline().split()
        self.query_filename = os.path.basename(line[0])

        # read and process line 4 (MF n_queries n_targets magic_number)
        line = f.readline().split()
        assert line[0] in ['MF','MB']
        file_type = line[0][1]
        
        self.n_queries = int(line[1])
        self.n_targets = int(line[2])
        
        big_endian = struct.pack(">I",0x12345678)
        little_endian = struct.pack("<I",0x12345678)
        
        if line[3] != big_endian and line[3] != little_endian:
            print "Warning unsupported magic number is BEE matrix: 0x%s"%binascii.hexlify(line[3])
            
        self.magic_number = struct.unpack_from("=I",line[3])[0]
        if self.magic_number == 0x12345678:
            byteswap = False
        elif self.magic_number == 0x78563412:
            byteswap = True
        else:
            raise ValueError("Unknown magic number in similarity matrix.")
        
        # Read the matrix data
        if file_type=='F':
            self.matrix = np.fromfile(f,dtype=np.float32)
        elif file_type=='B':
            self.matrix = np.fromfile(f,dtype=np.byte)
        else:
            raise TypeError("Unknown matrix file_type: %s"%file_type)
        
        if file_type=='F' and byteswap:
            self.matrix = self.matrix.byteswap()
        assert self.matrix.shape[0] == self.n_targets*self.n_queries
        self.matrix = self.matrix.reshape(self.n_queries,self.n_targets)
        
        # Try to read the sigsets.
        if sigset_dir == None:
            sigset_dir = os.path.dirname(self.filename)
        self.queries = None
        try:
            ss_name = os.path.join(sigset_dir,self.query_filename)
            self.queries = parseSigSet(ss_name)
            assert len(self.queries) == self.n_queries
        except:
            pass
            #print "Warning: cound not read the query sigset for distance matrix %s"%self.shortname
            #print "         SigSet File:",ss_name
            #print "         Expected:",self.n_queries,"Read:",len(self.queries)
        
        self.targets = None
        try:
            ss_name = os.path.join(sigset_dir,self.target_filename)
            self.targets = parseSigSet(ss_name)

            assert len(self.targets) == self.n_targets
        except:
            pass
            #print "Warning: cound not read the target sigset for distance matrix %s"%self.shortname
            #print "         SigSet File:",ss_name
            #print "         Expected:",self.n_targets,"Read:",len(self.targets)
dataset/ETHPy150Open bolme/pyvision/src/pyvision/analysis/bee.py/BEEDistanceMatrix.loadFile/original