groovygate

duplicate output using writeLine in Groovy


I am using the following code to output annotations to csv:

new File(scriptParams.outputFile).withWriterAppend{ out ->
  ([Default:(doc.getAnnotations("MS").get("EDSS"))]).each{setName, set ->
    set.each{ anno ->
        anno.getFeatures().each{
            def f = anno.getFeatures()
          out.writeLine(/"${doc.getName()}",${anno.getId()},"${anno.getType()}",${anno.start()},${anno.end()},"${f.get('value')}","${f.get('valueLower')}","${f.get('valueUpper')}"/)
        }       
    }
  }
}

which works fine, however I get exactly 2 rows for each annotation found. i.e. there is 1 duplicate for each annotation. I can't seem to see anywhere in the script why this might be happening. Any pointers?


Solution

  • What about:

    new File(scriptParams.outputFile).withWriterAppend{ out ->
      doc.getAnnotations("MS").get("EDSS").each{
        anno ->
          def f = anno.getFeatures()
          out.writeLine(/"${doc.getName()}",${anno.getId()},"${anno.getType()}",${anno.start()},${anno.end()},"${f.get('value')}","${f.get('valueLower')}","${f.get('valueUpper')}"/)
      }
    }
    

    The main problem is the anno.getFeatures().each{, which iterates all the features the annotation has and for each feature it prints the line to the output. You need to print the line for each annotation only, not for each annotation feature.