{ "metadata": { "name": "" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "code", "collapsed": false, "input": [ "import synapseclient" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 1 }, { "cell_type": "code", "collapsed": false, "input": [ "syn=synapseclient.Synapse()\n", "syn.login()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Welcome, Abhishek Pratap!\n" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "#Testing Synapse" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#new connection and login using .configSynapse file\n", "syn = synapseclient.Synapse()\n", "syn.login()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Welcome, Abhishek Pratap!\n" ] } ], "prompt_number": 3 }, { "cell_type": "code", "collapsed": false, "input": [ "#get info about a current project/folder in Synapse\n", "existing_project = syn.get('syn2171593')\n", "url = 'http://www.ncbi.nlm.nih.gov/geo/download/?acc=GSM649323&format=file&file=GSM649323%2ECEL%2Egz'\n", "\n", "data_file = synapseclient.File(path=url,parent=existing_project,synapseStore=False)\n", "data_file = syn.store(data_file)" ], "language": "python", "metadata": {}, "outputs": [ { "ename": "SynapseHTTPError", "evalue": "400 Client Error: Bad Request\nInvalid Entity name: '?acc=GSM649323&format=file&file=GSM649323%2ECEL%2Egz'. Entity names may only contain: letters, numbers, spaces, underscores, hypens, periods, plus signs, and parentheses", "output_type": "pyerr", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mSynapseHTTPError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0mdata_file\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0msynapseclient\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mFile\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mpath\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0murl\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mparent\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mexisting_project\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0msynapseStore\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mFalse\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 6\u001b[0;31m \u001b[0mdata_file\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0msyn\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mstore\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdata_file\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;32m/Users/abhishek/anaconda/lib/python2.7/site-packages/synapseclient/client.pyc\u001b[0m in \u001b[0;36mstore\u001b[0;34m(self, obj, **kwargs)\u001b[0m\n\u001b[1;32m 671\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 672\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 673\u001b[0;31m \u001b[0mproperties\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_createEntity\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mproperties\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 674\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mSynapseHTTPError\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0mex\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 675\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mcreateOrUpdate\u001b[0m \u001b[0;32mand\u001b[0m \u001b[0mex\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mresponse\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mstatus_code\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;36m409\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m/Users/abhishek/anaconda/lib/python2.7/site-packages/synapseclient/client.pyc\u001b[0m in \u001b[0;36m_createEntity\u001b[0;34m(self, entity)\u001b[0m\n\u001b[1;32m 1975\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1976\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdebug\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;32mprint\u001b[0m \u001b[0;34m\"\\n\\n~~~ creating ~~~\\n\"\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mjson\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdumps\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mget_properties\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mentity\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mindent\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1977\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrestPOST\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0muri\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m'/entity'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mbody\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mjson\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdumps\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mget_properties\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mentity\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 1978\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1979\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m/Users/abhishek/anaconda/lib/python2.7/site-packages/synapseclient/retry.pyc\u001b[0m in \u001b[0;36mwith_retry\u001b[0;34m(*args, **kwargs)\u001b[0m\n\u001b[1;32m 53\u001b[0m \u001b[0;31m# Try making the call\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 54\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 55\u001b[0;31m \u001b[0mresponse\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mfn\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 56\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mException\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0mex\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 57\u001b[0m \u001b[0mexc_info\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0msys\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mexc_info\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m/Users/abhishek/anaconda/lib/python2.7/site-packages/synapseclient/client.pyc\u001b[0m in \u001b[0;36mrestPOST\u001b[0;34m(self, uri, body, endpoint, headers, **kwargs)\u001b[0m\n\u001b[1;32m 2099\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdebug\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2100\u001b[0m \u001b[0mutils\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdebug_response\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mresponse\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 2101\u001b[0;31m \u001b[0mexceptions\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_raise_for_status\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mresponse\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2102\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2103\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mresponse\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mheaders\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'content-type'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mNone\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;34m'application/json'\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m/Users/abhishek/anaconda/lib/python2.7/site-packages/synapseclient/exceptions.pyc\u001b[0m in \u001b[0;36m_raise_for_status\u001b[0;34m(response)\u001b[0m\n\u001b[1;32m 102\u001b[0m \u001b[0;31m## TODO: Might as well append more information to the exception message\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 103\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 104\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mSynapseHTTPError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmessage\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mresponse\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mresponse\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 105\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mSynapseHTTPError\u001b[0m: 400 Client Error: Bad Request\nInvalid Entity name: '?acc=GSM649323&format=file&file=GSM649323%2ECEL%2Egz'. Entity names may only contain: letters, numbers, spaces, underscores, hypens, periods, plus signs, and parentheses" ] } ], "prompt_number": 6 }, { "cell_type": "code", "collapsed": false, "input": [ "existing_project" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 103, "text": [ "Project(modifiedOn=u'2013-06-04T20:55:57.107Z', modifiedBy=u'Brian Bot', entityType=u'org.sagebionetworks.repo.model.Project', creationDate=u'1370379357107', name=u'Wondrous Research Example', uri=u'/entity/syn1901847/annotations', etag=u'025f1606-8dc4-40e2-af60-08ab39ac1ab4', createdBy=u'Brian Bot', parentId=u'syn4489', concreteType='org.sagebionetworks.repo.model.Project', uri=u'/repo/v1/entity/syn1901847', annotations=u'/repo/v1/entity/syn1901847/annotations', accessControlList=u'/repo/v1/entity/syn1901847/acl', createdOn=u'2013-06-04T20:55:57.107Z', id=u'syn1901847')" ] } ], "prompt_number": 103 }, { "cell_type": "code", "collapsed": false, "input": [ "#get only the metadata\n", "proj_metadata_only = syn.get('syn1906480',downloadFile=False)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 104 }, { "cell_type": "code", "collapsed": false, "input": [ "#create a project\n", "project = synapseclient.Project('test_apratap')\n", "project = syn.store(project)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 112 }, { "cell_type": "code", "collapsed": false, "input": [ "print project" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Project: test_apratap (syn2171593)\n", "properties:\n", " accessControlList=/repo/v1/entity/syn2171593/acl\n", " annotations=/repo/v1/entity/syn2171593/annotations\n", " concreteType=org.sagebionetworks.repo.model.Project\n", " createdBy=Abhishek Pratap\n", " createdOn=2013-08-21T17:09:11.418Z\n", " entityType=org.sagebionetworks.repo.model.Project\n", " etag=a0431fff-3aa3-46d0-a39a-33cca45a3d8e\n", " id=syn2171593\n", " modifiedBy=Abhishek Pratap\n", " modifiedOn=2013-08-26T22:20:22.383Z\n", " name=test_apratap\n", " parentId=syn4489\n", " uri=/repo/v1/entity/syn2171593\n", "annotations:\n", " creationDate=1377104951418\n", " etag=a0431fff-3aa3-46d0-a39a-33cca45a3d8e\n", " id=syn2171593\n", " uri=/entity/syn2171593/annotations\n", "\n" ] } ], "prompt_number": 113 }, { "cell_type": "code", "collapsed": false, "input": [ "#print the project entity id\n", "print project.id" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "syn2171593\n" ] } ], "prompt_number": 114 }, { "cell_type": "code", "collapsed": false, "input": [ "#create a folder under a project\n", "data_folder = synapseclient.Folder('Data',parent=project)\n", "data_folder = syn.store(data_folder)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 115 }, { "cell_type": "code", "collapsed": false, "input": [ "#print the data folder id\n", "print data_folder.id" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "syn2171607\n" ] } ], "prompt_number": 116 }, { "cell_type": "code", "collapsed": false, "input": [ "#poplulate Snyapse with annotation\n", "project.test = 'pass'\n", "project.foo = 'bar'\n", "project = syn.store(project)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 117 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "#Full example of getting data from Synpase and making a plot" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import numpy as np" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 118 }, { "cell_type": "code", "collapsed": false, "input": [ "#GET THE FILE WITH RESPONSES\n", "resp_file = syn.get('syn1906479',)\n", "resp = np.loadtxt(resp_file.path, skiprows=1, usecols=(1,))" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 119 }, { "cell_type": "code", "collapsed": false, "input": [ "#GET THE EXPRESSION DATA\n", "expr_file = syn.get('syn1906480')\n", "expr = np.loadtxt(expr_file.path, skiprows=1, usecols=range(1,len(resp) + 1))" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 120 }, { "cell_type": "code", "collapsed": false, "input": [ "#Calculate p-values for the expression differences between response groups\n", "import scipy.stats as stats\n", "pvals = [stats.ttest_ind(row[resp==0], row[resp==1])[1] for row in expr]" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 121 }, { "cell_type": "code", "collapsed": false, "input": [ "#make a p-value histogram and store in synapse\n", "import pylab\n", "pylab.hist(pvals)\n", "pylab.savefig('hist.png')\n", "\n", "\n", "#store in our folder we created earlier\n", "plot_file = synapseclient.File('hist.png',description='Histogram pf P-values', parentId=data_folder.id)\n", "plot_file = syn.store(plot_file)\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "." ] }, { "output_type": "stream", "stream": "stdout", "text": [ "." ] }, { "output_type": "stream", "stream": "stdout", "text": [ "," ] }, { "output_type": "stream", "stream": "stdout", "text": [ "!" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "!\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Upload completed in 12 seconds.\n" ] } ], "prompt_number": 122 }, { "cell_type": "code", "collapsed": false, "input": [ "syn.onweb(data_folder.id)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 123 } ], "metadata": {} } ] }