Beispiel #1
0
def run_target_list_compilation(pInteractionFilesList,
                                pTargetList,
                                pArgs,
                                pViewpointObj,
                                pQueue=None):
    outfile_names = []
    target_regions_intervaltree = None
    if pArgs.batchMode and len(pTargetList) == 1:
        target_regions = utilities.readBed(pTargetList[0])
        hicmatrix = hm.hiCMatrix()
        target_regions_intervaltree = hicmatrix.intervalListToIntervalTree(
            target_regions)[0]

    for i, interactionFile in enumerate(pInteractionFilesList):
        for sample in interactionFile:
            if pArgs.interactionFileFolder != '.':
                absolute_sample_path = pArgs.interactionFileFolder + '/' + sample
            else:
                absolute_sample_path = sample
            header, interaction_data, interaction_file_data = pViewpointObj.readInteractionFileForAggregateStatistics(
                absolute_sample_path)
            log.debug('len(pTargetList) {}'.format(len(pTargetList)))
            if pArgs.batchMode and len(pTargetList) > 1:
                if pArgs.targetFileFolder != '.':
                    target_file = pArgs.targetFileFolder + '/' + pTargetList[i]
                else:
                    target_file = pTargetList[i]
            elif pArgs.batchMode and len(pTargetList) == 1:
                target_file = None
            else:
                target_file = pTargetList[i]

            accepted_scores = filter_scores_target_list(
                interaction_file_data,
                pTargetList=target_file,
                pTargetIntervalTree=target_regions_intervaltree)

            if len(accepted_scores) == 0:
                # do not call 'break' or 'continue'
                # with this an empty file is written and no track of 'no significant interactions' detected files needs to be recorded.
                if pArgs.batchMode:
                    with open('errorLog.txt', 'a+') as errorlog:
                        errorlog.write('Failed for: {} and {}.\n'.format(
                            interactionFile[0], interactionFile[1]))
                else:
                    log.info('No target regions found')
            outFileName = '.'.join(sample.split('/')[-1].split('.')
                                   [:-1]) + '_' + pArgs.outFileNameSuffix

            if pArgs.batchMode:
                outfile_names.append(outFileName)
            if pArgs.outputFolder != '.':
                outFileName = pArgs.outputFolder + '/' + outFileName

            write(outFileName, header, accepted_scores, interaction_file_data)
    if pQueue is None:
        return
    pQueue.put(outfile_names)
    return
Beispiel #2
0
def filter_scores_target_list(pScoresDictionary,
                              pTargetList=None,
                              pTargetIntervalTree=None):

    accepted_scores = {}
    same_target_dict = {}
    target_regions_intervaltree = None
    if pTargetList is not None:
        target_regions = utilities.readBed(pTargetList)
        if len(target_regions) == 0:
            return accepted_scores

        hicmatrix = hm.hiCMatrix()
        target_regions_intervaltree = hicmatrix.intervalListToIntervalTree(
            target_regions)[0]
    elif pTargetIntervalTree is not None:
        target_regions_intervaltree = pTargetIntervalTree
    else:
        log.error('No target list given.')
        exit(1)
    for key in pScoresDictionary:
        # try:
        chromosome = pScoresDictionary[key][0]
        start = int(pScoresDictionary[key][1])
        end = int(pScoresDictionary[key][2])
        if chromosome in target_regions_intervaltree:
            target_interval = target_regions_intervaltree[chromosome][
                start:end]
        else:
            continue
        if target_interval:
            target_interval = sorted(target_interval)[0]
            if target_interval in same_target_dict:
                same_target_dict[target_interval].append(key)
            else:
                same_target_dict[target_interval] = [key]

    for target in same_target_dict:

        values = np.array([0.0, 0.0, 0.0])
        same_target_dict[target] = sorted(same_target_dict[target])

        for key in same_target_dict[target]:
            values += np.array(list(map(float, pScoresDictionary[key][-3:])))
        new_data_line = pScoresDictionary[same_target_dict[target][0]]
        new_data_line[2] = pScoresDictionary[same_target_dict[target][-1]][2]
        new_data_line[-5] = pScoresDictionary[same_target_dict[target][-1]][-5]
        new_data_line[-3] = values[0]
        new_data_line[-2] = values[1]
        new_data_line[-1] = values[2]

        accepted_scores[same_target_dict[target][0]] = new_data_line

    return accepted_scores
def run_target_list_compilation(pInteractionFilesList,
                                pTargetList,
                                pArgs,
                                pViewpointObj,
                                pQueue=None,
                                pOneTarget=False):
    outfile_names_list = []
    accepted_scores_list = []
    target_regions_intervaltree = None
    try:
        if pOneTarget == True:
            try:
                target_regions = utilities.readBed(pTargetList)
            except Exception as exp:
                pQueue.put('Fail: ' + str(exp) + traceback.format_exc())
                return
            hicmatrix = hm.hiCMatrix()
            target_regions_intervaltree = hicmatrix.intervalListToIntervalTree(
                target_regions)[0]

        for i, interactionFile in enumerate(pInteractionFilesList):
            outfile_names_list_intern = []
            accepted_scores_list_intern = []
            for sample in interactionFile:

                interaction_data, interaction_file_data, _ = pViewpointObj.readInteractionFile(
                    pArgs.interactionFile, sample)
                if pOneTarget == True:
                    target_file = None
                else:
                    target_file = pTargetList[i]

                accepted_scores = filter_scores_target_list(
                    interaction_file_data,
                    pTargetList=target_file,
                    pTargetIntervalTree=target_regions_intervaltree,
                    pTargetFile=pArgs.targetFile)

                outfile_names_list_intern.append(sample)
                accepted_scores_list_intern.append(accepted_scores)
            outfile_names_list.append(outfile_names_list_intern)
            accepted_scores_list.append(accepted_scores_list_intern)

    except Exception as exp:
        pQueue.put('Fail: ' + str(exp) + traceback.format_exc())
        return
    if pQueue is None:
        return
    counter = 0
    for item in accepted_scores_list_intern:
        if len(item) == 0:
            counter += 1
    pQueue.put([outfile_names_list, accepted_scores_list])
    return