Reads percolator PSM results from a txt file and returns a list of
    :class:`Sii <maspy.core.Sii>` elements.

    :param filelocation: file path of the percolator result file
    :param specfile: unambiguous identifier of a ms-run file. Is also used as
        a reference to other MasPy file containers.
    :param psmEngine: PSM PSM search engine used for peptide spectrum matching
        before percolator. This is important to specify, since the scanNr
        information is written in a different format by some engines. It might
        be necessary to adjust the settings for different versions of percolator
        or the PSM search engines used.

        Possible values are 'comet', 'xtandem', 'msgf'.

    :returns: [sii, sii, sii, ...]

def readPercolatorResults(filelocation, specfile, psmEngine):
    """Reads percolator PSM results from a txt file and returns a list of
    :class:`Sii <maspy.core.Sii>` elements.

    :param filelocation: file path of the percolator result file
    :param specfile: unambiguous identifier of a ms-run file. Is also used as
        a reference to other MasPy file containers.
    :param psmEngine: PSM PSM search engine used for peptide spectrum matching
        before percolator. This is important to specify, since the scanNr
        information is written in a different format by some engines. It might
        be necessary to adjust the settings for different versions of percolator
        or the PSM search engines used.

        Possible values are 'comet', 'xtandem', 'msgf'.

    :returns: [sii, sii, sii, ...]
    """
    if psmEngine not in ['comet', 'msgf', 'xtandem']:
        raise Exception('PSM search engine not supported: ', psmEngine)
    itemList = list()

    #Note: regarding headerline, xtandem seperates proteins with ';',
    #msgf separates proteins with a tab
    with io.open(filelocation, 'r', encoding='utf-8') as openfile:
        lines = openfile.readlines()

    headerDict = dict([[y,x] for (x,y) in
                       enumerate(lines[0].strip().split('\t'))
                       ])
    scanEntryList = list()
    for line in lines[1:]:
        if len(line.strip()) == 0:
            continue
        fields = line.strip().split('\t')

        if psmEngine in ['comet', 'msgf']:
            scanNr = fields[headerDict['PSMId']].split('_')[-3]
        elif psmEngine in ['xtandem']:
            scanNr = fields[headerDict['PSMId']].split('_')[-2]

        peptide = fields[headerDict['peptide']]
        if peptide.find('.') != -1:
            peptide = peptide.split('.')[1]
        #Change to the new unimod syntax
        peptide = peptide.replace('[UNIMOD:', '[u:')
        sequence = maspy.peptidemethods.removeModifications(peptide)

        qValue = fields[headerDict['q-value']]
        score = fields[headerDict['score']]
        pep = fields[headerDict['posterior_error_prob']]

        sii = maspy.core.Sii(scanNr, specfile)
        sii.peptide = peptide
        sii.sequence = sequence
        sii.qValue = float(qValue)
        sii.score = float(score)
        sii.pep = float(pep)
        sii.isValid = False

        itemList.append(sii)
    return itemList