Pagine

martedì 25 ottobre 2011

I Dizionari in Python

Quando si usa il linguaggio di programmazione python l'utilizzo dei dizionari può essere la chiave di volta per risolvere un marea di problemi.
Un dizionario e' una struttura dati, ovvero un oggetto in cui e' possibile memorizzare dati, che funziona come una tabella. Per meglio dire, si puo' immaginare il dizionario come se fosse una tabella con due colonne in cui, ad ogni campo della prima colonna (chiave), ne corrisponde uno della seconda (valore). I campi della prima colonna sono unici (non ci puo' essere piu' volte la stessa chiave) .


Ad esempio immaginiamo questa tabella:



Alunno(CHIAVI) Eta'(VALORI)
Pippo 10
Ciccio 11
Franco 10
Gianni 9


 Il relativo dizionario sara':


D={'Pippo':10, Ciccio':11, Franco':10, 'Gianni':9}


D e' la variabile che contiene il dizionario, il contenuto si deve scrivere tra parentesi graffe con la sintassi chiave : valore ,
Quindi ogni volta che si vuole memorizzare una serie di dati di tipo chiave-valore il dizionario e' la struttura dati piu' adatta. Inoltre, grazie ad un sistema che puo' tranquillamente rimanere oscuro ad un biologo, le operazioni che un dizionario consente sono veloci e molto pratiche.
Provero' ad elencarne alcune:


- Per aggiungere elementi al dizionario
D['chiave']='valore'


-Se ho una chiave e ho bisogno di andare a ripescare il corrispondente valore
V=D['chiave']


-Per testare se una determinata chiave c'e' nel dizionario
T=D.has_key('chiave')
se la chiave c'e' T=True, se la chiave non c'e' T=False


-D.keys()
Ti da una lista che contiene tutte le chiavi del dizionario ['Pippo','Ciccio','Franco','Gianni']


-D.values()
Ti da una lista che contiene tutti i valori di un dizionario [10,11,10,9]


-D.items()
Ti da una tupla che contiene tutti i dati [('Pippo', 10),('Ciccio',11),('Franco',10),('Gianni',9)]
A mio avviso le tuple non servono a niente (qualcuno mi potrebbe tirare una sassata, e me la prenderei volentieri se mi venisse anche spiegato il perche')





DNA to RNA

As well as for "reverse strand", the transformation of a string od DNA in a string of RNA is trivial for any biologist. The basic idea is very similar, here the script:


#!/usr/bin/python
import sys
DNA=sys.argv[1]
CODE={'A':'U','T':'A','C':'G','G':'C'} 
RNA=''
for c in DNA:
        RNA=RNA+CODE[c]
print RNA


In comparison of "reverse strand" script, you have to change the cariable CODE contents. Such code is passed in form of dictionary, one of the most virtuous python data structures . Also in this case the script request a DNA sequence as argument.





lunedì 24 ottobre 2011

Reverse strand

Data una sequenza di DNA, scrivere la sequenza complementare e' cosa banale, senza dubbio. Ogni biologo conosce il codice genetico e sa come fare...ma se la sequenza e' lunga lunga lunga farlo con carta e penna puo' essere un lavoretto lungo e noioso.
Ecco poche righe di codice python che ci semplificano la vita:


#!/usr/bin/python
import sys
plus_seq=sys.argv[1]
CODE={'A':'T','T':'A','C':'G','G':'C'} 
minus_seq=''
for c in plus_seq:
        minus_seq=minus_seq+CODE[c]
print minus_seq


Questo script va eseguito dal terminale dando come argomento la sequenza di DNA.
Il risultato dello script e' la sequenza complementare. Se la sequenza di input viene data in direzione 5'-3' allora la sequenza di output sara' 3'-5'.
Talvolta puo' essere utile ottenere la sequenza complementare in direzione 5'-3', in questo caso non dovete fare altro che aggiungere una sola riga di codice:




#!/usr/bin/python
import sys
plus_seq=sys.argv[1]
CODE={'A':'T','T':'A','C':'G','G':'C'} 
minus_seq=''
for c in plus_seq:
        minus_seq=minus_seq+CODE[c]
reverse_seq=minus_seq[::-1]
print reverse_seq


Piu' in generale la riga di codice in rosso permette di scrivere una qualsiasi stringa al contrario.
Provate questo esempio:


stringa1='acetone'
stringa2=stringa1[::-1]
print stringa2


Sulla variabile stringa2 sara' contenuta la parola 'enoteca'.


 





What is an argument???


Before understanding what an argument was, I spent a lot of time!
Now I try to explain it in a less nerdy as possible.


When you run a script, the goal is, given the input, compute the output.
The input may be passed as argument, from command line instead of directly inside the script.
For example, suppose you have a script that take as input a DNA sequence and convert it in RNA. Such string of character is passed as follow:


python script.py ATCTAGTGTCTGATCCTAGTCTTGATC


the string is an argument.


As well as input, you can pass as arguments also the script parameters.
For example, if you have a script that, given a protein sequence, count the frequency of a certain amino acid, you have:


python script.py PPLMAAIEKSNISAWWNF S


Where the first argument is the protein sequence, and the second argument is the character that you want to count (Serine in this case). 
By this you obtain a more general script, that is useful to count any amino acid in any sequence.
You can apply the same criteria for both python and bach scripts.
Within the script you will find the functions suitable to recognize and to read the arguments.


With python first you have to import the sys module; the sintax is the follow:
import sys
ARGUMENT1=sys.argv[1]
ARGUMENTO2=sys.argv[2]


With bash:
ARGUMENT1=$1
ARGUMENT2=$2


By this, the variables ARGUMENT1 and ARGUMENT2 are the "object" that you passed from command line.



domenica 23 ottobre 2011

Script Execution

In order to execute a script you have to perform the following steps:


1)Copy the script in txt file;


2)Rename the file with a proper extension:
      if the script is in python the name will be script.py
      if the script is in bash the name will be script.sh


3)Open the Terminal and change directory where you saved the script;


4)Execute the script:
     if the script is in python type python script.py
     if the script is in bash type bash script.sh

     typing enter the script will be executed.
     In the case of some arguments are request be careful!!


5)In my scripts usually the results will be printed in the terminal as standard outputl. You can redirect the output in a file in this way:
    python script.py > script_results