<commit_before>package us.kbase.kbasegenefamilies.test;

import java.io.*;
import java.util.*;
import java.net.URL;

import org.junit.Test;
import static junit.framework.Assert.*;

import org.strbio.IO;
import org.strbio.util.*;
import com.fasterxml.jackson.databind.*;

import us.kbase.auth.AuthService;
import us.kbase.auth.AuthToken;
import us.kbase.common.service.*;
import us.kbase.workspace.*;
import us.kbase.kbasegenomes.*;
import us.kbase.kbasegenefamilies.*;
import us.kbase.common.taskqueue.TaskQueueConfig;

/**
   Tests for setting up sample db and annotating E. coli locally
*/
public class EColiTest {
    private static final String genomeWsName = "KBasePublicGenomesV4";
    private static final String domainWsName = "KBasePublicGeneDomains";
    private static final String domainLibraryType = "KBaseGeneFamilies.DomainLibrary";
    private static final String domainModelSetType = "KBaseGeneFamilies.DomainModelSet";
    private static final String domainAnnotationType = "KBaseGeneFamilies.DomainAnnotation";
    private static final String ecoliRef = genomeWsName+"/kb|g.0";
    private static final String smartRef = domainWsName+"/SMART-only";
    private static final String tigrRef = domainWsName+"/TIGRFAMs-only";
    private static final String allLibsRef = domainWsName+"/All";

    /**
       check that we can read E coli genome from WS or file;
       if we get from WS, cache in file to avoid delays/loading server
    */
    @Test public void getEColi() throws Exception {
	Genome genome = null;
	
	ObjectMapper mapper = new ObjectMapper();

	File f = new File("/kb/dev_container/modules/gene_families/data/tmp/g.0");
	if (f.canRead()) {
	    System.out.println("Reading genome from file");
	    try {
		genome = mapper.readValue(f, Genome.class);
	    }
	    catch (Exception e) {
		genome = null;
	    }
	}

	if (genome==null) {
	    System.out.println("Reading genome from WS");
	    WorkspaceClient wc = createWsClient(null);
	    genome = wc.getObjects(Arrays.asList(new ObjectIdentity().withRef(ecoliRef))).get(0).getData().asClassInstance(Genome.class);
	    mapper.writeValue(f,genome);
	}
	
	System.out.println(genome.getScientificName());
	assertEquals(genome.getScientificName(), "Escherichia coli K12");
    }

    /**
       Check that we can get the SMART-only DomainModelSet from the
       public workspace.
    */
    @Test
    public void getSMART() throws Exception {
	WorkspaceClient wc = createWsClient(getDevToken());
	DomainModelSet smart = wc.getObjects(Arrays.asList(new ObjectIdentity().withRef(smartRef))).get(0).getData().asClassInstance(DomainModelSet.class);

	assertEquals(smart.getSetName(),"SMART-only");
    }

    /**
       Check that we can annotate E. coli with SMART.  This is
       fairly fast.
    */
    @Test
	public void searchEColiPSSM() throws Exception {

	AuthToken token = getDevToken();
	WorkspaceClient wc = createWsClient(token);

	ObjectStorage storage = SearchDomainsBuilder.createDefaultObjectStorage(wc);

	DomainSearchTask dst = new DomainSearchTask(new File("/kb/dev_container/modules/gene_families/data/tmp"), storage);
	
	DomainAnnotation results = dst.runDomainSearch(token.toString(),
						       smartRef,
						       ecoliRef);

	/*
	wc.saveObjects(new SaveObjectsParams()
		       .withWorkspace(domainWsName)
		       .withObjects(Arrays.asList(new ObjectSaveData()
						  .withType(domainAnnotationType)
						  .withName("SMART-g.0-2")
						  .withMeta(DomainSearchTask.getMetadata(results))
						  .withData(new UObject(results)))));
	*/
    }

    /**
       Check that we can annotate E. coli with TIGRFAMs.  Takes ~12 min<commit_msg>Enabled saving of E coli test results, to ensure no regressions caused by recent changes<commit_after>package us.kbase.kbasegenefamilies.test;

import java.io.*;
import java.util.*;
import java.net.URL;

import org.junit.Test;
import static junit.framework.Assert.*;

import org.strbio.IO;
import org.strbio.util.*;
import com.fasterxml.jackson.databind.*;

import us.kbase.auth.AuthService;
import us.kbase.auth.AuthToken;
import us.kbase.common.service.*;
import us.kbase.workspace.*;
import us.kbase.kbasegenomes.*;
import us.kbase.kbasegenefamilies.*;
import us.kbase.common.taskqueue.TaskQueueConfig;

/**
   Tests for setting up sample db and annotating E. coli locally
*/
public class EColiTest {
    private static final String genomeWsName = "KBasePublicGenomesV4";
    private static final String domainWsName = "KBasePublicGeneDomains";
    private static final String domainLibraryType = "KBaseGeneFamilies.DomainLibrary";
    private static final String domainModelSetType = "KBaseGeneFamilies.DomainModelSet";
    private static final String domainAnnotationType = "KBaseGeneFamilies.DomainAnnotation";
    private static final String ecoliRef = genomeWsName+"/kb|g.0";
    private static final String smartRef = domainWsName+"/SMART-only";
    private static final String tigrRef = domainWsName+"/TIGRFAMs-only";
    private static final String allLibsRef = domainWsName+"/All";

    /**
       check that we can read E coli genome from WS or file;
       if we get from WS, cache in file to avoid delays/loading server
    */
    @Test public void getEColi() throws Exception {
	Genome genome = null;
	
	ObjectMapper mapper = new ObjectMapper();

	File f = new File("/kb/dev_container/modules/gene_families/data/tmp/g.0");
	if (f.canRead()) {
	    System.out.println("Reading genome from file");
	    try {
		genome = mapper.readValue(f, Genome.class);
	    }
	    catch (Exception e) {
		genome = null;
	    }
	}

	if (genome==null) {
	    System.out.println("Reading genome from WS");
	    WorkspaceClient wc = createWsClient(null);
	    genome = wc.getObjects(Arrays.asList(new ObjectIdentity().withRef(ecoliRef))).get(0).getData().asClassInstance(Genome.class);
	    mapper.writeValue(f,genome);
	}
	
	System.out.println(genome.getScientificName());
	assertEquals(genome.getScientificName(), "Escherichia coli K12");
    }

    /**
       Check that we can get the SMART-only DomainModelSet from the
       public workspace.
    */
    @Test
    public void getSMART() throws Exception {
	WorkspaceClient wc = createWsClient(getDevToken());
	DomainModelSet smart = wc.getObjects(Arrays.asList(new ObjectIdentity().withRef(smartRef))).get(0).getData().asClassInstance(DomainModelSet.class);

	assertEquals(smart.getSetName(),"SMART-only");
    }

    /**
       Check that we can annotate E. coli with SMART.  This is
       fairly fast.
    */
    @Test
	public void searchEColiPSSM() throws Exception {

	AuthToken token = getDevToken();
	WorkspaceClient wc = createWsClient(token);

	ObjectStorage storage = SearchDomainsBuilder.createDefaultObjectStorage(wc);

	DomainSearchTask dst = new DomainSearchTask(new File("/kb/dev_container/modules/gene_families/data/tmp"), storage);
	
	DomainAnnotation results = dst.runDomainSearch(token.toString(),
						       smartRef,
						       ecoliRef);

	wc.saveObjects(new SaveObjectsParams()
		       .withWorkspace(domainWsName)
		       .withObjects(Arrays.asList(new ObjectSaveData()
						  .withType(domainAnnotationType)
						  .withName("SMART-g.0-2")
						  .withMeta(DomainSearchTask.getMetadata(results))
						  .withData(new UObject(results)))));
    }

    /**
       Check that we can annotate E. coli with TIGRFAMs.  Takes ~12 min
{'fraction_non_alphanumeric': 0.11023515019709121, 'fraction_numerical': 0.0029903493271714013, 'mean_word_length': 3.831254103742613, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5038564', 'n_tokens_mistral': 2443, 'n_tokens_neox': 2178, 'n_words': 537}