package org.pdxfinder.repositories;

import org.pdxfinder.dao.ModelCreation;
import org.springframework.data.neo4j.annotation.Query;
import org.springframework.data.neo4j.repository.Neo4jRepository;
import org.springframework.data.repository.query.Param;

import java.util.Collection;

/**
 * Interface describing operations for adding/finding/deleting PDX strain records
 */
public interface ModelCreationRepository extends Neo4jRepository<ModelCreation, Long> {


    ModelCreation findBySourcePdxId(@Param("modelId") String modelId);

    @Query("MATCH (s:Sample)-[i:IMPLANTED_IN]-(mod:ModelCreation) " +
            "WHERE s.sourceSampleId = {sampleId} " +
            "RETURN mod")
    ModelCreation findBySampleId(@Param("sampleId") String sampleId);


    @Query("MATCH (s:Sample)-[o:ORIGIN_TISSUE]-(t:Tissue) " +
            "MATCH (s:Sample)-[cb:CHARACTERIZED_BY]-(mc:MolecularCharacterization)-[aw:ASSOCIATED_WITH]-(ma:MarkerAssociation)-[mar:MARKER]-(m:Marker) " +
            "MATCH (s:Sample)-[ot:OF_TYPE]-(tt:TumorType) " +
            "MATCH (s:Sample)-[ii:IMPLANTED_IN]-(mod:ModelCreation) "+
            "WHERE (toLower(s.diagnosis) CONTAINS toLower({diag}) OR {diag}='') " +
            "AND (m.name IN {markers} OR {markers}=[]) " +
            "AND (s.dataSource IN {dataSource} OR {dataSource}=[]) " +
            "AND (tt.name IN {tumorType} OR {tumorType}=[]) " +
            "RETURN s,o,t,ot, tt, mc, ma, m, mar, cb, aw, ii, mod")
    Collection<ModelCreation> findByMultipleFilters(@Param("diag") String diag, @Param("markers") String[] markers,
                                             @Param("dataSource") String[] dataSource, @Param("tumorType") String[] tumorType);


    @Query("MATCH (st:OntologyTerm)<-[*]-(ot:OntologyTerm) " +
            "MATCH (ot)-[m:MAPPED_TO]-(s:Sample)-[i:IMPLANTED_IN]-(mod:ModelCreation) " +
            "WHERE st.label = {query} " +
            "RETURN s, i, mod")
    Collection<ModelCreation> findByOntology(@Param("query") String query);

}

@@ -747,24 +747,63 @@
 sampleId);%0A%0A
+    //Simple search on sample diagnosis
 %0A    @Query(
@@ -1732,16 +1732,45 @@
 Type);%0A%0A
+    //Ontology powered search
 %0A    @Qu
@@ -1804,18 +1804,20 @@
 )%3C-%5B*%5D-(
-o
 t
+erm
 :Ontolog
@@ -1851,14 +1851,19 @@
 CH (
-o
 t
+erm
 )-%5Bm
+app
 :MAP
@@ -1939,142 +1939,822 @@
    %22
-WHERE st.label = %7Bquery%7D %22 +%0A            %22RETURN s, i, mod%22)%0A    Collection%3CModelCreation%3E findByOntology(@Param(%22query%22) String query
+MATCH (s:Sample)-%5Bo:ORIGIN_TISSUE%5D-(t:Tissue) %22 +%0A            %22MATCH (s:Sample)-%5Bcb:CHARACTERIZED_BY%5D-(mc:MolecularCharacterization)-%5Baw:ASSOCIATED_WITH%5D-(ma:MarkerAssociation)-%5Bmar:MARKER%5D-(m:Marker) %22 +%0A            %22MATCH (s:Sample)-%5Bot:OF_TYPE%5D-(tt:TumorType) %22 +%0A            %22WHERE st.label = %7Bquery%7D %22 +%0A            %22AND (m.name IN %7Bmarkers%7D OR %7Bmarkers%7D=%5B%5D)  %22 +%0A            %22AND (s.dataSource IN %7BdataSource%7D OR %7BdataSource%7D=%5B%5D)  %22 +%0A            %22AND (tt.name IN %7BtumorType%7D OR %7BtumorType%7D=%5B%5D)  %22 +%0A            %22RETURN s,i,o,t,ot, tt, mc, ma, m, mar, cb, aw, mod, mapp%22)%0A    Collection%3CModelCreation%3E findByOntology(@Param(%22query%22) String query, @Param(%22markers%22) String%5B%5D markers,%0A                                             @Param(%22dataSource%22) String%5B%5D dataSource, @Param(%22tumorType%22) String%5B%5D tumorType
 );%0A%0A

Add filters for ontology powered search