Example #1
0
def main():
     Print('Merge finder starting')

     with open(JSON, 'r') as f: # Read current table data
               olddat = json.load(f)['aaData']

     ids = {}
     merged = []
     for ali in olddat:
          this = ali[3] # this = ali.id
          current = ids.get(this) # I'm assuming/hoping this is O(1), i.e. independent of the size of the dict
          if current is None: # No match for this id
               ids[this] = ali[0] # ids[this] = ali.seq # this id corresponds to this seq
          else: # found a match (i.e. a merge)
               seq = ali[0]
               if seq > current:
                    big = seq, current
               else:
                    big = current, seq
               Print(big[0], 'seems to have merged with', big[1])
               merged.append(big)

     if merged:
          try:
               email('Aliquot merge!', '\n'.join('{} seems to have merged with {}'.format(*merge) for merge in merged))
          except Exception as e:
               Print("alimerge email failed")

     Print('Merge finder finished')
Example #2
0
def main():
     err = "Error: commands are 'add', 'drop', 'send', 'update', 'print', or 'spider'"
     from sys import argv, exit
     if len(argv) < 2:
          print(err)
          exit(-1)

     get_all_reservations()
     if argv[1] == 'send':
          if len(argv[2:]) < 1: argv.append('')
          send(' '.join(argv[2:]))
     elif argv[1] == 'add':
          backup()
          db = read_db()
          if len(argv[2:]) < 2:
               Print("Error: {} add <name> <seq> [<seq>...]".format(argv[0]))
          else:
               Print("Add {} seqs".format(len(argv[3:])))
               add_db(db, argv[2], [int(seq.replace(',','')) for seq in argv[3:]])
          write_db(db)
     elif argv[1] == 'drop':
          backup()
          db = read_db()
          if len(argv[2:]) < 2:
               Print("Error: {} add <name> <seq> [<seq>...]".format(argv[0]))
          else:
               Print("Drop {} seqs".format(len(argv[3:])))
               drop_db(db, argv[2], [int(seq.replace(',','')) for seq in argv[3:]])
          write_db(db)
     elif argv[1] == 'print':
          write_db(read_db())
          with open(resfile, 'r') as f:
               for line in f:
                    print(line, end='') # Line newline + print newline = extra whitespace
     elif argv[1] == 'update':
          update()
     elif argv[1] == 'spider':
          try:
               with open(pid_file, 'r') as f:
                    last_pid = int(f.read())
          except FileNotFoundError:
               last_pid = None
          last_pid = spider(last_pid)
          with open(pid_file, 'w') as f:
               f.write(str(last_pid) + '\n')
     else:
          print(err)
          exit(-1)
     if email_msg:
          try:
               email('Reservations script warnings', email_msg)
          except Exception as e:
               Print('Email failed:', e)
               Print('Message:\n', email_msg)
Example #3
0
def inner_main(special=None):
    global error_msg
    print("\n" + strftime(datefmt))
    total = linecount(seqfile)
    if special:
        this = special
    else:
        this, start = current_update(per_hour)
    reserves, updated = get_reservations(res_post_ids)
    data, oldinfo = get_old_info(JSON, reserves, this, drop)
    Print("Init complete, starting FDB queries")

    count = 0
    for old in oldinfo:  # Loop over every sequence to be updated
        if quitting:
            data.append(old)
            continue
        ali = check(old, reserves=reserves, special=special)
        if ali:
            data.append(ali)
            if not quitting:
                count += 1
                Print(count, "sequences complete:", old.seq)
        sleep(1)

    with open(template, "r") as f:  # Read in webpage templates
        html = f.read()
    with open(template2, "r") as f:
        stats = f.read()

    # dato = {ali.seq: ali for ali in data}
    # data = [dato[seq] for seq in set(dato.keys())]
    # Now get all the stats (i.e. count all the instances of stuff)
    sizes = Counter()
    lens = Counter()
    guides = Counter()
    progs = Counter()
    cofacts = Counter()
    totsiz = 0
    totlen = 0
    avginc = 0
    totprog = 0
    txtdata = ""
    for ali in sorted(data, key=lambda ali: ali.seq):
        sizes[ali.size] += 1
        totsiz += ali.size
        lens[ali.index] += 1
        totlen += ali.index
        guides[ali.guide] += 1
        avginc += ali.index / ali.size
        progs[ali.progress] += 1
        cofacts[ali.cofact] += 1
        txtdata += str(ali)

        if isinstance(ali.progress, int):
            totprog += 1

    # Create broken sequences HTML
    if broken:
        # horizontal table: create a list of tuples containing each column (i.e. each sequence)
        entries = (
            (
                """<a href="http://factordb.com/sequences.php?se=1&aq={}&action=last20">{}</a>""".format(
                    broken[seq][1], seq
                ),
                str(broken[seq][0]),
            )
            for seq in sorted(broken)
        )
        row1, row2 = zip(*entries)  # zip converts column data into row order
        r1 = "".join("<td>{}</td>".format(datum) for datum in row1)
        r2 = "".join("<td>{}</td>".format(datum) for datum in row2)
        borken_html = '<table><tr><th scope="row">Sequence</th>{}</tr><tr><th scope="row">Index offset</th>{}</tr></table>'.format(
            r1, r2
        )
        unborken_html = ""
    else:
        borken_html = ""
        unborken_html = "none currently."

    html = html.format(
        updated, unborken_html, borken_html
    )  # Imbue the template with the reservation time and broken sequences

    # Put stats table in json-able format
    lentable = []
    lencount = 0
    sizetable = [[key, value] for key, value in sizes.items()]
    cofactable = [[key, value] for key, value in cofacts.items()]
    for leng, cnt in sorted(lens.items(), key=lambda tup: tup[0]):
        lentable.append([leng, cnt, "{:2.2f}".format(lencount / (total - cnt) * 100)])
        lencount += cnt
    guidetable = [[key, value] for key, value in guides.items()]
    progtable = [[key, value] for key, value in progs.items()]
    stats = stats.format(totinc=totlen / totsiz, avginc=avginc / total, totprog=totprog, progcent=totprog / total)

    # Write all the data and webpages
    with open(FILE, "w") as f:
        f.write(html)
    with open(TXT, "w") as f:
        f.write(txtdata)
    with open(STATS, "w") as f:
        f.write(stats)
    with open(JSON, "w") as f:
        f.write(json.dumps({"aaData": data}).replace("],", "],\n") + "\n")
    with open(STATSON, "w") as f:
        f.write(
            json.dumps(
                {
                    "aSizes": sizetable,
                    "aCofacts": cofactable,
                    "aGuides": guidetable,
                    "aProgress": progtable,
                    "aLens": lentable,
                }
            ).replace("],", "],\n")
            + "\n"
        )

    # Cleanup
    if not special:
        start += count
        if start >= total:
            start = 0
        if count != per_hour and start != 0:
            error_msg += "Something went wrong. Only {} seqs were updated.\n".format(count)
        Print("Next start is", start)
        with open(statefile, "w") as conf:  # Save how many sequences we updated
            conf.write(str(start) + "\n")

    if error_msg:
        try:
            email("Aliquot failure!", error_msg)
        except Exception as e:
            Print("Email failed:", e)
            Print("Message:\n", error_msg)

    Print("Written HTML and saved state.")