Parse the results into a report

def reporter(self):
        """
        Parse the results into a report
        """
        # Initialise variables
        header = ''
        row = ''
        databasedict = dict()
        # Load the database sequence type into a dictionary
        strainprofile = os.path.join(self.profilelocation, 'strainprofiles.txt')
        databaseprofile = DictReader(open(strainprofile))
        # Put the strain profile dictionary into a more easily searchable format
        for data in databaseprofile:
            databasedict[data['Strain']] = data['SequenceType']
        for sample in self.metadata.samples:
            closestmatches = list()
            if sample[self.analysistype].reportdir != 'NA':
                if type(sample[self.analysistype].allelenames) == list:
                    # Populate the header with the appropriate data, including all the genes in the list of targets
                    header = 'Strain,SequenceType,Matches,Mismatches,NA,TotalGenes,ClosestDatabaseMatch,{},\n' \
                        .format(','.join(sorted(sample[self.analysistype].allelenames)))
                sortedmatches = sorted(sample[self.analysistype].profilematches.items(),
                                       key=operator.itemgetter(1), reverse=True)[0]
                closestseqtype = sortedmatches[0]
                # Pull out the closest database match
                for strain, seqtype in databasedict.items():
                    if seqtype == closestseqtype:
                        closestmatches.append(strain)
                sample[self.analysistype].closestseqtype = closestseqtype
                nummatches = int(sortedmatches[1])
                numna = 0
                queryallele = list()
                # Get all the alleles into a list
                for gene, allele in sorted(sample[self.analysistype].profiledata[closestseqtype].items()):
                    try:
                        # Extract the allele (anything after the -) from the allele matches
                        query = sample[self.analysistype].allelematches[gene].split('-')[1]
                        if allele == query:
                            queryallele.append(query)
                        else:
                            queryallele.append('{} ({})'.format(query, allele))
                    except KeyError:
                        queryallele.append('NA')
                        numna += 1
                mismatches = len(sample[self.analysistype].alleles) - nummatches - numna
                row += '{},{},{},{},{},{},{},{}'\
                    .format(sample.name, closestseqtype, nummatches, mismatches, numna,
                            len(sample[self.analysistype].alleles), ','.join(closestmatches), ','.join(queryallele))
                row += '\n'
        # Create the report folder
        make_path(self.reportpath)
        # Create the report containing all the data from all samples
        with open(os.path.join(self.reportpath, '{}.csv'.format(self.analysistype)), 'w') as combinedreport:
            # Write the results to this report
            combinedreport.write(header)
            combinedreport.write(row)