def parserunstats(self):
        """Parses the XML run statistics file (GenerateFASTQRunStatistics.xml). In some cases, the file is not
        available. Equivalent data can be pulled from Basespace.Generate a text file  name indexingQC.txt containing
        the copied tables from the Indexing QC tab of the run on Basespace"""
        # metadata = GenObject()
        # If the default file GenerateFASTQRunStatistics.xml is present, parse it
        if os.path.isfile(os.path.join(self.path, "GenerateFASTQRunStatistics.xml")):
            # Create a list of keys for which values are to be extracted
            datalist = ["SampleNumber", "SampleID", "SampleName", "NumberOfClustersPF"]
            # Load the file as an xml ElementTree object
            runstatistics = ElementTree.ElementTree(file=os.path.join(self.path, "GenerateFASTQRunStatistics.xml"))
            # Iterate through all the elements in the object
            # .iterfind() allow for the matching and iterating though matches
            # This is stored as a float to allow subsequent calculations
            tclusterspf = [float(element.text) for element in runstatistics.iterfind("RunStats/NumberOfClustersPF")][0]
            # Iterate through all the elements (strains) in the OverallSamples/SummarizedSampleStatistics category
            for element in runstatistics.iterfind("OverallSamples/SummarizedSampleStatistics"):
                # List comprehension. Essentially iterate through each element for each category in datalist:
                # (element.iter(category) and pull out the value for nestedelement
                straindata = [nestedelement.text for category in datalist for nestedelement in element.iter(category)]
                # Try and replicate the Illumina rules to create file names from "Sample_Name"
                samplename = samplenamer(straindata, 1)
                # Calculate the percentage of clusters associated with each strain
                # noinspection PyTypeChecker
                percentperstrain = "{:.2f}".format((float(straindata[3]) / tclusterspf * 100))
                try:
                    # Use the sample number -1 as the index in the list of objects created in parsesamplesheet
                    strainindex = int(straindata[0]) - 1
                    # Set run to the .run object of self.samples[index]
                    run = self.samples[strainindex].run
                    # An assertion that compares the sample computer above to the previously entered sample name
                    # to ensure that the samples are the same
                    assert self.samples[strainindex].name == samplename, \
                        "Sample name does not match object name {0!r:s}".format(straindata[1])
                    # Add the appropriate values to the strain metadata object
                    run.SampleNumber = straindata[0]
                    run.NumberofClustersPF = straindata[3]
                    run.TotalClustersinRun = tclusterspf
                    run.PercentOfClusters = percentperstrain
                    run.flowcell = self.flowcell
                    run.instrument = self.instrument
                except IndexError:
                    pass
        elif os.path.isfile(os.path.join(self.path, 'indexingQC.txt')):
            # Grab the first element from the second line in the file
            tclusterspf = float(getline(os.path.join(self.path, "indexingQC.txt"), 2).split("\t")[0])
            # Open the file and extract the relevant data
            with open(os.path.join("indexingQC.txt")) as indexqc:
                # Iterate through the file
                for line in indexqc:
                    # Once "Index" is encountered, iterate through the rest of the file
                    if "Index" in line:
                        for subline in indexqc:
                            straindata = [x.rstrip() for x in subline.rstrip().split("\t")]
                            # Try and replicate the Illumina rules to create file names from "Sample_Name"
                            samplename = samplenamer(straindata, 1)
                            # Use the sample number -1 as the index in the list of objects created in parsesamplesheet
                            strainindex = int(straindata[0]) - 1
                            # Set run to the .run object of self.samples[index]
                            run = self.samples[strainindex].run
                            # An assertion that compares the sample computer above to the previously entered sample name
                            # to ensure that the samples are the same
                            assert self.samples[strainindex].name == samplename, \
                                "Sample name {} does not match object name {}" \
                                .format(self.samples[strainindex].name, samplename)
                            # Extract and format the percent of reads (passing filter) associated with each sample
                            # noinspection PyTypeChecker
                            percentperstrain = float("{:.2f}".format(float(straindata[5])))
                            # Calculate the number of reads passing filter associated with each sample:
                            # percentage of reads per strain times the total reads passing filter divided by 100
                            numberofclusterspf = int(percentperstrain * tclusterspf / 100)
                            # Update the object with the variables
                            run.SampleNumber = straindata[0]
                            run.NumberofClustersPF = numberofclusterspf
                            run.TotalClustersinRun = tclusterspf
                            run.PercentOfClusters = percentperstrain
                            run.flowcell = self.flowcell
                            run.instrument = self.instrument
        else:
            strainindex = 0
            for i in range(len(self.samples)):
                # Set run to the .run object of self.samples[index]
                run = self.samples[strainindex].run
                # Update the object with the variables
                run.SampleNumber = strainindex + 1
                run.NumberofClustersPF = 'NA'
                run.TotalClustersinRun = 'NA'
                run.PercentOfClusters = 'NA'
                run.flowcell = self.flowcell
                run.instrument = self.instrument
                strainindex += 1
def parserunstats ( self ) : if os . path . isfile ( os . path . join ( self . path , "GenerateFASTQRunStatistics.xml" ) ) : datalist = [ "SampleNumber" , "SampleID" , "SampleName" , "NumberOfClustersPF" ] runstatistics = ElementTree . ElementTree ( file = os . path . join ( self . path , "GenerateFASTQRunStatistics.xml" ) ) tclusterspf = [ float ( element . text ) for element in runstatistics . iterfind ( "RunStats/NumberOfClustersPF" ) ] [ 0 ] for element in runstatistics . iterfind ( "OverallSamples/SummarizedSampleStatistics" ) : straindata = [ nestedelement . text for category in datalist for nestedelement in element . iter ( category ) ] samplename = samplenamer ( straindata , 1 ) percentperstrain = "{:.2f}" . format ( ( float ( straindata [ 3 ] ) / tclusterspf * 100 ) ) try : strainindex = int ( straindata [ 0 ] ) - 1 run = self . samples [ strainindex ] . run assert self . samples [ strainindex ] . name == samplename , "Sample name does not match object name {0!r:s}" . format ( straindata [ 1 ] ) run . SampleNumber = straindata [ 0 ] run . NumberofClustersPF = straindata [ 3 ] run . TotalClustersinRun = tclusterspf run . PercentOfClusters = percentperstrain run . flowcell = self . flowcell run . instrument = self . instrument except IndexError : pass elif os . path . isfile ( os . path . join ( self . path , 'indexingQC.txt' ) ) : tclusterspf = float ( getline ( os . path . join ( self . path , "indexingQC.txt" ) , 2 ) . split ( "\t" ) [ 0 ] ) with open ( os . path . join ( "indexingQC.txt" ) ) as indexqc : for line in indexqc : if "Index" in line : for subline in indexqc : straindata = [ x . rstrip ( ) for x in subline . rstrip ( ) . split ( "\t" ) ] samplename = samplenamer ( straindata , 1 ) strainindex = int ( straindata [ 0 ] ) - 1 run = self . samples [ strainindex ] . run assert self . samples [ strainindex ] . name == samplename , "Sample name {} does not match object name {}" . format ( self . samples [ strainindex ] . name , samplename ) percentperstrain = float ( "{:.2f}" . format ( float ( straindata [ 5 ] ) ) ) numberofclusterspf = int ( percentperstrain * tclusterspf / 100 ) run . SampleNumber = straindata [ 0 ] run . NumberofClustersPF = numberofclusterspf run . TotalClustersinRun = tclusterspf run . PercentOfClusters = percentperstrain run . flowcell = self . flowcell run . instrument = self . instrument else : strainindex = 0 for i in range ( len ( self . samples ) ) : run = self . samples [ strainindex ] . run run . SampleNumber = strainindex + 1 run . NumberofClustersPF = 'NA' run . TotalClustersinRun = 'NA' run . PercentOfClusters = 'NA' run . flowcell = self . flowcell run . instrument = self . instrument strainindex += 1
Parses the XML run statistics file (GenerateFASTQRunStatistics.xml). In some cases, the file is not
        available. Equivalent data can be pulled from Basespace.Generate a text file  name indexingQC.txt containing
        the copied tables from the Indexing QC tab of the run on Basespace