Example #1
0
def process_files(*args):
    conn = tinys3.Connection(os.environ.get('AWS_ACCESS_KEY'), os.environ.get('AWS_SECRET_ACCESS_KEY'),
                             default_bucket=os.environ.get('S3_BUCKET_NAME'))
    filename = args[0]
    url = "http://s3.amazonaws.com/{}/{}".format(os.environ.get('S3_BUCKET_NAME'), filename)
    urllib.urlretrieve(url, filename)
    raw_name = os.path.splitext(filename)[0]
    Reconcile(args)
    freqSummation(args)
    with open("{}freqFile.txt".format(raw_name)) as f:
        lines = f.readlines()

    score_list = ast.literal_eval(lines[0])
    total_freq = float(lines[1][:-2])
    total_recon = float(lines[3])
    total_cost = float(lines[2][:-2])

    scoring = args[4]
    if scoring == "Frequency":
        score_method = "Frequency"
    elif scoring == "xscape":
        score_method = "Xscape Scoring"
    else:
        score_method = "Unit Scoring"

    results_list = []
    for x, score in enumerate(score_list):
        tree = treelib1.read_tree("{}.tree".format(raw_name))
        stree = treelib1.read_tree("{}{}.stree".format(raw_name, x))
        brecon = phylo.read_brecon("{}{}.mowgli.brecon".format(raw_name, x), tree, stree)
        output = "{}{}.svg".format(raw_name, x)
        phylo.add_implied_spec_nodes_brecon(tree, brecon)
        transsvg.draw_tree(tree, brecon, stree, filename=output)
        conn.upload(output, open(output, 'rb'), content_type='image/svg+xml')

        percent = 100.0 * score / total_freq
        running_tot_score = sum(score_list[:x])
        running_tot = min(100.0 * running_tot_score / total_freq, 100)

        results_list.append((score, percent, running_tot))

    return {'results_list': results_list, 'raw_name': raw_name, 'dup': args[1], 'trans': args[2], 'loss': args[3],
            'total_cost': total_cost, 'score_method': score_method, 'total_freq': total_freq,
            'total_recon': total_recon}
def reconcile(carousel = None):
  """ Creates the results page using Master/Reconciliation and vistrans"""
  if request.method == 'POST':
    file = request.files['newick']
    if file:
      filename = secure_filename(file.filename)
      Name = filename[:-7]
      os.system("mkdir "+Name)
      f = open(filename, 'w+')
      f.write(file.read())
      f.close()
      os.system("mv "+filename+" "+ Name)
    else: return render_template("documentation.html")
    if request.form['dup'] != '':
      Dup = request.form['dup']
    else: Dup = 2
    if request.form['trans'] != '':
      Trans = request.form['trans']
    else: Trans = 3
    if request.form['loss'] != '':
      Loss = request.form["loss"]
    else: Loss = 1

    if request.form['switchhigh'] != '':
      switchHi = request.form['switchhigh']
    else: switchHi = 4.5

    if request.form['switchlow'] != '':      
      switchLo = request.form['switchlow']
    else: switchLo = 1.5

    if request.form['losshigh'] != '':
      lossHi = request.form['losshigh']
    else: lossHi = 3  

    if request.form['losslow'] != '':  
      lossLo = request.form['losslow']
    else: lossLo = 1
    path2files = Name + '/' + Name

    # os.system("python test.py")
    MasterReconciliation.Reconcile(['nothing', path2files+".newick", Dup,
Trans, Loss, request.form["scoring"], switchLo, switchHi, lossLo, lossHi])
    reconConversion.freqSummation(["nothing",path2files+".newick",Dup,
Trans, Loss,
request.form["scoring"], switchLo, switchHi, lossLo, lossHi])
    with open(path2files + "freqFile.txt") as f:
     lines = f.readlines()
    scoreList = string2List(lines[0])
    totalFreq = float(lines[1][:-2])
    totalRecon = float(lines[3])
    totalCost = float(lines[2][:-2])
    if request.form['scoring'] == "Frequency":
      scoreMethod = "Frequency"
    elif request.form['scoring'] == "xscape":
      scoreMethod = "Xscape Scoring"
    else: scoreMethod = "Unit Scoring"
    carouselstr = ""
    carouselcap= ""
    staticString = "<h4>Duplication Cost:" + str(Dup) + ", Transfer Cost: " + \
    str(Trans) + ", Loss Cost: " + str(Loss) + "<br>Maximum Parsimony Cost: " + \
    str(totalCost) +  "<br>Your scoring method: " + scoreMethod + ", Total Sum" + \
    " of Scores: " + str(totalFreq) + "<br>Total Number of Optimal " + \
    "Reconciliations: " + str(totalRecon) + "</h4>"
    for x in range(len(scoreList)):
      os.system("python vistrans.py -t " + path2files + ".tree -s " + path2files + \
        str(x) + ".stree -b " + path2files + str(x) + ".mowgli.brecon -o " + \
        path2files +  str(x) + ".svg")
  
      score = scoreList[x]
      percent = 100.0*score/totalFreq
      if x ==0:
        runningTot = percent
      
        carouselstr +='<li data-target="#results" data-slide-to="0" ' + \
        'class="active"></li>' + "\n"
      
        carouselcap +="<div class='item active'><img src='/uploads/" +  Name + \
        str(x) + ".svg' alt='First slide' width='460' height='345'>" +  \
          "<div class='carousel-caption'><font color='black'><h3>" + \
          "Reconciliation 1 of " + str(len(scoreList)) + "</h3><p>Score = " + \
          str(score) + "<br>Percent of total = " + str(percent) + "%<br>Running" + \
          " total = " + str(runningTot) + "%</font></p></div></div>" + "\n"
      
        os.system("cp /Users/Annalise/GitHub/CompBioSummer2015/" + \
          str(path2files) + str(x) + '.svg ' + UPLOAD_FOLDER)
      else:
        runningTotScore = runningTotal(scoreList, x)
        runningTot = 100.0*runningTotScore/totalFreq
        if runningTot > 100:
          runningTot = 100
        carouselstr +='<li data-target="#results" data-slide-to="' + str(x) + \
        '"></li>' + "\n"
        carouselcap +="<div class='item'><img src='/uploads/" +  Name + str(x) + \
        ".svg' alt='First slide' width='460' height='345'>" + \
         "<div class='carousel-caption'><font color='black'><h3>" + \
         "Reconciliation " + str(x + 1) + " of " + str(len(scoreList)) + \
         "</h3><p>Score = " +  str(score) + " <br>Percent of total = " + \
         str(percent) + "%<br>Running total = " + str(runningTot) + \
         "%</font></p></div></div>" + "\n"
        os.system("cp " + path2files +  str(x) + '.svg ' + UPLOAD_FOLDER)
  staticString = Markup(staticString)  
  carouselstr = Markup(carouselstr)   
  carouselcap = Markup(carouselcap)
  os.system("rm -r " + Name)
  return render_template("results.html", carouselstr = carouselstr, \
    carouselcap = carouselcap, staticString = staticString)