Ejemplo n.º 1
0
    def test_export(self):
        rf = RandomForestClassifier()
        rf.fit(np.array([[0, 1]]), np.array(['cluster_1']))

        classifier = SampleClassifier(rf, np.array(['feature_1', 'feature_2']))
        classifier.logger = MagicMock()

        with context_aware_tempfile('w', delete=False) as output_file:
            classifier.export(output_file)
            output_file.close()
            input_file = open(output_file.name, 'r')
            new_classifier = SampleClassifier.load(input_file, force=True)


################################################################################
##   Uncomment me if you want to create a new classifier test file.  Remember to
##   update the md5sum and sha256sum values in the test_load function above
##   afterwards even if you haven't changed how the classifier is created.
################################################################################
#
#    path_to_classifier = os.path.join(test_data(), 'classifier.pkl')
#    with open(path_to_classifier, 'wb') as output_file:
#      classifier.export(output_file)
#
################################################################################

        self.assertIsInstance(new_classifier.classifier,
                              RandomForestClassifier)
        assert_array_equal(new_classifier.feature_labels,
                           np.array(['feature_1', 'feature_2']))
Ejemplo n.º 2
0
  def test_create_vcf_from_sequences_does_not_delete_existing(self, os_mock):
    builder = SNPFeatureBuilder()
    os_mock.remove.side_effect = os.remove
    with context_aware_tempfile('w', delete=False) as temp_vcf_file:
      builder.vcf_input_file = temp_vcf_file

      fasta_filename = os.path.join(test_data(), 'file_with_SNPs.aln')
      fasta_file = open(fasta_filename, 'r')

      builder.load_fasta_sequences(fasta_file)
      builder.create_vcf_from_sequences()

      self.assertFalse(temp_vcf_file.name in os_mock.remove.call_args_list)
Ejemplo n.º 3
0
    def create_vcf_from_sequences(self):
        with context_aware_tempfile('w', delete=False) as temp_fasta_file:
            try:
                self.vcf_input_file.remove()
            except AttributeError:
                # The file might have already been deleted or might not have been created
                # by us so we shouldn't delete it.
                pass
            self.vcf_input_file = DeletableFile(
                tempfile.NamedTemporaryFile('w', delete=False))
            self.logger.info("Writing sequences to %s" % temp_fasta_file.name)
            self._write_sequences(self.sequences.values(), temp_fasta_file)
            temp_fasta_file.close()
            self.vcf_input_file.close()

            with context_aware_tempdir() as output_directory:
                self.logger.info("Writing snps to %s" %
                                 self.vcf_input_file.name)
                self._run_snp_sites(self.snp_sites_exec,
                                    {'-o': self.vcf_input_file.name},
                                    temp_fasta_file.name, output_directory)

            self.vcf_input_file = DeletableFile(
                open(self.vcf_input_file.name, 'r'))
Ejemplo n.º 4
0
 def context_aware_tempfile_mock(*args, **kwargs):
   with context_aware_tempfile(*args, **kwargs) as tempfile:
     filenames.append(tempfile.name)
     yield tempfile