def gen_nexttraining():
    message = ""
    ppl_training = open_file(f"{date.today()}.train")
    now = datetime.now()
    now_hhmm_s = timestring(now.hour, now.minute)
    now_hhmm = datetime.strptime(now_hhmm_s, '%H:%M')
    if ppl_training:
        next_time = min([
            datetime.strptime(item['when'], '%H:%M') for item in ppl_training
        ])
        next_time_s = timestring(next_time.hour, next_time.minute)
        if next_time.time() > now_hhmm.time():
            next_training = next(item for item in ppl_training
                                 if item['when'] == next_time_s)
            message = f"Next training is @ {next_training['where']} at {next_training['when']} with {next_training['who']}."
    if not message:
        ppl_training_tm = open_file(
            f"{date.today() + timedelta(days=1)}.train")
        if ppl_training_tm:
            next_time = min([
                datetime.strptime(item['when'], '%H:%M')
                for item in ppl_training_tm
            ])
            next_time_s = timestring(next_time.hour, next_time.minute)
            next_training = next(item for item in ppl_training_tm
                                 if item['when'] == next_time_s)
            message = f"Next training is tomorrow @ {next_training['where']} at {next_training['when']} with {next_training['who']}."
    if not message:
        message = "There are no trainings left today or tomorrow :("
    return message
def gen_votes():
    message = ""
    votes_fm = open_file("FM.train")
    votes_tn = open_file("TN.train")
    for votes, event in zip([votes_fm, votes_tn], ["FM", "TN"]):
        if votes:
            message += f"Votes for next {event}:\n"
            for item in votes:
                message += f"{item['who']}: {item['where']}\n"
        else:
            message += f"No votes for next {event} :( \n\n"
    return message
def gen_rm_vote_0(username):
    message = f"Deleting all current votes for {username}...\n"
    voted_fm = open_file("FM" ".train")
    voted_tn = open_file("TN" ".train")
    for votes, event in zip([voted_fm, voted_tn], ["FM", "TN"]):
        if username in [item['who'] for item in votes]:
            old_vote = next(item for item in votes if item["who"] == username)
            message += f"{username} has taken back their vote for {old_vote['event']} ({old_vote['where']}).\n"
            votes.remove(old_vote)
            write_file(f"{event}.train", votes)
    if message == f"Deleting all current votes for {username}...\n":
        message = "You have no vote to delete."
    return message
Esempio n. 4
0
def serial_file_setup():  #{{{
    """ setup the file to store the serial data into """

    if len(sys.argv) > 1 and (sys.argv[1] == "-n" or sys.argv[1] == "-N"):
        fhser = helper.open_file(cfg.TMP_DIR + \
                '/serial-akkumatik.dat', 'w+b')
        cfg.FLOG.write("%s opened (new or create binary)" % cfg.TMP_DIR + \
                '/serial-akkumatik.dat\n')
    else:
        fhser = helper.open_file(cfg.TMP_DIR + '/serial-akkumatik.dat', 'ab')
        cfg.FLOG.write("%s opened (new or create binary)" % cfg.TMP_DIR + \
                '/serial-akkumatik.dat\n')

    return fhser
Esempio n. 5
0
    def __init__(self):
        self.data_dir = get_user_data_dir()
        self.data_dir = os.path.join(self.data_dir, 'clipon')
        self.data_file = os.path.join(self.data_dir, 'history.txt')
        self.data_fd = helper.open_file(self.data_dir, self.data_file, 'r+')
        if self.data_fd is None:
            raise Exception("Failed to open data file")

        self.meta_file = os.path.join(self.data_dir, 'clipon.xml')
        self.meta_fd = helper.open_file(self.data_dir, self.meta_file, 'r+')
        if self.meta_fd is None:
            raise Exception("Failed to open meta file")

        self.meta_man = MetaManager(self.meta_file)
def serial_file_setup(): #{{{
    """ setup the file to store the serial data into """

    if len(sys.argv) > 1 and (sys.argv[1] == "-n" or sys.argv[1] == "-N"):
        fhser = helper.open_file(cfg.TMP_DIR + \
                '/serial-akkumatik.dat', 'w+b')
        cfg.FLOG.write("%s opened (new or create binary)" % cfg.TMP_DIR + \
                '/serial-akkumatik.dat\n')
    else:
        fhser = helper.open_file(cfg.TMP_DIR + '/serial-akkumatik.dat', 'ab')
        cfg.FLOG.write("%s opened (new or create binary)" % cfg.TMP_DIR + \
                '/serial-akkumatik.dat\n')

    return fhser
def gen_join(username):
    train = open_file("tmp.train")
    parent = train['who']
    day = ""
    if train['day'] != date.today():
        day = "tomorrow "
    message = f"{username} joined {parent} {day}@ {train['where']}, {train['when']}."
    ppl_training = open_file(train['day'])
    ppl_training.append({
        'who': username,
        'where': train['where'],
        'when': train['when']
    })
    write_file(train['day'], ppl_training)
    return message
def gen_whotraining():
    message = ""
    ppl_training = open_file(f"{date.today()}.train")
    ppl_training_tm = open_file(f"{date.today() + timedelta(days=1)}.train")
    if ppl_training or ppl_training_tm:
        for ppl, day in zip([ppl_training, ppl_training_tm],
                            ["today", "tomorrow"]):
            if not ppl:
                message += f"\nNobody is training {day} :(\n"
            else:
                message += f"\nPeople training {day}:\n"
                for item in ppl:
                    message += f"{item['who']} @ {item['where']}, {item['when']} \n"
    else:
        message = "There are no trainings today or tomorrow :("
    return message
Esempio n. 9
0
    def event_simple_cb(widget, event, data):
        """ general event callback function """
        if data == "para":
            (cmd1, cmd2) = akkupara_dialog()
            if not cmd1:
                return

            cfg.COMMAND_ABORT = False  #reset

            helper.akkumatik_command(cmd1, "Übertragen")
            cfg.FLOG.write("Sending Command %s \"Übertragen\"\n" % cmd1)

            if cmd2:
                time.sleep(0.6)  #else threads may get out of order somehow
                helper.akkumatik_command(cmd2, "Start")
                cfg.FLOG.write("Sending Command %s \"Start\"\n" % cmd2)
        elif data == "chart":
            ra_gnuplot.gnuplot()
            #ra_matplot.matplot()
        elif data == "recycle":
            cfg.FILE_BLOCK = True  #stop reading in files (read_line)
            cfg.FSER.close()
            #truncates old file
            cfg.FSER = helper.open_file(cfg.TMP_DIR + '/serial-akkumatik.dat',
                                        'w+b')
            cfg.FLOG.write("%s opened (new or create binary)" % \
                    cfg.TMP_DIR + '/serial-akkumatik.dat\n')
            cfg.FILE_BLOCK = False
            message_dialog(cfg.GTK_WINDOW,
                           "Alte serielle Daten wurden geloescht.")
            return

        elif data == "quit":
            gtk.main_quit()
Esempio n. 10
0
    def get_akkulist():
        """ load akkulist from harddrive """
        if os.path.exists(cfg.EXE_DIR + "/liste_akkus.dat"):
            fha = helper.open_file(cfg.EXE_DIR + "/liste_akkus.dat", "rb")
        else:
            return []

        ret = []
        for item in fha.readlines():
            tmp = item.split("\xff")
            # tmp = tmp[:-1] # remove -  last is newline
            if len(tmp) != 12:
                tmp = "Error in liste_akkus.dat - is " + str(len(tmp)) + " - should be 12"
                print(tmp)
                cfg.FLOG.write(tmp + "\n")
                continue

            # shoveling into r but with integer values now (besides of first)
            rtmp = []
            flag = True
            for xyx in tmp:
                if flag == True:
                    rtmp.append(str(xyx))
                    flag = False
                else:
                    rtmp.append(int(xyx))
            ret.append(rtmp)

        fha.close()
        return ret
Esempio n. 11
0
    def event_simple_cb(widget, event, data):
        """ general event callback function """
        if data == "para":
            (cmd1, cmd2) = akkupara_dialog()
            if not cmd1:
                return

            cfg.COMMAND_ABORT = False  # reset

            helper.akkumatik_command(cmd1, "Übertragen")
            cfg.FLOG.write('Sending Command %s "Übertragen"\n' % cmd1)

            if cmd2:
                time.sleep(0.6)  # else threads may get out of order somehow
                helper.akkumatik_command(cmd2, "Start")
                cfg.FLOG.write('Sending Command %s "Start"\n' % cmd2)
        elif data == "chart":
            ra_gnuplot.gnuplot()
            # ra_matplot.matplot()
        elif data == "recycle":
            cfg.FILE_BLOCK = True  # stop reading in files (read_line)
            cfg.FSER.close()
            # truncates old file
            cfg.FSER = helper.open_file(cfg.TMP_DIR + "/serial-akkumatik.dat", "w+b")
            cfg.FLOG.write("%s opened (new or create binary)" % cfg.TMP_DIR + "/serial-akkumatik.dat\n")
            cfg.FILE_BLOCK = False
            message_dialog(cfg.GTK_WINDOW, "Alte serielle Daten wurden geloescht.")
            return

        elif data == "quit":
            gtk.main_quit()
Esempio n. 12
0
    def get_akkulist():
        """ load akkulist from harddrive """
        if os.path.exists(cfg.EXE_DIR + "/liste_akkus.dat"):
            fha = helper.open_file(cfg.EXE_DIR + "/liste_akkus.dat", "rb")
        else:
            return []

        ret = []
        for item in fha.readlines():
            tmp = item.split('\xff')
            #tmp = tmp[:-1] # remove -  last is newline
            if len(tmp) != 12:
                tmp = "Error in liste_akkus.dat - is " +\
                        str(len(tmp)) + " - should be 12"
                print(tmp)
                cfg.FLOG.write(tmp + '\n')
                continue

            #shoveling into r but with integer values now (besides of first)
            rtmp = []
            flag = True
            for xyx in tmp:
                if flag == True:
                    rtmp.append(str(xyx))
                    flag = False
                else:
                    rtmp.append(int(xyx))
            ret.append(rtmp)

        fha.close()
        return ret
Esempio n. 13
0
def __main__():
    """
    main function 
    """

    try:
        bed_fname = sys.argv[1]
    except:
        print __doc__
        sys.exit(-1)

    bed_fh = helper.open_file(bed_fname)

    for line in bed_fh:
        line = line.strip("\n\r")

        if not line or line[0] in ["#"]:
            continue

        parts = line.split("\t")
        assert len(parts) >= 12, line

        rstarts = parts[-1].split(",")
        rstarts.pop() if rstarts[-1] == "" else rstarts

        exon_lens = parts[-2].split(",")
        exon_lens.pop() if exon_lens[-1] == "" else exon_lens

        if len(rstarts) != len(exon_lens):
            continue  # checking the consistency col 11 and col 12

        if len(rstarts) != int(parts[-3]):
            continue  # checking the number of exons and block count are same

        if not parts[5] in ["+", "-"]:
            parts[5] = "."  # replace the unknown strand with '.'

        # bed2gff result line
        sys.stdout.write(
            "%s\tbed2gff\tgene\t%d\t%s\t%s\t%s\t.\tID=Gene:%s;Name=Gene:%s\n"
            % (parts[0], int(parts[1]) + 1, parts[2], parts[4], parts[5], parts[3], parts[3])
        )
        sys.stdout.write(
            "%s\tbed2gff\ttranscript\t%d\t%s\t%s\t%s\t.\tID=%s;Name=%s;Parent=Gene:%s\n"
            % (parts[0], int(parts[1]) + 1, parts[2], parts[4], parts[5], parts[3], parts[3], parts[3])
        )

        st = int(parts[1])
        for ex_cnt in range(int(parts[-3])):
            start = st + int(rstarts[ex_cnt]) + 1
            stop = start + int(exon_lens[ex_cnt]) - 1
            sys.stdout.write(
                "%s\tbed2gff\texon\t%d\t%d\t%s\t%s\t.\tParent=%s\n"
                % (parts[0], start, stop, parts[4], parts[5], parts[3])
            )

    bed_fh.close()
Esempio n. 14
0
def __main__():
    """
    main function 
    """

    try:
        bed_fname = sys.argv[1]
    except:
        print __doc__
        sys.exit(-1)

    bed_fh = helper.open_file(bed_fname)

    for line in bed_fh:
        line = line.strip('\n\r')

        if not line or line[0] in ['#']:
            continue

        parts = line.split('\t')
        assert len(parts) >= 12, line

        rstarts = parts[-1].split(',')
        rstarts.pop() if rstarts[-1] == '' else rstarts

        exon_lens = parts[-2].split(',')
        exon_lens.pop() if exon_lens[-1] == '' else exon_lens

        if len(rstarts) != len(exon_lens):
            continue  # checking the consistency col 11 and col 12

        if len(rstarts) != int(parts[-3]):
            continue  # checking the number of exons and block count are same

        if not parts[5] in ['+', '-']:
            parts[5] = '.'  # replace the unknown strand with '.'

        # bed2gff result line
        sys.stdout.write(
            '%s\tbed2gff\tgene\t%d\t%s\t%s\t%s\t.\tID=Gene:%s;Name=Gene:%s\n' %
            (parts[0], int(parts[1]) + 1, parts[2], parts[4], parts[5],
             parts[3], parts[3]))
        sys.stdout.write(
            '%s\tbed2gff\ttranscript\t%d\t%s\t%s\t%s\t.\tID=%s;Name=%s;Parent=Gene:%s\n'
            % (parts[0], int(parts[1]) + 1, parts[2], parts[4], parts[5],
               parts[3], parts[3], parts[3]))

        st = int(parts[1])
        for ex_cnt in range(int(parts[-3])):
            start = st + int(rstarts[ex_cnt]) + 1
            stop = start + int(exon_lens[ex_cnt]) - 1
            sys.stdout.write(
                '%s\tbed2gff\texon\t%d\t%d\t%s\t%s\t.\tParent=%s\n' %
                (parts[0], start, stop, parts[4], parts[5], parts[3]))

    bed_fh.close()
Esempio n. 15
0
def gen_rm_vote(username, args):
    event = args[0].upper()
    message = ""
    if event == "FM":
        voted = open_file("FM" ".train")
    elif event == "TN":
        voted = open_file("TN" ".train")
    else:
        message = "Invalid argument. Valid arguments are 'FM', 'TN'."
        voted = []
    if not message:
        if username in [item['who'] for item in voted]:
            old_vote = next(item for item in voted if item["who"] == username)
            message = f"{username} has taken back their vote for {old_vote['event']} ({old_vote['where']})."
            voted.remove(old_vote)
            write_file(f"{event}.train", voted)
        else:
            message = f"You have not yet voted for {event}."
    return message
Esempio n. 16
0
def gen_vote(username, args):
    loc, event = " ".join(args[1:]).title(), args[0].upper()
    message = ""
    voted = []
    if event == "FM":
        voted = open_file("FM" ".train")
    elif event == "TN":
        voted = open_file("TN" ".train")
    else:
        message = "Invalid argument. Valid arguments are 'FM', 'TN'."
    if not message:
        if username in [item['who'] for item in voted]:
            old_vote = next(item for item in voted if item["who"] == username)
            message = f"{username} has changed their vote for {event} to {loc}."
            voted.remove(old_vote)
        else:
            message = f"{username} has voted for the next {event} to be @ {loc}."
        voted.append({'who': username, 'where': loc, 'event': event})
        write_file(f"{event}.train", voted)
    return message
Esempio n. 17
0
 def save_akkulist():
     """ Save the current akku-list """
     fha = helper.open_file(cfg.EXE_DIR + "/liste_akkus.dat", "wb")
     for item in akkulist:
         line = ""
         for tmp in item:
             line += str(tmp) + "\xff"
         line = line[:-1]  # remove last xff
         line += "\n"
         fha.write(line)
     fha.close()
Esempio n. 18
0
 def save_akkulist():
     """ Save the current akku-list """
     fha = helper.open_file(cfg.EXE_DIR + "/liste_akkus.dat", "wb")
     for item in akkulist:
         line = ""
         for tmp in item:
             line += str(tmp) + '\xff'
         line = line[:-1]  # remove last xff
         line += "\n"
         fha.write(line)
     fha.close()
Esempio n. 19
0
def join_list(update, context):
    logging.info('join_list command called')
    query = update.callback_query
    query.answer()
    if query.data == "yeslist":
        group_id = str(query.message.chat.id)
        group_ids = open_file("groups.list")
        if group_id not in group_ids:
            group_ids.append(group_id)
            write_file("groups.list", group_ids)
            message = "I have added this group to the update list."
        else:
            message = "This group is already on the update list."
    else:
        message = "I have not added this group to the update list."
    query.edit_message_text(text=message)
Esempio n. 20
0
def gen_notraining(username, args, day=f"{date.today()}.train", daystring=""):
    ppl_training = open_file(day)
    loc, timestamp = " ".join(args[1:]).title(), parse_time(args[0])
    try:
        timestamp = parse_time(timestamp)
        datetime.strptime(timestamp, '%H:%M')
    except ValueError:
        message = "Please specify time in the format HH:MM."
    else:
        message = f"{username} is no longer training {daystring}@ {loc}, {timestamp} :("
        this_training = {'who': username, 'where': loc, 'when': timestamp}
        if this_training in ppl_training:
            ppl_training.remove(this_training)
            write_file(day, ppl_training)
        else:
            message = "Could not find the specified training."
    return message
Esempio n. 21
0
def gen_notraining_0(username,
                     day=f"{date.today()}.train",
                     daystring=" today"):
    ppl_training = open_file(day)
    user_trainings = [item for item in ppl_training if item['who'] == username]
    if len(user_trainings) > 1:
        message = f"You have multiple Trainings planned{daystring}. Please specify which one you want to delete."
    elif len(user_trainings) == 1:
        message = f"{username} is no longer training{daystring} @ {user_trainings[0]['where']}," \
                  f" {user_trainings[0]['when']}  :( "
        if user_trainings[0] in ppl_training:
            ppl_training.remove(user_trainings[0])
            write_file(day, ppl_training)
    else:
        message = "You have no training planned today. If you want to cancel a training tomorrow, you have to " \
                  "specify it manually. "
    return message
Esempio n. 22
0
 def compare_topics(self, cat_id, *args, filename='topic_5.json'):
     logging.info('building list of newest topics')
     topics = [{', '.join(map(str, name)): rec_get(dic, list(name)) for name in args} for dic in
               self.get_latest_topics(cat_id)['topic_list']['topics'] if
               dic['pinned'] is False][:10]
     old_topics = open_file(filename)
     write_file(filename, topics)
     new_topic_ids = {i['id'] for i in topics} - {i['id'] for i in old_topics}
     if new_topic_ids:
         try:
             result_list = [entry for entry in topics if
                            entry['id'] in new_topic_ids and DP.parse(entry.get('created_at')) > DP.parse(
                                old_topics[0].get('created_at'))]
         except (KeyError, IndexError):
             logging.info('no old_topics found')
             result_list = []
     else:
         result_list = []
     return result_list
Esempio n. 23
0
def gen_training(username, args, day=f"{date.today()}.train", daystring=""):
    ppl_training = open_file(day)
    loc, timestamp = " ".join(args[1:]).title(), parse_time(args[0])
    try:
        datetime.strptime(timestamp, '%H:%M')
    except ValueError:
        if day == f"{date.today()}.train":
            timestamp = timestring(datetime.now().hour, datetime.now().minute)
            loc = " ".join(args).title()
        else:
            return "Please specify time in the format HH:MM"
    message = f"{username} is training {daystring}@ {loc}, {timestamp}."
    this_training = {'who': username, 'where': loc, 'when': timestamp}
    if this_training not in ppl_training:
        ppl_training.append(this_training)
    write_file(day, ppl_training)
    this_training['day'] = f"{date.today()}.train"
    write_file("tmp.train", this_training)
    return message
Esempio n. 24
0
from helper import open_file

api_settings = {
    'forum_url': 'http://beta.parkourvienna.at',
    'api_name': 'Spotbot',
    'api_key':
    '2d6a1397fafdb361d9f4e3236d14e8cb3c1418de93883cd53cf7ad34ffe932e1'
}

bot_settings = {
    'bot_token': '1634462832:AAEUuVYbbfQ47VrcximEFkMlBp9HF1a9xog',
    'bot_chatID': open_file("groups.list")
}

route_settings = {'text_dest': "telegram"}
Esempio n. 25
0
def filesplit(): #{{{
    """Create files for gnuplot"""

    line_counter2 = 0
    file_zaehler1 = 1
    file_zaehler2 = 1
    ausgang1_part = ""
    ausgang2_part = ""
    current_time1 = 0
    previous_time1 = 0

    tmp = "\n* [Serial Splitting] ********************************************"
    print (tmp)
    cfg.FLOG.write(tmp + '\n')

    for fil in os.listdir(cfg.TMP_DIR):
        if len(fil) == 12 and fil[0:4] == "Akku":
            os.remove(cfg.TMP_DIR + "/" + fil)

    cfg.FILE_BLOCK = True #Block (on read_line) while doing stuff here
    cfg.FSER.close()

    if os.path.getsize(cfg.TMP_DIR + '/serial-akkumatik.dat') < 8000:
        #reopen (append) and return
        cfg.FSER = helper.open_file(cfg.TMP_DIR+'/serial-akkumatik.dat', 'ab')
        cfg.FILE_BLOCK = False
        return

    cfg.FSER = helper.open_file(cfg.TMP_DIR + '/serial-akkumatik.dat', 'rb')

    for line in cfg.FSER.readlines(): #get all lines in one step
        if cfg.FILE_BLOCK == True:
            cfg.FSER.close()
            #reopen
            cfg.FSER = helper.open_file(cfg.TMP_DIR+'/serial-akkumatik.dat', \
                    'ab')
            cfg.FILE_BLOCK = False #allow further getting serial adding..

        if line[0:1] == "1":

            current_time1 = int(line[2:4]) * 60 + int(line[5:7]) * 60 +\
                    int(line[8:10]) #in seconds


            if current_time1 < previous_time1:
                fname = cfg.TMP_DIR+'/Akku1-'+ "%02i" % (file_zaehler1)+'.dat'
                fh1 = helper.open_file(fname, "w+b")

                if platform.system() == "Windows":
                    ausgang1_part = ausgang1_part.replace('\xff', " ")

                fh1.write(ausgang1_part)
                fh1.close()

                tmp = "Generiert:  " + "%48s" % (fname[-47:])
                print (tmp)
                cfg.FLOG.write(tmp + '\n')

                cfg.FILE_BLOCK = False
                file_zaehler1 += 1
                ausgang1_part = line
            else:
                ausgang1_part += line

            previous_time1 = current_time1

        elif line[0:1] == "2": #"2"

            #current_time2 = int(line[2:4]) * 60 + int(line[5:7])\
            #        * 60 + int(line[8:10]) #in seconds

            line_counter2 += 1
            #only write when did not just begun
            if line[2:10] == "00:00:01" and line_counter2 > 1:
                fname = cfg.TMP_DIR+'/Akku2-'+ "%02i" % (file_zaehler2)+'.dat'
                fh2 = helper.open_file(fname, "w+b")

                if platform.system() == "Windows":
                    ausgang2_part = ausgang2_part.replace('\xff', " ")

                fh2.write(ausgang2_part)
                fh2.close()
                tmp = "Generiert:  " + "%48s" % (fname[-47:])
                print (tmp)
                cfg.FLOG.write(tmp + '\n')

                file_zaehler2 += 1
                ausgang2_part = line
                line_counter2 = 0
            else:
                ausgang2_part += line

        else:
            tmp = "\n= [Spez Line...] ========================================"
            tmp += "SPEZ: " + line
            print (tmp)
            cfg.FLOG.write(tmp + '\n')

    if len(ausgang1_part) > 0:
        fname = cfg.TMP_DIR + '/Akku1-'+ "%02i" % (file_zaehler1)+'.dat'
        fh1 = helper.open_file(fname, "w+b")

        if platform.system() == "Windows":
            ausgang1_part = ausgang1_part.replace('\xff', " ")

        fh1.write(ausgang1_part)
        fh1.close()
        tmp = "Generiert:  " + "%48s" % (fname[-47:])
        print (tmp)
        cfg.FLOG.write(tmp + '\n')
    if len(ausgang2_part) > 0:
        fname = cfg.TMP_DIR + '/Akku2-'+ "%02i" % (file_zaehler2)+'.dat'
        fh2 = helper.open_file(fname, "w+b")

        if platform.system() == "Windows":
            ausgang2_part = ausgang2_part.replace('\xff', " ")

        fh2.write(ausgang2_part)
        tmp = "Generiert:  " + "%48s" % (fname[-47:])
        print (tmp)
        cfg.FLOG.write(tmp + '\n')
Esempio n. 26
0
def gnuplot(): #{{{
    """Create charts"""

    filesplit() #delete and generate new .dat files

    gpl = Gnuplot.Gnuplot(debug=0)

    qiv_files = ""
    dir_list = os.listdir(cfg.TMP_DIR)
    dir_list.sort()
    tmp = "\n* [Gnu-Plotting] ************************************************"
    print (tmp)
    cfg.FLOG.write(tmp + '\n')

    anzbild = 0
    for fname in dir_list:
        if fname[0:4] == "Akku" and fname[4:6] == str(cfg.GEWAEHLTER_AUSGANG)+\
                "-" and fname [8:12] == ".dat":

            #byte - about 2 minutes minimum
            if os.path.getsize(cfg.TMP_DIR + "/" + fname) < 8000:
                #skip on too little data-packages
                continue

            qiv_files += cfg.CHART_DIR + "/" + fname[:-4] + ".png "
            anzbild += 1

            fhan = helper.open_file(cfg.TMP_DIR + "/" + fname, "r")
            while True: #ignore other than real data lines
                lin = fhan.readline()
                if lin[0] != "#":
                    break
            fhan.close()
            if platform.system() == "Windows":
                line_a = lin.split(" ")
            else:
                line_a = lin.split("\xff")

            phasenr = int(line_a[9])
            atyp_i = int(line_a[12])

            #titel stuff
            atyp_str = cfg.AKKU_TYP[int(line_a[12])] #Akkutyp
            prg_str = cfg.AMPROGRAMM[int(line_a[13])] #Programm
            lart_str = cfg.LADEART[int(line_a[14])] #Ladeart
            stromw_str = cfg.STROMWAHL[int(line_a[15])] #stromwahl
            stoppm_str = cfg.STOPPMETHODE[int(line_a[16])] #stromwahl
            #Stop >= 50?
            anz_zellen = int(line_a[8]) #Zellenzahl / bei Stop -> 'Fehlercode'
            anz_z = anz_zellen
            if anz_zellen >= 40: # not really needed there in the title anyway.
                anz_z_str = ""
                anz_z = -1 #unknown
            else:
                anz_z_str = str(anz_zellen) + "x"

            titel_plus = " ["+anz_z_str+atyp_str+", "+prg_str+", "+lart_str+", " \
                    +stromw_str+", "+stoppm_str+"] - "
            titel_little = " ["+anz_z_str+atyp_str+"] - "

            rangeval = -1 # stays like that when no balancer attached
            #lipo -> Balancer graph TODO what when no balancer?
            if atyp_i == 5 and len(line_a) > 19:
                fhan = helper.open_file(cfg.TMP_DIR + "/" + fname, "r")
                rangeval = get_balancer_range(fhan)
                fhan.close()

            if phasenr >= 1 and phasenr <= 5:
                titel = "LADEN" + titel_plus
                gpl('set yrange [0:*];')
            elif phasenr >= 7 and phasenr < 9:
                titel = "ENTLADEN" + titel_little
                gpl('set yrange [*:0];')
            elif phasenr == 10:
                titel = "PAUSE - Entladespannung erreicht" + titel_little
                gpl('set yrange [*:*];')
            elif phasenr == 0:
                titel = "STOP (Erhaltungladung)" + titel_plus
                gpl('set yrange [*:*];')
            else:
                titel = "Unbekannte Phase <"+str(phasenr)+">" + titel_plus
                gpl('set yrange [*:*];')

            gpl('set terminal png size 1280, 1024;')
            #gnuplot does not like MS-Windoof's \
            gpl('set output "' + (cfg.CHART_DIR).replace('\\','/') +\
            "/" + fname[:-4] + '.png"')

            gpl('set xdata time;')

            if platform.system() == "Windows":
                gpl("set datafile separator ' ';")
            else:
                gpl("set datafile separator '\xff';")

            gpl('set timefmt "%H:%M:%S";')
            gpl('set grid')

            #set bmargin 5
            gpl('set lmargin 10')
            gpl('set rmargin 10')
            #set tmargin 5

            gpl('set multiplot;')

            gpl('set key box')
            gpl('set ylabel "Laden mA / Kapazitaet mAh"')
            gpl('set ytics nomirror;')

            gpl('set y2range [-10:70];')
            gpl('set y2label "Grad Celsius";')
            gpl('set y2tics border;')

            gpl('set nolabel;')
            gpl('set xtics axis;')

            gpl('set size 1.0,0.45;')
            gpl('set origin 0.0,0.5;')


            #gnuplot does not like MS-Windoof's \
            gpl('wfile="' + cfg.TMP_DIR.replace('\\', '/') + "/" + fname + '";')
            gpl('set title "Akkumatik - ' + titel + ' (' + str(anzbild) + ')";')


            gpl('plot \
                wfile using 2:4 with lines title "mA" lw 2 lc rgbcolor\
                "#009900" , \
                wfile using 2:5 smooth bezier with lines title "mAh" lw\
                2 lc rgbcolor "#0000ff", \
                wfile using 2:8 smooth bezier with lines title "Bat C"\
                axes x1y2 lc rgbcolor "#cc0000" , \
                wfile using 2:18 smooth bezier with lines title "KK C"\
                axes x1y2 lc rgbcolor "#222222";')

            gpl('set nolabel;')
            gpl('set notitle;')

            gpl('set size 1.0,0.45;')
            gpl('set origin 0.0,0.0;')

            if atyp_i == 5:
                gpl(lipo_gnuplot(line_a, rangeval, anz_z))
            elif atyp_i == 0 or atyp_i == 1:
                gpl(nixx_gnuplot())
            else:
                gpl(else_gnuplot())

            gpl('set nomultiplot;')
            gpl('reset')
            tmp = "Generiert:  "+"%44s" % (cfg.CHART_DIR + "/"+\
                    fname[-27:-4])+".png"
            print (tmp)
            cfg.FLOG.write(tmp + '\n')
        else:
            continue

    if len(qiv_files) > 0:
        time.sleep(1.8) #sonst finded qiv (noch) nichts allenfalls
        args = shlex.split(qiv_files)
        arguments = ' '.join(str(n) for n in args)
        if platform.system() == "Windows":
            for xtmp in args[-1:]:
                # os.startfile(xtmp)
                thread.start_new_thread(os.startfile,(xtmp,))
                break #one (letzte) is enough for eg. irfanview
        else:
            thread.start_new_thread(os.system,(cfg.PICTURE_EXE+' '+arguments,))
    else:
        tmp = "Nicht genügend serielle Daten vorhanden\n"
        tmp += "(Etwa 2 min von der selben 'Phase' werden benoetigt.)"
        gtk_stuff.message_dialog(cfg.GTK_WINDOW, tmp)
Esempio n. 27
0
def Parse(ga_file):
    """
    Parsing GFF/GTF file based on feature relationship, it takes the input file.
    @args ga_file: input file name 
    @type ga_file: str 
    """
    child_map = defaultdict(list)
    parent_map = dict()

    ga_handle = utils.open_file(ga_file)

    for rec in ga_handle:
        rec = rec.strip('\n\r')

        # skip empty line fasta identifier and commented line
        if not rec or rec[0] in ['#', '>']:
            continue
        # skip the genome sequence
        if not re.search('\t', rec):
            continue

        parts = rec.split('\t')
        assert len(parts) >= 8, rec

        # process the attribute column (9th column)
        ftype, tags = attribute_tags(parts[-1])
        if not tags:  # skip the line if no attribute column.
            continue

        # extract fields
        if parts[1]:
            tags["source"] = parts[1]
        if parts[7]:
            tags["phase"] = parts[7]

        gff_info = dict()
        gff_info['info'] = dict(tags)
        gff_info["is_gff3"] = ftype
        gff_info['chr'] = parts[0]
        gff_info['score'] = parts[5]

        if parts[3] and parts[4]:
            gff_info['location'] = [int(parts[3]), int(parts[4])]
            gff_info['type'] = parts[2]
            gff_info['id'] = tags.get('ID', [''])[0]
            if parts[6] in ['?', '.']:
                parts[6] = None
            gff_info['strand'] = parts[6]

            # key word according to the GFF spec.
            # is_gff3 flag is false check this condition and get the attribute fields
            if not ftype:
                gff_info = spec_features_keywd(gff_info)

            # link the feature relationships
            if gff_info['info'].has_key('Parent'):
                for p in gff_info['info']['Parent']:
                    if p == gff_info['id']:
                        gff_info['id'] = ''
                        break
                rec_category = 'child'
            elif gff_info['id']:
                rec_category = 'parent'
            else:
                rec_category = 'record'

            # depends on the record category organize the features
            if rec_category == 'child':
                for p in gff_info['info']['Parent']:
                    # create the data structure based on source and feature id
                    child_map[(gff_info['chr'], gff_info['info']['source'],
                               p)].append(
                                   dict(type=gff_info['type'],
                                        location=gff_info['location'],
                                        strand=gff_info['strand'],
                                        score=gff_info['score'],
                                        ID=gff_info['id'],
                                        gene_name=gff_info['info'].get(
                                            'gene_name', [''])[0],
                                        gene_id=gff_info['info'].get(
                                            'GParent', [''])[0]))
            elif rec_category == 'parent':
                parent_map[(gff_info['chr'], gff_info['info']['source'],
                            gff_info['id'])] = dict(
                                type=gff_info['type'],
                                location=gff_info['location'],
                                strand=gff_info['strand'],
                                score=gff_info['score'],
                                name=tags.get('Name', [''])[0])
            elif rec_category == 'record':
                #TODO how to handle plain records?
                considering_soon = 1
    ga_handle.close()

    # depends on file type create parent feature
    if not ftype:
        parent_map, child_map = create_missing_feature_type(
            parent_map, child_map)

    # connecting parent child relations
    # essentially the parent child features are here from any type of GTF/GFF2/GFF3 file
    gene_mat = format_gene_models(parent_map, child_map)

    return gene_mat
Esempio n. 28
0
def gbk_parse(fname):
    """
    Extract genome annotation recods from genbank format 

    @args fname: gbk file name 
    @type fname: str
    """
    fhand = helper.open_file(gbkfname)
    unk = 1

    for record in SeqIO.parse(fhand, "genbank"):
        gene_tags = dict()
        tx_tags = collections.defaultdict(list)
        exon = collections.defaultdict(list)
        cds = collections.defaultdict(list)
        mol_type, chr_id = None, None

        for rec in record.features:

            if rec.type == 'source':
                try:
                    mol_type = rec.qualifiers['mol_type'][0]
                except:
                    mol_type = '.'
                    pass
                try:
                    chr_id = rec.qualifiers['chromosome'][0]
                except:
                    chr_id = record.name
                continue

            strand = '-'
            strand = '+' if rec.strand > 0 else strand

            fid = None
            try:
                fid = rec.qualifiers['gene'][0]
            except:
                pass

            transcript_id = None
            try:
                transcript_id = rec.qualifiers['transcript_id'][0]
            except:
                pass

            if re.search(r'gene', rec.type):
                gene_tags[fid] = (rec.location._start.position + 1,
                                  rec.location._end.position, strand, rec.type)
            elif rec.type == 'exon':
                exon[fid].append((rec.location._start.position + 1,
                                  rec.location._end.position))
            elif rec.type == 'CDS':
                cds[fid].append((rec.location._start.position + 1,
                                 rec.location._end.position))
            else:
                # get all transcripts
                if transcript_id:
                    tx_tags[fid].append(
                        (rec.location._start.position + 1,
                         rec.location._end.position, transcript_id, rec.type))
        # record extracted, generate feature table
        unk = feature_table(chr_id, mol_type, strand, gene_tags, tx_tags, cds,
                            exon, unk)

    fhand.close()
Esempio n. 29
0
def gnuplot():  #{{{
    """Create charts"""

    filesplit()  #delete and generate new .dat files

    gpl = Gnuplot.Gnuplot(debug=0)

    qiv_files = ""
    dir_list = os.listdir(cfg.TMP_DIR)
    dir_list.sort()
    tmp = "\n* [Gnu-Plotting] ************************************************"
    print(tmp)
    cfg.FLOG.write(tmp + '\n')

    anzbild = 0
    for fname in dir_list:
        if fname[0:4] == "Akku" and fname[4:6] == str(cfg.GEWAEHLTER_AUSGANG)+\
                "-" and fname [8:12] == ".dat":

            #byte - about 2 minutes minimum
            if os.path.getsize(cfg.TMP_DIR + "/" + fname) < 8000:
                #skip on too little data-packages
                continue

            qiv_files += cfg.CHART_DIR + "/" + fname[:-4] + ".png "
            anzbild += 1

            fhan = helper.open_file(cfg.TMP_DIR + "/" + fname, "r")
            while True:  #ignore other than real data lines
                lin = fhan.readline()
                if lin[0] != "#":
                    break
            fhan.close()
            if platform.system() == "Windows":
                line_a = lin.split(" ")
            else:
                line_a = lin.split("\xff")

            phasenr = int(line_a[9])
            atyp_i = int(line_a[12])

            #titel stuff
            atyp_str = cfg.AKKU_TYP[int(line_a[12])]  #Akkutyp
            prg_str = cfg.AMPROGRAMM[int(line_a[13])]  #Programm
            lart_str = cfg.LADEART[int(line_a[14])]  #Ladeart
            stromw_str = cfg.STROMWAHL[int(line_a[15])]  #stromwahl
            stoppm_str = cfg.STOPPMETHODE[int(line_a[16])]  #stromwahl
            #Stop >= 50?
            anz_zellen = int(line_a[8])  #Zellenzahl / bei Stop -> 'Fehlercode'
            anz_z = anz_zellen
            if anz_zellen >= 40:  # not really needed there in the title anyway.
                anz_z_str = ""
                anz_z = -1  #unknown
            else:
                anz_z_str = str(anz_zellen) + "x"

            titel_plus = " ["+anz_z_str+atyp_str+", "+prg_str+", "+lart_str+", " \
                    +stromw_str+", "+stoppm_str+"] - "
            titel_little = " [" + anz_z_str + atyp_str + "] - "

            rangeval = -1  # stays like that when no balancer attached
            #lipo -> Balancer graph TODO what when no balancer?
            if atyp_i == 5 and len(line_a) > 19:
                fhan = helper.open_file(cfg.TMP_DIR + "/" + fname, "r")
                rangeval = get_balancer_range(fhan)
                fhan.close()

            if phasenr >= 1 and phasenr <= 5:
                titel = "LADEN" + titel_plus
                gpl('set yrange [0:*];')
            elif phasenr >= 7 and phasenr < 9:
                titel = "ENTLADEN" + titel_little
                gpl('set yrange [*:0];')
            elif phasenr == 10:
                titel = "PAUSE - Entladespannung erreicht" + titel_little
                gpl('set yrange [*:*];')
            elif phasenr == 0:
                titel = "STOP (Erhaltungladung)" + titel_plus
                gpl('set yrange [*:*];')
            else:
                titel = "Unbekannte Phase <" + str(phasenr) + ">" + titel_plus
                gpl('set yrange [*:*];')

            gpl('set terminal png size 1280, 1024;')
            #gnuplot does not like MS-Windoof's \
            gpl('set output "' + (cfg.CHART_DIR).replace('\\','/') +\
            "/" + fname[:-4] + '.png"')

            gpl('set xdata time;')

            if platform.system() == "Windows":
                gpl("set datafile separator ' ';")
            else:
                gpl("set datafile separator '\xff';")

            gpl('set timefmt "%H:%M:%S";')
            gpl('set grid')

            #set bmargin 5
            gpl('set lmargin 10')
            gpl('set rmargin 10')
            #set tmargin 5

            gpl('set multiplot;')

            gpl('set key box')
            gpl('set ylabel "Laden mA / Kapazitaet mAh"')
            gpl('set ytics nomirror;')

            gpl('set y2range [-10:70];')
            gpl('set y2label "Grad Celsius";')
            gpl('set y2tics border;')

            gpl('set nolabel;')
            gpl('set xtics axis;')

            gpl('set size 1.0,0.45;')
            gpl('set origin 0.0,0.5;')

            #gnuplot does not like MS-Windoof's \
            gpl('wfile="' + cfg.TMP_DIR.replace('\\', '/') + "/" + fname +
                '";')
            gpl('set title "Akkumatik - ' + titel + ' (' + str(anzbild) +
                ')";')

            gpl('plot \
                wfile using 2:4 with lines title "mA" lw 2 lc rgbcolor\
                "#009900" , \
                wfile using 2:5 smooth bezier with lines title "mAh" lw\
                2 lc rgbcolor "#0000ff", \
                wfile using 2:8 smooth bezier with lines title "Bat C"\
                axes x1y2 lc rgbcolor "#cc0000" , \
                wfile using 2:18 smooth bezier with lines title "KK C"\
                axes x1y2 lc rgbcolor "#222222";')

            gpl('set nolabel;')
            gpl('set notitle;')

            gpl('set size 1.0,0.45;')
            gpl('set origin 0.0,0.0;')

            if atyp_i == 5:
                gpl(lipo_gnuplot(line_a, rangeval, anz_z))
            elif atyp_i == 0 or atyp_i == 1:
                gpl(nixx_gnuplot())
            else:
                gpl(else_gnuplot())

            gpl('set nomultiplot;')
            gpl('reset')
            tmp = "Generiert:  "+"%44s" % (cfg.CHART_DIR + "/"+\
                    fname[-27:-4])+".png"
            print(tmp)
            cfg.FLOG.write(tmp + '\n')
        else:
            continue

    if len(qiv_files) > 0:
        time.sleep(1.8)  #sonst finded qiv (noch) nichts allenfalls
        args = shlex.split(qiv_files)
        arguments = ' '.join(str(n) for n in args)
        if platform.system() == "Windows":
            for xtmp in args[-1:]:
                # os.startfile(xtmp)
                thread.start_new_thread(os.startfile, (xtmp, ))
                break  #one (letzte) is enough for eg. irfanview
        else:
            thread.start_new_thread(os.system,
                                    (cfg.PICTURE_EXE + ' ' + arguments, ))
    else:
        tmp = "Nicht genügend serielle Daten vorhanden\n"
        tmp += "(Etwa 2 min von der selben 'Phase' werden benoetigt.)"
        gtk_stuff.message_dialog(cfg.GTK_WINDOW, tmp)
Esempio n. 30
0
def filesplit():  #{{{
    """Create files for gnuplot"""

    line_counter2 = 0
    file_zaehler1 = 1
    file_zaehler2 = 1
    ausgang1_part = ""
    ausgang2_part = ""
    current_time1 = 0
    previous_time1 = 0

    tmp = "\n* [Serial Splitting] ********************************************"
    print(tmp)
    cfg.FLOG.write(tmp + '\n')

    for fil in os.listdir(cfg.TMP_DIR):
        if len(fil) == 12 and fil[0:4] == "Akku":
            os.remove(cfg.TMP_DIR + "/" + fil)

    cfg.FILE_BLOCK = True  #Block (on read_line) while doing stuff here
    cfg.FSER.close()

    if os.path.getsize(cfg.TMP_DIR + '/serial-akkumatik.dat') < 8000:
        #reopen (append) and return
        cfg.FSER = helper.open_file(cfg.TMP_DIR + '/serial-akkumatik.dat',
                                    'ab')
        cfg.FILE_BLOCK = False
        return

    cfg.FSER = helper.open_file(cfg.TMP_DIR + '/serial-akkumatik.dat', 'rb')

    for line in cfg.FSER.readlines():  #get all lines in one step
        if cfg.FILE_BLOCK == True:
            cfg.FSER.close()
            #reopen
            cfg.FSER = helper.open_file(cfg.TMP_DIR+'/serial-akkumatik.dat', \
                    'ab')
            cfg.FILE_BLOCK = False  #allow further getting serial adding..

        if line[0:1] == "1":

            current_time1 = int(line[2:4]) * 60 + int(line[5:7]) * 60 +\
                    int(line[8:10]) #in seconds

            if current_time1 < previous_time1:
                fname = cfg.TMP_DIR + '/Akku1-' + "%02i" % (
                    file_zaehler1) + '.dat'
                fh1 = helper.open_file(fname, "w+b")

                if platform.system() == "Windows":
                    ausgang1_part = ausgang1_part.replace('\xff', " ")

                fh1.write(ausgang1_part)
                fh1.close()

                tmp = "Generiert:  " + "%48s" % (fname[-47:])
                print(tmp)
                cfg.FLOG.write(tmp + '\n')

                cfg.FILE_BLOCK = False
                file_zaehler1 += 1
                ausgang1_part = line
            else:
                ausgang1_part += line

            previous_time1 = current_time1

        elif line[0:1] == "2":  #"2"

            #current_time2 = int(line[2:4]) * 60 + int(line[5:7])\
            #        * 60 + int(line[8:10]) #in seconds

            line_counter2 += 1
            #only write when did not just begun
            if line[2:10] == "00:00:01" and line_counter2 > 1:
                fname = cfg.TMP_DIR + '/Akku2-' + "%02i" % (
                    file_zaehler2) + '.dat'
                fh2 = helper.open_file(fname, "w+b")

                if platform.system() == "Windows":
                    ausgang2_part = ausgang2_part.replace('\xff', " ")

                fh2.write(ausgang2_part)
                fh2.close()
                tmp = "Generiert:  " + "%48s" % (fname[-47:])
                print(tmp)
                cfg.FLOG.write(tmp + '\n')

                file_zaehler2 += 1
                ausgang2_part = line
                line_counter2 = 0
            else:
                ausgang2_part += line

        else:
            tmp = "\n= [Spez Line...] ========================================"
            tmp += "SPEZ: " + line
            print(tmp)
            cfg.FLOG.write(tmp + '\n')

    if len(ausgang1_part) > 0:
        fname = cfg.TMP_DIR + '/Akku1-' + "%02i" % (file_zaehler1) + '.dat'
        fh1 = helper.open_file(fname, "w+b")

        if platform.system() == "Windows":
            ausgang1_part = ausgang1_part.replace('\xff', " ")

        fh1.write(ausgang1_part)
        fh1.close()
        tmp = "Generiert:  " + "%48s" % (fname[-47:])
        print(tmp)
        cfg.FLOG.write(tmp + '\n')
    if len(ausgang2_part) > 0:
        fname = cfg.TMP_DIR + '/Akku2-' + "%02i" % (file_zaehler2) + '.dat'
        fh2 = helper.open_file(fname, "w+b")

        if platform.system() == "Windows":
            ausgang2_part = ausgang2_part.replace('\xff', " ")

        fh2.write(ausgang2_part)
        tmp = "Generiert:  " + "%48s" % (fname[-47:])
        print(tmp)
        cfg.FLOG.write(tmp + '\n')
Esempio n. 31
0
def Parse(ga_file):
    """
    Parsing GFF/GTF file based on feature relationship, it takes the input file.

    @args ga_file: input file name 
    @type ga_file: str 
    """
    child_map = defaultdict(list)
    parent_map = dict()

    ga_handle = utils.open_file(ga_file)

    for rec in ga_handle:
        rec = rec.strip('\n\r')
        
        # skip empty line fasta identifier and commented line
        if not rec or rec[0] in  ['#', '>']:
            continue
        # skip the genome sequence 
        if not re.search('\t', rec):
            continue

        parts = rec.split('\t')
        assert len(parts) >= 8, rec

        # process the attribute column (9th column)
        ftype, tags = attribute_tags(parts[-1])
        if not tags: # skip the line if no attribute column.
	        continue 

        # extract fields  
        if parts[1]:
            tags["source"] = parts[1]
        if parts[7]:
            tags["phase"] = parts[7]

        gff_info = dict()
        gff_info['info'] = dict(tags)
        gff_info["is_gff3"] = ftype
        gff_info['chr'] = parts[0]
        gff_info['score'] = parts[5]

        if parts[3] and parts[4]:
            gff_info['location'] = [int(parts[3]) ,
                        int(parts[4])]
            gff_info['type'] = parts[2]
            gff_info['id'] = tags.get('ID', [''])[0]
            if parts[6] in ['?', '.']:
                parts[6] = None 
            gff_info['strand'] = parts[6]

            # key word according to the GFF spec.
            # is_gff3 flag is false check this condition and get the attribute fields 
            if not ftype:
                gff_info = spec_features_keywd(gff_info)
            
            # link the feature relationships
            if gff_info['info'].has_key('Parent'): 
                for p in gff_info['info']['Parent']:
                    if p == gff_info['id']:
                        gff_info['id'] = ''
                        break
                rec_category = 'child'
            elif gff_info['id']:
                rec_category = 'parent'
            else:
                rec_category = 'record'

            # depends on the record category organize the features
            if rec_category == 'child':
                for p in gff_info['info']['Parent']:
                    # create the data structure based on source and feature id 
                    child_map[(gff_info['chr'], gff_info['info']['source'], p)].append(
                                            dict( type = gff_info['type'], 
                                            location =  gff_info['location'], 
                                            strand = gff_info['strand'], 
                                            score = gff_info['score'], 
                                            ID = gff_info['id'],
                                            gene_name = gff_info['info'].get('gene_name', [''])[0],
                                            gene_id = gff_info['info'].get('GParent', [''])[0] 
                                            ))
            elif rec_category == 'parent':
                parent_map[(gff_info['chr'], gff_info['info']['source'], gff_info['id'])] = dict( 
                                            type = gff_info['type'], 
                                            location = gff_info['location'],
                                            strand = gff_info['strand'],
                                            score = gff_info['score'], 
                                            name = tags.get('Name', [''])[0])
            elif rec_category == 'record':
                #TODO how to handle plain records?
                considering_soon = 1 
    ga_handle.close()

    # depends on file type create parent feature  
    if not ftype:
        parent_map, child_map = create_missing_feature_type(parent_map, child_map)    
    
    # connecting parent child relations  
    # essentially the parent child features are here from any type of GTF/GFF2/GFF3 file
    gene_mat = format_gene_models(parent_map, child_map) 

    return gene_mat 
Esempio n. 32
0
def Parse(ga_file):
    """
    Parsing GFF/GTF file based on feature relationship, it takes the input file.

    @args ga_file: input file name 
    @type ga_file: str 
    """
    child_map = defaultdict(list)
    parent_map = dict()

    ga_handle = utils.open_file(ga_file)

    for rec in ga_handle:
        rec = rec.strip("\n\r")

        # skip empty line fasta identifier and commented line
        if not rec or rec[0] in ["#", ">"]:
            continue
        # skip the genome sequence
        if not re.search("\t", rec):
            continue

        parts = rec.split("\t")
        assert len(parts) >= 8, rec

        # process the attribute column (9th column)
        ftype, tags = attribute_tags(parts[-1])
        if not tags:  # skip the line if no attribute column.
            continue

        # extract fields
        if parts[1]:
            tags["source"] = parts[1]
        if parts[7]:
            tags["phase"] = parts[7]

        gff_info = dict()
        gff_info["info"] = dict(tags)
        # gff_info["is_gff3"] = ftype
        gff_info["chr"] = parts[0]
        gff_info["score"] = parts[5]

        if parts[3] and parts[4]:
            gff_info["location"] = [int(parts[3]), int(parts[4])]
            gff_info["type"] = parts[2]
            gff_info["id"] = tags.get("ID", [""])[0]
            if parts[6] in ["?", "."]:
                parts[6] = None
            gff_info["strand"] = parts[6]

            # key word according to the GFF spec.
            if not ftype:
                gff_info = spec_features_keywd(gff_info)

            # link the feature relationships
            if gff_info["info"].has_key("Parent"):
                for p in gff_info["info"]["Parent"]:
                    if p == gff_info["id"]:
                        gff_info["id"] = ""
                        break
                rec_category = "child"
            elif gff_info["id"]:
                rec_category = "parent"
            else:
                rec_category = "record"

            # depends on the record category organize the features
            if rec_category == "child":
                for p in gff_info["info"]["Parent"]:
                    # create the data structure based on source and feature id
                    child_map[(gff_info["chr"], gff_info["info"]["source"], p)].append(
                        dict(
                            type=gff_info["type"],
                            location=gff_info["location"],
                            strand=gff_info["strand"],
                            score=gff_info["score"],
                            ID=gff_info["id"],
                            gene_id=gff_info["info"].get("GParent", ""),
                        )
                    )
            elif rec_category == "parent":
                parent_map[(gff_info["chr"], gff_info["info"]["source"], gff_info["id"])] = dict(
                    type=gff_info["type"],
                    location=gff_info["location"],
                    strand=gff_info["strand"],
                    score=gff_info["score"],
                    name=tags.get("Name", [""])[0],
                )
            elif rec_category == "record":
                # TODO how to handle plain records?
                c = 1
    ga_handle.close()

    # depends on file type create parent feature
    if not ftype:
        parent_map, child_map = create_missing_feature_type(parent_map, child_map)

    # connecting parent child relations
    # // essentially the parent child features are here from any type of GTF/GFF2/GFF3 file
    gene_mat = format_gene_models(parent_map, child_map)

    return gene_mat
Esempio n. 33
0
if __name__ == '__main__':
    logging.basicConfig(level=logging.INFO)
    logging.info('initializing forum connection')
    f = Forum(api_settings['forum_url'], api_settings['api_name'], api_settings['api_key'])
    logging.info('testing connectivity')
    f.check_connection()

    todayday = datetime.datetime.today().weekday()
    message = ""
    t_id = 0

    logging.info('checking day')
    topics = [dic for dic in f.get_latest_topics(5)['topic_list']['topics'] if dic['pinned'] is False][:10]
    if todayday == 2:
        votes_tn = open_file("tn.train")
        if votes_tn:
            message = "Votes for this TN:\n"
            for item in votes_tn:
                message += item['who'] + ": " + item['where'] + "\n"
        else:
            message = "No votes from Chats for this FM :( \n"
        t_id = next(item['id'] for item in topics if "Tuesdayness" in item['title'])

    if todayday == 5:
        votes_fm = open_file("fm.train")
        if votes_fm:
            message = "Votes for this FM:\n"
            for item in votes_fm:
                message += item['who'] + ": " + item['where'] + "\n"
        else:
Esempio n. 34
0
    #if lin[11:16] == "00000": #no volt lines
    #print ("FILTER OUT: Volt has Zero value")
    #return ""

    cfg.OLDTIME[int(daten[0])] = curtime

    try:
        if yeswrite:
            cfg.FSER.write(lin)
    except ValueError, err:
        tmp = "%s\n" % err
        tmp += "Sollte nicht passieren, erneutes Oeffnen sowieso\n\n"
        print(tmp)
        cfg.FLOG.write(tmp)
        gtk_stuff.message_dialog(cfg.GTK_WINDOW, tmp)
        cfg.FSER = helper.open_file(cfg.TMP_DIR + '/serial-akkumatik.dat',
                                    'ab')
        return True

    if (daten[0] == "1" and cfg.GEWAEHLTER_AUSGANG == 1) \
            or (daten[0] == "2" and cfg.GEWAEHLTER_AUSGANG == 2):
        (labstr1, labstr2) = generate_output_strs(daten)
        output_data(labstr1, labstr2)

    return True


#}}}
def serial_setup():  #{{{
    """ try to connect to the serial port """

    tmp = "* [ Serial Port ] ***********************************\n"
Esempio n. 35
0
def matplot():
    def hms_formatter(value, loc):
        h = value // 3600        
        m = (value - h * 3600) // 60
        s = value % 60              
        return "%02d:%02d:%02d" % (h,m,s)

    def to_sec(tstring):
        return int(tstring[:2])*3600 + int(tstring[3:5])*60 + int(tstring[6:8])

    qiv_files = ""
    dirList = os.listdir(cfg.TMP_DIR)
    dirList.sort()
    print "\n* [Mat-Plotting] ****************************************************"

    ip = 0
    for fname in dirList:
        if fname[0:4] == "Akku" and fname[4:6] == str(cfg..gewaehlter_ausgang) + "-" and fname [8:12] == ".dat":
            qiv_files += cfg.CHART_DIR + "/" + fname[:-4] + ".png "

            f = helper.open_file(cfg.TMP_DIR + "/" + fname, "r")
            while True: #ignore other than real data lines
                l = f.readline()
                break

            f.close()
            if platform.system() == "Windows":
                line_a = l.split(" ")
            else:
                line_a = l.split("\xff")

            phasenr = int(line_a[9])
            atyp = int(line_a[12])

            #titel stuff
            atyp_str = cfg.AKKU_TYP[int(line_a[12])] #Akkutyp
            prg = cfg.AMPROGRAMM[int(line_a[13])] #Programm
            lart = cfg.LADEART[int(line_a[14])] #Ladeart
            stromw = cfg.STROMWAHL[int(line_a[15])] #stromwahl
            stoppm = cfg.STOPPMETHODE[int(line_a[16])] #stromwahl
            #Stop >= 50?
            anz_zellen = int(line_a[8]) #Zellenzahl / bei Stop -> 'Fehlercode'
            if anz_zellen >= 40: # not really needed there in the title anyway.
                anz_z_str = ""
            else:
                anz_z_str = str(anz_zellen) + "x"

            titel_plus = " ["+anz_z_str+atyp_str+", "+prg+", "+lart+", "+stromw+", "+stoppm+"] - "
            titel_little = " ["+anz_z_str+atyp_str+"] - "

            #if atyp == 5 and len(line_a) > 19: #lipo -> Balancer graph TODO what when no balancer?
                #f = cfg.open_file(cfg.TMP_DIR + "/" + fname, "r")
                #rangeval = cfg.get_balancer_range(f)
                #f.close()

            if phasenr >= 1 and phasenr <= 5:
                titel = "LADEN" + titel_plus
            elif phasenr >= 7 and phasenr < 9:
                titel = "ENTLADEN" + titel_little
            elif phasenr == 10:
                titel = "PAUSE - Entladespannung erreicht" + titel_little
            elif phasenr == 0:
                titel = "STOP (Erhaltungladung)" + titel_plus
            else:
                titel = "Unbekannte Phase <"+str(phasenr)+">" + titel_plus

            # file to array
            fh  = cfg.open_file(cfg.TMP_DIR + "/" + fname, "r")
            total_a = []
            for thing in fh.readlines():
                if platform.system() == "Windows":
                    hm = thing.split(" ")
                else:
                    hm = thing.split("\xff")
                total_a.append(hm)
        #break
    
            t = []
            y = []
            y2 = []
            for val in total_a:
                t.append(to_sec(val[1]))
                y.append(int(val[2]))
                y2.append(int(val[3]))

            fig = plt.figure(ip)

            sp1 = fig.add_subplot(211)
            xaxis = sp1.get_xaxis()
            xaxis.set_major_formatter(matplotlib.ticker.FuncFormatter(hms_formatter))
            sp1.plot(t, y)

            sp2 = fig.add_subplot(212)
            xaxis2 = sp2.get_xaxis()
            xaxis2.set_major_formatter(matplotlib.ticker.FuncFormatter(hms_formatter))
            sp2.plot(t, y2)

            fig.canvas.draw()
            fig.show()
            ip += 1
Esempio n. 36
0
def gbk_parse(fname):
    """
    Extract genome annotation recods from genbank format 

    @args fname: gbk file name 
    @type fname: str
    """
    fhand = helper.open_file(gbkfname)
    unk = 1 

    for record in SeqIO.parse(fhand, "genbank"):
        gene_tags = dict()
        tx_tags = collections.defaultdict(list) 
        exon = collections.defaultdict(list) 
        cds = collections.defaultdict(list) 
        mol_type, chr_id = None, None 

        for rec in record.features:

            if rec.type == 'source':
                try:
                    mol_type = rec.qualifiers['mol_type'][0]
                except:
                    mol_type = '.'
                    pass 
                try:
                    chr_id = rec.qualifiers['chromosome'][0]
                except:
                    chr_id = record.name 
                continue 

            strand='-'
            strand='+' if rec.strand>0 else strand
            
            fid = None 
            try:
                fid = rec.qualifiers['gene'][0]
            except:
                pass

            transcript_id = None
            try:
                transcript_id = rec.qualifiers['transcript_id'][0]
            except:
                pass 

            if re.search(r'gene', rec.type):
                gene_tags[fid] = (rec.location._start.position+1, 
                                    rec.location._end.position, 
                                    strand,
                                    rec.type
                                    )
            elif rec.type == 'exon':
                exon[fid].append((rec.location._start.position+1, 
                                    rec.location._end.position))
            elif rec.type=='CDS':
                cds[fid].append((rec.location._start.position+1, 
                                    rec.location._end.position))
            else: 
                # get all transcripts 
                if transcript_id: 
                    tx_tags[fid].append((rec.location._start.position+1,
                                    rec.location._end.position, 
                                    transcript_id,
                                    rec.type))
        # record extracted, generate feature table
        unk = feature_table(chr_id, mol_type, strand, gene_tags, tx_tags, cds, exon, unk)
        
    fhand.close()
Esempio n. 37
0
    #if lin[11:16] == "00000": #no volt lines
        #print ("FILTER OUT: Volt has Zero value")
        #return ""

    cfg.OLDTIME[int(daten[0])] = curtime

    try:
        if yeswrite:
            cfg.FSER.write(lin)
    except  ValueError, err:
        tmp = "%s\n" % err
        tmp += "Sollte nicht passieren, erneutes Oeffnen sowieso\n\n"
        print(tmp)
        cfg.FLOG.write(tmp)
        gtk_stuff.message_dialog(cfg.GTK_WINDOW, tmp)
        cfg.FSER = helper.open_file(cfg.TMP_DIR+'/serial-akkumatik.dat', 'ab')
        return True

    if (daten[0] == "1" and cfg.GEWAEHLTER_AUSGANG == 1) \
            or (daten[0] == "2" and cfg.GEWAEHLTER_AUSGANG == 2):
        (labstr1, labstr2) = generate_output_strs(daten)
        output_data(labstr1, labstr2)

    return True

#}}}
def serial_setup(): #{{{
    """ try to connect to the serial port """

    tmp = "* [ Serial Port ] ***********************************\n"
    tmp += ("Versuche zu oeffen den seriellen Port '%s': " % cfg.SERIAL_PORT)