Commit ec08bfe7 authored by Lingling Jin (lij313)'s avatar Lingling Jin (lij313)
Browse files

add python-examples

parent af2ca9eb
Dive Date Lat Lon Depth Notes
Tiburon 596 19-Jul-03 36 36.12 N 122 22.48 W 1190 holotype
JSL II 1411 16-Sep-86 39 56.4 N 70 14.3 W 518 paratype
JSL II 930 18-Aug-84 40 05.03 N 69 03.01 W 686 Youngbluth (1989)
Ventana 1575 11-Mar-99 36 42.24 N 122 02.52 W 767
Ventana 1777 16-Jun-00 36 42.60 N 122 02.70 W 934
Ventana 2243 9-Sep-02 36 42.48 N 122 03.84 W 1001
Tiburon 515 24-Nov-02 36 42.00 N 122 01.98 W 1156
Tiburon 531 13-Mar-03 24 19.02 N 109 12.18 W 1144
Tiburon 547 31-Mar-03 24 14.04 N 109 40.02 W 1126
JSL II 3457 26-Sep-03 40 17.77 N 68 06.68 W 862 Francesc Pages (pers.comm)
\ No newline at end of file
#!/usr/local/bin/python3
my_in=input('Enter your name: ')
if ( my_in != '' ) :
print( 'Thank you, '+my_in )
print( 'Programming running, with or without your input' )
#!/usr/local/bin/python3
my_rand_list = [5, 6, 4, 1, 7, 3, 2, 0, 9, 8]
largest = -1
for item in my_rand_list:
if( item > largest ):
largest = item
print( largest )
print
print( 'largest number is ', largest )
#! /usr/bin/env python
DNASeq = "ATGTCTCATTCAAAGCA"
SeqLength = float(len(DNASeq))
BaseList = "ACGT"
for Base in BaseList:
Percent = 100 * DNASeq.count(Base) / SeqLength
print "%s: %4.1f" % (Base,Percent)
#!/usr/bin/env python
DNASeq = "ATGTCTCATTCAAAGCA"
SeqLength = float(len(DNASeq))
BaseList = list(set(DNASeq))
for Base in BaseList:
Percent = 100 * DNASeq.count(Base) / SeqLength
print "%s: %4.1f" % (Base,Percent)
#! /usr/bin/env python
DNASeq = "ATGAAC"
print 'Sequence:', DNASeq
SeqLength = float(len(DNASeq))
print "Sequence Length:", SeqLength
NumberA = DNASeq.count('A')
NumberC = DNASeq.count('C')
NumberG = DNASeq.count('G')
NumberT = DNASeq.count('T')
print "A:", NumberA/SeqLength
print "C:", NumberC/SeqLength
print "G:", NumberG/SeqLength
print "T:", NumberT/SeqLength
#! /usr/bin/env python
# This program takes a DNA sequence (without checking)
# and shows its length and the nucleotide composition
# This program is described in Chapter 8 of PCfB
DNASeq = "ATGTCTCATTCAAAGCA"
# gather user input for sequence
# this overrides the definition of DNASeq above
# DNASeq = raw_input("Enter a sequence: ")
DNASeq = DNASeq.upper() # convert to uppercase for .count() function
DNASeq = DNASeq.replace(" ","") # remove spaces
print 'Sequence:', DNASeq
# below are nested functions: first find the length, then make it float
SeqLength = float(len(DNASeq))
print "Sequence Length:", SeqLength
NumberA = DNASeq.count('A')
NumberC = DNASeq.count('C')
NumberG = DNASeq.count('G')
NumberT = DNASeq.count('T')
# Old way to output the Numbers
# print "A:", NumberA/SeqLength
# print "C:", NumberC/SeqLength
# print "G:", NumberG/SeqLength
# print "T:", NumberT/SeqLength
# Calculate percentage and output to 1 decimal
print "A: %.1f" % (100 * NumberA / SeqLength)
print "C: %.1f" % (100 * NumberC / SeqLength)
print "G: %.1f" % (100 * NumberG / SeqLength)
print "T: %.1f" % (100 * NumberT / SeqLength)
#
# End of Chapter 8
# Beginning of Chapter 9
#
# Calculating primer melting points with different formulas by length
TotalStrong = NumberG + NumberC
TotalWeak = NumberA + NumberT
if SeqLength >= 14:
#formula for sequences > 14 nucleotides long
MeltTempLong = 64.9 + 41 * (TotalStrong - 16.4) / SeqLength
print "Tm Long (>14): %.1f C" % (MeltTempLong)
else:
#formula for sequences less than 14 nucleotides long
MeltTemp = (4 * TotalStrong) + (2 * TotalWeak)
print "Tm Short: %.1f C" % (MeltTemp)
#!/usr/bin/env python
print( 'Good morning!' )
# in python2, the above would be
# print 'Good morning!'
#! /usr/bin/env python
# Revision of "dnacalc.py" from Ch 8 of Haddock & Dunn
# Converted from Python 2 to Python 3
DNASeq = "ATGAAC"
print ( 'Sequence:', DNASeq )
SeqLength = len(DNASeq)
print ( "Sequence Length:", SeqLength )
NumberA = DNASeq.count('A')
NumberC = DNASeq.count('C')
NumberG = DNASeq.count('G')
NumberT = DNASeq.count('T')
print ( "A:", NumberA/SeqLength )
print ( "C:", NumberC/SeqLength )
print ( "G:", NumberG/SeqLength )
print ( "T:", NumberT/SeqLength )
#! /usr/bin/env python
# Revision of "dnacalc.py" from Ch 8 of Haddock & Dunn
# Converted from Python 2 to Python 3
# Converted ratio to percentage and used formatted output
DNASeq = "ATGAAC"
print ( 'Sequence:', DNASeq )
SeqLength = len(DNASeq)
print ( "Sequence Length:", SeqLength )
NumberA = DNASeq.count('A')
NumberC = DNASeq.count('C')
NumberG = DNASeq.count('G')
NumberT = DNASeq.count('T')
print ( "A: %.1f%%" % (100*NumberA/SeqLength) )
print ( "C: %.1f%%" % (100*NumberC/SeqLength) )
print ( "G: %.1f%%" % (100*NumberG/SeqLength) )
print ( "T: %.1f%%" % (100*NumberT/SeqLength) )
#! /usr/bin/env python
## Revision of "dnacalc1.py" from Ch 8 of Haddock & Dunn
## Original comments
# This program takes a DNA sequence (without checking)
# and shows its length and the nucleotide composition
# This program is described in Chapter 8 of PCfB
## Converted from Python 2 to Python 3
## Modified formatted output to include percent symbol
## Removed material from Ch 9 from the end of the script
DNASeq = "ATGTCTCATTCAAAGCA"
# gather user input for sequence
# this overrides the definition of DNASeq above
DNASeq = input("Enter a sequence: ")
DNASeq = DNASeq.upper() # convert to uppercase for .count() function
DNASeq = DNASeq.replace(" ","") # remove spaces
print ( 'Sequence:', DNASeq )
SeqLength = len(DNASeq)
print ( "Sequence Length:", SeqLength )
NumberA = DNASeq.count('A')
NumberC = DNASeq.count('C')
NumberG = DNASeq.count('G')
NumberT = DNASeq.count('T')
# Old way to output the Numbers
# print "A:", NumberA/SeqLength
# print "C:", NumberC/SeqLength
# print "G:", NumberG/SeqLength
# print "T:", NumberT/SeqLength
# Calculate percentage and output to 1 decimal
print ( "A: %.1f%%" % (100 * NumberA / SeqLength) )
print ( "C: %.1f%%" % (100 * NumberC / SeqLength) )
print ( "G: %.1f%%" % (100 * NumberG / SeqLength) )
print ( "T: %.1f%%" % (100 * NumberT / SeqLength) )
#! /usr/bin/env python
## Revision of "dnacalc1.py" from Ch 9 of Haddock & Dunn
## Original comments
# This program takes a DNA sequence (without checking)
# and shows its length and the nucleotide composition
# This program is described in Chapter 8 of PCfB
## Converted from Python 2 to Python 3
## Modified formatted output to include percent symbol
DNASeq = "ATGTCTCATTCAAAGCA"
# gather user input for sequence
# this overrides the definition of DNASeq above
DNASeq = input("Enter a sequence: ")
DNASeq = DNASeq.upper() # convert to uppercase for .count() function
DNASeq = DNASeq.replace(" ","") # remove spaces
print ( 'Sequence:', DNASeq )
SeqLength = len(DNASeq)
print ( "Sequence Length:", SeqLength )
NumberA = DNASeq.count('A')
NumberC = DNASeq.count('C')
NumberG = DNASeq.count('G')
NumberT = DNASeq.count('T')
# Old way to output the Numbers
# print "A:", NumberA/SeqLength
# print "C:", NumberC/SeqLength
# print "G:", NumberG/SeqLength
# print "T:", NumberT/SeqLength
# Calculate percentage and output to 1 decimal
print ( "A: %.1f%%" % (100 * NumberA / SeqLength) )
print ( "C: %.1f%%" % (100 * NumberC / SeqLength) )
print ( "G: %.1f%%" % (100 * NumberG / SeqLength) )
print ( "T: %.1f%%" % (100 * NumberT / SeqLength) )
#
# End of Chapter 8
# Beginning of Chapter 9
#
# Calculating primer melting points with different formulas by length
TotalStrong = NumberG + NumberC
TotalWeak = NumberA + NumberT
if SeqLength >= 14:
#formula for sequences > 14 nucleotides long
MeltTempLong = 64.9 + 41 * (TotalStrong - 16.4) / SeqLength
print ( "Tm Long (>14): %.1f C" % (MeltTempLong) )
else:
#formula for sequences less than 14 nucleotides long
MeltTemp = (4 * TotalStrong) + (2 * TotalWeak)
print ( "Tm Short: %.1f C" % (MeltTemp) )
#!/usr/bin/env python
## Revision of "proteincalc.py" from Ch 9 of Haddock & Dunn
## Original comments
# This program takes a protein sequence
# and determines its molecular weight
# The look-up table is generated from a web page
# throught a series of regular expression replacements
# This program is described in Chapter 9 of PCfB
## Converted from Python 2 to Python 3
AminoDict={
'A':89.09,
'R':174.20,
'N':132.12,
'D':133.10,
'C':121.15,
'Q':146.15,
'E':147.13,
'G':75.07,
'H':155.16,
'I':131.17,
'L':131.17,
'K':146.19,
'M':149.21,
'F':165.19,
'P':115.13,
'S':105.09,
'T':119.12,
'W':204.23,
'Y':181.19,
'V':117.15,
'X':0.0,
'-':0.0,
'*':0.0 }
#starting sequence string, on which to perform calculations
ProteinSeq="FDILSATFTYGNR"
MolWeight=0
# step through each character in the ProteinSeq string,
# setting the AminoAcid variable to its value
for AminoAcid in ProteinSeq:
# look up the value corresponding to the current amino acid
# add its value of the present amino acid to the running total
MolWeight = MolWeight + AminoDict[AminoAcid]
# once the loop is completed, print protseq and the molecular weight
print ( "Protein:", ProteinSeq )
print ( "Molecular weight: %.1f" % (MolWeight) )
[lij313@tux6 ~]$ python
Python 3.8.3 (default, Jul 2 2020, 16:21:59)
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> ListofLists = [ [34.5, -120.6], [33.8, -122.7] ]
>>> Dict = dict( ListofLists )
>>> Dict
{34.5: -120.6, 33.8: -122.7}
>>> Dict[34.5]
-120.6
>>> Dict[M]=0
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'M' is not defined
>>> Dict['M']=0
>>> Dict
{34.5: -120.6, 33.8: -122.7, 'M': 0}
>>> Dict['M']
0
>>> Dict['m']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'm'
>>> Dict.keys()
dict_keys([34.5, 33.8, 'M'])
>>> Dict.values()
dict_values([-120.6, -122.7, 0])
>>> exit()
[lij313@tux6 ~]$
\ No newline at end of file
#! /usr/bin/env python
## Revision of "compositioncalc1.py" from Ch 9 of Haddock & Dunn
## Converted from Python 2 to Python 3
## Make output of program more like that of "dnacalc.py"
DNASeq = "ATGTCTCATTCAAAGCA"
print ( 'Sequence:', DNASeq )
SeqLength = len(DNASeq)
print ( "Sequence Length:", SeqLength )
BaseList = "ACGT"
for Base in BaseList:
Percent = 100 * DNASeq.count(Base) / SeqLength
print ( "%s: %4.1f%%" % (Base,Percent) )
[lij313@tux6 ~]$ python
Python 3.8.3 (default, Jul 2 2020, 16:21:59)
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> MyList=list( "abcde" )
>>> MyList
['a', 'b', 'c', 'd', 'e']
>>> MyList[0:2]
['a', 'b']
>>> MyList[:3]
['a', 'b', 'c']
>>> MyList[3:]
['d', 'e']
>>> MyList[:]
['a', 'b', 'c', 'd', 'e']
>>>
>>> MyList[-2:]
['d', 'e']
>>> MyList[:-2]
['a', 'b', 'c']
>>>
>>> MyList[3]='x'
>>> MyList
['a', 'b', 'c', 'x', 'e']
>>> exit()
[lij313@tux6 ~]$
[lij313@tux6 ~]$ python
Python 3.8.3 (default, Jul 2 2020, 16:21:59)
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> range(0,10)
range(0, 10)
>>> X=range(0,10)
>>> X
range(0, 10)
>>> X[:]
range(0, 10)
>>> for i in range(10,0,-1):
... print( i )
...
10
9
8
7
6
5
4
3
2
1
>>> exit()
[lij313@tux6 ~]$
[lij313@tux6 ~]$ python
Python 3.8.3 (default, Jul 2 2020, 16:21:59)
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> X=list( "abcde" )
>>> Y=X
>>> Y
['a', 'b', 'c', 'd', 'e']
>>> X[2]='x'
>>> X
['a', 'b', 'x', 'd', 'e']
>>> Y
['a', 'b', 'x', 'd', 'e']
>>> X=list( "fghij" )
>>> X
['f', 'g', 'h', 'i', 'j']
>>> Y
['a', 'b', 'x', 'd', 'e']
>>> exit()
[lij313@tux6 ~]$
[lij313@tux6 ~]$ python
Python 3.8.3 (default, Jul 2 2020, 16:21:59)
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> X=['a','b']
>>> X[2]='c'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list assignment index out of range
>>> X.append('c')
>>> X
['a', 'b', 'c']
>>> X.remove('b')
>>> X
['a', 'c']
>>> Y=list("abbracadabra")
>>>
>>> Y
['a', 'b', 'b', 'r', 'a', 'c', 'a', 'd', 'a', 'b', 'r', 'a']
>>> Y.count('a')
5
>>> Y.index('r')
3
>>>
>>> 'd' in Y
True
>>> 'm' in Y
False
>>> exit()
[lij313@tux6 ~]$
#!/usr/bin/env python
## Revision of "compositioncalc2.py" from Ch 9 of Haddock & Dunn
## Converted from Python 2 to Python 3
## Make output of program more like that of "dnacalc.py"
DNASeq = "ATGTCTCATTCAAAGCA"
print ( 'Sequence:', DNASeq )
SeqLength = len(DNASeq)
print ( "Sequence Length:", SeqLength )
BaseList = list(set(DNASeq))
for Base in BaseList:
Percent = 100 * DNASeq.count(Base) / SeqLength
print ( "%s: %4.1f%%" % (Base,Percent) )
[lij313@tux6 ~]$ python
Python 3.8.3 (default, Jul 2 2020, 16:21:59)
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> S={'a','b','c'}
>>> type(S)
<class 'set'>
>>> S
{'b', 'a', 'c'}
>>> T={'a','b','c','a'}
>>> T
{'b', 'a', 'c'}
>>> exit()
[lij313@tux6 ~]$
[lij313@tux6 ~]$ diff compositioncalc2.py ex14.py
1a2,4
> ## Revision of "compositioncalc1.py" from Ch 9 of Haddock & Dunn
> ## Converted from Python 2 to Python 3
> ## Make output of program more like that of "dnacalc.py"
3c6,10
< SeqLength = float(len(DNASeq))
---
> print ( 'Sequence:', DNASeq )
>
> SeqLength = len(DNASeq)
>
> print ( "Sequence Length:", SeqLength )
8c15
< print "%s: %4.1f" % (Base,Percent)
---
> print "%s: %4.1f%%" % (Base,Percent)
[lij313@tux6 ~]$ diff ex10.py ex14.py
1,2c1,2
< #! /usr/bin/env python
< ## Revision of "compositioncalc1.py" from Ch 9 of Haddock & Dunn
---
> #!/usr/bin/env python
> ## Revision of "compositioncalc2.py" from Ch 9 of Haddock & Dunn
12,14c12,14
< BaseList = "ACGT"
< for Base in BaseList:
< Percent = 100 * DNASeq.count(Base) / SeqLength
---
> BaseList = list(set(DNASeq))
> for Base in BaseList:
> Percent = 100 * DNASeq.count(Base) / SeqLength
[lij313@tux6 ~]$
[lij313@tux6 ~]$ chmod u+x ex14.py
[lij313@tux6 ~]$ ./ex14.py
Sequence: ATGTCTCATTCAAAGCA
Sequence Length: 17
G: 11.8%
C: 23.5%
A: 35.3%
T: 29.4%
[lij313@tux6 ~]$
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment