summaryrefslogtreecommitdiff
path: root/_posts/2012-08-15-how-to-add-data-entries-to-opentox-datasets.md
blob: 656c5cbe275e9735013519298f4b38ec97919c0a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
---
layout: post
title: "How to add data entries to OpenTox datasets"
description: ""
category: dataset
tags: [dataset]
---
{% include JB/setup %}

Data entries in `OpenTox::Dataset`s follow the table metaphor with feature values in columns and data entries (compounds) in rows.
To define the sequence of features you will have to set the dataset features first (think of it as setting the table header). After that you can append data entries sequentially with the `<<` operator which takes an Array as argument. The first array object is reserved for a `OpenTox::Compound`, the remaining entries are feature values (in the same order as the dataset features). Data entries are sorted in the same sequence as they are created, multiple entries for the same compound are allowed.

Example:

    # create dataset
    dataset = OpenTox::Dataset.new nil, @@subjectid

    # create and save features
    feature1 = Feature.new nil, @@subjectid
    feature1.title = "my first feature title"
    # set more metadata
    feature1.put
    feature2 = Feature.new nil, @@subjectid
    feature2.title = "my second feature title"
    # set more metadata
    feature2.put

    # add features (i.e. set table headers)
    dataset.features = [feature1, feature2]

    # add a data entry
    compound = Compound.from_smiles "c1ccccc1"
    dataset << [ compound, true, 0.5 ]

    # add another data entry
    compound = Compound.from_smiles "CCC(CC)CN"
    dataset << [ compound, false, 1.0 ]

    # save
    dataset.save

You can find more examples in [opentox-test/test/dataset.rb](https://github.com/opentox/opentox-test/blob/development/test/dataset.rb).