<commit_before># coding=utf-8
#
# fysom - pYthOn Finite State Machine - this is a port of Jake
#         Gordon's javascript-state-machine to python
#         https://github.com/jakesgordon/javascript-state-machine
#
# Copyright (C) 2011 Mansour Behabadi <mansour@oxplot.com>, Jake Gordon
#                                        and other contributors
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#

import unittest

from fysom import Fysom, FysomError

class FysomStateTests(unittest.TestCase):
    def setUp(self):
        self.fsm = Fysom({
            'initial': 'green',
            'events': [
                {'name': 'warn', 'src': 'green', 'dst': 'yellow'},
                {'name': 'panic', 'src': 'yellow', 'dst': 'red'},
                {'name': 'calm', 'src': 'red', 'dst': 'yellow'},
                {'name': 'clear', 'src': 'yellow', 'dst': 'green'},
                {'name': 'warm', 'src': 'green', 'dst': 'blue'}
            ]
        })

    def test_is_state_should_succeed_for_initial_state(self):
        self.assertTrue(self.fsm.isstate('green'))

    def test_identity_transition_should_not_be_allowed_by_default(self):
        self.assertFalse(self.fsm.can('clear'))
        self.assertTrue(self.fsm.cannot('clear'))

    def test_configured_transition_should_work(self):
        self.assertTrue(self.fsm.can('warn'))

    def test_transition_should_change_state(self):
        self.fsm.warn()
        self.assertTrue(self.fsm.isstate('yellow'))

    def test_should_raise_exception_when_state_transition_is_not_allowed(self):
        self.assertRaises(FysomError, self.fsm.panic)
        self.assertRaises(FysomError, self.fsm.calm)
        self.assertRaises(FysomError, self.fsm.clear)

    def test_event_handler_has_name_and_docstring(self):
        self.assertEqual(self.fsm.warm.__name__, "warm", "Event handlers do not have appropriate name.")
        self.assertNotEqual(self.fsm.warm.__name__, None, "Docstring for event handler is None!")

    def test_trigger_should_trigger_the_event_handler(self):
        self.assertEqual(self.fsm.current, "green", "The initial state isn't the expected state.")
        self.fsm.trigger("warm")
        self.assertEqual(self.fsm.current, "blue", "The initial state isn't the expected state.")
<commit_msg>Adding test cases to check event handler's name, docstring and trigger method as well<commit_after># coding=utf-8
#
# fysom - pYthOn Finite State Machine - this is a port of Jake
#         Gordon's javascript-state-machine to python
#         https://github.com/jakesgordon/javascript-state-machine
#
# Copyright (C) 2011 Mansour Behabadi <mansour@oxplot.com>, Jake Gordon
#                                        and other contributors
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#

import unittest

from fysom import Fysom, FysomError

class FysomStateTests(unittest.TestCase):
    def setUp(self):
        self.fsm = Fysom({
            'initial': 'green',
            'events': [
                {'name': 'warn', 'src': 'green', 'dst': 'yellow'},
                {'name': 'panic', 'src': 'yellow', 'dst': 'red'},
                {'name': 'calm', 'src': 'red', 'dst': 'yellow'},
                {'name': 'clear', 'src': 'yellow', 'dst': 'green'},
                {'name': 'warm', 'src': 'green', 'dst': 'blue'}
            ]
        })

    def test_is_state_should_succeed_for_initial_state(self):
        self.assertTrue(self.fsm.isstate('green'))

    def test_identity_transition_should_not_be_allowed_by_default(self):
        self.assertFalse(self.fsm.can('clear'))
        self.assertTrue(self.fsm.cannot('clear'))

    def test_configured_transition_should_work(self):
        self.assertTrue(self.fsm.can('warn'))

    def test_transition_should_change_state(self):
        self.fsm.warn()
        self.assertTrue(self.fsm.isstate('yellow'))

    def test_should_raise_exception_when_state_transition_is_not_allowed(self):
        self.assertRaises(FysomError, self.fsm.panic)
        self.assertRaises(FysomError, self.fsm.calm)
        self.assertRaises(FysomError, self.fsm.clear)

    def test_event_handler_has_name_and_docstring(self):
        self.assertEqual(self.fsm.warm.__name__, "warm", "Event handlers do not have appropriate name.")
        self.assertNotEqual(self.fsm.warm.__name__, None, "Docstring for event handler is None!")

    def test_trigger_should_trigger_the_event_handler(self):
        self.assertEqual(self.fsm.current, "green", "The initial state isn't the expected state.")
        self.fsm.trigger("warm")
        make_callable = lambda: self.fsm.trigger("unknowevent")
        self.assertRaises(FysomError, make_callable)
        self.assertEqual(self.fsm.current, "blue", "The initial state isn't the expected state.")
<|endoftext|>


<commit_before># Monocyte - An AWS Resource Destroyer
# Copyright 2015 Immobilien Scout GmbH
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import boto.rds2
import boto.regioninfo
from unittest import TestCase
from mock import patch, Mock
from monocyte.handler import rds2

class RDSHandlerTest(TestCase):
    def setUp(self):
        self.boto_mock = patch("monocyte.handler.rds2.boto").start()
        self.instance_mock = self._given_instance_mock()

        self.positive_fake_region = Mock(boto.regioninfo.RegionInfo)
        self.positive_fake_region.name = "allowed_region"
        self.negative_fake_region = Mock(boto.regioninfo.RegionInfo)
        self.negative_fake_region.name = "forbidden_region"
        self.boto_mock.rds2.regions.return_value = [self.positive_fake_region, self.negative_fake_region]
        self.rds_instance = rds2.Instance(lambda region_name: True)

    def tearDown(self):
        patch.stopall()

    def test_fetch_unwanted_resources_filtered(self):
        self.boto_mock.rds2.connect_to_region.return_value.describe_db_instances.return_value = \
            self._given_db_instances_response()

        only_resource = list(self.rds_instance.fetch_unwanted_resources())[0]
        self.assertEquals(only_resource.wrapped, self.instance_mock)

    def _given_db_instances_response(self):
        return {
            'DescribeDBInstancesResponse': {
                'DescribeDBInstancesResult': {
                    'DBInstances': [self.instance_mock]
                }
            }
        }

    def _given_instance_mock(self):
        instance_mock = {
            "DBInstanceIdentifier": "myInstanceIdentifier",
            "DBInstanceStatus": "myStatus"
        }
        return instance_mock
<commit_msg>adds to_string test for rds2.<commit_after># Monocyte - An AWS Resource Destroyer
# Copyright 2015 Immobilien Scout GmbH
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import boto.rds2
import boto.regioninfo
from unittest import TestCase
from mock import patch, Mock
from monocyte.handler import rds2

class RDSHandlerTest(TestCase):
    def setUp(self):
        self.boto_mock = patch("monocyte.handler.rds2.boto").start()
        self.instance_mock = self._given_instance_mock()

        self.positive_fake_region = Mock(boto.regioninfo.RegionInfo)
        self.positive_fake_region.name = "allowed_region"
        self.negative_fake_region = Mock(boto.regioninfo.RegionInfo)
        self.negative_fake_region.name = "forbidden_region"
        self.boto_mock.rds2.regions.return_value = [self.positive_fake_region, self.negative_fake_region]
        self.rds_instance = rds2.Instance(lambda region_name: True)

    def tearDown(self):
        patch.stopall()

    def test_fetch_unwanted_resources_filtered(self):
        self.boto_mock.rds2.connect_to_region.return_value.describe_db_instances.return_value = \
            self._given_db_instances_response()

        only_resource = list(self.rds_instance.fetch_unwanted_resources())[0]
        self.assertEquals(only_resource.wrapped, self.instance_mock)

    def test_to_string(self):
        self.boto_mock.rds2.connect_to_region.return_value.describe_db_instances.return_value = \
            self._given_db_instances_response()

        only_resource = list(self.rds_instance.fetch_unwanted_resources())[0]
        resource_string = self.rds_instance.to_string(only_resource)

        self.assertTrue(self.instance_mock["DBInstanceIdentifier"] in resource_string)
        self.assertTrue(self.instance_mock["DBInstanceStatus"] in resource_string)

    def _given_db_instances_response(self):
        return {
            'DescribeDBInstancesResponse': {
                'DescribeDBInstancesResult': {
                    'DBInstances': [self.instance_mock]
                }
            }
        }

    def _given_instance_mock(self):
        instance_mock = {
            "DBInstanceIdentifier": "myInstanceIdentifier",
            "DBInstanceStatus": "myStatus"
        }
        return instance_mock
<|endoftext|>


<commit_before>"""
flake8 plugin for dcos code base.

This is flake8 extension that provides custom code quality checks for DCOS project. The syntax rules can be added as
Regex in check_rules.py module. New plugins can be introduced by following the flake8 extension guide
(http://flake8.pycqa.org/en/latest/plugin-development/index.html). And they will automatically be run as part of the
syntax-check.
"""
from __version__ import PLUGIN_NAME, PLUGIN_VERSION

from setuptools import setup

setup(
    name=PLUGIN_NAME,
    version=PLUGIN_VERSION,
    description='flake8 plugin for custom dcos checks',
    py_modules=["check_rules", "checker"],
    install_requires=[
        'pycodestyle==2.2.0',
        'flake8==3.3.0',
        'flake8-import-order==0.9.2',
        'pep8-naming'
    ],
    entry_points={
        'flake8.extension': [
            '{} = checker:check'.format(PLUGIN_NAME),
        ],
    }
)
<commit_msg>Pin the dependency for pep8-naming.<commit_after>"""
flake8 plugin for dcos code base.

This is flake8 extension that provides custom code quality checks for DCOS project. The syntax rules can be added as
Regex in check_rules.py module. New plugins can be introduced by following the flake8 extension guide
(http://flake8.pycqa.org/en/latest/plugin-development/index.html). And they will automatically be run as part of the
syntax-check.
"""
from __version__ import PLUGIN_NAME, PLUGIN_VERSION

from setuptools import setup

setup(
    name=PLUGIN_NAME,
    version=PLUGIN_VERSION,
    description='flake8 plugin for custom dcos checks',
    py_modules=["check_rules", "checker"],
    install_requires=[
        'pycodestyle==2.2.0',
        'flake8==3.3.0',
        'flake8-import-order==0.9.2',
        'pep8-naming==0.7.0'
    ],
    entry_points={
        'flake8.extension': [
            '{} = checker:check'.format(PLUGIN_NAME),
        ],
    }
)
<|endoftext|>


<commit_before>from intelmq.lib.bot import Bot, sys
from intelmq.lib.event import Event
from intelmq.bots import utils
import re

class DshieldParserBot(Bot):

    # Function to clean leading 0's maybe we need to add it to the sanitizer
    def cleanip(ip):
        ip = ".".join([octet.lstrip('0') for octet in ip.split('.')])
        return ip

    def process(self):
        report = self.receive_message()

        if report:
            regex_ip = "^(\d+\.\d+\.\d+\.\d+)"
            regex_timestamp = "(\d+\-\d+\-\d+\s\d+\:\d+\:\d+)"

            for row in report.split('\n'):

                if row.startswith('#'):
                    continue

                event = Event()

                match = re.search(regex_ip, row)
                if match:
                    ip = cleanip(match.group())

                match = re.search(regex_timestamp, row)
                if match:
                    timestamp = match.group(1) + " UTC"

                event.add("source_ip", ip)
                event.add("source_time", timestamp)
                event.add('feed', 'Dshield')
                event.add('feed_url', 'http://dshield.org/asdetailsascii.html')
                event.add('type', 'brute-force')

                event = utils.parse_source_time(event, "source_time")
                event = utils.generate_observation_time(event, "observation_time")
                event = utils.generate_reported_fields(event)

                self.send_message(event)
        self.acknowledge_message()

if __name__ == "__main__":
    bot = DshieldParserBot(sys.argv[1])
    bot.start()
<commit_msg>dshield bot<commit_after>from intelmq.lib.bot import Bot, sys
from intelmq.lib.message import Event
from intelmq.bots import utils
import re

class DshieldParserBot(Bot):

    def process(self):
        report = self.receive_message()

        if report:
            regex_ip = "^(\d+\.\d+\.\d+\.\d+)"
            regex_timestamp = "(\d+\-\d+\-\d+\s\d+\:\d+\:\d+)"

            for row in report.split('\n'):

                if row.startswith('#'):
                    continue

                event = Event()

                match = re.search(regex_ip, row)
                if match:
                    ip = ".".join([octet.lstrip('0') for octet in match.group().split('.')])

                match = re.search(regex_timestamp, row)
                if match:
                    timestamp = match.group(1) + " UTC"

                event.add("source_ip", ip)
                event.add("source_time", timestamp)
                event.add('feed', 'dshield')
                event.add('feed_url', 'http://dshield.org/asdetailsascii.html')
                event.add('type', 'brute-force')

                event = utils.parse_source_time(event, "source_time")
                event = utils.generate_observation_time(event, "observation_time")
                event = utils.generate_reported_fields(event)

                self.send_message(event)
        self.acknowledge_message()

if __name__ == "__main__":
    bot = DshieldParserBot(sys.argv[1])
    bot.start()
<|endoftext|>


<commit_before># test plugin
from bot.pluginDespatch import Plugin
import re
import datetime
import logging
import pyowm
from logos.roomlib import get_global_option

from django.conf import settings
logger = logging.getLogger(__name__)
logging.config.dictConfig(settings.LOGGING)

class WeatherPlugin(Plugin):
    plugin = ("weather", "Weather Plugin")
    def __init__(self, *args, **kwargs):
        Plugin.__init__(self, *args, **kwargs)

        self.commands = (\
         (r'w (?P<arg>\S.*)$', self.weather, "Weather query"),
        )

    def privmsg(self, user, channel, message):
        pass

    def weather(self, regex, chan, nick, **kwargs):
        api_key = get_global_option('weather_api_key')
        owm = pyowm.OWM(api_key)
        arg = regex.group('arg')
        observation = owm.weather_at_place(arg)
        w = observation.get_weather()
        tm = w.get_reference_time()
        tm_str = datetime.datetime.fromtimestamp(tm).strftime("%b %d %Y %H:%M")
        str1 = "Reference time : " + tm_str
        str1 += " Status : {}. ".format(w.get_status())
        str1 += " Wind: speed {speed} ".format(**w.get_wind())
        str1 += " Humidity: {}.".format(w.get_humidity())
        celsius = w.get_temperature('celsius')['temp']
        fahrenheit = w.get_temperature('fahrenheit')['temp']
        str1 += " Temperature {} F {} C".format(celsius, fahrenheit)
#        str1 += " Temperature (F): {temp_min}, {temp}, {temp_max}.".format(**w.get_temperature())
#        str1 += " Temperature (C): {temp_min}, {temp}, {temp_max}.".format(**w.get_temperature('celsius'))
        self.say(chan, str1)

<commit_msg>Fix fahrenheit, celsius order<commit_after># test plugin
from bot.pluginDespatch import Plugin
import re
import datetime
import logging
import pyowm
from logos.roomlib import get_global_option

from django.conf import settings
logger = logging.getLogger(__name__)
logging.config.dictConfig(settings.LOGGING)

class WeatherPlugin(Plugin):
    plugin = ("weather", "Weather Plugin")
    def __init__(self, *args, **kwargs):
        Plugin.__init__(self, *args, **kwargs)

        self.commands = (\
         (r'w (?P<arg>\S.*)$', self.weather, "Weather query"),
        )

    def privmsg(self, user, channel, message):
        pass

    def weather(self, regex, chan, nick, **kwargs):
        api_key = get_global_option('weather_api_key')
        owm = pyowm.OWM(api_key)
        arg = regex.group('arg')
        observation = owm.weather_at_place(arg)
        w = observation.get_weather()
        tm = w.get_reference_time()
        tm_str = datetime.datetime.fromtimestamp(tm).strftime("%b %d %Y %H:%M")
        str1 = "Reference time : " + tm_str
        str1 += " Status : {}. ".format(w.get_status())
        str1 += " Wind: speed {speed} ".format(**w.get_wind())
        str1 += " Humidity: {} ".format(w.get_humidity())
        celsius = w.get_temperature('celsius')['temp']
        fahrenheit = w.get_temperature('fahrenheit')['temp']
        str1 += " Temperature {} F  {} C".format(fahrenheit, celsius)
#        str1 += " Temperature (F): {temp_min}, {temp}, {temp_max}.".format(**w.get_temperature())
#        str1 += " Temperature (C): {temp_min}, {temp}, {temp_max}.".format(**w.get_temperature('celsius'))
        self.say(chan, str1)

<|endoftext|>


<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

import random, math
import sys
sys.path.append('../..')

import GestureAgents.Screen as Screen
#from GestureAgents.Gestures import RecognizerStick, RecognizerPaint, RecognizerDoubleTap, RecognizerTap
from GestureAgents.AppRecognizer import AppRecognizer
from RecognizerMove import RecognizerMove
from RecognizerZoomRotate import RecognizerZoomRotate
import GestureAgents.Render
import numpy as np
import transformations as tr
import pygame.image
import OpenGL.GL as GL
from GestureAgents.Gestures import load_recognizer

def loadImage(image):
    textureSurface = pygame.image.load(image)

    textureData = pygame.image.tostring(textureSurface, "RGBA", 1)

    width = textureSurface.get_width()
    height = textureSurface.get_height()

    texture = GL.glGenTextures(1)
    GL.glBindTexture(GL.GL_TEXTURE_2D, texture)
    GL.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR)
    GL.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR)
    GL.glTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGBA, width, height, 0, GL.GL_RGBA,
        GL.GL_UNSIGNED_BYTE, textureData)

    return texture, width, height

class MapApp:
    def __init__(self):
        Screen.ScreenDraw.register(MapApp.draw,self)
        AppRecognizer(RecognizerMove).newAgent.register(MapApp.newAgentMove,self)
        AppRecognizer(RecognizerZoomRotate).newAgent.register(MapApp.newAgentZoomRotate,self)
        self.texname = "earth-map-big.jpg"
        self.texture = None
        self.tmatrix = tr.identity_matrix()
        self.Move = None

    def load_texture(self):
        self.texture, self.texturew, self.textureh = loadImage(self.texname)

    def draw(self):
        if not self.texture:
            self.load_texture()

        GL.glPushMatrix()
        GL.glMultMatrixf(self.tmatrix)
        GL.glEnable(GL.GL_TEXTURE_2D)
        GL.glBindTexture(GL.GL_TEXTURE_2D, self.texture)
        GL.glBegin(GL.GL_QUADS)

        # Bottom Left Of The Texture and Quad
        GL.glTexCoord2f(0, 1); GL.glVertex2f(0, 0)

        # Top Left Of The Texture and Quad
        GL.glTexCoord2f(0, 0); GL.glVertex2f(0, self.textureh)

        # Top Right Of The Texture and Quad
        GL.glTexCoord2f(1, 0); GL.glVertex2f( self.texturew,  self.textureh)

        # Bottom Right Of The Texture and Quad
        GL.glTexCoord2f(1, 1); GL.glVertex2f(self.texturew, 0)
        GL.glEnd()
        GL.glDisable(GL.GL_TEXTURE_2D)
        GL.glPopMatrix()

    def newAgentMove(self,Move):
        #if not self.Move:
        Move.newMove.register(MapApp.newMove,self)

    def newAgentZoomRotate(self,ZRotate):
        ZRotate.newZoomRotate.register(MapApp.newZoomRotate,self)

    def newMove(self,Move):
        self.Move = Move
        Move.newTranslation.register(MapApp.newTranslation,self)
        Move.endMove.register(MapApp.endMove,self)

    def newZoomRotate(self,ZRotate):
        ZRotate.newScale.register(MapApp.newScale,self)
        ZRotate.newRotation.register(MapApp.newRotation,self)
        #ZRotate.endZoom.register(MapApp.newRotation,self)

    def endMove(self,MZoom):
        #if MZoom == self.Move:
        self.Move = None

    def newTranslation(self,MZoom):
        #print MZoom.translation
        #self.tmatrix = tr.concatenate_matrices(tr.translation_matrix(list(MZoom.translation)+[0]),self.tmatrix)
        self.tmatrix = self.tmatrix.dot(tr.translation_matrix(list(MZoom.translation)+[0]).transpose())

    def newRotation(self,MZoom):
        pivot = list(MZoom.pivot)+[0]
        self.tmatrix = self.tmatrix.dot(tr.rotation_matrix(MZoom.rotation,[0, 0, 1],pivot).transpose())

    def newScale(self,MZoom):
        pivot = list(MZoom.pivot)+[0]
        self.tmatrix = self.tmatrix.dot(tr.scale_matrix(MZoom.scale,pivot).transpose())

from GestureAgents.Agent import Agent
@Agent.compatibility_policy.rule(0)
def zoom_over_move(r1,r2):
    if type(r1) == RecognizerMove and type(r2) == RecognizerZoomRotate:
        return True

if __name__ == "__main__":
    import GestureAgents
    RecognizerMove()
    RecognizerZoomRotate()
    app = MapApp()
    GestureAgents.run_apps()
<commit_msg>load_recognizer on MapApp<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

import random, math
import sys
sys.path.append('../..')

import GestureAgents.Screen as Screen
#from GestureAgents.Gestures import RecognizerStick, RecognizerPaint, RecognizerDoubleTap, RecognizerTap
from GestureAgents.AppRecognizer import AppRecognizer
from RecognizerMove import RecognizerMove
from RecognizerZoomRotate import RecognizerZoomRotate
import GestureAgents.Render
import numpy as np
import transformations as tr
import pygame.image
import OpenGL.GL as GL
from GestureAgents.Gestures import load_recognizer

def loadImage(image):
    textureSurface = pygame.image.load(image)

    textureData = pygame.image.tostring(textureSurface, "RGBA", 1)

    width = textureSurface.get_width()
    height = textureSurface.get_height()

    texture = GL.glGenTextures(1)
    GL.glBindTexture(GL.GL_TEXTURE_2D, texture)
    GL.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR)
    GL.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR)
    GL.glTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGBA, width, height, 0, GL.GL_RGBA,
        GL.GL_UNSIGNED_BYTE, textureData)

    return texture, width, height

class MapApp:
    def __init__(self):
        Screen.ScreenDraw.register(MapApp.draw,self)
        AppRecognizer(RecognizerMove).newAgent.register(MapApp.newAgentMove,self)
        AppRecognizer(RecognizerZoomRotate).newAgent.register(MapApp.newAgentZoomRotate,self)
        self.texname = "earth-map-big.jpg"
        self.texture = None
        self.tmatrix = tr.identity_matrix()
        self.Move = None

    def load_texture(self):
        self.texture, self.texturew, self.textureh = loadImage(self.texname)

    def draw(self):
        if not self.texture:
            self.load_texture()

        GL.glPushMatrix()
        GL.glMultMatrixf(self.tmatrix)
        GL.glEnable(GL.GL_TEXTURE_2D)
        GL.glBindTexture(GL.GL_TEXTURE_2D, self.texture)
        GL.glBegin(GL.GL_QUADS)

        # Bottom Left Of The Texture and Quad
        GL.glTexCoord2f(0, 1); GL.glVertex2f(0, 0)

        # Top Left Of The Texture and Quad
        GL.glTexCoord2f(0, 0); GL.glVertex2f(0, self.textureh)

        # Top Right Of The Texture and Quad
        GL.glTexCoord2f(1, 0); GL.glVertex2f( self.texturew,  self.textureh)

        # Bottom Right Of The Texture and Quad
        GL.glTexCoord2f(1, 1); GL.glVertex2f(self.texturew, 0)
        GL.glEnd()
        GL.glDisable(GL.GL_TEXTURE_2D)
        GL.glPopMatrix()

    def newAgentMove(self,Move):
        #if not self.Move:
        Move.newMove.register(MapApp.newMove,self)

    def newAgentZoomRotate(self,ZRotate):
        ZRotate.newZoomRotate.register(MapApp.newZoomRotate,self)

    def newMove(self,Move):
        self.Move = Move
        Move.newTranslation.register(MapApp.newTranslation,self)
        Move.endMove.register(MapApp.endMove,self)

    def newZoomRotate(self,ZRotate):
        ZRotate.newScale.register(MapApp.newScale,self)
        ZRotate.newRotation.register(MapApp.newRotation,self)
        #ZRotate.endZoom.register(MapApp.newRotation,self)

    def endMove(self,MZoom):
        #if MZoom == self.Move:
        self.Move = None

    def newTranslation(self,MZoom):
        #print MZoom.translation
        #self.tmatrix = tr.concatenate_matrices(tr.translation_matrix(list(MZoom.translation)+[0]),self.tmatrix)
        self.tmatrix = self.tmatrix.dot(tr.translation_matrix(list(MZoom.translation)+[0]).transpose())

    def newRotation(self,MZoom):
        pivot = list(MZoom.pivot)+[0]
        self.tmatrix = self.tmatrix.dot(tr.rotation_matrix(MZoom.rotation,[0, 0, 1],pivot).transpose())

    def newScale(self,MZoom):
        pivot = list(MZoom.pivot)+[0]
        self.tmatrix = self.tmatrix.dot(tr.scale_matrix(MZoom.scale,pivot).transpose())

from GestureAgents.Agent import Agent
@Agent.compatibility_policy.rule(0)
def zoom_over_move(r1,r2):
    if type(r1) == RecognizerMove and type(r2) == RecognizerZoomRotate:
        return True

if __name__ == "__main__":
    import GestureAgents
    load_recognizer(RecognizerMove)
    load_recognizer(RecognizerZoomRotate)
    app = MapApp()
    GestureAgents.run_apps()
<|endoftext|>


<commit_before>from typing import TypeVar, Generic, Callable, Union, Any
from typing import Tuple  # NOQA
from functools import wraps, partial
from operator import eq, is_not
import inspect
import traceback

from amino import boolean
from amino.tc.base import Implicits
from amino.func import call_by_name, I

A = TypeVar('A')
B = TypeVar('B')

class Maybe(Generic[A], Implicits, implicits=True):

    __slots__ = ()

    def __new__(tp, value: A, checker=partial(is_not, None)):
        return Maybe.check(value, checker)

    @staticmethod
    def check(value: A, checker=partial(is_not, None)):
        return Just(value) if checker(value) else Empty()

    @staticmethod
    def from_call(f: Callable[..., A], *args, **kwargs):
        exc = kwargs.pop('exc', Exception)
        try:
            return Maybe.check(f(*args, **kwargs))
        except exc:
            if exc == Exception:
                from amino.logging import log
                frame = inspect.currentframe().f_back  # type: ignore
                stack = traceback.format_stack(frame)
                log.exception('Maybe.from_call:')
                log.error(''.join(stack))
            return Empty()

    @staticmethod
    def typed(value: A, tpe: type):
        return Maybe.check(value, lambda a: isinstance(a, tpe))

    @staticmethod
    def wrap(mb: Union['Maybe[A]', None]):
        return mb if mb is not None and isinstance(mb, Just) else Empty()

    @property
    def _get(self) -> Union[A, None]:
        pass

    def cata(self, f: Callable[[A], B], b: Union[B, Callable[[], B]]) -> B:
        return (
            f(self._get)  # type: ignore
            if self.is_just
            else call_by_name(b)
        )

    def filter(self, f: Callable[[A], B]):
        l = lambda a: self if f(a) else Empty()
        return self.flat_map(l)

    def get_or_else(self, a: Union[A, Callable[[], A]]):
        return self.cata(I, a)

    __or__ = get_or_else

    def get_or_raise(self, e: Exception):
        def raise_e():
            raise e
        return self.cata(I, raise_e)

    def get_or_fail(self, err):
        return self.get_or_raise(Exception(call_by_name(err)))

    def __contains__(self, v):
        return self.contains(v)

    def foreach(self, f: Callable[[A], Any]):
        self.cata(f, None)

    def error(self, f: Callable[[], Any]) -> 'Maybe[A]':
        self.cata(I, f)
        return self

    def observe(self, f: Callable[[A], Any]):
        self.foreach(f)
        return self

    effect = observe

    def __iter__(self):
        return iter(self.to_list)

    @property
    def is_just(self):
        return boolean.Boolean(isinstance(self, Just))

    @property
    def is_empty(self):
        return not self.is_just

    empty = is_empty

    def __nonzero__(self):
        return self.is_just

    @property
    def to_list(self):
        from amino.list import List
        return self.cata(lambda v: List(v), List())

    @property
    async def unsafe_await(self):
        if self.is_just:
            ret = await self._get()
            return Maybe(ret)
        else:
            return self

    async def unsafe_await_or(self, b: Union[B, Callable[[], B]]):
        return (Maybe(await(self._get)) if self.is_just  # type: ignore
                else call_by_name(b))

    @property
    def contains_coro(self):
        return self.exists(inspect.iscoroutine)

    @property
    def json(self):
        return self.cata(I, lambda: None)

class Just(Maybe):

    __slots__ = 'x',

    def __new__(tp, value: A, *args, **kwargs):
        return object.__new__(tp)

    def __init__(self, value):
        self.x = value

    @property
    def _get(self) -> Union[A, None]:
        return self.x

    def __str__(self):
        return 'Just({!s})'.format(self.x)

    def __repr__(self):
        return 'Just({!r})'.format(self.x)

    def __eq__(self, other):
        if not isinstance(other, Just):
            return False
        return eq(self.x, other.x)

    def __hash__(self):
        return hash(self._get)

class Empty(Maybe):

    __object = None  # type: Empty

    def __new__(tp, *args, **kwargs):
        if Empty.__object is None:
            Empty.__object = object.__new__(tp)
        return Empty.__object

    def __str__(self):
        return 'Empty()'

    __repr__ = __str__

    def __eq__(self, other):
        return isinstance(other, Empty)

    def __hash__(self):
        return hash('Empty')

def may(f):
    @wraps(f)
    def wrapper(*args, **kwargs):
        return Maybe.check(f(*args, **kwargs))
    return wrapper

def flat_may(f):
    @wraps(f)
    def wrapper(*args, **kwargs):
        res = f(*args, **kwargs)
        return res if isinstance(res, Maybe) else Maybe(res)
    return wrapper

__all__ = ('Maybe', 'Just', 'Empty', 'may')
<commit_msg>`Maybe.getattr` ctor<commit_after>from typing import TypeVar, Generic, Callable, Union, Any
from typing import Tuple  # NOQA
from functools import wraps, partial
from operator import eq, is_not
import inspect
import traceback

from amino import boolean
from amino.tc.base import Implicits
from amino.func import call_by_name, I

A = TypeVar('A')
B = TypeVar('B')

class Maybe(Generic[A], Implicits, implicits=True):

    __slots__ = ()

    def __new__(tp, value: A, checker=partial(is_not, None)):
        return Maybe.check(value, checker)

    @staticmethod
    def check(value: A, checker=partial(is_not, None)):
        return Just(value) if checker(value) else Empty()

    @staticmethod
    def from_call(f: Callable[..., A], *args, **kwargs):
        exc = kwargs.pop('exc', Exception)
        try:
            return Maybe.check(f(*args, **kwargs))
        except exc:
            if exc == Exception:
                from amino.logging import log
                frame = inspect.currentframe().f_back  # type: ignore
                stack = traceback.format_stack(frame)
                log.exception('Maybe.from_call:')
                log.error(''.join(stack))
            return Empty()

    @staticmethod
    def typed(value: A, tpe: type):
        return Maybe.check(value, lambda a: isinstance(a, tpe))

    @staticmethod
    def wrap(mb: Union['Maybe[A]', None]):
        return mb if mb is not None and isinstance(mb, Just) else Empty()

    @staticmethod
    def getattr(obj, attr):
        return Maybe.check(getattr(obj, attr, None))

    @property
    def _get(self) -> Union[A, None]:
        pass

    def cata(self, f: Callable[[A], B], b: Union[B, Callable[[], B]]) -> B:
        return (
            f(self._get)  # type: ignore
            if self.is_just
            else call_by_name(b)
        )

    def filter(self, f: Callable[[A], B]):
        l = lambda a: self if f(a) else Empty()
        return self.flat_map(l)

    def get_or_else(self, a: Union[A, Callable[[], A]]):
        return self.cata(I, a)

    __or__ = get_or_else

    def get_or_raise(self, e: Exception):
        def raise_e():
            raise e
        return self.cata(I, raise_e)

    def get_or_fail(self, err):
        return self.get_or_raise(Exception(call_by_name(err)))

    def __contains__(self, v):
        return self.contains(v)

    def foreach(self, f: Callable[[A], Any]):
        self.cata(f, None)

    def error(self, f: Callable[[], Any]) -> 'Maybe[A]':
        self.cata(I, f)
        return self

    def observe(self, f: Callable[[A], Any]):
        self.foreach(f)
        return self

    effect = observe

    def __iter__(self):
        return iter(self.to_list)

    @property
    def is_just(self):
        return boolean.Boolean(isinstance(self, Just))

    @property
    def is_empty(self):
        return not self.is_just

    empty = is_empty

    def __nonzero__(self):
        return self.is_just

    @property
    def to_list(self):
        from amino.list import List
        return self.cata(lambda v: List(v), List())

    @property
    async def unsafe_await(self):
        if self.is_just:
            ret = await self._get()
            return Maybe(ret)
        else:
            return self

    async def unsafe_await_or(self, b: Union[B, Callable[[], B]]):
        return (Maybe(await(self._get)) if self.is_just  # type: ignore
                else call_by_name(b))

    @property
    def contains_coro(self):
        return self.exists(inspect.iscoroutine)

    @property
    def json(self):
        return self.cata(I, lambda: None)

class Just(Maybe):

    __slots__ = 'x',

    def __new__(tp, value: A, *args, **kwargs):
        return object.__new__(tp)

    def __init__(self, value):
        self.x = value

    @property
    def _get(self) -> Union[A, None]:
        return self.x

    def __str__(self):
        return 'Just({!s})'.format(self.x)

    def __repr__(self):
        return 'Just({!r})'.format(self.x)

    def __eq__(self, other):
        if not isinstance(other, Just):
            return False
        return eq(self.x, other.x)

    def __hash__(self):
        return hash(self._get)

class Empty(Maybe):

    __object = None  # type: Empty

    def __new__(tp, *args, **kwargs):
        if Empty.__object is None:
            Empty.__object = object.__new__(tp)
        return Empty.__object

    def __str__(self):
        return 'Empty()'

    __repr__ = __str__

    def __eq__(self, other):
        return isinstance(other, Empty)

    def __hash__(self):
        return hash('Empty')

def may(f):
    @wraps(f)
    def wrapper(*args, **kwargs):
        return Maybe.check(f(*args, **kwargs))
    return wrapper

def flat_may(f):
    @wraps(f)
    def wrapper(*args, **kwargs):
        res = f(*args, **kwargs)
        return res if isinstance(res, Maybe) else Maybe(res)
    return wrapper

__all__ = ('Maybe', 'Just', 'Empty', 'may')
<|endoftext|>


<commit_before>import sublime_plugin
import sublime
import subprocess
import os
import sys
import logging

class ScanIndexCommand(sublime_plugin.TextCommand):

    def run(self, edit):
        print('HERE')
        sublime.set_timeout_async(self.run_scan, 0)

    def run_scan(self):
        c_dir = os.path.dirname(os.path.realpath(__file__))
        run_scanner = os.path.realpath(
            os.path.join(
                c_dir,
                '..',
                'dataparser',
                'run_scanner.py')
        )
        startupinfo = subprocess.STARTUPINFO()
        startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
        log_file = open('D:\\popen.log', 'w')
        p = subprocess.Popen(
            ['C:\\Python27\\python.exe',
             run_scanner, 'all',
             '--workspace',
             'D:\\workspace\\robotframework-from-sublime\\test\\resource\\test_data\\suite_tree',
             '--db_path',
             'D:\\db_path',
             '--extension',
             'robot'],
            stderr=subprocess.STDOUT,
            stdout=log_file,
            startupinfo=startupinfo
        )
        rc = p.wait()
        print('rc: ', rc)
<commit_msg>Example how to get the settings<commit_after>import sublime_plugin
import sublime
import subprocess
import os
import sys
import logging

class ScanIndexCommand(sublime_plugin.TextCommand):

    def run(self, edit):
        print('HERE')
        plugin_settings = sublime.load_settings('RobotFrameworkDataEditor.sublime-settings')
        print(plugin_settings)
        sublime.set_timeout_async(self.run_scan, 0)

    def run_scan(self):
        c_dir = os.path.dirname(os.path.realpath(__file__))
        run_scanner = os.path.realpath(
            os.path.join(
                c_dir,
                '..',
                'dataparser',
                'run_scanner.py')
        )
        startupinfo = subprocess.STARTUPINFO()
        startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
        log_file = open('D:\\popen.log', 'w')
        p = subprocess.Popen(
            ['C:\\Python27\\python.exe',
             run_scanner, 'all',
             '--workspace',
             'D:\\workspace\\robotframework-from-sublime\\test\\resource\\test_data\\suite_tree',
             '--db_path',
             'D:\\db_path',
             '--extension',
             'robot'],
            stderr=subprocess.STDOUT,
            stdout=log_file,
            startupinfo=startupinfo
        )
        rc = p.wait()
        print('rc: ', rc)
<|endoftext|>


<commit_before>import sys
from config_test import *

import numpy as np
import os
import re
import argparse
from medpy.io import load, header, save
from utils_dataRV import *

parser = argparse.ArgumentParser()
parser.add_argument('-f', '--file',
            action="store", dest="files",
            help="files we want to compute the segmentation")
parser.add_argument('-r', '--rep',
            action="store", dest="rep_dataset",
            help="repertory of the dataset")
parser.add_argument('-d', '--directory',
            action="store", dest="directorySave",
            help="directory where to save the results")

args = parser.parse_args()
filenames_test = args.files
directorySave = args.directorySave
rep_dataset = args.rep_dataset

mri3D, gt3D = load_dataset(filenames_test, rep_dataset)

print ("Begening of testing ...")
print ("Predictions will be save in {}".format(directorySave))

net_deploy = caffe.Net(net_deploy_name,      # defines the structure of the model
                pretrainedModel, caffe.TEST)

hm3D = {}
dice = []
file_dataset = open(filenames_test)
for patientfile in file_dataset.read().splitlines():
	split_line = patientfile.split("\t")
	inputs_name = split_line[0]
	image_data, header = load(rep_dataset + "/" + inputs_name)
	output_name = split_line[1]
	gt_data, header = load(rep_dataset + "/" + output_name)
	nb_slices = gt_data.shape[2]

	basename = os.path.basename(inputs_name)

	size_im = preprocessing_im(image_data[:,:,0]).shape

	mri3D = np.zeros((size_im[0], size_im[1], nb_slices))
	gt3D = np.zeros((size_im[0], size_im[1], nb_slices))
	hm3D = np.zeros((size_im[0], size_im[1], 2, nb_slices))

	for i in range(nb_slices):
		mri3D[:,:,i] = preprocessing_im(image_data[:,:,i])
		mri3D[:,:, i] -= np.min(mri3D[:,:, i])
		mri3D[:,:, i] = mri3D[:,:,i] / np.max(mri3D[:,:,i])
		hm3D[:,:,:,i] = get_heat_map(mri3D[:,:, i], net_deploy)
		gt3D[:,:,i] = preprocessing_label(gt_data[:,:,i])

	output_file = directorySave + "/" + basename.replace(".nii", "_pred.nii")
	createDirectoryPath(output_file)
	save(hm3D.argmax(2).astype(np.uint8), output_file,hdr=header, force=True)

	dice.append(np.mean(compute_dice_dataset(gt3D, hm3D.argmax(2), countBgImgs=True)))

	print("Dice for the file {} : {}".format(basename, dice[-1]))

net_deploy = None # free the memory
file_dataset.close()

print ("Mean Dice on the test set : {}".format(np.mean(dice)))
<commit_msg>change preprocessing<commit_after>import sys
from config_test import *

import numpy as np
import os
import re
import argparse
from medpy.io import load, header, save
from utils_dataRV import *

parser = argparse.ArgumentParser()
parser.add_argument('-f', '--file',
            action="store", dest="files",
            help="files we want to compute the segmentation")
parser.add_argument('-r', '--rep',
            action="store", dest="rep_dataset",
            help="repertory of the dataset")
parser.add_argument('-d', '--directory',
            action="store", dest="directorySave",
            help="directory where to save the results")

args = parser.parse_args()
filenames = args.files
directorySave = args.directorySave
rep_dataset = args.rep_dataset

mri3D, gt3D = load_dataset(filenames_test, rep_dataset)

print ("Begening of testing ...")
print ("Predictions will be save in {}".format(directorySave))

net_deploy = caffe.Net(net_deploy_name,      # defines the structure of the model
                pretrainedModel, caffe.TEST)

hm3D = {}
dice = []
file_dataset = open(filenames)
for patientfile in file_dataset.read().splitlines():
	split_line = patientfile.split("\t")
	inputs_name = split_line[0]
	image_data, header = load(rep_dataset + "/" + inputs_name)
	output_name = split_line[1]
	gt_data, header = load(rep_dataset + "/" + output_name)
	nb_slices = gt_data.shape[2]

	basename = os.path.basename(inputs_name)

	size_im = preprocessing_im(image_data[:,:,0]).shape

	mri3D = np.zeros((size_im[0], size_im[1], nb_slices))
	gt3D = np.zeros((size_im[0], size_im[1], nb_slices))
	hm3D = np.zeros((size_im[0], size_im[1], 2, nb_slices))

	for i in range(nb_slices):
		mri3D[:,:,i] = preprocessing_im(image_data[:,:,i])
		mri3D[:,:, i] -= np.min(mri3D[:,:, i])
		mri3D[:,:, i] = mri3D[:,:,i] / np.max(mri3D[:,:,i])
		hm3D[:,:,:,i] = get_heat_map(mri3D[:,:, i], net_deploy)
		gt3D[:,:,i] = preprocessing_label(gt_data[:,:,i])

	output_file = directorySave + "/" + basename.replace(".nii", "_pred.nii")
	createDirectoryPath(output_file)
	save(hm3D.argmax(2).astype(np.uint8), output_file,hdr=header, force=True)

	dice.append(np.mean(compute_dice_dataset(gt3D, hm3D.argmax(2), countBgImgs=True)))

	print("Dice for the file {} : {}".format(basename, dice[-1]))

net_deploy = None # free the memory
file_dataset.close()

print ("Mean Dice on the test set : {}".format(np.mean(dice)))
<|endoftext|>


<commit_before>#!/usr/bin/python

import argparse
import commands
import logging
import os
import random
import signal
import sys
import thread
import time

from apscheduler.scheduler import Scheduler
from apscheduler.threadpool import ThreadPool

from core.alive import Alive
from core.irlp import Irlp
from core.voicesynthetizer import VoiceSynthetizer

# Production

from modules.aprstracker import AprsTracker
from modules.aprstt import Aprstt
from modules.clock import Clock
from modules.identification import Identification
from modules.meteorology import Meteorology
from modules.news import News
from modules.seismology import Seismology
from modules.selfie import Selfie
from modules.sstv import SSTV
from modules.voicecommand import VoiceCommand
from modules.voicemail import VoiceMail
from modules.weather import Weather
from modules.wolframalpha import WolframAlpha

# Experimental

from modules.assistant import Assistant
from modules.messages import Messages
from learning.morseteacher import MorseTeacher
from modules.voiceexperimental import VoiceExperimental
from modules.voicemailer import VoiceMailer

class NuupXe(object):

    def __init__(self, irlp):

        self.irlp = irlp
        self.pidfile = None
        self.scheduler_status = False
        self.pidfile = "/tmp/nuupxe.pid"

    def __del__(self):
        pass

    def voicesynthetizer(self):
        self.voicesynthetizer = VoiceSynthetizer("google", "spanish")

    def voicesynthetizerget(self):
        return self.voicesynthetizer

    def modules_setup(self):

        # Production Modules
        self.alive = Alive()
        self.aprstracker = AprsTracker(self.voicesynthetizer)
        self.aprstt = Aprstt(self.voicesynthetizer)
        self.clock = Clock(self.voicesynthetizer)
        self.identification = Identification(self.voicesynthetizer)
        self.meteorology = Meteorology(self.voicesynthetizer)
        self.news = News(self.voicesynthetizer)
        self.selfie = Selfie(self.voicesynthetizer)
        self.voicecommand = VoiceCommand(self.voicesynthetizer)
        self.voicemail = VoiceMail(self.voicesynthetizer)
        self.weather = Weather(self.voicesynthetizer)
        self.wolframalpha = WolframAlpha(self.voicesynthetizer)

        # Experimental Modules
        self.assistant = Assistant(self.voicesynthetizer)
        self.messages = Messages(self.voicesynthetizer)
        self.morseteacher = MorseTeacher(self.voicesynthetizer)
        self.seismology = Seismology(self.voicesynthetizer)
        self.sstv = SSTV(self.voicesynthetizer)
        self.voiceexperimental = VoiceExperimental(self.voicesynthetizer)
        self.voicemailer = VoiceMailer(self.voicesynthetizer)

    def dtmf_setup(self,dtmf):
        dtmf_codes = {
        'PS0'  : 'alive',
        'PS1'  : 'aprstracker',
        'PS2'  : 'news',
        'PS3'  : 'meteorology',
        'PS4'  : 'seismology',
        'PS5'  : 'selfie',
        'PS6'  : 'voicecommand',
        'PS7'  : 'voiceexperimental',
        'PS8'  : 'wolframalpha',
        'PS9'  : 'voicemail',
        'PS10' : 'sstv',
        }
        return dtmf_codes.get(dtmf)

    def enabled(self):
        return os.path.isfile(self.pidfile)

    def enable(self):

        pid = str(os.getpid())
        logging.info('Process Id' + pid)
        file(self.pidfile, 'w').write(pid)

    def disable(self):

        if self.enabled():
            os.unlink(self.pidfile)
        if self.scheduler_status:
            self.scheduler.shutdown()

    def scheduler_mode(self):

        logging.info('Mode Scheduler')
        self.voicesynthetizer.speechit("Modo Planificador")
        self.scheduler = Scheduler(misfire_grace_time=900, coalesce=True, threadpool=ThreadPool(max_threads=1))
        self.schedule()
        self.scheduler.start()
        self.schedule_print()
        self.scheduler_status = True

        while True:
            time.sleep(5)
            if self.irlp.active():
                time.sleep(5)
                self.irlp.busy()
                self.voicesynthetizer.speechit("Se ha activado el nodo, Proyecto NuupXe dice hasta pronto!")
                break

        self.disable()

    def writing_mode(self):

        logging.info('Mode')
        self.voicesynthetizer.speechit("Modo Escritura")

        while True:
            print " Type any text to make use of Text to Speech infraestructure"
            x = raw_input(" Type 'e' for exit: ")
            if x.lower() == 'e':
                self.disable()
                break;
            else:
                self.voicesynthetizer.speechit(x)
            time.sleep(1)

    def module_mode(self, module, dtmf=None):

        logging.info('Mode Module')

        # Custom Decode Activated Modules

        if module == 'identification':
            self.identification = Identification(self.voicesynthetizer)
            self.identification.identify()
        elif module == 'date':
            self.clock = Clock(self.voicesynthetizer)
            self.clock.date()
        elif module == 'hour':
            self.clock = Clock(self.voicesynthetizer)
            self.clock.hour()
        elif module == 'temperature':
            self.weather = Weather(self.voicesynthetizer)
            self.weather.temperature()
        elif module == 'weather':
            self.weather = Weather(self.voicesynthetizer)
            self.weather.report()

        # PS Activated Modules

        elif module == 'alive':
            self.alive = Alive()
            self.alive.report()
        elif module == 'aprstracker':
            self.aprstracker = AprsTracker(self.voicesynthetizer)
            self.aprstracker.query()
        elif module == 'news':
            self.news = News(self.voicesynthetizer)
            self.news.getitems()
        elif module == 'meteorology':
            self.meteorology = Meteorology(self.voicesynthetizer)
            self.meteorology.conagua_clima()
        elif module == 'selfie':
            self.selfie = Selfie(self.voicesynthetizer)
            self.selfie.get()
        elif module == 'voicecommand':
            self.voicecommand = VoiceCommand(self.voicesynthetizer)
            self.voicecommand.listen()
        elif module == 'voicemail':
            self.voicemail = VoiceMail(self.voicesynthetizer)
            self.voicemail.run(dtmf)
        elif module == 'wolframalpha':
            self.wolframalpha = WolframAlpha(self.voicesynthetizer)
            self.wolframalpha.ask()

        # SS Activated Modules

        # Experimental Modules

        elif module == 'aprstt':
            self.aprstt = Aprstt(self.voicesynthetizer)
            self.aprstt.query(dtmf)
        elif module == 'seismology':
            self.seismology = Seismology(self.voicesynthetizer)
            self.seismology.SismologicoMX()
        elif module == 'morselearn':
            self.morseteacher = MorseTeacher(self.voicesynthetizer)
            self.morseteacher.learn()
        elif module == 'morsecontest'	:
            self.morseteacher = MorseTeacher(self.voicesynthetizer)
            self.morseteacher.contest()
	elif module == 'regulations':
            self.messages = Messages(self.voicesynthetizer)
            self.messages.readfile('learning/reglamentos.1')
	elif module == 'radioclub':
            self.messages = Messages(self.voicesynthetizer)
            self.messages.readfile('learning/arej.radioclubs')
        elif module == 'stations':
            self.messages = Messages(self.voicesynthetizer)
            self.messages.stations()
        elif module == 'sstv':
            self.sstv = SSTV(self.voicesynthetizer)
            self.sstv.decode()
        elif module == 'assistant':
            self.assistant = Assistant(self.voicesynthetizer)
            self.assistant.demo1()
        elif module == 'voicebackground':
            self.voicecommand = VoiceCommand(self.voicesynthetizer)
            self.voicecommand.background()
        elif module == 'voiceexperimental':
            self.voiceexperimental = VoiceExperimental(self.voicesynthetizer)
            self.voiceexperimental.listen()
        elif module == 'voicemailer':
            self.voicemailer = VoiceMailer(self.voicesynthetizer)
            self.voicemailer.attend(dtmf)
        else:
            print 'Module not found! Please check its name...\n'

        self.disable()

    def schedule_print(self):
        self.scheduler.print_jobs()

    def schedule(self):

        # Production Modules
        self.scheduler.add_interval_job(self.alive.report, minutes=120)
        self.scheduler.add_cron_job(self.clock.date,month='*',day_of_week='*',hour='6,12,22',minute ='00',second='00')
        self.scheduler.add_interval_job(self.clock.hour, minutes=15)
        self.scheduler.add_interval_job(self.identification.identify, minutes=30)
        self.scheduler.add_cron_job(self.selfie.get,month='*',day_of_week='*',hour='6,12,22',minute ='00',second='00')
        self.scheduler.add_interval_job(self.weather.report, minutes=120)

        # Experimental Modules
        self.scheduler.add_interval_job(self.seismology.SismologicoMX, minutes=240)
        self.scheduler.add_interval_job(self.news.getitems, minutes=240)
        self.scheduler.add_interval_job(self.meteorology.conagua_clima, minutes=240)
        self.scheduler.add_interval_job(self.messages.stations, minutes=240)

	# Learning Modules, AREJ
        self.scheduler.add_cron_job(self.messages.readfile,args=['learning/arej.radioclubs'],month='*',day_of_week='*',hour='7,12,17',minute ='00',second='0')

        # Learning Modules, Morse
        self.scheduler.add_cron_job(self.morseteacher.learn,month='*',day='*',hour='7,12,17',minute ='30',second='0')
        self.scheduler.add_cron_job(self.morseteacher.contest,month='*',day='*',hour='7,12,17',minute ='45',second='0')

        # Learning Modules, Reglamentos
        self.scheduler.add_cron_job(self.messages.readfile,args=['learning/reglamentos.1'],month='*',day_of_week='mon',hour='8,13,18',minute ='00',second='0')
        self.scheduler.add_cron_job(self.messages.readfile,args=['learning/reglamentos.2'],month='*',day_of_week='tue',hour='8,13,18',minute ='00',second='0')
        self.scheduler.add_cron_job(self.messages.readfile,args=['learning/reglamentos.3'],month='*',day_of_week='wed',hour='8,13,18',minute ='00',second='0')
        self.scheduler.add_cron_job(self.messages.readfile,args=['learning/reglamentos.4'],month='*',day_of_week='thu',hour='8,13,18',minute ='00',second='0')
        self.scheduler.add_cron_job(self.messages.readfile,args=['learning/reglamentos.5'],month='*',day_of_week='fri',hour='8,13,18',minute ='00',second='0')

def set_exit_handler(func):
    signal.signal(signal.SIGTERM, func)

def on_exit(sig, func=None):
    print "exit handler triggered"
    sys.exit(1)

def main(argv):

    logging.basicConfig(filename='output/nuupxe.log', level=logging.INFO, format='%(asctime)s %(levelname)s %(message)s')

    irlp = Irlp()

    parser = argparse.ArgumentParser(description='NuupXe Project, Voice Services Experimental Project')
    parser.add_argument('-m', '--module', help='Module Mode')
    parser.add_argument('-s', '--server', help='Server mode')
    parser.add_argument('-d', '--dtmf', help='DMTF Code')
    args = parser.parse_args()

    if irlp.active():
        logging.info("Nodo activo, no podemos iniciar Proyecto NuupXe")
        sys.exit(0)

    experimental = NuupXe(irlp)
    experimental.voicesynthetizer()
    voicesynthetizer = experimental.voicesynthetizerget()

    if (args.module or args.server) and experimental.enabled():
        logging.info("Proyecto NuupXe ya habilitado, no podemos iniciar otra instancia")
        sys.exit(1)

    if args.server == 'stop' and not experimental.enabled():
        voicesynthetizer.speechit("Proyecto NuupXe deshabilitado")
        status, output = commands.getstatusoutput('./nuupxe.sh stop')
        sys.exit(1)

    if args.server == 'stop' and experimental.enabled():
        voicesynthetizer.speechit("Deshabilitando Proyecto NuupXe, hasta pronto!")
        status, output = commands.getstatusoutput('./nuupxe.sh stop')
        sys.exit(1)

    if args.module:
        experimental.module_mode(args.module, args.dtmf)

    elif args.server == 'scheduler':
        experimental.modules_setup()
        experimental.scheduler_mode()

    elif args.server == 'writing':
        experimental.modules_setup()
        experimental.writing_mode()

    elif args.dtmf:
        logging.info(args.dtmf)
        if args.dtmf.startswith('PS') and len(args.dtmf) == 3 or len(args.dtmf) == 4:
            module = experimental.dtmf_setup(args.dtmf)
            experimental.module_mode(module, args.dtmf)
        elif args.dtmf.startswith('SS') and len(args.dtmf) == 4:
            experimental.module_mode('voicemailer', args.dtmf)
        elif len(args.dtmf) > 10:
            experimental.module_mode('aprstt', args.dtmf)

    experimental.disable()

if __name__ == "__main__":

    main(sys.argv[1:])
<commit_msg>NuupXe: PS DTMF Range +<commit_after>#!/usr/bin/python

import argparse
import commands
import logging
import os
import random
import signal
import sys
import thread
import time

from apscheduler.scheduler import Scheduler
from apscheduler.threadpool import ThreadPool

from core.alive import Alive
from core.irlp import Irlp
from core.voicesynthetizer import VoiceSynthetizer

# Production

from modules.aprstracker import AprsTracker
from modules.aprstt import Aprstt
from modules.clock import Clock
from modules.identification import Identification
from modules.meteorology import Meteorology
from modules.news import News
from modules.seismology import Seismology
from modules.selfie import Selfie
from modules.sstv import SSTV
from modules.voicecommand import VoiceCommand
from modules.voicemail import VoiceMail
from modules.weather import Weather
from modules.wolframalpha import WolframAlpha

# Experimental

from modules.assistant import Assistant
from modules.messages import Messages
from learning.morseteacher import MorseTeacher
from modules.voiceexperimental import VoiceExperimental
from modules.voicemailer import VoiceMailer

class NuupXe(object):

    def __init__(self, irlp):

        self.irlp = irlp
        self.pidfile = None
        self.scheduler_status = False
        self.pidfile = "/tmp/nuupxe.pid"

    def __del__(self):
        pass

    def voicesynthetizer(self):
        self.voicesynthetizer = VoiceSynthetizer("google", "spanish")

    def voicesynthetizerget(self):
        return self.voicesynthetizer

    def modules_setup(self):

        # Production Modules
        self.alive = Alive()
        self.aprstracker = AprsTracker(self.voicesynthetizer)
        self.aprstt = Aprstt(self.voicesynthetizer)
        self.clock = Clock(self.voicesynthetizer)
        self.identification = Identification(self.voicesynthetizer)
        self.meteorology = Meteorology(self.voicesynthetizer)
        self.news = News(self.voicesynthetizer)
        self.selfie = Selfie(self.voicesynthetizer)
        self.voicecommand = VoiceCommand(self.voicesynthetizer)
        self.voicemail = VoiceMail(self.voicesynthetizer)
        self.weather = Weather(self.voicesynthetizer)
        self.wolframalpha = WolframAlpha(self.voicesynthetizer)

        # Experimental Modules
        self.assistant = Assistant(self.voicesynthetizer)
        self.messages = Messages(self.voicesynthetizer)
        self.morseteacher = MorseTeacher(self.voicesynthetizer)
        self.seismology = Seismology(self.voicesynthetizer)
        self.sstv = SSTV(self.voicesynthetizer)
        self.voiceexperimental = VoiceExperimental(self.voicesynthetizer)
        self.voicemailer = VoiceMailer(self.voicesynthetizer)

    def dtmf_setup(self,dtmf):
        dtmf_codes = {
        'PS0'  : 'alive',
        'PS1'  : 'aprstracker',
        'PS2'  : 'news',
        'PS3'  : 'meteorology',
        'PS4'  : 'seismology',
        'PS5'  : 'selfie',
        'PS6'  : 'voicecommand',
        'PS7'  : 'voiceexperimental',
        'PS8'  : 'wolframalpha',
        'PS9'  : 'voicemail',
        'PS10' : 'sstv',
        }
        return dtmf_codes.get(dtmf)

    def enabled(self):
        return os.path.isfile(self.pidfile)

    def enable(self):

        pid = str(os.getpid())
        logging.info('Process Id' + pid)
        file(self.pidfile, 'w').write(pid)

    def disable(self):

        if self.enabled():
            os.unlink(self.pidfile)
        if self.scheduler_status:
            self.scheduler.shutdown()

    def scheduler_mode(self):

        logging.info('Mode Scheduler')
        self.voicesynthetizer.speechit("Modo Planificador")
        self.scheduler = Scheduler(misfire_grace_time=900, coalesce=True, threadpool=ThreadPool(max_threads=1))
        self.schedule()
        self.scheduler.start()
        self.schedule_print()
        self.scheduler_status = True

        while True:
            time.sleep(5)
            if self.irlp.active():
                time.sleep(5)
                self.irlp.busy()
                self.voicesynthetizer.speechit("Se ha activado el nodo, Proyecto NuupXe dice hasta pronto!")
                break

        self.disable()

    def writing_mode(self):

        logging.info('Mode')
        self.voicesynthetizer.speechit("Modo Escritura")

        while True:
            print " Type any text to make use of Text to Speech infraestructure"
            x = raw_input(" Type 'e' for exit: ")
            if x.lower() == 'e':
                self.disable()
                break;
            else:
                self.voicesynthetizer.speechit(x)
            time.sleep(1)

    def module_mode(self, module, dtmf=None):

        logging.info('Mode Module ' + module)

        # Custom Decode Activated Modules

        if module == 'identification':
            self.identification = Identification(self.voicesynthetizer)
            self.identification.identify()
        elif module == 'date':
            self.clock = Clock(self.voicesynthetizer)
            self.clock.date()
        elif module == 'hour':
            self.clock = Clock(self.voicesynthetizer)
            self.clock.hour()
        elif module == 'temperature':
            self.weather = Weather(self.voicesynthetizer)
            self.weather.temperature()
        elif module == 'weather':
            self.weather = Weather(self.voicesynthetizer)
            self.weather.report()

        # PS Activated Modules

        elif module == 'alive':
            self.alive = Alive()
            self.alive.report()
        elif module == 'aprstracker':
            self.aprstracker = AprsTracker(self.voicesynthetizer)
            self.aprstracker.query()
        elif module == 'news':
            self.news = News(self.voicesynthetizer)
            self.news.getitems()
        elif module == 'meteorology':
            self.meteorology = Meteorology(self.voicesynthetizer)
            self.meteorology.conagua_clima()
        elif module == 'selfie':
            self.selfie = Selfie(self.voicesynthetizer)
            self.selfie.get()
        elif module == 'voicecommand':
            self.voicecommand = VoiceCommand(self.voicesynthetizer)
            self.voicecommand.listen()
        elif module == 'voicemail':
            self.voicemail = VoiceMail(self.voicesynthetizer)
            self.voicemail.run(dtmf)
        elif module == 'wolframalpha':
            self.wolframalpha = WolframAlpha(self.voicesynthetizer)
            self.wolframalpha.ask()

        # SS Activated Modules

        # Experimental Modules

        elif module == 'aprstt':
            self.aprstt = Aprstt(self.voicesynthetizer)
            self.aprstt.query(dtmf)
        elif module == 'seismology':
            self.seismology = Seismology(self.voicesynthetizer)
            self.seismology.SismologicoMX()
        elif module == 'morselearn':
            self.morseteacher = MorseTeacher(self.voicesynthetizer)
            self.morseteacher.learn()
        elif module == 'morsecontest'	:
            self.morseteacher = MorseTeacher(self.voicesynthetizer)
            self.morseteacher.contest()
	elif module == 'regulations':
            self.messages = Messages(self.voicesynthetizer)
            self.messages.readfile('learning/reglamentos.1')
	elif module == 'radioclub':
            self.messages = Messages(self.voicesynthetizer)
            self.messages.readfile('learning/arej.radioclubs')
        elif module == 'stations':
            self.messages = Messages(self.voicesynthetizer)
            self.messages.stations()
        elif module == 'sstv':
            self.sstv = SSTV(self.voicesynthetizer)
            self.sstv.decode()
        elif module == 'assistant':
            self.assistant = Assistant(self.voicesynthetizer)
            self.assistant.demo1()
        elif module == 'voicebackground':
            self.voicecommand = VoiceCommand(self.voicesynthetizer)
            self.voicecommand.background()
        elif module == 'voiceexperimental':
            self.voiceexperimental = VoiceExperimental(self.voicesynthetizer)
            self.voiceexperimental.listen()
        elif module == 'voicemailer':
            self.voicemailer = VoiceMailer(self.voicesynthetizer)
            self.voicemailer.attend(dtmf)
        else:
            print 'Module not found! Please check its name...\n'

        self.disable()

    def schedule_print(self):
        self.scheduler.print_jobs()

    def schedule(self):

        # Production Modules
        self.scheduler.add_interval_job(self.alive.report, minutes=120)
        self.scheduler.add_cron_job(self.clock.date,month='*',day_of_week='*',hour='6,12,22',minute ='00',second='00')
        self.scheduler.add_interval_job(self.clock.hour, minutes=15)
        self.scheduler.add_interval_job(self.identification.identify, minutes=30)
        self.scheduler.add_cron_job(self.selfie.get,month='*',day_of_week='*',hour='6,12,22',minute ='00',second='00')
        self.scheduler.add_interval_job(self.weather.report, minutes=120)

        # Experimental Modules
        self.scheduler.add_interval_job(self.seismology.SismologicoMX, minutes=240)
        self.scheduler.add_interval_job(self.news.getitems, minutes=240)
        self.scheduler.add_interval_job(self.meteorology.conagua_clima, minutes=240)
        self.scheduler.add_interval_job(self.messages.stations, minutes=240)

	# Learning Modules, AREJ
        self.scheduler.add_cron_job(self.messages.readfile,args=['learning/arej.radioclubs'],month='*',day_of_week='*',hour='7,12,17',minute ='00',second='0')

        # Learning Modules, Morse
        self.scheduler.add_cron_job(self.morseteacher.learn,month='*',day='*',hour='7,12,17',minute ='30',second='0')
        self.scheduler.add_cron_job(self.morseteacher.contest,month='*',day='*',hour='7,12,17',minute ='45',second='0')

        # Learning Modules, Reglamentos
        self.scheduler.add_cron_job(self.messages.readfile,args=['learning/reglamentos.1'],month='*',day_of_week='mon',hour='8,13,18',minute ='00',second='0')
        self.scheduler.add_cron_job(self.messages.readfile,args=['learning/reglamentos.2'],month='*',day_of_week='tue',hour='8,13,18',minute ='00',second='0')
        self.scheduler.add_cron_job(self.messages.readfile,args=['learning/reglamentos.3'],month='*',day_of_week='wed',hour='8,13,18',minute ='00',second='0')
        self.scheduler.add_cron_job(self.messages.readfile,args=['learning/reglamentos.4'],month='*',day_of_week='thu',hour='8,13,18',minute ='00',second='0')
        self.scheduler.add_cron_job(self.messages.readfile,args=['learning/reglamentos.5'],month='*',day_of_week='fri',hour='8,13,18',minute ='00',second='0')

def set_exit_handler(func):
    signal.signal(signal.SIGTERM, func)

def on_exit(sig, func=None):
    print "exit handler triggered"
    sys.exit(1)

def main(argv):

    logging.basicConfig(filename='output/nuupxe.log', level=logging.INFO, format='%(asctime)s %(levelname)s %(message)s')

    irlp = Irlp()

    parser = argparse.ArgumentParser(description='NuupXe Project, Voice Services Experimental Project')
    parser.add_argument('-m', '--module', help='Module Mode')
    parser.add_argument('-s', '--server', help='Server mode')
    parser.add_argument('-d', '--dtmf', help='DMTF Code')
    args = parser.parse_args()

    if irlp.active():
        logging.info("Nodo activo, no podemos iniciar Proyecto NuupXe")
        sys.exit(0)

    experimental = NuupXe(irlp)
    experimental.voicesynthetizer()
    voicesynthetizer = experimental.voicesynthetizerget()

    if (args.module or args.server) and experimental.enabled():
        logging.info("Proyecto NuupXe ya habilitado, no podemos iniciar otra instancia")
        sys.exit(1)

    if args.server == 'stop' and not experimental.enabled():
        voicesynthetizer.speechit("Proyecto NuupXe deshabilitado")
        status, output = commands.getstatusoutput('./nuupxe.sh stop')
        sys.exit(1)

    if args.server == 'stop' and experimental.enabled():
        voicesynthetizer.speechit("Deshabilitando Proyecto NuupXe, hasta pronto!")
        status, output = commands.getstatusoutput('./nuupxe.sh stop')
        sys.exit(1)

    if args.module:
        experimental.module_mode(args.module, args.dtmf)

    elif args.server == 'scheduler':
        experimental.modules_setup()
        experimental.scheduler_mode()

    elif args.server == 'writing':
        experimental.modules_setup()
        experimental.writing_mode()

    elif args.dtmf:
        logging.info(args.dtmf)
        if args.dtmf.startswith('PS'):
            module = experimental.dtmf_setup(args.dtmf)
            experimental.module_mode(module, args.dtmf)
        elif args.dtmf.startswith('SS') and len(args.dtmf) == 4:
            experimental.module_mode('voicemailer', args.dtmf)
        elif len(args.dtmf) > 10:
            experimental.module_mode('aprstt', args.dtmf)

    experimental.disable()

if __name__ == "__main__":

    main(sys.argv[1:])
<|endoftext|>


<commit_before>#!/usr/bin/env python3
# encoding: utf-8
'''
Ligandum -- Analysis of ligandability mass spectrometry

Created by Matthias Stahl at TU Muenchen, 2017
'''

import ursgal
import pymzml
import pyqms
import sys
import pickle
import os
import pprint
import random

def showStartHello():
    print('{0:-^100}'.format('###'))
    print('{0:-^100}'.format('This is Ligandum speaking! Let\'s start...'))
    print('{0:-^100}'.format('###'))
    print()
    return

def new_userdefined_unimod_molecule(mass, name, composition):
    # make modification dictionary
    modification_dict = {
        'mass' : mass,
        'name' : name,
        'composition' : composition,
        'id': '1'
    }

    # initialize unimod mapper and write to unimod.xml
    u = ursgal.UnimodMapper()
    u.writeXML(modification_dict, xmlFile = '/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pyqms/kb/ext/unimod.xml')
    return

def msms_identification(mzml_file, database_file):
    # define parameters
    params = {
        'enzyme': 'trypsin',
        'frag_mass_tolerance': 0.5,
        'frag_mass_tolerance_unit': 'da',
        'decoy_generation_mode' : 'reverse_protein',
        'precursor_min_charge' : '2',
        'modifications' : [
            'M,opt,any,Oxidation',
            'C,fix,any,Carbamidomethyl',
            'K,opt,any,TEV_H',
            'K,opt,any,TEV_L'
        ],
    }

    # initialize Ursgal controller
    uc = ursgal.UController(
        params = params
    )

    # generate reverse protein sequences and initialize new database
    new_target_decoy_db_name = uc.generate_target_decoy(
        input_files = database_file,
        output_file_name = 'new_target_decoy.fasta',
    )
    print('Generated target decoy database: {0}'.format(new_target_decoy_db_name))
    uc.params['database'] = new_target_decoy_db_name

    # perform search with search engine (writes output files to file system)
    search_result = uc.search(
        input_file = mzml_file,
        engine = 'msgfplus_v2016_09_16'
    )

    # validate search engine results with percolator (writes output files to file system)
    validated_result = uc.validate(
        input_file = search_result,
        engine     = 'percolator_2_08',
    )

    return validated_result

def ligandability_quantification(mzml_file, molecule_list, evidence_lookup, formatted_fixed_labels):
    run = pymzml.run.Reader(mzml_file)
    params = {
        'molecules'        : molecule_list,
        'charges'          : [1, 2, 3, 4, 5],
        'fixed_labels'     : formatted_fixed_labels,
        'verbose'          : True,
        'evidences'        : evidence_lookup
    }

    lib = pyqms.IsotopologueLibrary( **params )

    results = None
    mzml_file_basename = os.path.basename(mzml_file)
    for spectrum in run:
        scan_time, unit = spectrum.get('scan time', (None, None ))
        print(unit)
        if unit == 'second':
            time_div_factor = 60.0
            # convert seconds to minutes...
        else:
            time_div_factor = 1

        if spectrum['ms level'] == 1:
            results = lib.match_all(
                mz_i_list = spectrum.centroidedPeaks,
                file_name = mzml_file_basename,
                spec_id   = spectrum['id'],
                spec_rt   = spectrum['scan time'] / time_div_factor,
                results   = results
            )
    return results

def edit_molecule_list(molecule_list, labels):
    # delete molecules with more than one TEV modification
    for molecule in molecule_list[:]:
        num_of_labels = 0
        for label in labels:
            num_of_labels += molecule.count(label['name'])
        if not num_of_labels == 1:
            molecule_list.remove(molecule)
    print('Initially found {0} peptides via MS2 sequencing. Now I\'ll look for nonsequenced partners.'.format(len(molecule_list)))

    for molecule in molecule_list[:]:
        check_pairs(molecule, molecule_list, labels)

    print('There are {0} peptides after partner generation.'.format(len(molecule_list)))
    return

def check_pairs(molecule, molecule_list, labels):
    partner_label_name = ''
    current_label_name = ''
    for label in labels:
        if molecule.count(label['name']) == 0:
            partner_label_name = label['name']
        else:
            current_label_name = label['name']

    partner_molecule = molecule.replace(current_label_name, partner_label_name)

    if not partner_molecule in molecule_list:
        molecule_list.append(partner_molecule)

    return

def main():
    showStartHello()

    # add new modifications
    labels = [
        {
            'name': 'TEV_H',
            'mass': '470.26338',
            'composition': {'C': 15, '13C': 5, 'H': 32, 'N': 7, '15N': 1, 'O': 5}
        },
        {
            'name': 'TEV_L',
            'mass': '464.24957',
            'composition': {'C': 20, 'H': 32, 'N': 8, 'O': 5}
        }
    ]

    for label in labels:
        new_userdefined_unimod_molecule(label['mass'], label['name'], label['composition'])

    # MS/MS identification and validation, output is written to file system
    database_file = '/Users/MS/Desktop/special_projects/SMHacker/28092017human.fasta'
    # mzml_file = '/Users/MS/Desktop/special_projects/SMHacker/170209_SMH_170205_P9_05_ultrashort.mzML'
    mzml_file = '/Users/MS/Desktop/special_projects/SMHacker/170209_SMH_170205_P9_05_short.mzML'
    validated_result = msms_identification(mzml_file, database_file)

    # MS isotopic ligandability quantification
    # evidence_file = '/Users/MS/Desktop/special_projects/SMHacker/msgfplus_v2016_09_16/170209_SMH_170205_P9_05_ultrashort_msgfplus_v2016_09_16_pmap_unified_percolator_validated.csv'
    evidence_file = '/Users/MS/Desktop/special_projects/SMHacker/msgfplus_v2016_09_16/170209_SMH_170205_P9_05_short_msgfplus_v2016_09_16_pmap_unified_percolator_validated.csv'
    out_folder = '/Users/MS/Desktop/special_projects/SMHacker/msgfplus_v2016_09_16'

    tmp_fixed_labels = {
        'C' : [
            {
                'element_composition' : {'O': 1, 'H': 3, '14N': 1, 'C': 2},
                'evidence_mod_name': 'Carbamidomethyl'
            }
        ]
    }
    formatted_fixed_labels, evidence_lookup, molecule_list = pyqms.adaptors.parse_evidence(
        fixed_labels         = tmp_fixed_labels,
        evidence_files       = [ evidence_file ],
        evidence_score_field = 'PEP'
    )
    edit_molecule_list(molecule_list, labels)

    results = ligandability_quantification(mzml_file, molecule_list, evidence_lookup, formatted_fixed_labels)
    results.write_result_csv(out_folder + '/ligand_quant_res.csv')

    # serialize, not really necessary...
    pickle.dump(
        results,
        open(
            '/Users/MS/Desktop/special_projects/SMHacker/pyQms_results.pkl',
            'wb'
        )
    )

    # deserialize
    results_class = pickle.load(
        open(
            '/Users/MS/Desktop/special_projects/SMHacker/pyQms_results.pkl',
            'rb'
        )
    )
    rt_border_tolerance = 5

    quant_summary_file  = '/Users/MS/Desktop/special_projects/SMHacker/quant_summary.xlsx'

    results_class.write_rt_info_file(
        output_file         = quant_summary_file,
        list_of_csvdicts    = None,
        trivial_name_lookup = None,
        rt_border_tolerance = rt_border_tolerance,
        update              = True
    )

    results_class.calc_amounts_from_rt_info_file(
        rt_info_file         = quant_summary_file,
        rt_border_tolerance  = rt_border_tolerance,
        calc_amount_function = None
    )

    return

if __name__ == '__main__':
    main()
<commit_msg>filter added<commit_after>#!/usr/bin/env python3
# encoding: utf-8
'''
Ligandum -- Analysis of ligandability mass spectrometry

Created by Matthias Stahl at TU Muenchen, 2017
'''

import ursgal
import pymzml
import pyqms
import sys
import pickle
import os
import pprint
import random

def showStartHello():
    print('{0:-^100}'.format('###'))
    print('{0:-^100}'.format('This is Ligandum speaking! Let\'s start...'))
    print('{0:-^100}'.format('###'))
    print()
    return

def new_userdefined_unimod_molecule(mass, name, composition):
    # make modification dictionary
    modification_dict = {
        'mass' : mass,
        'name' : name,
        'composition' : composition,
        'id': '1'
    }

    # initialize unimod mapper and write to unimod.xml
    u = ursgal.UnimodMapper()
    u.writeXML(modification_dict, xmlFile = '/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pyqms/kb/ext/unimod.xml')
    return

def msms_identification(mzml_file, database_file):
    # define parameters
    params = {
        'enzyme': 'trypsin',
        'frag_mass_tolerance': 0.5,
        'frag_mass_tolerance_unit': 'da',
        'decoy_generation_mode' : 'reverse_protein',
        'precursor_min_charge' : '2',
        'modifications' : [
            'M,opt,any,Oxidation',
            'C,fix,any,Carbamidomethyl',
            'K,opt,any,TEV_H',
            'K,opt,any,TEV_L'
        ],
    }

    # initialize Ursgal controller
    uc = ursgal.UController(
        params = params
    )

    # generate reverse protein sequences and initialize new database
    new_target_decoy_db_name = uc.generate_target_decoy(
        input_files = database_file,
        output_file_name = 'new_target_decoy.fasta',
    )
    print('Generated target decoy database: {0}'.format(new_target_decoy_db_name))
    uc.params['database'] = new_target_decoy_db_name

    # perform search with search engine (writes output files to file system)
    search_result = uc.search(
        input_file = mzml_file,
        engine = 'msgfplus_v2016_09_16'
    )

    # validate search engine results with percolator (writes output files to file system)
    validated_result = uc.validate(
        input_file = search_result,
        engine     = 'percolator_2_08',
    )

    filter_params = {
        'csv_filter_rules': [
            ['PEP', 'lte', 0.01],
            ['Is decoy', 'equals', 'false']
        ]
    }
    csv_file_to_filter = '/Users/MS/Desktop/special_projects/SMHacker/msgfplus_v2016_09_16/170209_SMH_170205_P9_05_ultrashort_msgfplus_v2016_09_16_pmap_unified_percolator_validated.csv'
    uc = ursgal.UController(
        params = filter_params
    )

    filtered_csv = uc.filter_csv(
        input_file = csv_file_to_filter,
    )

    return validated_result

def ligandability_quantification(mzml_file, molecule_list, evidence_lookup, formatted_fixed_labels):
    run = pymzml.run.Reader(mzml_file)
    params = {
        'molecules'        : molecule_list,
        'charges'          : [1, 2, 3],
        'fixed_labels'     : formatted_fixed_labels,
        'verbose'          : True,
        'evidences'        : evidence_lookup
    }

    lib = pyqms.IsotopologueLibrary( **params )

    results = None
    mzml_file_basename = os.path.basename(mzml_file)
    for spectrum in run:
        unit = 'second'
        if unit == 'second':
            time_div_factor = 60.0
        else:
            time_div_factor = 1

        if spectrum['ms level'] == 1:
            results = lib.match_all(
                mz_i_list = spectrum.centroidedPeaks,
                file_name = mzml_file_basename,
                spec_id   = spectrum['id'],
                spec_rt   = spectrum['scan time'] / time_div_factor,
                results   = results
            )
    return results

def edit_molecule_list(molecule_list, labels):
    # delete molecules with more than one TEV modification
    for molecule in molecule_list[:]:
        num_of_labels = 0
        for label in labels:
            num_of_labels += molecule.count(label['name'])
        if not num_of_labels == 1:
            molecule_list.remove(molecule)
    print('Initially found {0} peptides via MS2 sequencing. Now I\'ll look for nonsequenced partners.'.format(len(molecule_list)))

    for molecule in molecule_list[:]:
        print(molecule)
        check_pairs(molecule, molecule_list, labels)

    print('There are {0} peptides after partner generation.'.format(len(molecule_list)))
    return

# Todo: change in the way that not molecule_list is modified but the evidence file is modified! ;) That's the solution!!! 42!!
def check_pairs(molecule, molecule_list, labels):
    partner_label_name = ''
    current_label_name = ''
    for label in labels:
        if molecule.count(label['name']) == 0:
            partner_label_name = label['name']
        else:
            current_label_name = label['name']

    partner_molecule = molecule.replace(current_label_name, partner_label_name)

    if not partner_molecule in molecule_list:
        molecule_list.append(partner_molecule)

    return

def main():
    showStartHello()

    # add new modifications
    labels = [
        {
            'name': 'TEV_H',
            'mass': '470.26338',
            'composition': {'C': 15, '13C': 5, 'H': 32, 'N': 7, '15N': 1, 'O': 5}
        },
        {
            'name': 'TEV_L',
            'mass': '464.24957',
            'composition': {'C': 20, 'H': 32, 'N': 8, 'O': 5}
        }
    ]

    for label in labels:
        new_userdefined_unimod_molecule(label['mass'], label['name'], label['composition'])

    # MS/MS identification and validation, output is written to file system
    database_file = '/Users/MS/Desktop/special_projects/SMHacker/28092017human.fasta'
    mzml_file = '/Users/MS/Desktop/special_projects/SMHacker/170209_SMH_170205_P9_05_ultrashort.mzML'
    # mzml_file = '/Users/MS/Desktop/special_projects/SMHacker/170209_SMH_170205_P9_05_short.mzML'
    validated_result = msms_identification(mzml_file, database_file)

    # MS isotopic ligandability quantification
    evidence_file = '/Users/MS/Desktop/special_projects/SMHacker/msgfplus_v2016_09_16/170209_SMH_170205_P9_05_ultrashort_msgfplus_v2016_09_16_pmap_unified_percolator_validated_accepted.csv'
    # evidence_file = '/Users/MS/Desktop/special_projects/SMHacker/msgfplus_v2016_09_16/170209_SMH_170205_P9_05_short_msgfplus_v2016_09_16_pmap_unified_percolator_validated.csv'
    out_folder = '/Users/MS/Desktop/special_projects/SMHacker/msgfplus_v2016_09_16'

    tmp_fixed_labels = {
        'C' : [
            {
                'element_composition' : {'O': 1, 'H': 3, '14N': 1, 'C': 2},
                'evidence_mod_name': 'Carbamidomethyl'
            }
        ]
    }
    formatted_fixed_labels, evidence_lookup, molecule_list = pyqms.adaptors.parse_evidence(
        fixed_labels         = tmp_fixed_labels,
        evidence_files       = [ evidence_file ],
        evidence_score_field = 'PEP'
    )
    edit_molecule_list(molecule_list, labels)

    results = ligandability_quantification(mzml_file, molecule_list, evidence_lookup, formatted_fixed_labels)
    results.write_result_csv(out_folder + '/ligand_quant_res.csv')

    # serialize, not really necessary...
    pickle.dump(
        results,
        open(
            '/Users/MS/Desktop/special_projects/SMHacker/pyQms_results.pkl',
            'wb'
        )
    )

    # deserialize
    results_class = pickle.load(
        open(
            '/Users/MS/Desktop/special_projects/SMHacker/pyQms_results.pkl',
            'rb'
        )
    )
    rt_border_tolerance = 10

    quant_summary_file  = '/Users/MS/Desktop/special_projects/SMHacker/quant_summary.xlsx'
    results_class.write_rt_info_file(
        output_file         = quant_summary_file,
        list_of_csvdicts    = None,
        trivial_name_lookup = None,
        rt_border_tolerance = rt_border_tolerance,
        update              = True
    )
    # Todo: does not work with imputed pair peptides...
    results_class.calc_amounts_from_rt_info_file(
        rt_info_file         = quant_summary_file,
        rt_border_tolerance  = rt_border_tolerance,
        calc_amount_function = None
    )

    return

if __name__ == '__main__':
    main()
<|endoftext|>