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);

    //Simple search on sample diagnosis
    @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);

    //Ontology powered search
    @Query("MATCH (st:OntologyTerm)<-[*]-(term:OntologyTerm) " +
            "MATCH (term)-[mapp:MAPPED_TO]-(s:Sample)-[i:IMPLANTED_IN]-(mod:ModelCreation) " +
            "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) " +
            "WHERE st.label = {query} " +
            "AND (m.name IN {markers} OR {markers}=[])  " +
            "AND (s.dataSource IN {dataSource} OR {dataSource}=[])  " +
            "AND (tt.name IN {tumorType} OR {tumorType}=[])  " +
            "RETURN s,i,o,t,ot, tt, mc, ma, m, mar, cb, aw, mod, mapp")
    Collection<ModelCreation> findByOntology(@Param("query") String query, @Param("markers") String[] markers,
                                             @Param("dataSource") String[] dataSource, @Param("tumorType") String[] tumorType);

}

@@ -1761,16 +1761,920 @@
 d search
+: molchar on samples%0A    //TODO: Look up mol char and markers, deal with missing nodes%0A    @Query(%22MATCH (st:OntologyTerm)%3C-%5B*%5D-(term:OntologyTerm)-%5Bmapp:MAPPED_TO%5D-(s:Sample)-%5Bi:IMPLANTED_IN%5D-(mod:ModelCreation) %22 +%0A            %22MATCH (s:Sample)-%5Bo:ORIGIN_TISSUE%5D-(t:Tissue) %22 +%0A            %22MATCH (s:Sample)-%5Bot:OF_TYPE%5D-(tt:TumorType) %22 +%0A            %22WHERE st.label = %7Bquery%7D %22 +%0A            %22OR term.label = %7Bquery%7D %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 mod, s,i,o,t,ot, tt%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    //Ontology powered search: molchar on specimen // BROKEN
 %0A    @Qu
@@ -2730,38 +2730,8 @@
 erm)
- %22 +%0A            %22MATCH (term)
 -%5Bma
@@ -3110,32 +3110,73 @@
 l = %7Bquery%7D %22 +%0A
+            %22OR term.label = %7Bquery%7D %22 +%0A
             %22AND
@@ -3375,16 +3375,21 @@
  %22RETURN
+ mod,
  s,i,o,t
@@ -3423,19 +3423,8 @@
 , aw
-, mod, mapp
 %22)%0A 
@@ -3458,32 +3458,33 @@
 %3E findByOntology
+2
 (@Param(%22query%22)
@@ -3663,10 +3663,11 @@
 Type);%0A%0A
+%0A
 %7D%0A

Remove matching MolChar and Markers data