title: number of connected nodes to specific nodes in a path
body: <p>I have a cypher query (below). 
  It works but I was wondering if there's a more elegant way to write this. </p>
  
  <p>Based on a given starting node, the query tries to:</p>
  
  <ol>
  <li><p>Find the following pattern/motif: <code>(inputko)--&gt;(:cpd)--&gt;(ko2:ko)--&gt;(:cpd)--&gt;(ko3:ko)</code>.</p></li>
  <li><p>Foreach the motifs/patterns found, find connected nodes with labels <code>contigs</code>,  for the following nodes in the pattern: [<code>inputko</code>, <code>ko2</code>, <code>ko3</code>]. </p></li>
  <li><p>A summary of the 3 nodes and their connected contigs, ie. the name property <code>.ko</code> of the 3 nodes and the number of connected <code>:contig</code> nodes in each of the  <code>(inputko)--&gt;(:cpd)--&gt;(ko2:ko)--&gt;(:cpd)--&gt;(ko3:ko)</code> motifs that were found.</p>
  
  <pre><code>+--------------------------------------------------------------------------+
  | KO1         | KO1count | KO2         | KO2count | KO3         | KO3count |
  +--------------------------------------------------------------------------+
  | "ko:K00001" | 102      | "ko:K14029" | 512      | "ko:K03736" | 15       |
  | "ko:K00001" | 102      | "ko:K00128" | 792      | "ko:K12972" | 7        |
  | "ko:K00001" | 102      | "ko:K00128" | 396      | "ko:K01624" | 265      |
  | "ko:K00001" | 102      | "ko:K03735" | 448      | "ko:K00138" | 33       |
  | "ko:K00001" | 102      | "ko:K14029" | 512      | "ko:K15228" | 24       |
  +--------------------------------------------------------------------------+
  </code></pre></li>
  </ol>
  
  <p>I'm puzzled for the syntax to operate on each match. 
  From the documentation the <code>foreach</code> clause doesn't seem to be what I need. 
  Any ideas guys?</p>
  
  <blockquote>
    <p>The FOREACH clause is used to update data within a collection, whether
    components of a path, or result of aggregation.</p>
    
    <p>Collections and paths are key concepts in Cypher. To use them for
    updating data, you can use the FOREACH construct. It allows you to do
    updating commands on elements in a collection — a path, or a
    collection created by aggregation.</p>
  </blockquote>
  
  <pre><code>START 
      inputko=node:koid('ko:\"ko:K00001\"') 
  MATCH
      (inputko)--(c1:contigs)
  WITH
      count(c1) as KO1count, inputko
  MATCH
      (inputko)--&gt;(:cpd)--&gt;(ko2:ko)--&gt;(:cpd)--&gt;(ko3:ko)
  WITH
      inputko.ko as KO1,
      KO1count,
      ko2,
      ko3
  MATCH
      (ko2)--(c2:contigs)
  WITH
      KO1,
      KO1count,
      ko2.ko as KO2,
      count(c2) as KO2count,
      ko3
  MATCH
      (ko3)--(c3:contigs)
  RETURN 
      KO1,
      KO1count,
      KO2,
      KO2count,
      ko3.ko     AS KO3,
      count(c3)  AS KO3count
  LIMIT
      5;
  </code></pre>
  
  <p>realised that i have to place distinct for in <code>count(distinct cX)</code> to get a accurate count. Do not know why. </p>