def query_prep(self):
     """
     Create metadata objects for each sample
     """
     logging.info('Preparing query files')
     # Find all the sequence files in the path
     fastas = sorted(glob(os.path.join(self.query_path, '*.fasta')))
     for fasta in fastas:
         name = os.path.splitext(os.path.basename(fasta))[0]
         if name != 'combinedtargets':
             # Create a metadata object for each sample
             metadata = MetadataObject()
             metadata.samples = list()
             # Populate the metadata object with the required attributes
             metadata.name = name
             metadata.general = GenObject()
             metadata.commands = GenObject()
             metadata.alleles = GenObject()
             metadata.alleles.outputdirectory = os.path.join(self.query_path, metadata.name)
             # Set the name of the BLAST output file
             metadata.alleles.blast_report = os.path.join(metadata.alleles.outputdirectory,
                                                          '{seq_id}.tsv'.format(seq_id=metadata.name))
             try:
                 os.remove(metadata.alleles.blast_report)
             except FileNotFoundError:
                 pass
             make_path(metadata.alleles.outputdirectory)
             metadata.general.bestassemblyfile = relative_symlink(src_file=fasta,
                                                                  output_dir=metadata.alleles.outputdirectory,
                                                                  export_output=True)
             metadata.samples.append(metadata)
             self.runmetadata.samples.append(metadata)
 def run_blast(self):
     """
     BLAST the alleles against the genomes
     """
     logging.info('BLASTing alleles against sequence files')
     for query_file in self.query_files:
         # Create a metadata object to store all the sample-specific information
         sample = MetadataObject()
         sample.alleles = GenObject()
         local_db = os.path.splitext(query_file)[0]
         sample.name = os.path.basename(local_db)
         # Set the name of the BLAST output file
         sample.alleles.blast_report = os.path.join(
             self.reportpath, '{seq_id}.tsv'.format(seq_id=sample.name))
         # Update the list of metadata objects with this sample
         self.runmetadata.samples.append(sample)
         self.blast_reports.append(sample.alleles.blast_report)
         # Run the appropriate BLAST command: BLASTn for nt; tBLASTn for aa against translated nt
         if self.amino_acid:
             blast = NcbitblastnCommandline(db=local_db,
                                            query=self.target_file,
                                            num_alignments=100000000,
                                            evalue=0.001,
                                            num_threads=self.cpus,
                                            task='tblastn',
                                            outfmt=self.outfmt,
                                            word_size=3,
                                            out=sample.alleles.blast_report)
         else:
             blast = NcbiblastnCommandline(db=local_db,
                                           query=self.target_file,
                                           num_alignments=100000000,
                                           evalue=0.001,
                                           num_threads=self.cpus,
                                           task='blastn',
                                           outfmt=self.outfmt,
                                           out=sample.alleles.blast_report)
         if not os.path.isfile(sample.alleles.blast_report):
             # Run BLAST - supply the record sequence as stdin, so BLAST doesn't look for an input file
             try:
                 blast()
             # BLAST can have issues with genomes that have very large contigs. Retry the analysis using only one
             # thread
             except ApplicationError:
                 os.remove(sample.alleles.blast_report)
                 blast = NcbitblastnCommandline(
                     db=local_db,
                     query=self.target_file,
                     num_alignments=100000000,
                     evalue=0.001,
                     num_threads=1,
                     task='tblastn',
                     outfmt=self.outfmt,
                     word_size=3,
                     out=sample.alleles.blast_report)
                 blast()