<commit_before>"""Python wrapper for DAPPLE"""
__version__ = "0.0.1"
import urllib, urllib2
import time
import os
import argparse
import sys

#TODO: Try BeautifulSoup instead of the splits

def create_parser():
    parser = argparse.ArgumentParser(formatter_class=
                                     argparse.RawTextHelpFormatter)
    parser.add_argument('-e', '--email', nargs=1)
    parser.add_argument('-d', '--description', nargs=1)
    parser.add_argument('-f', '--snpfile', nargs=1)
    parser.add_argument('-w', '--wait', nargs='?', default=1,
                        help='Check status page every -w minutest.\n'
                        + 'Longer wait times suggested for larger datasets.\n'
                        'Default is 1 minute.')
    parser.add_argument('-p', '--permutations', nargs='?', default=1000,
                        help='Number of permutations to run. Default is 1000')
    parser.add_argument('-g', '--genome', nargs='?', default='19',
                        help='Genome Assembly Options:\n19: Hg19/HapMap\n'
                        + '18: Hg18/HapMap\n1kg: Hg19/1000Genomes SNPs')
    parser.add_argument('-c', '--ci_cutoff', nargs='?', default=2,
                        help='Common Interactor Binding Degree Cuttoff:\n'
                        +'Options:2-10\nDefault:2')
    parser.add_argument('-i', '--input', nargs=1, help='Specify the type '
                        +'of input.S: SNP\nR: Region\nC: Combination\n'+
                        'GR: Gene-Region\nG: Gene\nDefault is Gene',
                        default='G')
    parser.add_argument('-us', '--upstream', nargs='?', default=50,
                        help='Define gene regulatory region. Can only be '
                        + 'used with SNP and region input. In kb')
    parser.add_argument('-ds', '--downstream', nargs='?', default=50,
                        help='Define gene regulatory region. Can only be '
                        + 'with SNP and region input. In kb')
    parser.add_argument('-n', '--nearest', action='store_true',
                        help='Use nearest gene for SNP input, instead of all '
                        + 'genes in the chosen region')
    parser.add_argument('-gs', '--gene_specified', nargs='?',
                        help='Genes to specify as causal')
    parser.add_argument('-pl', '--plot', help='Plot the network',
                        action='store_true')
    parser.add_argument('-cp', '--color_plot', help='Color plot by p-value',
                        action='store_true')
    parser.add_argument('-s', '--simplify_plot', help='Simplify plot',
                        action='store_true')
    parser.add_argument('-z', '--zoom_to_gene',
                        help='Create a subplot with only the genes specified')
    parser.add_argument('-v', '--version', action='version',
                        version='%(prog)s ' + __version__)

    return parser

def create_request(args):
    if args.snpfile != None:
        filename = args.snpfile[0]
    else:
        print 'You need to provide a input file'
        sys.exit()

    if args.description == None:
        print 'You need to provide a description'
        sys.exit()

    if args.email == None:
        print 'You need to provide an email'
        sys.exit()

    cutoff = int(args.ci_cutoff)

    check_args(args.genome, cutoff, args.nearest, args.gene_specified,
               args.input)

    args.plot = change_true(args.plot)
    args.simplify_plot = change_true(args.simplify_plot)
    args.color_plot = change_true(args.color_plot)
    args.nearest = change_true(args.nearest)

    zoomed_genes = get_zoomed_genes(args.zoom_to_gene)

    snps = get_snps(filename)

    specified_genes = get_specified_genes(args.gene_specified)

    if not args.nearest:
        args.nearest = ''

#    page = 'http://www.broadinstitute.org/mpg/dapple/dappleTMP.php'
    raw_params = {'genome':args.genome, 'numberPermutations':args.permutations,
                  'CIcutoff':cutoff, 'regUp':args.upstream,
                  'regDown':args.downstream, 'nearestgene':args.nearest,
                  'snpListFile':'filename=""', 'snpList':snps,
                  'genesToSpecify':specified_genes, 'plot':args.plot,
                  'plotP':args.color_plot, 'collapseCI':args.simplify_plot,
                  'zoomedGenes':zoomed_genes, 'email':args.email[0],
                  'description':args.description[0], 'submit':'submit'}
    params = urllib.urlencode(raw_params)
    print params
#    request = urllib2.Request(page, params)
#    return send_parameters(request, args.wait, args.description[0])
    return (params, args.wait, args.description[0])

def get_results(link, description):
    """Saves results when finished"""
    try:
        status_page = urllib2.urlopen(link)
        status_page_list = status_page.read().split("\n")
    except urllib2.URLError:
        #time.sleep(wait*60)
#        get_results(link, description)
        return (False, None)

    for status_line in status_page_list:
        if '_summary' in status_line:
            temp = status_line.split(">")
            #make_dir = 'mkdir ' + description
            #os.system(make_dir)
            #time.sleep(60)
            commands = []
            for tag in temp:
                if 'http:' in tag:
                    outfile = tag.split("/")[-1].split("\"")[0]
                    outfile = description+'/'+ outfile
                    command = 'curl ' + tag.split("\"")[1] + ' -o ' + outfile
                    available = False
                    while not available:
                        #TODO: Add try counter
                        try:
                            urllib2.urlopen(tag.split("\"")[1])
                            available = True
                        except urllib2.HTTPError, err:
                            print err
                            print 'Oops, 404'
                            time.sleep(30)
                    commands.append(command)
                    #os.system(command)
                    #print command
                    
    return (True, commands)
def check_status(link, wait, description):
    """Updates status of run"""
    try:
        status_page = urllib2.urlopen(link)
        status_page_list = status_page.read().split("\n")
    except urllib2.URLError:<commit_msg>Added return for 404 in get_results<commit_after>"""Python wrapper for DAPPLE"""
__version__ = "0.0.1"
import urllib, urllib2
import time
import os
import argparse
import sys

#TODO: Try BeautifulSoup instead of the splits

def create_parser():
    parser = argparse.ArgumentParser(formatter_class=
                                     argparse.RawTextHelpFormatter)
    parser.add_argument('-e', '--email', nargs=1)
    parser.add_argument('-d', '--description', nargs=1)
    parser.add_argument('-f', '--snpfile', nargs=1)
    parser.add_argument('-w', '--wait', nargs='?', default=1,
                        help='Check status page every -w minutest.\n'
                        + 'Longer wait times suggested for larger datasets.\n'
                        'Default is 1 minute.')
    parser.add_argument('-p', '--permutations', nargs='?', default=1000,
                        help='Number of permutations to run. Default is 1000')
    parser.add_argument('-g', '--genome', nargs='?', default='19',
                        help='Genome Assembly Options:\n19: Hg19/HapMap\n'
                        + '18: Hg18/HapMap\n1kg: Hg19/1000Genomes SNPs')
    parser.add_argument('-c', '--ci_cutoff', nargs='?', default=2,
                        help='Common Interactor Binding Degree Cuttoff:\n'
                        +'Options:2-10\nDefault:2')
    parser.add_argument('-i', '--input', nargs=1, help='Specify the type '
                        +'of input.S: SNP\nR: Region\nC: Combination\n'+
                        'GR: Gene-Region\nG: Gene\nDefault is Gene',
                        default='G')
    parser.add_argument('-us', '--upstream', nargs='?', default=50,
                        help='Define gene regulatory region. Can only be '
                        + 'used with SNP and region input. In kb')
    parser.add_argument('-ds', '--downstream', nargs='?', default=50,
                        help='Define gene regulatory region. Can only be '
                        + 'with SNP and region input. In kb')
    parser.add_argument('-n', '--nearest', action='store_true',
                        help='Use nearest gene for SNP input, instead of all '
                        + 'genes in the chosen region')
    parser.add_argument('-gs', '--gene_specified', nargs='?',
                        help='Genes to specify as causal')
    parser.add_argument('-pl', '--plot', help='Plot the network',
                        action='store_true')
    parser.add_argument('-cp', '--color_plot', help='Color plot by p-value',
                        action='store_true')
    parser.add_argument('-s', '--simplify_plot', help='Simplify plot',
                        action='store_true')
    parser.add_argument('-z', '--zoom_to_gene',
                        help='Create a subplot with only the genes specified')
    parser.add_argument('-v', '--version', action='version',
                        version='%(prog)s ' + __version__)

    return parser

def create_request(args):
    if args.snpfile != None:
        filename = args.snpfile[0]
    else:
        print 'You need to provide a input file'
        sys.exit()

    if args.description == None:
        print 'You need to provide a description'
        sys.exit()

    if args.email == None:
        print 'You need to provide an email'
        sys.exit()

    cutoff = int(args.ci_cutoff)

    check_args(args.genome, cutoff, args.nearest, args.gene_specified,
               args.input)

    args.plot = change_true(args.plot)
    args.simplify_plot = change_true(args.simplify_plot)
    args.color_plot = change_true(args.color_plot)
    args.nearest = change_true(args.nearest)

    zoomed_genes = get_zoomed_genes(args.zoom_to_gene)

    snps = get_snps(filename)

    specified_genes = get_specified_genes(args.gene_specified)

    if not args.nearest:
        args.nearest = ''

#    page = 'http://www.broadinstitute.org/mpg/dapple/dappleTMP.php'
    raw_params = {'genome':args.genome, 'numberPermutations':args.permutations,
                  'CIcutoff':cutoff, 'regUp':args.upstream,
                  'regDown':args.downstream, 'nearestgene':args.nearest,
                  'snpListFile':'filename=""', 'snpList':snps,
                  'genesToSpecify':specified_genes, 'plot':args.plot,
                  'plotP':args.color_plot, 'collapseCI':args.simplify_plot,
                  'zoomedGenes':zoomed_genes, 'email':args.email[0],
                  'description':args.description[0], 'submit':'submit'}
    params = urllib.urlencode(raw_params)
    print params
#    request = urllib2.Request(page, params)
#    return send_parameters(request, args.wait, args.description[0])
    return (params, args.wait, args.description[0])

def get_results(link, description):
    """Saves results when finished"""
    try:
        status_page = urllib2.urlopen(link)
        status_page_list = status_page.read().split("\n")
    except urllib2.URLError:
        #time.sleep(wait*60)
#        get_results(link, description)
        return (False, None)

    for status_line in status_page_list:
        if '_summary' in status_line:
            temp = status_line.split(">")
            #make_dir = 'mkdir ' + description
            #os.system(make_dir)
            #time.sleep(60)
            commands = []
            for tag in temp:
                if 'http:' in tag:
                    outfile = tag.split("/")[-1].split("\"")[0]
                    outfile = description+'/'+ outfile
                    command = 'curl ' + tag.split("\"")[1] + ' -o ' + outfile
                    available = False
                    while not available:
                        #TODO: Add try counter
                        try:
                            urllib2.urlopen(tag.split("\"")[1])
                            available = True
                        except urllib2.HTTPError, err:
                            print err
                            print 'Oops, 404'
                            #time.sleep(30)
                            return (False, None)
                    commands.append(command)
                    #os.system(command)
                    #print command
                    
    return (True, commands)
def check_status(link, wait, description):
    """Updates status of run"""
    try:
        status_page = urllib2.urlopen(link)
        status_page_list = status_page.read().split("\n")
    except urllib2.URLError:
{'fraction_non_alphanumeric': 0.128, 'fraction_numerical': 0.011058823529411765, 'mean_word_length': 1.9012514220705348, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '852238', 'n_tokens_mistral': 3891, 'n_tokens_neox': 3615, 'n_words': 813}