2014-06-09 91 views
1

我想實現一個邏輯門類型聚合操作。而且我在編寫一個在合理時間內執行計算的實現方面遇到問題。我認爲我的邏輯上有效,但速度很慢,我認爲它不是必須的。我認爲應該可以做到這一點,而不是使用許多'findall或cut'。prolog邏輯門聚合遞歸優化

我有一個約10,000列和70行的表。行對應於樣本,列對應於探針。表中的每個值都是一個或一個零(樣本中探針的狀態)。

多個探針編碼一種蛋白質。 (多對一的關係)所以我想用Logical OR操作將探針列聚合到蛋白質列。

除此之外,一些蛋白質是蛋白質複合物或蛋白質組的一部分。除了含有蛋白質之外,蛋白質複合物和蛋白質集合都可以包含蛋白質複合物或蛋白質集合。所以它們可以是一種遞歸關係。我想將蛋白質組作爲OR門和蛋白質複合體建模爲AND門。我一般將蛋白質,蛋白質組和複合體稱爲「實體」。

因此,我希望有一個謂詞,我可以問一個蛋白質或實體是否在快速工作的樣本中打開或關閉。

如果其他一些謂詞不清楚,我可以告訴你他們做了什麼。

protein(Sample, Reactome_Id, State):- 
    setof(Sample, Probe^samples(Sample, Probe, ProbeValue), Samples), 
    %sample/3 is a set of facts that correspond to the described table 
    member(X, Samples), %used to generate Sample Id's %this seems wasteful 
    protein_reactome_Id_to_Uniprot_Id(Reactome_Id, UniprotId), % a set of facts matching two types of id 
    %used to generate uniprot ids 
    findall(Value, uniProt_Sample_Probes(UniprotId,X,_,Value),Vs), 
    Vs = [_|_],  %Check list is not empty already 
    delete(Vs,0,ListOfOnes), 
    (ListOfOnes=[]-> (State is 0, write('OFF'));(State is 1,write('ON'))). 
    %As this is an or I think I should just be able to find a single 1 and cut for the on case and if this is not possible to say it is off. 

%if a (simple) entity is a protein set and its state is on 
%this is a base case where an entity does not have complexs or sets inside it 
state_of_entity(Entity,State,Sample):- 
    all_children_proteins(Entity), %checks that all children are of type protein 
    type(Entity, protein_set), 
    child_component(Entity,Child), %generates the children of an entity 
    protein(Sample,Child,1), 
    State is 1,!. 

%if a (simple) entity is a protein set and it's state if off 
%this is a base case where an entity does not have complexs or sets inside it 
%I find all proteins for a sample, this is a list of values, I delete all the 
%zeros and the remaining list will unify with the empty list. 
state_of_entity(Entity,State,Sample):- 
    all_children_proteins(Entity), 
    type(Entity, protein_set), 
    child_component(Entity,Child), 
bagof(Value, Value^protein(Sample,Child,Value),Vs), 
delete(Vs,0,ListOfOnes),ListOfOnes=[], 
State is 0,!. 

%if a (simple) entity is a complex and is off 
%this is a base case where an entity does not have complexs or sets inside it 
state_of_entity(Entity,State,Sample):- 
    all_children_proteins(Entity), 
    type(Entity, complex), 
    child_component(Entity,Child), 
    protein(Sample,Child,0), 
    State is 0,!. 

%if a (simple) entity is a complex and is on. 
%this is a base case where an entity does not have complexs or sets inside it 
%I find all protein in a sample, this is a list of values, I delete all the 
%zeros and the remaining list will unify with the empty list. 
state_of_entity(Entity,State,Sample):- 
    all_children_proteins(Entity), 
    type(Entity, complex), 
    child_component(Entity,Child), 
    bagof(Value, Value^protein(Sample,Child,Value),Vs), 
    delete(Vs,1,ListOfZeros),ListOfZeros=[], 
    State is 1,!. 

%if a complex with components is off 
%recursive case 
state_of_entity(Entity,State,Sample):- 
    type(Entity, complex), 
    child_component(Entity,Child), 
    (state_of_entity(Child,0,Sample); 
    protein(Sample,Child,0)), %if it has any proteins as input as well as other  components 
    State is 0,!. 

%if a complex with components is on 
%recursive case 
state_of_entity(Entity,State,Sample):- 
    type(Entity, complex), 
    child_component(Entity,Child), 
    bagof(Value, Value^state_of_entity(Child,Value,Sample),Vs),%if it has component inputs 
    bagof(Value2, Value2^protein(Sample,Child,Value2),Vs2),%if it has protein inputs 
    append(Vs, Vs2, Vs3), 
    delete(Vs3,1,ListOfZeros),ListOfZeros=[],%delete all the ones, the list of zeros will be empty if all inputs are on 
    State is 1,!. 

%if a protein set with components is on 
%recursive case 
state_of_entity(Entity,State,Sample):- 
    type(Entity, protein_set), 
    child_component(Entity,Child), 
    (state_of_entity(Child,1,Sample); 
    protein(Sample,Child,1)), %if it has any proteins as input as well as other entities 
    State is 1,!. 

%if a protein set with components is off 
%recursive case 
state_of_entity(Entity,State,Sample):- 
    type(Entity, protein_set), 
    child_component(Entity,Child), 
    bagof(Value, Value^state_of_entity(Child,Value,Sample),Vs), %if it has entity inputs 
    bagof(Value2, Value2^protein(Sample,Child,Value2),Vs2), %if it has protein inputs 
    append(Vs, Vs2, Vs3), %join the list of inputs together 
    delete(Vs3,0,ListOfOnes),ListOfOnes=[], %delete all the zeros, the list of 1's will be empty if all inputs are off 
    State is 0,!. 

更新 我結束了本作的蛋白質位,因爲我想要的工作。

samples(Samples):- 
    setof(Sample_in, Probe^samples(Sample_in, Probe, ProbeValue), Samples). 
sample(Sample):- 
    once(samples(Samples)), %why do I need this?! 
    member(Sample, Samples). 

protein_stack(Sample, Reactome_Id, State):- 
     (
      protein_reactome_Id_to_Uniprot_Id(Reactome_Id, UniprotId), 
      uniProt_Sample_Probes(UniprotId, Sample, Probe, 1), 
      !, 
      State is 1 
     ; 
      State is 0 
     ). 

protein_good(Sample, Reactome_Id,State):- 
    sample(Sample), 
    protein_reactome_Id_to_Uniprot_Id(Reactome_Id, _), 
    protein_stack(Sample, Reactome_Id,State). 

回答

2

讓我們來看看protein/3的第一條規則。

  • Reactome_IdUniprotId獨特的關係?如果是,請在setof(Sample ...), member(X, Samples)之前移動並在其後放置一個剪輯。否則,你會盡力滿足它的每個結果setof(...), member(X, Samples)。更重要的是,綠色切割有助於提高性能。

  • 該規則有一個目的,看看Vs中是否至少有一個值爲1。您不應生成Vs的所有成員,然後搜索值1,但在您滿足uniProt_Sample_Probes(UniprotId, X, _, Value)時找到第一個成員時停止。

    protein(Sample, Reactome_Id, State):- 
         (
          protein_reactome_Id_to_Uniprot_Id(Reactome_Id, UniprotId), 
          setof(Sample, Probe^samples(Sample, Probe, ProbeValue), Samples), 
          member(X, Samples), 
          uniProt_Sample_Probes(UniprotId, X, _, 1), 
          !, 
          State is 1, write('ON)) 
         ; 
          State is 0, write('OFF') 
         ). 
    

其它的規則可以用相同的模式進行優化:

state_of_x(X, State) :- Goal, !, State = 1. 
state_of_x(X, State) :- State = 0. 

,或者更簡潔,你把我在正確的軌道上

state_of_x(X, 1) :- Goal, !. 
state_of_x(X, 0). 
+0

感謝。 – user27815