Пример #1
0
def pergola_rules(path, map_file_path, sel_tracks=None, list=None, range=None, track_actions=None, 
                  data_types_actions=None, data_types_list=None, write_format=None, relative_coord=False,
                  intervals_gen=False, multiply_f=None, no_header=False, fields2read=None, window_size=None,
                  no_track_line=False, separator=None, bed_lab_sw=False, color_dict=None, window_mean=False,
                  value_mean=False, min_t=None, max_t=None, interval_step=None):
    
    print >> stderr, "@@@Pergola_rules.py: Input file: %s" % path 
    print >> stderr, "@@@Pergola_rules.py: Configuration file: %s" % map_file_path
    
    # Tracks selected by user
    print >> stderr, "@@@Pergola_rules.py: Selected tracks are: ", sel_tracks
    
    # Configuration file
    map_file_dict = mapping.MappingInfo(map_file_path)
    
    # Reading color dictionary to set data_types
    if color_dict:
        print >> stderr, "@@@Pergola_rules.py: Color for data_types in file............ %s" % color_dict
        d_colors_data_types = parsers.read_colors (color_dict)
    else:
        d_colors_data_types = None    
        
    # Handling list or range of tracks to join if set
    if list and range:
        raise ValueError("@@@Pergola_rules.py: Argument -l/--list and -r/--range are not compatible. " \
                         "As both arguments set tracks to be joined.")    
    elif (list):
        tracks2merge = list
    elif (range):
        tracks2merge = range
    else:
        tracks2merge = ""
    
    if tracks2merge: print >> stderr, "@@@Pergola_rules.py: Tracks to join are............ ", tracks2merge
    
    # Handling argument track actions
    if tracks2merge and track_actions:
        raise ValueError ("Options --list -l or --range -r are incompatible with " \
                          "--track_actions -a, please change your options")
    
    track_act = track_actions
    print >> stderr, "@@@Pergola_rules.py: Track actions are.............................. ", track_act
    
    data_types_list = data_types_list
    print >> stderr, "@@@Pergola_rules.py: data_types list is: ", data_types_list
      
    # Handling argument data_types actions
    data_types_act = data_types_actions
    print >> stderr, "@@@Pergola_rules.py: data_types actions are......................... ", data_types_act
    
    # Handling argument format    
    if write_format:
        print >> stderr, "@@@Pergola_rules.py format to write files....................... ", write_format
    else:
        write_format = 'bed'
        print >>stderr, "@@@Pergola_rules.py format to write files has been set" \
                        " to default value:", write_format

    # Handling relative coordinates
    print >> stderr, "@@@Pergola_rules.py: Relative coordinates set to................. %s" % relative_coord
    
    # Handling intervals_gen
    print >> stderr, "@@@Pergola_rules.py: Intervals parameter set to.................. %s" % intervals_gen

    # Handling interval_step
    if interval_step:
        if intervals_gen:
            print >> stderr, "@@@Pergola_rules.py: Interval step set to........................ %s" % interval_step
        else:
            raise ValueError("Interval step needs intervals paramater to be set -n/--intervals_gen")

    # Handling multiply_intervals
    if multiply_f:
        print >>stderr, "@@@Pergola_rules.py: Multiply intervals parameter set to...... %s" % multiply_f                        
    else:
        multiply_f = 1
    
    print >> stderr, "@@@Pergola_rules.py: Selected tracks are......................... ", sel_tracks
    
    # Setting whether input file has header or not
    header_sw = True
    
    if no_header:
        header_sw = False
        print >> stderr, "@@@Pergola_rules.py: Data file has header set to............. ", header_sw

    # Handling fields to read
    if fields2read:
        print >>stderr, "@@@Pergola_rules.py: Fields to read from the file are......... %s" % fields2read                        
    else:
        fields2read = None    
    
    # When binning data setting the window of time used in seconds
    # if not size provided set to False
    if window_size:
        print >>stderr, "@@@Pergola_rules.py: Window size set to....................... %d" % window_size
    else:
#         window_size = 300
        window_size = False
        print >>stderr, "@@@Pergola_rules.py: Window size set by default to............ %d" % window_size
    
    if window_mean:
        print >>stderr, "@@@Pergola_rules.py: Window mean set to....................... %d" % window_mean
    else:      
        window_mean = False

    if value_mean:
        print >> stderr, "@@@Pergola_rules.py: Value mean set to....................... %d" % value_mean
    else:
        value_mean = False

    if no_track_line:
        track_line=False
    else:
        track_line=True
        
    print >>stderr, "@@@Pergola_rules.py: track_line set to............................ %s" % track_line
    
    # Handling input file field delimiter    
    if not separator:
        separator = "\t"
        print >> stderr, "@@@Pergola_rules.py input file field separator set by default to...... \"\\t\"."
    else:        
        print >>stderr, "@@@Pergola_rules.py input file field separator set to..... \"%s\"" % separator
    
    if bed_lab_sw:
        bed_lab = True
        print >>stderr, "@@@Pergola_rules.py: bed_label set to......................... %s" % bed_lab
    else:
        bed_lab = False

    intData = intervals.IntData(path, map_dict=map_file_dict.correspondence,
                                fields_names=fields2read,
                                header=header_sw, delimiter=separator)

    start = intData.min
    end = intData.max

    if relative_coord:
        start = 0
        end = intData.max - intData.min

    print >> stderr, "@@@Pergola_rules.py: min time in file......................... %d" % start
    print >> stderr, "@@@Pergola_rules.py: max time in file......................... %d" % end

    if min_t or min_t == 0:
        min_time = min_t
        print >> stderr, "@@@Pergola_rules.py: min_time set by user to.............. %d" % min_t
    else:
        min_time = start

    if max_t:
        max_time = max_t
        print >> stderr, "@@@Pergola_rules.py: max_time set by user to............... %d" % max_t
    else:
        if interval_step:
            max_time = end + interval_step
        else:
            max_time = end + 1

    if multiply_f:
        min_time = min_time * multiply_f
        max_time = max_time * multiply_f

    if track_act: tracks2merge = parsers.read_track_actions(tracks=intData.tracks, track_action=track_act)

    data_read = intData.read(relative_coord=relative_coord,
                             intervals=intervals_gen,
                             multiply_t=multiply_f,
                             min_time=min_time, max_time=max_time,
                             int_step=interval_step)

    mapping.write_chr(data_read)#mantain
    mapping.write_chr_sizes(data_read)

    # writes cytoband and light, dark and light_dark bed files
    mapping.write_cytoband(end=end, track_line=track_line, lab_bed=False)
#     mapping.write_period_seq(start=0, end=intData.max, delta=43200, name_file="phases_dark", track_line=False) 
    
    data_read.save_track(name_file="all_intervals")

    bed_str = data_read.convert(mode=write_format, tracks=sel_tracks,
                                tracks_merge=tracks2merge, data_types=data_types_list,
                                data_types_actions=data_types_act, window=window_size,
                                mean_win=window_mean, mean_value=value_mean, color_restrictions=d_colors_data_types)
                                #min_t_trim=min_t, max_t_trim=max_t)
    
    for key in bed_str:
        bedSingle = bed_str[key]
        bedSingle.save_track(track_line=track_line, bed_label=bed_lab)
                                  norm=True,
                                  data_type=annotated_behavior)

scores_chase_int = intervals.IntData(
    tmp_track.name, map_dict=mappings_jaaba.correspondence).read()

data_type_col = {annotated_behavior: 'blue'}

if tag_group == "pBDPGAL4":
    #    data_type_col = {annotated_behavior: 'orange'}
    data_type_col = {annotated_behavior: 'red'}

dict_bed_annotated_int = scores_chase_int.convert(
    mode="bed", color_restrictions=data_type_col)

chr_file_n = "chrom"
mapping.write_chr_sizes(scores_chase_int, file_n=chr_file_n)
mapping.write_chr(scores_chase_int)

chr_file = chr_file_n + ".sizes"

for tr, bed_tr in dict_bed_annotated_int.iteritems():
    id_worm, var = tr
    pybed_tr = bed_tr.create_pybedtools()
    pybed_tr.saveas('tr_' + id_worm + '_dt_' + tag_group + '.bed')
    df_by_tr = pybed_tr.genome_coverage(
        g=chr_file).to_dataframe()  # .saveas('tr_' + id_worm + '.bed')
    print "%f\t%s" % (df_by_tr.score[(df_by_tr.chrom == 'chr1')
                                     & (df_by_tr.start == 1)].values[0],
                      annotated_behavior + '_' + tag_group)
    # pybedtools.BedTool.to_dataframe()
Пример #3
0
else:
    print >> stderr, "Behavioral data type not available in script, please try again with \"drinking\" or \"feeding\""

mapping_data = mapping.MappingInfo(args.mapping_file)

behavior_mice = dict()
end_time = -10000
data_read_all_batches = None

for f in args.file_mice_behavior:
    int_data = intervals.IntData(f, map_dict=mapping_data.correspondence)

    data_read = int_data.read(relative_coord=True)

    chr_file_n = "chrom"
    mapping.write_chr_sizes(data_read, file_n=chr_file_n)
    chr_file = chr_file_n + ".sizes"

    if end_time < int_data.max - int_data.min:
        end_time = int_data.max - int_data.min

    if not data_read_all_batches:
        data_read_all_batches = data_read
    else:
        data_read_all_batches = tracks.merge_tracks(data_read_all_batches,
                                                    data_read)

stdout.write(str(end_time))

list_ctrl = [1, 3, 5, 7, 9, 11, 13, 15, 17]
list_hf = [2, 4, 8, 10, 12, 14, 16, 18]
Пример #4
0
###################
# Generate to BedTool objects containing light and dark phases

# Write phases file
mapping.write_cytoband(end=int_data.max - int_data.min,
                       delta=43200,
                       start_phase="dark")

light_ph_f = out_dir + "phases_light.bed"
dark_ph_f = out_dir + "phases_dark.bed"
light_bed = pybedtools.BedTool(light_ph_f)
dark_bed = pybedtools.BedTool(dark_ph_f)

# Generate a chr.size file in order to calculate complement of merged meals
chr_file_n = "chrom"
mapping.write_chr_sizes(data_read, path_w=out_dir, file_n=chr_file_n)
chr_file = out_dir + chr_file_n + ".sizes"

# Dictionary to set colors of each type of food
data_type_col = {'food_sc': 'orange', 'food_fat': 'blue'}
data_type_col_m = {'food_sc': 'orange', 'food_fat_food_sc': 'blue'}

bed_str = data_read.convert(mode="bed",
                            data_types=["food_sc", "food_fat"],
                            data_types_actions="all",
                            color_restrictions=data_type_col)
bed_str_out = data_read.convert(mode="bed",
                                data_types=["food_sc", "food_fat"],
                                data_types_actions="all",
                                color_restrictions=data_type_col)
mapping_bed = mapping.MappingInfo(out_dir + "/pybed2perg.txt")