Dynamically build SQL query to generate entries for the multi-set
        merged protein and peptide tables. E.g.

        SELECT g.gene_acc, pc.channel_name, pc.amount_psms_name,
               giq.quantvalue giq.amount_psms gfdr.fdr
        FROM genes AS g
        JOIN biosets AS bs
        JOIN gene_tables AS gt ON gt.set_id=bs.set_id
        JOIN genequant_channels AS pc ON pc.gene_table_id=gt.genetable_id
        JOIN gene_iso_quanted AS giq ON giq.gene_id=g.gene_id
             AND giq.channel_id=pc.channel_id
        JOIN gene_fdr AS gfdr ON gfdr.gene_id=g.gene_id
             AND gfdr.genetable_id=gt.genetable_id
        ORDER BY g.gene

        This is multi-set output because we join on biosets. The output is
        then parsed to its respective set by the action code.

def prepare_mergetable_sql(self, precursor=False, isobaric=False,
                               probability=False, fdr=False, pep=False):
        """Dynamically build SQL query to generate entries for the multi-set
        merged protein and peptide tables. E.g.

        SELECT g.gene_acc, pc.channel_name, pc.amount_psms_name,
               giq.quantvalue giq.amount_psms gfdr.fdr
        FROM genes AS g
        JOIN biosets AS bs
        JOIN gene_tables AS gt ON gt.set_id=bs.set_id
        JOIN genequant_channels AS pc ON pc.gene_table_id=gt.genetable_id
        JOIN gene_iso_quanted AS giq ON giq.gene_id=g.gene_id
             AND giq.channel_id=pc.channel_id
        JOIN gene_fdr AS gfdr ON gfdr.gene_id=g.gene_id
             AND gfdr.genetable_id=gt.genetable_id
        ORDER BY g.gene

        This is multi-set output because we join on biosets. The output is
        then parsed to its respective set by the action code.
        """
        featcol = self.colmap[self.table_map[self.datatype]['feattable']][1]
        selectmap, count = self.update_selects({}, ['p_acc', 'set_name'], 0)
        joins = []
        if self.datatype == 'protein':
            selects = ['pgm.{}'.format(featcol), 'bs.set_name']
            firstselect = 'pgm'
            joins.append(('proteins', 'g', ['pgm']))
        else:
            selects = ['g.{}'.format(featcol), 'bs.set_name']
            firstselect = 'g'
        if isobaric:
            selects.extend(['pc.channel_name',
                            'pc.amount_psms_name', 'giq.quantvalue',
                            'giq.amount_psms'])
            joins.extend([(self.table_map[self.datatype]['isochtable'], 'pc',
                           ['gt']),
                          (self.table_map[self.datatype]['isoqtable'], 'giq',
                           ['g', 'pc'], True),
                          ])
            fld = ['channel', 'isoq_psmsfield', 'isoq_val',
                   'isoq_psms']
            selectmap, count = self.update_selects(selectmap, fld, count)
        if precursor:
            selects.extend(['preq.quant'])
            joins.append((self.table_map[self.datatype]['prectable'], 'preq',
                          ['g', 'gt'], True))
            fld = ['preq_val']
            selectmap, count = self.update_selects(selectmap, fld, count)
        if probability:
            selects.extend(['gprob.probability'])
            joins.append((self.table_map[self.datatype]['probabilitytable'],
                          'gprob', ['g', 'gt'], True))
            fld = ['prob_val']
            selectmap, count = self.update_selects(selectmap, fld, count)
        if fdr:
            selects.extend(['gfdr.fdr'])
            joins.append((self.table_map[self.datatype]['fdrtable'], 'gfdr',
                          ['g', 'gt'], True))
            fld = ['fdr_val']
            selectmap, count = self.update_selects(selectmap, fld, count)
        if pep:
            selects.extend(['gpep.pep'])
            joins.append((self.table_map[self.datatype]['peptable'], 'gpep',
                          ['g', 'gt'], True))
            fld = ['pep_val']
            selectmap, count = self.update_selects(selectmap, fld, count)
        sql = ('SELECT {} FROM {} AS {} JOIN biosets AS bs '
               'JOIN {} AS gt ON gt.set_id=bs.set_id'.format(
                   ', '.join(selects),
                   self.table_map[self.datatype]['feattable'],
                   firstselect,
                   self.table_map[self.datatype]['fntable']))
        sql = self.get_sql_joins_mergetable(sql, joins, self.datatype)
        sql = '{} ORDER BY g.{}'.format(sql, featcol)
        return sql, selectmap