Use bbmerge to merge paired FASTQ files for use in metagenomics pipelines. Create a report with the
        total number of reads, and the number of reads that could be paired
        :param sample: metadata sample object flagged as a metagenome

def merge(self, sample):
        """
        Use bbmerge to merge paired FASTQ files for use in metagenomics pipelines. Create a report with the
        total number of reads, and the number of reads that could be paired
        :param sample: metadata sample object flagged as a metagenome
        """
        # Set the assembly file to 'NA' as assembly is not desirable for metagenomes
        sample.general.assemblyfile = 'NA'
        # Can only merge paired-end
        if len(sample.general.fastqfiles) == 2:
            outpath = os.path.join(sample.general.outputdirectory, 'merged_reads')
            make_path(outpath)
            # Merge path - keep all the merged FASTQ files in one directory
            merge_path = os.path.join(self.path, 'merged_reads')
            make_path(merge_path)
            # Set the name of the merged, and unmerged files
            sample.general.mergedreads = \
                os.path.join(merge_path, '{}_paired.fastq.gz'.format(sample.name))
            log = os.path.join(outpath, 'log')
            error = os.path.join(outpath, 'err')
            try:
                if not os.path.isfile(sample.general.mergedreads):
                    # Run the merging command
                    out, err, cmd = bbtools.bbmerge(forward_in=sorted(sample.general.trimmedcorrectedfastqfiles)[0],
                                                    merged_reads=sample.general.mergedreads,
                                                    mix=True,
                                                    returncmd=True,
                                                    threads=self.cpus)
                    write_to_logfile(out, err, self.logfile, sample.general.logout, sample.general.logerr, None, None)
                    with open(log, 'w') as log_file:
                        log_file.write(out)
                    with open(error, 'w') as error_file:
                        error_file.write(err)
            except (CalledProcessError, IndexError):
                delattr(sample.general, 'mergedreads')
            # Set the name of the report to store the metagenome file merging results
            report = os.path.join(self.reportpath, 'merged_metagenomes.csv')
            # Extract the total number of reads, and the number of reads that could be paired from the bbmerge
            # err stream
            num_reads, num_pairs = self.reads(error)
            # If the report doesn't exist, create it with the header and the results from the first sample
            if not os.path.isfile(report):
                with open(report, 'w') as report_file:
                    report_file.write('Sample,TotalReads,PairedReads\n{sample},{total},{paired}\n'
                                      .format(sample=sample.name,
                                              total=num_reads,
                                              paired=num_pairs))
            # If the report exists, open it to determine which samples have already been added - useful if re-running
            # the analysis
            else:
                lines = list()
                with open(report, 'r') as report_file:
                    for line in report_file:
                        lines.append(line.split(',')[0])
                # Add the results to the report
                if sample.name not in lines:
                    with open(report, 'a+') as report_file:
                        report_file.write('{sample},{total},{paired}\n'
                                          .format(sample=sample.name,
                                                  total=num_reads,
                                                  paired=num_pairs))