Programatically setting a topic on an attribtue

Permalink
How do you set a topic on an attribute that has been set programatically

When a topic is added as an attribute the topic tree is loaded but initially a topic/category node is not selected. If you then try to use this attribute in a composer you will get a parsing error displayed on the composer page.

Can anyone tell me what I need to run to set the initial topic from within a controller.

This is my current code

$navAttributeSet = AttributeSet::getByHandle('navigation');
$themeTopics = CollectionKey::getByHandle('topic_tags');
        if (!$themeTopics instanceof CollectionKey) {
            $themeTopics = CollectionKey::add(
                'topics',
                array(
                    'akHandle' => 'topic_tags',
                    'akName' => t('Theme Tag'),
                    'akIsSearchable' => true,
                    'akIsSearchableIndexed' => false
                ),
                $pkg);
            //Get the topic by name and then update its settings to link the topic settings with the attribute
            $tree = Topic::getByName('Themes');
            $db = Database::connection();


The code above links the correct topic tree with the attributes, but I can't figure out the last piece of the puzzle here.

Any help of pointers in the right direction would really be appreciated.

TheRealSean