示例#1
0
def import_HXcolumns(infile, sequence, name=None, percentD=False, conditions=None, error_estimate=5.0, n_fastamides=2, offset=0):
    """ Function for converting a set of HX columns intoa Dataset object
    """

    if type(conditions) is not Conditions:
        print("Standard Conditions used.  Please modify these in the script if you are not at 283K and pH=7")
        conditions = Conditions()

    f = open(infile,"r")
    line = f.readline()

    dataset = Dataset(name=name, sequence=sequence, conditions=conditions, error_estimate=error_estimate, offset=offset, input_file=infile)

    column_headers = line.rstrip().split(",")  # Pre-set to None.  See #XXXX

    for line in f.readlines():

        fields = line.split(",")
        sequence = fields[column_headers.index("peptide_seq")]
        start_res = int(fields[column_headers.index("start_res")]) + offset
        time = float(fields[column_headers.index("time")])
        deut = float(fields[column_headers.index("D_inc")])
        if not percentD:
            deut = deut / tools.calculate_number_of_observable_amides(sequence, n_fastamides) * 100
        score = float(fields[column_headers.index("score")])

        new_peptide = dataset.create_peptide(sequence, start_res)

        if new_peptide is not None:
            # If the time is 0.0, that's weird.  Ignore that.
            if time==0.0:
                continue

            if deut < 105:  # XXX Hard coded cap on %D value.  Not ideal.
                new_timepoint=True

                # If timepoint is already in fragment, grab that timepoint
                if time in [tp.time for tp in new_peptide.get_timepoints()]:
                    tp = new_peptide.get_timepoint_by_time(time)
                #If not, add a new timepoint at this time.  What to put for sigma??
                else:
                    tp = new_peptide.add_timepoint(time)

                # add the deuteration value as a replicate.
                # Any other replicate information from the file should be added at this step.
                tp.add_replicate(deut, score=score)

            new_peptide.add_timepoint(time)

    dataset.calculate_observable_rate_bounds()
    dataset.calculate_observable_protection_factors()

    return dataset
示例#2
0
def import_HDXWorkbench(infile, macromolecule=None, name="Data", sequence=None, error_estimate=5.0, n_fastamides=2, offset=0,
                    max_t=36000):
    '''
    HDXWorbench files are different because they contain information from two experiments.
    They recieve a macromolecule rather than a single state and will create
    each state within that macromolecule

    Will also create a list of two Dataset objects if no macromolecule is passed
    '''
    # set up the dataset conditions
    conditions = Conditions()

    if macromolecule is not None and sequence is None:
        sequence = macromolecule.get_sequence()

    column_headers = None  # Pre-set to None.  See #XXXX

    f = open(infile,"r")
    line = f.readline()
    # First, let's get the header information from the workbench file
    # All lines are param, value pairs separated by a comma
    while len(line.split(','))==2:
        param=line.split(',')[0]
        value=line.split(',')[1]
        if param=="Used fully deuterated controls":
            if value=="true" or value=="True" or value=="T":
                conditions.fully_deuterated = True
            elif value=="false" or value=="False" or value=="F":
                conditions.fully_deuterated=False
        if param=="Temperature":
            conditions.temperature = float(value)+273.15
        if param=="Offset":
            conditions.offset = int(value)
        if param=="Deuterium solution concentration":
            conditions.saturation = float(value)
        if param=="Recovery estimate":
            conditions.recovery = float(value)
        if param=="Experiment Protein Sequence":
            file_sequence = str(value)
            if file_sequence != sequence and sequence is not None:
                print("WARNING: Sequence in HDXWorkbench file does not match inputted sequence")
            sequence = file_sequence.strip()
        if param=="Experiment name":
            name = str(value).replace(" ","_")
        line=f.readline()
    states = set()
    for line in f:

        # For nothing, just keep going
        if len(line.split(','))==0:
            continue

        #XXXX - the first line beginning with "peptide" is the list of column headers.
        if line.split(',')[0]=="peptide" and column_headers is None:
            column_headers = line.split(",")
            continue

        # This is a consolidated fragment entry. Just grab the state names for now 
        # (only two states in current format).
        if len(line.split(',')) >= 1 and column_headers != None and line.split(',')[column_headers.index("percentd_replicate")]=="NA" and len(states)==0:
            
            
            states.add(line.split(',')[column_headers.index("sample1_name")].replace(" ","_"))
            states.add(line.split(',')[column_headers.index("sample2_name")].replace(" ","_"))

            # Now create the different dataset
            datasets=[]
            for s in states:
                d = Dataset(name=s, sequence=sequence, conditions=conditions, error_estimate=error_estimate, input_file=infile, percent_deuterium=True)
                datasets.append(d)

        #############################################################
        #  This is replicate data. 
        #############################################################
        if len(line.split(',')) >= 1 and column_headers != None and line.split(',')[column_headers.index("percentd_replicate")]!="NA":
            # First see if it was discarded:
            discarded = line.split(',')[column_headers.index("discarded_replicate")]
            if discarded==True or discarded=="T" or discarded == "true" or discarded == "True" or discarded == "TRUE":
                continue

            # If not, get the peptide seq / start 
            # ********** Also, potentially other parameters *************
            fields = line.split(",")
            charge_state = int(fields[column_headers.index("charge")])
            peptide_sequence = fields[column_headers.index("peptide")]
            start_residue = int(fields[column_headers.index("start")])
            state_name = fields[column_headers.index("sample")].replace(" ","_")
            replicate_id = int(fields[column_headers.index("replicate")])

            try:
                replicate_score = float(fields[column_headers.index("score_replicate")])
            except ValueError:
                try:
                    replicate_score = float(fields[column_headers.index("score_replicate")+1])
                    #print("DHJSDNSLCNSIOACJSNCSAJ", line)
                except ValueError:
                    replicate_score = 0
                    #print("DHJSDNSLCNSIOACJSNCSAJ", 0, line)

            # retention time is a tuple with the start and end
            replicate_rt = (float(fields[column_headers.index("rt_start_replicate")]), float(line.split(',')[column_headers.index("rt_end_replicate")]))

            for data in datasets:
                if state_name == data.name:
                    # If the peptide is not there...create the peptide
                    is_pep_in, new_peptide = data.is_this_peptide_in_dataset(peptide_sequence, start_residue, charge_state)
                    
                    if new_peptide is None:
                        new_peptide = data.create_peptide(sequence=peptide_sequence, start_residue=start_residue, charge_state=charge_state)
            #print("XX", state_name, replicate_id, peptide_sequence, charge_state, new_peptide)            
            #print("Replicate", new_peptide)
            # Now, once the we know the peptide is there (or has just been created), add the data
            if new_peptide is not None:
                time = float(line.split(',')[column_headers.index("timepoint")].replace("s",""))

                # If the time is 0.0, that's weird.  Ignore that.
                if time==0.0 or time > max_t:
                    continue

                deut=float(line.split(',')[column_headers.index("percentd_replicate")])

                if deut < 155:  # XXX Hard coded cap on %D value.  Not ideal.
                    new_timepoint=True

                    # If timepoint is already in fragment, grab that timepoint
                    if time in [tp.time for tp in new_peptide.get_timepoints()]:
                        tp = new_peptide.get_timepoint_by_time(time)
                    #If not, add a new timepoint at this time.  What to put for sigma??
                    else:
                        tp = new_peptide.add_timepoint(time)
         
                    # add the deuteration value as a replicate.
                    # Any other replicate information from the file should be added at this step.
                    tp.add_replicate(deut, experiment_id=replicate_id, score=replicate_score, rt=replicate_rt)

                new_peptide.add_timepoint(time)

    if macromolecule is not None:
        for d in datasets:
            d.calculate_observable_rate_bounds()
            d.calculate_observable_protection_factors()
            s = macromolecule.add_state(d.name)
            s.add_dataset(d)

    return datasets