##################################
#                                #
# Last modified 2017/05/30       # 
#                                #
# Georgi Marinov                 #
#                                # 
##################################

import sys
import string
import math
from sets import Set

def run():

    if len(sys.argv) < 4:
        print 'usage: python %s table fields minClusterCounts outputfilename' % sys.argv[0]
        sys.exit(1)
    
    table = sys.argv[1]
    DataFields = sys.argv[2].split(',')
    DF = []
    for ID in DataFields:
        DF.append(int(ID))
    minCC = int(sys.argv[3])

    DXClusterDict = {}
    linelist = open(table)
    L=0
    for line in linelist:
        L+=1
        if L == 1:
            continue
        fields=line.strip().split(' ')
        cluster = fields[0].split(':')[3]
        if DXClusterDict.has_key(cluster):
            pass
        else:
            DXClusterDict[cluster] = {}
            for ID in DF:
                DXClusterDict[cluster][ID] = 0
        for ID in DF:
            counts = int(float(fields[ID]))
            DXClusterDict[cluster][ID] += counts

    outfile = open(sys.argv[4], 'w')

    SC = {}

    OutDict = {}

    L = 0
    linelist = open(table)
    for line in linelist:
        L += 1
        if L == 1:
            outfile.write(line)
            continue
        fields=line.strip().split(' ')
        cluster = fields[0].split(':')[3]
        if OutDict.has_key(cluster):
            pass
        else:
            OutDict[cluster] = []
        PassesMinCC = False
        for ID in DF:
            if DXClusterDict[cluster][ID] >= minCC:
                 PassesMinCC = True
                 break
        TotalSum = 0
        for ID in DF:
            counts = int(float(fields[ID]))
            TotalSum += counts
        if PassesMinCC and TotalSum > 0:
            OutDict[cluster].append(line)

    for cluster in OutDict.keys():
        OutDict[cluster] = list(Set(OutDict[cluster]))
        if len(OutDict[cluster]) >= 2:
            for line in OutDict[cluster]:
                outfile.write(line)

    outfile.close()
   
run()