Python

Time to get past shell scripting.

Getting Easier Now

#In a terminal window launch the ipython notebook (opens a new tab in default web browser)

#Transpose a matrix and create a pivot table on another matrix

IPython Notebook with transpose and pivot examples

#

#See build a Mac for install of python3

$ python3
Python 3.4.2 (v3.4.2:ab2c023a9432, Oct  5 2014, 20:42:22)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>    ###THIS IS THE PYTHON PROMPT###
#Lets get things loaded to do some work
#Import os so you can do same basic things like navigation and list files in the directory
>>> import os
>>> os.getcwd()
'/Users/jkeats'
#Navigate to the Desktop (one directory above
>>> os.chdir("Desktop")
>>> os.getcwd()
'/Users/jkeats/Desktop'
#Import the SciPy Tools
>>> import pandas as pd
>>> import numpy as np
>>> import matplotlib.pyplot as plt
#Read in a tab-deliminated table (test1.txt)

Gene P1 P2 P3

MYC 1 0 0

TRAF3 2 1 2

CYLD 0 2 1

>>> table = pd.read_csv("test1.txt", sep="\t", index_col=0)
>>> table.head()
            P1  P2  P3
Gene
MYC     1   0   0
TRAF3   2   1   2
CYLD    0   2   1
#Transpose the table (This is too easy!)
>>> table.T
Gene  MYC  TRAF3  CYLD
P1      1      2     0
P2      0      1     2
P3      0      2     1
#Make the transposed table a new object
>>> trans = table.T
#Write the new transposed object to a new tab-deliminated file
>>> trans.to_csv("transposed.txt", sep="\t")
## PIVOT TABLES SO WE NEVER NEED TO USE EXCEL AGAIN!!

Some early basics.

1) To build a python script

#!/bin/python #This must be the header line

2) Query the user

name = raw_input("What is your name?")

print "Hello %s" % (name)

3) Dates

from datetime import datetime

now = datetime.now()

print now

print now.year

print now.month

print now.day

print '%s/%s/%s' % (now.month, now.day, now.year)

print '%s:%s:%s' % (now.hour, now.minute, now.second)

print '%s/%s/%s %s:%s:%s' % (now.month, now.day, now.year, now.hour, now.minute, now.second)

4) IF, ELSE-IF, ELSE Decision Trees

Comparitors

== Equals

!= Does not equal

> Greater Than

< Less Than

>= Greater Than or Equal To

<= Less Than or Equal To

Summarizing

and - checks if both values are true

or - checks if at least one is true

not - gives the opposite of the statement

def lab():

print "You've just entered the lab!"

print "Is your lab on the left or the right?"

answer = raw_input("Type left or right and hit 'Enter'.").lower()

if answer == "left" or answer == "l":

print "Wrong Direction, the Carpten Lab is to the Left!"

elif answer == "right" or answer == "r":

print "Good job, now find your bench and get to work!"

else:

print "Hey Neuroguys, you need to pick Left or Right, try again."

lab()

lab()

# Transpose a Matrix File

python -c "import sys; print('\n'.join(' '.join(c) for c in zip(*(l.split() for l in sys.stdin.readlines() if l.strip()))))" < INPUT.txt > OUTPUT.txt

$ jupyter notebook