###ExonArray
#Copyright 2005-2008 J. David Gladstone Institutes, San Francisco California
#Author Nathan Salomonis - nsalomonis@gmail.com

#Permission is hereby granted, free of charge, to any person obtaining a copy 
#of this software and associated documentation files (the "Software"), to deal 
#in the Software without restriction, including without limitation the rights 
#to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
#copies of the Software, and to permit persons to whom the Software is furnished 
#to do so, subject to the following conditions:

#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
#INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 
#PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 
#HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 
#OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 
#SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

import sys,string,os
sys.path.insert(1, os.path.join(sys.path[0], '..')) ### import parent dir dependencies
import os.path
import unique
import time
import export

def filepath(filename):
    fn = unique.filepath(filename)
    return fn

def read_directory(sub_dir):
    dir_list = unique.read_directory(sub_dir)
    #add in code to prevent folder names from being included
    dir_list2 = []
    for file in dir_list:
        if '.txt' in file: dir_list2.append(file)
    return dir_list2

################# Begin Analysis

def cleanUpLine(line):
    line = string.replace(line,'\n','')
    line = string.replace(line,'\c','')
    data = string.replace(line,'\r','')
    data = string.replace(data,'"','')
    return data

def importAnnotations(filename):
    firstLine = True
    fn = filepath(filename)
    rows = 0
    for line in open(fn,'rU').xreadlines():             
        data = cleanUpLine(line);
        tab_delimited_data = string.split(data,'\t')
        if rows > 10: sys.exit()
        print tab_delimited_data#;sys.exit()
        rows+=1

def correlateMethylationData(filename,betaLow=0.4,betaHigh=0.6,counts=-1):
    ### Takes a filtered pre-processed beta-value file as input
    firstLine = True
    rows=0; filtered=0
    for line in open(filename,'rU').xreadlines():             
        data = cleanUpLine(line);
        t = string.split(data,'\t')
        if firstLine:
            header = t
            if len(t)>5 and 'Illumina_name' in header:
                delimiter = -50
                annot_export_object.write(string.join([t[0]]+t[delimiter:],'\t')+'\n')
            else:
                delimiter = len(header)
            headers = t[1:delimiter]
            firstLine = False
            export_object.write(string.join([t[0]]+headers,'\t')+'\n')
        else:
            probeID = t[0]
            #try: beta_values = map(float,t[1:50])
            beta_values = map(lambda x: conFloat(x,t[1:delimiter]),t[1:delimiter])
            if '' in beta_values:
                print beta_values;sys.exit()
            high = sum(betaHighCount(x,betaHigh) for x in beta_values)
            low = sum(betaLowCount(x,betaLow) for x in beta_values)
    

def importMethylationData(filename,betaLow=0.4,betaHigh=0.6,counts=-1, filter=None):
    annot_file = filepath('AltDatabase/ucsc/Hs/Illumina_methylation_genes.txt')
    export_object = open(filename[:-4]+'-filtered.txt','w')
    
    print filename[:-4]+'-filtered.txt', counts
    firstLine = True
    rows=0; filtered=0
    for line in open(filename,'rU').xreadlines():             
        data = cleanUpLine(line);
        t = string.split(data,'\t')
        #export_object.write(string.join(t,'\t')+'\n')
        #"""
        if firstLine:
            header = t
            if len(t)>5 and 'Illumina_name' in header:
                delimiter = -50
                annot_export_object = open(annot_file,'w')
                annot_export_object.write(string.join([t[0]]+t[delimiter:],'\t')+'\n')
            else:
                delimiter = len(header)
            headers = t[1:delimiter]
            firstLine = False
            export_object.write(string.join([t[0]]+headers,'\t')+'\n')
        else:
            probeID = t[0]
            #try: beta_values = map(float,t[1:50])
            beta_values = map(lambda x: conFloat(x,t[1:delimiter]),t[1:delimiter])
            if '' in beta_values:
                print beta_values;sys.exit()
            high = sum(betaHighCount(x,betaHigh) for x in beta_values)
            low = sum(betaLowCount(x,betaLow) for x in beta_values)
            #if rows<50: print high, low, max(beta_values), min(beta_values)
            #else:sys.exit()
            #export_object.write(string.join(t[:delimiter])+'\n')
            if high>=counts and low>=counts:
            #if (high-low) > 0.2:
                #if rows<50: print 1
                if filter!=None:
                    if probeID in filter: proceed=True; probeID = str(filter[probeID])+':'+probeID
                    else: proceed = False
                else: proceed = True
                if proceed:
                    filtered+=1
                    export_object.write(string.join([probeID]+map(str,beta_values),'\t')+'\n')
            if 'Illumina_name' in header:
                annot_export_object.write(string.join([t[0]]+t[delimiter:],'\t')+'\n')
            rows+=1
        #"""
    export_object.close()
    if delimiter == '-50':
        annot_export_object.close()
    print filtered, rows

def conFloat(x,betaValues):
    try: x = float(x)
    except Exception:  x=None
    if x== None or x == 0:
        floats=[]
        for i in betaValues:
            if i=='': pass
            elif float(i)==0: pass
            else: floats.append(float(i))
        try: return min(floats)
        except Exception: print betaValues;sys.exit()
    else:
        return x

def betaHighCount(x,betaHigh):
    if x>betaHigh:
        return 1
    else: return 0
    
def betaLowCount(x,betaLow):
    if x<betaLow:
        return 1
    else: return 0

def getIDsFromFile(filename):
    filterIDs = {}
    fn = filepath(filename)
    for line in open(fn,'rU').xreadlines():      
        data = cleanUpLine(line);