Changeset 710 for mds-and-trees


Ignore:
Timestamp:
10/06/17 16:06:17 (6 years ago)
Author:
konrad
Message:

Added multi-input-file support (eg. ./tree-genealogy.py -i file1.out file2.out file3.out -o tree.png)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • mds-and-trees/tree-genealogy.py

    r709 r710  
    154154                                  self.h_margin+self.h_no_margs*chi_pos['y']/max_height), line_style)
    155155
    156     def draw_scale(self, file, filename):
    157         self.add_text(file, "Generated from " + filename.split("\\")[-1], (5, 5), "start")
     156    def draw_scale(self, file, filenames):
     157        self.add_text(file, "Generated from " + filenames[0].split("\\")[-1]
     158                      + (" and " + str(len(filenames)-1) + " more" if len(filenames) > 1 else ""), (5, 5), "start")
    158159
    159160        start_text = ""
     
    692693        pass
    693694
    694     def load(self, filename, max_nodes=0):
     695    def load(self, filenames, max_nodes=0):
    695696        print("Loading...")
    696697
     
    726727            return creature
    727728
    728         file = open(filename)
    729729
    730730        # counting the number of expected nodes
     731
    731732        nodes = 0
    732         for line in file:
    733             line_arr = line.split(' ', 1)
    734             if len(line_arr) == 2:
    735                 if line_arr[0] == CLI_PREFIX:
    736                     line_arr = line_arr[1].split(' ', 1)
    737                 if line_arr[0] == "[BORN]":
    738                     nodes += 1
     733        for filename in filenames:
     734            file = open(filename)
     735            for line in file:
     736                line_arr = line.split(' ', 1)
     737                if len(line_arr) == 2:
     738                    if line_arr[0] == CLI_PREFIX:
     739                        line_arr = line_arr[1].split(' ', 1)
     740                    if line_arr[0] == "[BORN]":
     741                        nodes += 1
    739742
    740743        nodes = min(nodes, max_nodes if max_nodes != 0 else nodes)+1
     
    748751        print("nodes: %d" % len(self.parents))
    749752
    750         file.seek(0)
     753
     754        get_id("virtual_parent")
    751755        loaded_so_far = 0
    752         lasttime = timelib.time()
    753 
    754         get_id("virtual_parent")
    755 
    756         for line in file:
    757             line_arr = line.split(' ', 1)
    758             if len(line_arr) == 2:
    759                 if line_arr[0] == CLI_PREFIX:
    760                     line_arr = line_arr[1].split(' ', 1)
    761                 if line_arr[0] == "[BORN]":
    762                     creature = try_to_load(line_arr[1])
    763                     if not creature:
    764                         nodes -= 1
    765                         break
    766 
    767                     creature_id = get_id(creature["ID"])
    768 
    769                     # debug
    770                     if loaded_so_far%1000 == 0:
    771                         #print(". " + str(creature_id) + " " + str(timelib.time() - lasttime))
    772                         lasttime = timelib.time()
    773 
    774                     if "Time" in creature:
    775                         self.time[creature_id] = creature["Time"]
    776 
    777                     for prop in creature:
    778                         if prop not in default_props:
    779                             if prop not in self.props:
    780                                 self.props[prop] = [0 for i in range(nodes)]
    781                             self.props[prop][creature_id] = creature[prop]
    782 
    783                     loaded_so_far += 1
    784 
    785                 if line_arr[0] == "[OFFSPRING]":
    786                     creature = try_to_load(line_arr[1])
    787                     if not creature:
    788                         nodes -= 1
    789                         break
    790 
    791                     if "FromIDs" in creature:
    792                         # make sure that ID's of parents are lower than that of their children
    793                         for i in range(0, len(creature["FromIDs"])):
    794                             if creature["FromIDs"][i] not in self.ids:
    795                                 get_id("virtual_parent")
    796 
    797                         creature_id = get_id(creature["ID"])#, False)
    798 
    799                         # we assign to each parent its contribution to the genotype of the child
    800                         for i in range(0, len(creature["FromIDs"])):
    801                             if creature["FromIDs"][i] in self.ids:
    802                                 parent_id = get_id(creature["FromIDs"][i])
    803                             else:
    804                                 if creature["FromIDs"][i] not in merged_with_virtual_parent:
    805                                     merged_with_virtual_parent.append(creature["FromIDs"][i])
    806                                 parent_id = get_id("virtual_parent")
    807                             inherited = (creature["Inherited"][i] if 'Inherited' in creature else 1)
    808                             self.parents[creature_id][parent_id] = inherited
    809 
    810                         if "Kind" in creature:
    811                             self.kind[creature_id] = creature["Kind"]
    812                     else:
    813                         raise LoadingError("[OFFSPRING] misses the 'FromIDs' field!")
    814 
    815                 if line_arr[0] == "[DIED]" or line_arr[0] == "[BORN]":
    816                     creature = try_to_load(line_arr[1])
    817                     if not creature:
    818                         nodes -= 1
    819                         break
    820                     creature_id = get_id(creature["ID"], False)
    821                     if creature_id is not None:
     756        max_time = 0
     757        # rewind the file
     758
     759        for filename in filenames:
     760            file = open(filename)
     761            time_offset = max_time
     762            if max_time != 0:
     763                print("NOTE: merging the files, the time offset for the file " + filename + " is: " + str(time_offset))
     764
     765            lasttime = timelib.time()
     766
     767            for line in file:
     768                line_arr = line.split(' ', 1)
     769                if len(line_arr) == 2:
     770                    if line_arr[0] == CLI_PREFIX:
     771                        line_arr = line_arr[1].split(' ', 1)
     772                    if line_arr[0] == "[BORN]":
     773                        creature = try_to_load(line_arr[1])
     774                        if not creature:
     775                            nodes -= 1
     776                            break
     777
     778                        creature_id = get_id(creature["ID"])
     779
     780                        # debug
     781                        if loaded_so_far%1000 == 0:
     782                            #print(". " + str(creature_id) + " " + str(timelib.time() - lasttime))
     783                            lasttime = timelib.time()
     784
     785                        if "Time" in creature:
     786                            self.time[creature_id] = creature["Time"] + time_offset
     787                            max_time = max(self.time[creature_id], max_time)
     788
    822789                        for prop in creature:
    823790                            if prop not in default_props:
     
    826793                                self.props[prop][creature_id] = creature[prop]
    827794
    828 
     795                        loaded_so_far += 1
     796
     797                    if line_arr[0] == "[OFFSPRING]":
     798                        creature = try_to_load(line_arr[1])
     799                        if not creature:
     800                            nodes -= 1
     801                            break
     802
     803                        if "FromIDs" in creature:
     804                            # make sure that ID's of parents are lower than that of their children
     805                            for i in range(0, len(creature["FromIDs"])):
     806                                if creature["FromIDs"][i] not in self.ids:
     807                                    get_id("virtual_parent")
     808
     809                            creature_id = get_id(creature["ID"])#, False)
     810
     811                            # we assign to each parent its contribution to the genotype of the child
     812                            for i in range(0, len(creature["FromIDs"])):
     813                                if creature["FromIDs"][i] in self.ids:
     814                                    parent_id = get_id(creature["FromIDs"][i])
     815                                else:
     816                                    if creature["FromIDs"][i] not in merged_with_virtual_parent:
     817                                        merged_with_virtual_parent.append(creature["FromIDs"][i])
     818                                    parent_id = get_id("virtual_parent")
     819                                inherited = (creature["Inherited"][i] if 'Inherited' in creature else 1)
     820                                self.parents[creature_id][parent_id] = inherited
     821
     822                            if "Kind" in creature:
     823                                self.kind[creature_id] = creature["Kind"]
     824                        else:
     825                            raise LoadingError("[OFFSPRING] misses the 'FromIDs' field!")
     826
     827                    if line_arr[0] == "[DIED]" or line_arr[0] == "[BORN]":
     828                        creature = try_to_load(line_arr[1])
     829                        if not creature:
     830                            nodes -= 1
     831                            break
     832                        creature_id = get_id(creature["ID"], False)
     833                        if creature_id is not None:
     834                            for prop in creature:
     835                                if prop not in default_props:
     836                                    if prop not in self.props:
     837                                        self.props[prop] = [0 for i in range(nodes)]
     838                                    self.props[prop][creature_id] = creature[prop]
     839
     840                # breaking both loops
     841                if loaded_so_far >= max_nodes and max_nodes != 0:
     842                    break
    829843            if loaded_so_far >= max_nodes and max_nodes != 0:
    830844                break
     
    848862    parser = argparse.ArgumentParser(description='Draws a genealogical tree (generates a SVG file) based on parent-child relationship '
    849863                                                 'information from a text file. Supports files generated by Framsticks experiments.')
    850     parser.add_argument('-i', '--in', dest='input', required=True, help='input file name with stuctured evolutionary data')
     864    parser.add_argument('-i', '--in', nargs='+', dest='input', required=True, help='input file name with stuctured evolutionary data (or a list of input files)')
    851865    parser.add_argument('-o', '--out', dest='output', required=True, help='output file name for the evolutionary tree (SVG/PNG/JPG/BMP)')
    852866    parser.add_argument('-c', '--config', dest='config', default="", help='config file name ')
     
    886900
    887901    dir = args.input
     902
    888903    seed = args.seed
    889904    if seed == -1:
Note: See TracChangeset for help on using the changeset viewer.