#!/usr/bin/python
# File created on Nov 27 Jan 2012
from __future__ import division
import threading
import time 
from multiprocessing import Process

__author__ = "Kishori M Konwar, Niels W. Hanson"
__copyright__ = "Copyright 2013, MetaPathways"
__credits__ = ["r"]
__version__ = "1.0"
__maintainer__ = "Kishori M Konwar"
__status__ = "Release"

try:
     import re
     import sys
     from optparse import OptionParser, OptionGroup
     import traceback
except:
     print """ Could not load some user defined  module functions"""
     print """ Make sure your typed \"source MetaPathwayrc\""""
     print """ """
     sys.exit(3)

script_name = "format_cog_names.py"
usage= script_name + """ --product product_file --subsystems subsystems_file -o output"""
parser = OptionParser(usage)

parser.add_option( "--products", dest="products",  
                  help='the product file')

parser.add_option( "--subsystems", dest="subsystems",  
                  help='the subsystem file')

parser.add_option( "-o", dest="output_file",  
                  help='the output file')

parser.add_option( "-N", dest="numthreads",  type = 'int',
                  help='the number of threads')



def fprintf(file, fmt, *args):
    file.write(fmt % args)



def check_arguments(opts, args):
    if opts.products == None:
         print """Must have the \"products\" file"""
         return False

    if opts.subsystems == None:
         print """Must have the \"subsystems\" file"""
         return False

    if opts.output_file == None:
         print """Must have an output file"""
         return False

    return True


def split_attributes(str, attributes):
     rawattributes = re.split(';', str)
     for attribStr in rawattributes:
        insert_attribute(attributes, attribStr)

     return attributes


def create_dict_from_list(list, dictionary):
    for word in list:
      dictionary[word] = True
   

stopwords_list= ["a", "able", "about", "across", "after", "all", "almost", "also", "am",\
                  "among", "an", "and", "any", "are", "as", "at", "be",\
                  "because", "been", "but", "by", "can", "cannot", "could", "dear", "did",\
                  "do", "does", "either", "else", "ever", "every", "for", "from", "get",\
                  "got", "had", "has", "have", "he", "her", "hers", "him", "his", "how", "however",\
                   "i", "if", "in", "into", "is", "it", "its", "just", "least", "let", "like",\
                   "likely", "may", "me", "might", "most", "must", "my", "neither", "no",\
                   "nor", "not", "of", "off", "often", "on", "only" , "or", "other", "our",\
                   "own", "rather", "said", "say", "says", "she", "should", "since", "so", "some",\
                   "than", "that", "the", "their", "them", "then", "there", "these", "they",\
                   "this", "tis", "to", "too", "twas", "us", "wants", "was", "we", "were", "what",\
                   "when", "where", "which", "while", "who", "whom", "why", "will", "with",\
                   "would", "yet", "you", "your", "enzyme", "hypothetical", "protein"]

def remove_repeats(filtered_words):
    word_dict = {}
    newlist = []
    for word in filtered_words:
       if not word in word_dict:
           word_dict[word]=1
           newlist.append(word)
    return  newlist

def remove_stop_words(list):
    newlist = []
    for item in list:
      if not item  in stopwords_list:
         newlist.append(item)
    return newlist

def format_product(product):
    product0 = re.sub(r'\[.+?\]', '', product)
    product1 = re.sub(r'\(', '', product0)
    product2 = re.sub(r'\)', '', product1)
    product3 = re.sub(r':', '', product2)
    product4 = re.sub(r'\/', '', product3)

    product_end = product4
 #######   subproducts = re.split('\/', product0)
    list = create_words_list(product_end) 

    dict =  create_dictionary_from_list(list)
    return dict


def create_dictionary_from_list(list):
    dict = {}
    for item in list:
       dict[item] = 1;
    return dict

   
def create_words_list(string):
     Splits = re.split(' ', string)
     list = []
     for Split in Splits:
        smallsplits = Split.split(',') 
        list +=smallsplits

     list = remove_repeats(list)
     list = remove_stop_words(list)
     
     list_final = [] 
     for item in list:
        if len(item):  
          list_final.append(item)
     return list_final  


def process_products_file(output_file, t,  N):
       outputfile = open( output_file,'w')
       seq_beg_pattern = re.compile(">")
       stopwords = {}
       create_dict_from_list(stopwords_list, stopwords)
       count = 0