def amrsummary(self):
        """
        Create a report combining results from resfinder_assembled and mob_recon_summary reports
        """
        logging.info('Creating AMR summary table from ResFinder and MOB-recon outputs')
        with open(os.path.join(self.reportpath, 'amr_summary.csv'), 'w') as amr:
            data = 'Strain,Gene,Allele,Resistance,PercentIdentity,Contig,Location,PlasmidIncompatibilitySets\n'
            for sample in self.metadata:
                # Initialise a dictionary to store a set of all the incompatibility types listed for a contig.
                # As the inc type will only be located on one of possibly several contigs associated with a predicted
                # plasmid, it is nice to know details about the plasmid
                inc_dict = dict()
                for primarykey, results in sample[self.analysistype].report_dict.items():
                    try:
                        inc = results['cluster_id']
                        # Convert the rep_type field (predicted incompatibilities) into a more a consistent
                        # format - pandas will call empty fields 'nan', which is a float
                        rep = str(results['rep_type']).replace(',', ';') if str(results['rep_type']) != 'nan' else 'ND'
                        # Add the incompatibility to the set
                        try:
                            inc_dict[inc].add(rep)
                        except KeyError:
                            inc_dict[inc] = set()
                            inc_dict[inc].add(rep)
                    except KeyError:
                        pass
                #
                for primarykey, results in sample[self.analysistype].report_dict.items():
                    try:
                        contig = results['contig_id'].split('|')[1]
                        # Unicycler gives contigs names such as: 3_length=187116_depth=1.60x_circular=true - test
                        # to see if the contig name looks unicycler-like, and set the name appropriately (in this
                        # case, it would be 3)
                        if contig.split('_')[1].startswith('length'):
                            contig = contig.split('_')[0]
                        # Use the list of results from the resfinder analyses
                        for amr_result in sample.resfinder_assembled.sampledata:
                            # Ensure that the current contig is the same as the one in the resfinder results. Ensure
                            # that the slice of the amr result is treated as a string. Unicycler contigs seem to be
                            # treated as integers
                            if contig == str(amr_result[-1]):
                                # Set up the output string
                                data += '{sn},'.format(sn=sample.name)
                                # Add the resistance and MOB recon outputs for the strain
                                data += '{amr},{mob}\n'\
                                    .format(amr=','.join(str(res) if str(res) != 'nan' else 'ND' for res in
                                                         amr_result[0:4]),
                                            mob=','.join(str(res) if str(res) != 'nan' else 'ND' for res in
                                                         [contig, results['cluster_id'],
                                                          ';'.join(sorted(inc_dict[str(results['cluster_id'])]))
                                                          ]
                                                         )
                                            )
                    except KeyError:
                        pass
            amr.write(data)
def amrsummary ( self ) : logging . info ( 'Creating AMR summary table from ResFinder and MOB-recon outputs' ) with open ( os . path . join ( self . reportpath , 'amr_summary.csv' ) , 'w' ) as amr : data = 'Strain,Gene,Allele,Resistance,PercentIdentity,Contig,Location,PlasmidIncompatibilitySets\n' for sample in self . metadata : inc_dict = dict ( ) for primarykey , results in sample [ self . analysistype ] . report_dict . items ( ) : try : inc = results [ 'cluster_id' ] rep = str ( results [ 'rep_type' ] ) . replace ( ',' , ';' ) if str ( results [ 'rep_type' ] ) != 'nan' else 'ND' try : inc_dict [ inc ] . add ( rep ) except KeyError : inc_dict [ inc ] = set ( ) inc_dict [ inc ] . add ( rep ) except KeyError : pass for primarykey , results in sample [ self . analysistype ] . report_dict . items ( ) : try : contig = results [ 'contig_id' ] . split ( '|' ) [ 1 ] if contig . split ( '_' ) [ 1 ] . startswith ( 'length' ) : contig = contig . split ( '_' ) [ 0 ] for amr_result in sample . resfinder_assembled . sampledata : if contig == str ( amr_result [ - 1 ] ) : data += '{sn},' . format ( sn = sample . name ) data += '{amr},{mob}\n' . format ( amr = ',' . join ( str ( res ) if str ( res ) != 'nan' else 'ND' for res in amr_result [ 0 : 4 ] ) , mob = ',' . join ( str ( res ) if str ( res ) != 'nan' else 'ND' for res in [ contig , results [ 'cluster_id' ] , ';' . join ( sorted ( inc_dict [ str ( results [ 'cluster_id' ] ) ] ) ) ] ) ) except KeyError : pass amr . write ( data )
Create a report combining results from resfinder_assembled and mob_recon_summary reports