def main():

    #Create the Tkinter UI
    dialog = TkinterDialog(workingDirectory=getDataDirectory(),
                           scrollable=True)
    dialog.createMultipleFileSelector(
        "Nucleosome Mutation Counts files:",
        0,
        DataTypeStr.normNucCounts + ".tsv",
        ("Tab Seperated Values Files", ".tsv"),
        additionalFileEndings=(DataTypeStr.rawNucCounts + ".tsv", ))
    dialog.createFileSelector("Output File",
                              1, ("R Data File", ".rda"),
                              ("Tab Separated Values File", ".tsv"),
                              newFile=True)

    dialog.createCheckbox(
        "Use expected periodicity from nucleosome maps instead of peak periodicity",
        2, 0)
    dialog.createCheckbox(
        "Align both DNA strands to run 5' to 3' before running the analysis",
        3, 0)
    dialog.createLabel('', 4, 0)

    mainGroupSearchRefine = dialog.createDynamicSelector(5, 0)
    mainGroupSearchRefine.initCheckboxController("Filter counts files")
    mainGroupSearchRefine.initDisplay(True).createNucMutGroupSubDialog(
        "MainGroup", 0)
    mainGroupSearchRefine.initDisplayState()

    periodicityComparison = dialog.createDynamicSelector(6, 0)
    periodicityComparison.initCheckboxController(
        "Compare periodicities between two groups")
    periodicityGroupType = periodicityComparison.initDisplay(
        True, "periodicityGroupType")

    periodicityGroupTypeSelector = periodicityGroupType.createDynamicSelector(
        0, 0)
    periodicityGroupTypeSelector.initDropdownController(
        "Compare periodicities...",
        ("Within original selection", "Against a newly selected group"))
    periodicityGroupsWithin = periodicityGroupTypeSelector.initDisplay(
        "Within original selection", "withinGroup")
    secondaryPeriodicityGroup = periodicityGroupTypeSelector.initDisplay(
        "Against a newly selected group", "secondaryGroupFilePaths")

    # Create two "sub-dialogs" for each of the groups, allowing the user to specify the make-up of that group for the "withinGroup" dialog.
    for i, dialogID in enumerate(("Sub-Group 1", "Sub-Group 2")):
        periodicityGroupsWithin.createNucMutGroupSubDialog(dialogID, i + 1)

    # Create one multiple file selector and one sub-dialog for the "secondaryGroup" dialog
    secondaryPeriodicityGroup.createMultipleFileSelector(
        "Nucleosome Mutation Counts files:",
        0,
        DataTypeStr.normNucCounts + ".tsv",
        ("Tab Seperated Values Files", ".tsv"),
        additionalFileEndings=(DataTypeStr.rawNucCounts + ".tsv", ))

    secondaryPeriodicityGroupSearchRefine = secondaryPeriodicityGroup.createDynamicSelector(
        2, 0)
    secondaryPeriodicityGroupSearchRefine.initCheckboxController(
        "Filter counts files")
    secondaryPeriodicityGroupSearchRefine.initDisplay(
        True).createNucMutGroupSubDialog("Secondary Group", 0)
    secondaryPeriodicityGroupSearchRefine.initDisplayState()

    periodicityGroupTypeSelector.initDisplayState()
    periodicityComparison.initDisplayState()

    # Run the UI
    dialog.mainloop()

    # If no input was received (i.e. the UI was terminated prematurely), then quit!
    if dialog.selections is None: quit()

    # Get the user's input from the dialog.
    selections: Selections = dialog.selections
    nucleosomeMutationCountsFilePaths = selections.getFilePathGroups()[0]
    if periodicityGroupTypeSelector.getControllerVar(
    ) == "Against a newly selected group":
        secondaryNucMutCountsFilePaths = selections.getFilePathGroups(
            "secondaryGroupFilePaths")[0]
    outputFilePath = list(selections.getIndividualFilePaths())[0]

    # Get the default periodicity value, testing the string to see if it is a valid float, if necessary.
    overridePeakPeriodWithExpected = bool(selections.getToggleStates()[0])
    alignStrands = bool(selections.getToggleStates()[1])

    # If group comparisons were requested, get the respective groups.
    filePathGroups: List[list] = list()
    for i in range(3):
        filePathGroups.append(list())

    groups = ["MainGroup"]
    if periodicityComparison.getControllerVar():
        if periodicityGroupTypeSelector.getControllerVar(
        ) == "Within original selection":
            groups += ("Sub-Group 1", "Sub-Group 2")
        else:
            groups.append("Secondary Group")

    for i, dialogID in enumerate(groups):

        # Was filtering even requested for the main group?
        if i == 0 and not mainGroupSearchRefine.getControllerVar():
            filePathGroups[0] = nucleosomeMutationCountsFilePaths
            continue

        # If we are examining the secondary group, check to see if filtering was even requested.
        if dialogID == "Secondary Group" and not secondaryPeriodicityGroupSearchRefine.getControllerVar(
        ):
            assert i == 1, "Secondary group encountered on unexpected iteration of for loop: " + str(
                i)
            filePathGroups[2] = secondaryNucMutCountsFilePaths
            filePathGroups[1] = filePathGroups[0].copy()
            filePathGroups[0] += filePathGroups[2]
            continue

        # Determine what normalization methods were requested
        normalizationMethods = list()
        normalizationSelections = selections.getToggleStates(dialogID)[:5]
        if normalizationSelections[0]: normalizationMethods += (1, 2)
        if normalizationSelections[1]: normalizationMethods += (3, 4)
        if normalizationSelections[2]: normalizationMethods += (5, 6)
        if normalizationSelections[3]: normalizationMethods.append(-1)
        if normalizationSelections[4]: normalizationMethods.append(0)

        # Determine what microsatellite stability states were requested.
        acceptableMSCohorts = dict()
        if selections.getToggleStates(dialogID)[7]:
            MSSelection = selections.getDropdownSelections(dialogID + "MS")[0]
            if MSSelection == "MSS": acceptableMSCohorts["MSS"] = None
            elif MSSelection == "MSI": acceptableMSCohorts["MSI"] = None
            else:
                acceptableMSCohorts["MSS"] = None
                acceptableMSCohorts["MSI"] = None

        # Determine what mutation signature states were requested.
        acceptableMutSigCohorts = dict()
        if selections.getToggleStates(dialogID)[8]:
            with open(
                    selections.getIndividualFilePaths(dialogID + "MutSig")[0],
                    'r') as mutSigsFile:
                for line in mutSigsFile:
                    acceptableMutSigCohorts["mut_sig_" + line.strip()] = None

        # Check for custom cohort input
        acceptableCustomCohorts = dict()
        if selections.getToggleStates(dialogID)[9]:

            customCohortsFilePath = selections.getIndividualFilePaths(
                dialogID + "CustomCohorts")[0]
            with open(customCohortsFilePath, 'r') as customCohortsFile:

                for line in customCohortsFile:
                    acceptableCustomCohorts[line.strip()] = None

        # Check for nucleosome map input
        acceptableNucleosomeMaps = dict()
        if selections.getToggleStates(dialogID)[10]:

            acceptableNucMapsFilePath = selections.getIndividualFilePaths(
                dialogID + "NucleosomeMaps")[0]
            with open(acceptableNucMapsFilePath, 'r') as acceptableNucMapsFile:

                for line in acceptableNucMapsFile:
                    acceptableNucleosomeMaps[line.strip()] = None

        # Get the file paths associated with the given parameters.
        if dialogID != "Secondary Group":
            filePathGroups[i] += getFilePathGroup(
                nucleosomeMutationCountsFilePaths, normalizationMethods,
                selections.getToggleStates(dialogID)[5],
                selections.getToggleStates(dialogID)[6], acceptableMSCohorts,
                acceptableMutSigCohorts, acceptableCustomCohorts,
                acceptableNucleosomeMaps)
        else:
            assert i == 1, "Secondary group encountered on unexpected iteration of for loop: " + str(
                i)
            filePathGroups[2] += getFilePathGroup(
                secondaryNucMutCountsFilePaths, normalizationMethods,
                selections.getToggleStates(dialogID)[5],
                selections.getToggleStates(dialogID)[6], acceptableMSCohorts,
                acceptableMutSigCohorts, acceptableCustomCohorts,
                acceptableNucleosomeMaps)
            filePathGroups[1] = filePathGroups[0].copy()
            filePathGroups[0] += filePathGroups[2]

        #If this is the first pass through the loop, set the file paths list to the newly filtered list.
        if i == 0: nucleosomeMutationCountsFilePaths = filePathGroups[0]

    runNucleosomeMutationAnalysis(filePathGroups[0], outputFilePath,
                                  overridePeakPeriodWithExpected, alignStrands,
                                  filePathGroups[1], filePathGroups[2])
def main():

    # Create the Tkinter dialog.
    dialog = TkinterDialog(workingDirectory=getDataDirectory())
    dialog.createMultipleFileSelector("Bed Mutation Files:", 0,
                                      DataTypeStr.mutations + ".bed",
                                      ("Bed Files", ".bed"))

    dialog.createMultipleFileSelector("Nucleosome Map Files", 1,
                                      "nucleosome_map.bed",
                                      ("Bed Files", ".bed"))

    normalizationSelector = dialog.createDynamicSelector(2, 0)
    normalizationSelector.initDropdownController(
        "Normalization Method",
        ("No Normalization", "Singlenuc/Dinuc", "Trinuc/Quadrunuc",
         "Pentanuc/Hexanuc", "Custom Background"))
    customBackgroundFileSelector = normalizationSelector.initDisplay(
        "Custom Background", "customBackground")
    customBackgroundFileSelector.createFileSelector(
        "Custom Background Directory:",
        0, ("Bed Files", ".bed"),
        directory=True)
    customBackgroundFileSelector.createCheckbox("Generate Background now", 1,
                                                0)
    normalizationSelector.initDisplayState()

    dialog.createCheckbox(
        "Include alternative scaling factor indepedent of nucleosome map.", 3,
        0)
    dialog.createLabel('', 4, 0)

    selectNucleosomeDyadRadius = dialog.createDynamicSelector(5, 0)
    selectNucleosomeDyadRadius.initCheckboxController(
        "Run analysis with a single nucleosome dyad radius (73 bp)")
    linkerSelectionDialog = selectNucleosomeDyadRadius.initDisplay(
        1, "singleNuc")
    linkerSelectionDialog.createCheckbox(
        "Include 30 bp linker DNA on either side of single nucleosome dyad radius.",
        0, 0)
    selectNucleosomeDyadRadius.initDisplayState()

    dialog.createCheckbox("Count with a nucleosome group radius (1000 bp)", 6,
                          0)

    # Run the UI
    dialog.mainloop()

    # If no input was received (i.e. the UI was terminated prematurely), then quit!
    if dialog.selections is None: quit()

    # Get the user's input from the dialog.
    selections: Selections = dialog.selections
    mutationFilePaths = selections.getFilePathGroups()[
        0]  # A list of paths to bed mutation files
    nucleosomeMapNames = [
        getIsolatedParentDir(nucleosomeMapFile)
        for nucleosomeMapFile in selections.getFilePathGroups()[1]
    ]

    normalizationMethod = normalizationSelector.getControllerVar(
    )  # The normalization method to be used.
    if normalizationMethod == "Custom Background":
        customBackgroundDir = selections.getFilePaths("customBackground")[
            0]  # Where to find raw counts files to use as custom background
        generateCustomBackgroundNow = selections.getToggleStates(
            "customBackground"
        )[0]  # Whether or not to generate the custom background counts on the fly
    else:
        customBackgroundDir = None
        generateCustomBackgroundNow = False

    useSingleNucRadius = selectNucleosomeDyadRadius.getControllerVar(
    )  # Whether or not to generate data with a 73 bp single nuc dyad radius
    if useSingleNucRadius:
        includeLinker = selections.getToggleStates(
            "singleNuc"
        )[0]  # Whether or not to include 30 bp linker DNA in nucleosome dyad positions
    else:
        includeLinker = False
    useNucGroupRadius = selections.getToggleStates(
    )[1]  # Whether or not to generate data with a 1000 bp nuc group dyad radius
    includeAlternativeScaling = selections.getToggleStates()[
        0]  # Whether or not to include scaling independent of nucleosome map

    # If requested, generate the background counts file(s).
    if generateCustomBackgroundNow:
        generateCustomBackground(customBackgroundDir, nucleosomeMapNames,
                                 useSingleNucRadius, includeLinker,
                                 useNucGroupRadius)

    runAnalysisSuite(mutationFilePaths, nucleosomeMapNames,
                     normalizationMethod, customBackgroundDir,
                     useSingleNucRadius, includeLinker, useNucGroupRadius,
                     includeAlternativeScaling)
示例#3
0
    print("Writing to new file: ", filteredFilename)
    with open(filteredFilePath, 'w') as filteredFile:
        filteredFile.writelines(mutationsToKeep)


# Whitespace for AeStHeTiC pUrPoSeS
print()

# Create the list of mutations that can be omitted.
mutations = ("C>A", "C>G", "C>T", "T>A", "T>C", "T>G")

#Create the Tkinter UI
dialog = TkinterDialog(workingDirectory=getDataDirectory())
dialog.createFileSelector("Bed Mutation File:", 0, ("Bed Files", ".bed"))
# DEPRECATED: dialog.createDropdown("Mutation to omit:",1,0,options=mutations)
dialog.createLabel("Mutations:", 1, 0)
for i, mutation in enumerate(mutations):
    dialog.createCheckbox(mutation, 2 + int(i / 4), i % 4)
dialog.createLabel("Actions:", 4, 0)
dialog.createCheckbox("Omit selected mutations", 5, 0, 2)
dialog.createCheckbox("Keep selected mutations and omit others", 5, 2, 2)
dialog.createCheckbox("Create one file for each selected mutation", 6, 0, 2)
dialog.createReturnButton(7, 0, 2)
dialog.createQuitButton(7, 2, 2)

# Run the UI
dialog.mainloop()

# If no input was received (i.e. the UI was terminated prematurely), then quit!
if dialog.selections is None: quit()