@merge( buildExpressionTracks, "expression_full.correlation")
def buildExpressionFullCorrelation( infiles, outfile):
    '''compute correlation between all replicates in all expression sets.
    '''

    outf = open(outfile, "w")
    outf.write( "track1\treplicate1\ttrack2\ttreplicate2\t%s\n" % "\t".join(Stats.CorrelationTest().getHeaders()))

    nfiles = len(infiles)

    def _readVals( filename ):
        '''read replicate values from file.'''
        vals = []
        headers = []
        offset = 10

        lines = open( filename, "r").readlines()
        data = lines[0][:-1].split("\t")
        nreplicates = len(data) - offset
        for x in range(nreplicates):
            headers.append( data[offset+x] )
            vals.append( [] )
        del lines[0]
        lines.sort()
        for line in lines:
            data = line[:-1].split("\t")
            for x in range( 0, nreplicates ):
                vals[x].append( float(data[offset+x]) )
        return nreplicates, vals, headers
    
    for f1 in range( nfiles - 1):
        filename1 = infiles[f1]
        if not filename1.endswith(".data"): continue
        track1 = filename1[:-len(".data")]

        nreplicates1, vals1, headers1 = _readVals( filename1 )
        
        # do within track correlation
        for x in range(nreplicates1-1):
            for y in range( x+1, nreplicates1):
                outf.write( "\t".join( (track1,
                                        headers1[x],
                                        track1,
                                        headers1[y],
                                        str( Stats.doCorrelationTest( vals1[x],
                                                                      vals1[y]) ) ) ) + "\n" )
        
        for f2 in range(f1+1, nfiles):
            filename2 = infiles[f2]
            if not filename2.endswith(".data"): continue
            track2 = filename2[:-len(".data")]

            nreplicates2, vals2, headers2 = _readVals( filename2 )
            
            for x in range(nreplicates1):
                for y in range(nreplicates2):
                    outf.write( "\t".join( (track1,
                                            headers1[x],
                                            track2,
                                            headers2[y],
                                            str( Stats.doCorrelationTest( vals1[x],
                                                                          vals2[y]) ) ) ) + "\n" )
            
            
    outf.close()

############################################################
############################################################
############################################################
##
############################################################
dataset/ETHPy150Open CGATOxford/cgat/obsolete/pipeline_vitaminD.py/buildExpressionFullCorrelation/original