Beispiel #1
0
# Initialize model  (name, FASTA sequence, offset)
model = system_setup.HDXModel("simple", inseq, offset=offset)

# Create a fragment
frag = system_setup.Fragment(inseq, 1, 15, 0)

# Add data to the fragment
for t in data:
    tp = frag.add_timepoint(t)
    for d in data[t]:
        tp.add_replicate(d)

# enumerate and score all solutions

best_grid = sampling.enumerate_fragment(frag, exp_grid, sigma0)

###############################
###   Analysis:
###############################

grid = [0, 3, 2, 3, 1, 1, 2]
score = 0
for t in frag.timepoints:
    model = t.calc_model_deut(grid, exp_grid, frag.num_observable_amides)
    diff = model - t.replicates[0].deut
    #likelihood =
    this_tp_score = -1.0 * numpy.log(t.replicates[0].gaussian_model(
        t.replicates[0].deut, model, sigma0))
    tg_factor = truncated_gaussian_factor(t.replicates[0].deut, -10, 120,
                                          sigma0)
Beispiel #2
0
    def guess_init_exp_sequence(self, size_enum):
        ''' Guess initial state by finding most likely exp values for each fragment
            Returns a sequence of exp values
        '''
        frags = deepcopy(self.frags)

        exp_seq = [0] * len(self.exp_seq)  #, self.first_tp, self.last_tp)
        exp_grid = self.calc_exp_grid(size_enum)
        print("\n##### Initial Guess: Enumerate #####")
        print("# Enumerating all fragments with gridsize of", size_enum)
        print("# State:", self.state.state_name, "\n")
        print("> Fragment start_res end_res exp_grid")
        for f in frags:
            f.exp_vals = sampling.enumerate_fragment(f,
                                                     exp_grid,
                                                     2.0,
                                                     num_models=1)
            print(f.seq, f.exp_vals)
        print("Calculating overlaps and casting to gridsize",
              len(self.exp_grid))
        sectors = self.state.get_sectors(frags)
        # sort sectors by size
        sectors.sort(key=lambda x: x.num_amides, reverse=True)

        for s in sectors:
            sector_exp_vals = numpy.zeros(size_enum)
            sector_frags = deepcopy(s.fragments)
            # if there is only one fragment, just place the residues.  We are done.
            if len(sector_frags) == 1 and sum(
                    sector_frags[0].exp_vals) == s.num_amides:
                method = "Single fragment"
                sector_exp_vals = sector_frags[0].exp_vals
                sector_frags[0].exp_vals = deepcopy(
                    sector_frags[0].exp_vals) - sector_exp_vals
            else:
                # Populate sector with overlapping exp values
                # Is total overlap greater or less than s.num_amides?
                # Make an array of all sector fragment exp_grid vals
                # Overlap is the minimum value for each field
                # How to deal with the rest?  High to low or low to high?
                # Three possibilities:
                #    * Equal - assign overlap to each amide.  Location doesn't matter
                #    * sum(overlap) > len(seq) - take (random?) subset of overlap and assign to sequence
                #    * sum(overlap) < len(seq) - Assign overlap to sequence. Find closest non-overlapping values

                all_exp_vals = [
                ]  #numpy.zeros((len(frags),len(self.exp_grid)))
                for f in sector_frags:
                    all_exp_vals.append(f.exp_vals)

                overlap = numpy.amin(numpy.array(all_exp_vals), axis=0)
                #------------------
                # For equal number of overlap values and amides:
                #------------------
                if numpy.sum(overlap) == s.num_amides:
                    method = "Equal Overlap"
                    sector_exp_vals = overlap
                    #print("Overlap = num_amides", s.seq, overlap, s.start_res)
                    for f in sector_frags:
                        f.exp_vals = deepcopy(f.exp_vals) - overlap

                    #print(exp_seq[s.start_res:s.end_res+1])
                #------------------
                # For more overlap values than amides:
                #   - randomly pick amides to assign
                #------------------
                elif numpy.sum(overlap) > s.num_amides:
                    method = "Overlap > num_amides"
                    overlap_sub = numpy.zeros(len(overlap)).astype(int)
                    i = 1
                    while i <= s.num_amides:
                        picked_val = randint(0, len(overlap) - 1)
                        #print(i, picked_val, overlap, overlap[picked_val], overlap_sub)
                        if overlap[picked_val] != 0:
                            overlap_sub[picked_val] += 1
                            overlap[picked_val] -= 1
                            i += 1
                    #print("Overlap > num_amides", s.seq, overlap, overlap_sub, s.start_res)

                    sector_exp_vals = overlap_sub

                    for f in sector_frags:
                        f.exp_vals = deepcopy(f.exp_vals) - overlap_sub

                #------------------
                # For fewer overlap values than amides:
                #   - assign overlap
                #   - start from smallest fragment and assign values until you hit n_remaining
                #------------------
                else:
                    method = "Overlap < num_amides"
                    overlap_sub = numpy.zeros(len(overlap)).astype(int)
                    n_remaining = s.num_amides - numpy.sum(overlap)
                    for f in sector_frags:
                        f.exp_vals = deepcopy(f.exp_vals) - overlap

                    #sort frags from smallest to largest
                    sector_frags.sort(key=lambda x: x.num_observable_amides)
                    #print("***Overlap < num_amides", s.seq, s.num_amides - sum(overlap), overlap, sector_frags[0].exp_vals, s.start_res)
                    i = 1
                    #randomly pick value from smallest fragment
                    while i <= n_remaining:
                        picked_val = randint(0, len(overlap) - 1)
                        if sector_frags[0].exp_vals[picked_val] != 0:
                            overlap_sub[picked_val] += 1
                            sector_frags[0].exp_vals[picked_val] = deepcopy(
                                sector_frags[0].exp_vals[picked_val]) - 1
                            #print(i, n_remaining, picked_val, overlap_sub, sector_frags[0].exp_vals)
                            i = i + 1
                    sector_exp_vals = overlap + overlap_sub

                    # For each other frag, delete exp_vals closest to overlap_sub
                    for f in sector_frags[1:-1]:
                        nonzero_elements = f.exp_vals.nonzero()[0]
                        for i in range(len(exp_grid)):
                            if overlap_sub[i] != 0:
                                if i == 0:  # if we are at i=0, delete first nonzero
                                    first_nze = nonzero_elements[0]
                                    f.exp_vals[first_nze] = deepcopy(
                                        f.exp_vals[first_nze]) - 1

                                elif i == len(
                                        exp_grid
                                ) - 1:  # if we are at i=N, delete last nonzero
                                    last_nze = nonzero_elements[-1]
                                    f.exp_vals[last_nze] = deepcopy(
                                        f.exp_vals[last_nze]) - 1

                                else:  #Otherwise a bit more complicated
                                    direction = int(
                                        2 * (randint(0, 1)) -
                                        1)  #decide whether to go + or -
                                    #print(i, direction, f.exp_vals)
                                    if f.exp_vals[
                                            i +
                                            direction] != 0:  # try 1st direction
                                        f.exp_vals[i + direction] = deepcopy(
                                            f.exp_vals[i + direction]) - 1
                                    elif f.exp_vals[
                                            i -
                                            direction] != 0:  #if not, try second direction
                                        f.exp_vals[i - direction] = deepcopy(
                                            f.exp_vals[i - direction]) - 1
                                    else:  #cop out for this...just use the first one.
                                        f.exp_vals[
                                            nonzero_elements[0]] = deepcopy(
                                                f.exp_vals[
                                                    nonzero_elements[0]]) - 1

            if numpy.sum(sector_exp_vals) != s.num_amides:
                print("ERROR: sector ", s.seq,
                      "does not have right number of amides,", s.num_amides)
                print("Overlap not correctly calculated. Sector exp_vals:",
                      sector_exp_vals)
                print("Method:", method)
                sys.exit(1)

            # cast exp_grid to self.exp_grid for each sector
            long_sector_exp_vals = numpy.zeros(len(self.exp_grid)).astype(int)
            # first and last are identical for each.
            long_sector_exp_vals[0] = sector_exp_vals[0]
            long_sector_exp_vals[-1] = sector_exp_vals[-1]

            #Cast middle values

            #something is wrong with these indicies...need to fix. long_sector -1 is getting overwritten
            factor = float(len(sector_exp_vals)) / (float(
                len(long_sector_exp_vals)))

            for i in range(1, len(sector_exp_vals) - 1):
                for j in range(1, len(long_sector_exp_vals) - 1):
                    if j * factor <= i and (j + 1) * factor >= i:
                        ran_int = randint(0, 1)
                        long_sector_exp_vals[j + ran_int] += sector_exp_vals[i]
                        break
                    #print(j*factor, i,long_sector_exp_vals[j], sector_exp_vals[i])

            #print(s.seq, sector_exp_vals, method)
            #print(s.seq, long_sector_exp_vals)

            if numpy.sum(long_sector_exp_vals) != s.num_amides:
                print("ERROR: sector ", s.seq,
                      "does not have right number of amides,", s.num_amides)
                print(sector_exp_vals, "not correctly cast to",
                      long_sector_exp_vals)
                print("Method:", method)
                sys.exit(1)

            for n in range(s.start_res, s.end_res + 1):
                if self.seq[n] == 'P' or self.seq == 'p':
                    exp_seq[n] = 0
                else:
                    for i in range(len(self.exp_grid)):
                        if long_sector_exp_vals[i] != 0:
                            exp_seq[n] = i + 1
                            long_sector_exp_vals[
                                i] = long_sector_exp_vals[i] - 1
                            break
            #print("SSSS", s.seq, sector_exp_vals, exp_seq[s.start_res:s.end_res+1])

        #for f in frags:
        #print(f.seq, f.exp_vals)

        #for n in range(len(self.exp_seq)):
        #print(n,self.state.seq[n], exp_seq[n])
        #print(exp_seq)

        self.exp_seq = exp_seq
        return exp_seq