package owltools.gaf.rules.go;

import java.util.Arrays;
import java.util.List;

import org.semanticweb.owlapi.model.OWLOntology;

import owltools.gaf.rules.AnnotationRule;
import owltools.gaf.rules.AnnotationRulesFactoryImpl;
import owltools.graph.OWLGraphWrapper;
import owltools.io.ParserWrapper;

public class GoAnnotationRulesFactoryImpl extends AnnotationRulesFactoryImpl {

	private final BasicChecksRule basicChecksRule;
	private final GoAnnotationTaxonRule taxonRule;
	private final GoClassReferenceAnnotationRule referenceAnnotationRule;
	
	public GoAnnotationRulesFactoryImpl() {
		this("http://www.geneontology.org/quality_control/annotation_checks/annotation_qc.xml",
				"http://www.geneontology.org/doc/GO.xrf_abbs",
				Arrays.asList("http://www.geneontology.org/ontology/editors/gene_ontology_write.obo",
					"http://www.geneontology.org/quality_control/annotation_checks/taxon_checks/taxon_go_triggers.obo",
					"http://www.geneontology.org/quality_control/annotation_checks/taxon_checks/ncbi_taxon_slim.obo",
					"http://www.geneontology.org/quality_control/annotation_checks/taxon_checks/taxon_union_terms.obo"));
	}
	
	public GoAnnotationRulesFactoryImpl(String qcfile, String xrfabbslocation, List<String> ontologies) {
		this(qcfile, xrfabbslocation, getOntologies(ontologies));
	}
	
	public GoAnnotationRulesFactoryImpl(String qcfile, String xrfabbslocation, OWLGraphWrapper graph) {
		super(qcfile);
		basicChecksRule = new BasicChecksRule(xrfabbslocation);
		taxonRule = new GoAnnotationTaxonRule(graph);
		referenceAnnotationRule = new GoClassReferenceAnnotationRule(graph);
	}
	
	private static OWLGraphWrapper getOntologies(List<String> ontologylocations) {
		try {
			ParserWrapper p = new ParserWrapper();
			OWLGraphWrapper wrapper = null;
			for (String location : ontologylocations) {
				if(wrapper==null){
					wrapper = p.parseToOWLGraph(location);
				}else{
					wrapper.addSupportOntology(p.parse(location));
				}
			}
			for(OWLOntology sont: wrapper.getSupportOntologySet()){
				wrapper.mergeOntology(sont);
			}
			return wrapper;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	} 
	
	@Override
	protected AnnotationRule getClassForName(String className) throws Exception {
		if ("org.geneontology.gold.rules.BasicChecksRule".equals(className)) {
			return basicChecksRule;
		}
		if ("org.geneontology.gold.rules.AnnotationTaxonRule".equals(className)) {
			return taxonRule;
		}
		if ("org.geneontology.gold.rules.GoClassReferenceAnnotationRule".equals(className)) {
			return referenceAnnotationRule;
		}
		return super.getClassForName(className);
	}
}

@@ -1127,32 +1127,248 @@
 ms.obo%22));%0A%09%7D%0A%09%0A
+%09public GoAnnotationRulesFactoryImpl(OWLGraphWrapper graph) %7B%0A%09%09this(%22http://www.geneontology.org/quality_control/annotation_checks/annotation_qc.xml%22,%0A%09%09%09%09%22http://www.geneontology.org/doc/GO.xrf_abbs%22, graph);%0A%09%7D%0A%09%0A
 %09public GoAnnota

add additional constructor to just overwrite the ontology