##################################
#                                #
# Last modified 12/17/2014       # 
#                                #
# Georgi Marinov                 #
#                                # 
##################################

import sys
import string
from sets import Set

def run():

    if len(sys.argv) < 3:
        print 'usage: python %s input_list_1 input_list_2 outfileprefix' % sys.argv[0]
        print '\tThe input files should be just a list of labels, one per line; A-vs-B where is in list1 and B is in list2 will be outputted'
        print '\tThe script will print to stdout; the outprefix parameter referse to the DESeq output file'
        sys.exit(1)
    
    input1 = sys.argv[1]
    input2 = sys.argv[2]
    outprefix = sys.argv[3]

    linelist = open(input1)
    Labels1 = []
    for line in linelist:
        Labels1.append(line.strip().split('\t')[0])
    Labels1 = list(Set(Labels1))
    Labels1.sort()

    linelist = open(input2)
    Labels2 = []
    for line in linelist:
        Labels2.append(line.strip().split('\t')[0])
    Labels2 = list(Set(Labels2))
    Labels2.sort()

    Seen = {}

    for L1 in Labels1:
        for L2 in Labels2:
            if L1 != L2:
                S1 = (L1,L2)
                S2 = (L2,L1)
                if Seen.has_key(S1) or Seen.has_key(S2):
                    continue
                Seen[S1] = 1
                Seen[S2] = 1
                outline = 'res <- nbinomTest( cds, "' + L1 + '", "' + L2 + '" )'
                print outline
                outline = 'write.table(res, "' + outprefix + '.' + L1 + '-vs-' + L2 + '.txt")'
                print outline

  
run()