[False, True, False, False, True] def bindingSites( strTFBS, astrPromoters ): for strPromoter in astrPromoters: pass def bindingSites( strTFBS, astrPromoters ): for strPromoter in astrPromoters: pass return afResults "test".find( "es" ) "taste".find( "es" ) "testy".find( "t" ) "testy".find( "ty") "This is a test of the emergency broadborking system".find( "bork" ) def bindingSites( strTFBS, astrPromoters ): for strPromoter in astrPromoters: if strPromoter.find( strTFBS ) >= 0: afResults.append( True ) else: afResults.append( False ) return afResults bindingSites( "T", ["A", "C", "T", "G"] ) def bindingSites( strTFBS, astrPromoters ): for strPromoter in astrPromoters: print( "I'm at location 0, and strPromoter is: " + strPromoter ) if strPromoter.find( strTFBS ) >= 0: afResults.append( True ) else: print( "I'm at location 1, and strPromoter is: " + strPromoter ) afResults.append( False ) return afResults bindingSites( "T", ["A", "C", "T", "G"] ) def bindingSites( strTFBS, astrPromoters ): afResults = [] for strPromoter in astrPromoters: print( "I'm at location 0, and strPromoter is: " + strPromoter ) if strPromoter.find( strTFBS ) >= 0: afResults.append( True ) else: print( "I'm at location 1, and strPromoter is: " + strPromoter ) afResults.append( False ) return afResults bindingSites( "T", ["A", "C", "T", "G"] ) def bindingSites( strTFBS, astrPromoters ): afResults = [] for strPromoter in astrPromoters: if strPromoter.find( strTFBS ) >= 0: afResults.append( True ) else: afResults.append( False ) return afResults bindingSites( "T", ["A", "C", "T", "G"] ) def bindingSites( strTFBS, astrPromoters ): afResults = [] for strPromoter in astrPromoters: fFound = ( strPromoter.find( strTFBS ) >= 0 ) afResults.append( fFound ) return afResults bindingSites( "T", ["A", "C", "T", "G"] ) def bindingSites( strTFBS, astrPromoters ): afResults = [] for strPromoter in astrPromoters: afResults.append( strPromoter.find( strTFBS ) >= 0 ) return afResults bindingSites( "T", ["A", "C", "T", "G"] ) def bindingSites( strTFBS, astrPromoters ): afResults = [( s.find( strTFBS ) >= 0 ) for s in astrPromoters] return afResults bindingSites( "T", ["A", "C", "T", "G"] ) def bindingSites( strTFBS, astrPromoters ): return [( s.find( strTFBS ) >= 0 ) for s in astrPromoters] bindingSites( "T", ["A", "C", "T", "G"] ) strTFBS = "TGACTCA" astrPromoters = ["GGAGTGGTTG", "ATGACTCATA", "CTCCATTCGG", "TTTTTTCGTG", "CATTGACTCA"] bindingSites( strTFBS, astrPromoters ) import random def randomNucleotides( iLength ): astrNucleotides = [] for i in xrange( iLength ): astrNucleotides.append( "ACGT"[random.randrange( 4 )] ) # The join method targets a string, accepts a list of strings as input, # and returns a new string created by concatenating all strings in the list # separated by the target, e.g. # "-".join( ["a", "b", "c"] ) => "a-b-c" # ", ".join( ["one", "two"] ) => "one, two" # "".join( ["sm", "oo", "sh"] ) => "smoosh" return "".join( astrNucleotides ) strTFBS = randomNucleotides( 4 ) strTFBS astrPromoters = [] for i in xrange( 100 ): astrPromoters.append( randomNucleotides( 50 ) ) # We'll just show the first 10 for brevity's sake astrPromoters[:10] print( bindingSites( strTFBS, astrPromoters ) ) "ATGGATTTAATG" {"ATG" : 2, "GAT" : 1, "TTA" : 1} def codonCount( strORF ): return hashCounts # Slice the full string from beginning (index 0) to end (index 5) "abcde"[0:5] # Slice off only the first three characters "abcde"[0:3] # Slice off the middle two characters "abcde"[1:3] # A missing beginning index is assumed to be zero "abcde"[:3] # A missing end index is assumed to be the length of the string "abcde"[1:] # And by the way, you can slice lists as well as strings [1, 2, 3, 4, 5][1:3] "ATGGATTTAATG"[0:3] "ATGGATTTAATG"[3:6] i = 6 "ATGGATTTAATG"[i:( i + 3 )] # Default is just an end range( 5 ) # Two arguments mean a beginning and an end range( 1, 5 ) # Three arguments mean a beginning, end, and step, the last of which defaults to one range( 1, 5, 2 ) # 10 to 89 in multiples of 13 range( 10, 90, 13 ) # 100 to 1000 in multiples of 111 range( 100, 1001, 111 ) def codonCount( strORF ): # Start our counts as an empty dictionary to avoid our previous mistake! hashCounts = {} for i in xrange( 0, len( strORF ), 3 ): strCodon = strORF[i:( i + 3 )] print( strCodon ) return hashCounts codonCount( "ATGGATTTAATG" ) def codonCount( strORF ): hashCounts = {} for i in xrange( 0, len( strORF ), 3 ): strCodon = strORF[i:( i + 3 )] hashCounts[strCodon] = hashCounts[strCodon] + 1 return hashCounts codonCount( "ATGGATTTAATG" ) # This is OK since it's in the dictionary {"example" : 1, "hash" : 2}["example"] # This is not! {"example" : 1, "hash" : 2}["barf"] hashExample = {"example" : 1, "hash" : 2} hashExample["barf"] = "barfs no more" hashExample hashExample["barf"] {"example" : 1, "hash" : 2}.get( "example" ) print( {"example" : 1, "hash" : 2}.get( "doesn't barf!" ) ) def codonCount( strORF ): hashCounts = {} for i in xrange( 0, len( strORF ), 3 ): strCodon = strORF[i:( i + 3 )] iCount = hashCounts.get( strCodon ) if iCount == None: hashCounts[strCodon] = 1 else: hashCounts[strCodon] = iCount + 1 return hashCounts codonCount( "ATGGATTTAATG" ) {"example" : 1, "hash" : 2}.get( "doesn't barf!", "I'm not here" ) def codonCount( strORF ): hashCounts = {} for i in xrange( 0, len( strORF ), 3 ): strCodon = strORF[i:( i + 3 )] iCount = 1 + hashCounts.get( strCodon, 0 ) hashCounts[strCodon] = iCount return hashCounts codonCount( "ATGGATTTAATG" ) def codonCount( strORF ): hashCounts = {} for i in xrange( 0, len( strORF ), 3 ): strCodon = strORF[i:( i + 3 )] hashCounts[strCodon] = 1 + hashCounts.get( strCodon, 0 ) return hashCounts codonCount( "ATGGATTTAATG" ) codonCount( "ATGGATTTAATGATGGATTTAATGATGGATTTAATGATGGATTTAATGATGGATTTAATG" ) strORF = randomNucleotides( 100 ) strORF print( codonCount( strORF ) ) print( codonCount( randomNucleotides( 1000 ) ) ) def factorial( iN ): iNFactorial = 1 return iNFactorial factorial( 0 ) == 1 factorial( 1 ) == 1 factorial( 6 ) == 720 factorial( 20 ) == 2432902008176640000 # Even 14 % 2 # Odd 13 % 2 def anOddSum( iOne, iTwo ): iSum = 0 return iSum def anOddSum( iOne, iTwo ): if iOne > iTwo: iOne, iTwo = iTwo, iOne if not ( iOne % 2 ): iOne += 1 iSum = 0 for i in xrange( iOne, iTwo + 1, 2 ): iSum += i return iSum anOddSum( 1, 3 ) == 4 anOddSum( 3, 1 ) == 4 # 3 + 5 + 7 anOddSum( 2, 8 ) == 15 anOddSum( 100, 200 ) == 7500 print( "This will show up on standard output" ) sys.stdout.write( "This will show up on standard output\n" ) print( "I am the very model" ) print( "of a modern Major General" ) sys.stdout.write( "I am the very model" ) sys.stdout.write( "of a modern Major General" ) def someFunctionThatWritesOutput( ostm, astrOutput ): for strOutput in astrOutput: ostm.write( strOutput + "\n" ) someFunctionThatWritesOutput( sys.stdout, ["animal", "vegetable", "mineral"] ) def someFunctionThatReadsInput( istm ): astrInput = [] for strLine in istm: astrInput.append( strLine ) return astrInput someFunctionThatReadsInput( sys.stdin ) ostmTest = open( "test.txt", "w" ) ostmTest.write( "I am the very model\n" ) ostmTest.write( "of a modern Major General\n" ) I am the very model of a modern Major General ostmTest.write( "I've information vegetable, animal, and mineral\n" ) ostmTest.write( "I know the kings of England\n" ) I am the very model of a modern Major General I've information vegetable, animal, and mineral I know the kings of England someFunctionThatWritesOutput( open( "test.txt", "w" ), ["animal", "vegetable", "mineral"] ) animal vegetable mineral someFunctionThatWritesOutput( open( "test.txt", "a" ), ["Marathon", "Waterloo"] ) animal vegetable mineral Marathon Waterloo "this is a test".split( " " ) "the, argument, can, be, more, than, one, character, long".split( ", " ) "aaabbbcccbbbdddbbbeeebbbfffbbb".split( "bbb" ) ":Note the empty:first and last strings:".split( ":" ) " ".join( ["this", "is", "a", "test"] ) ", ".join( ["the", "target", "can", "be", "more", "than", "one", "character", "long"] ) "bbb".join( ["aaa", "ccc", "ddd", "eee", "fff", ""] ) ":".join( ":Note the empty:first and last strings:".split( ":" ) ) iNumberOfTranscripts = 1000 ostmExample = open( "transcripts.txt", "w" ) for i in xrange( iNumberOfTranscripts ): # Each gene has between 1 and 10 exons iNumberOfExons = random.randrange( 1, 11 ) astrExons = [] for j in xrange( iNumberOfExons ): # Each exon is between 100 and 1000 nucleotides long iNumberOfBases = random.randrange( 100, 1001 ) astrExons.append( randomNucleotides( iNumberOfBases ) ) ostmExample.write( ",".join( astrExons ) + "\n" ) istmTest = open( "test.txt", "r" ) for strLine in istmTest: print( strLine ) istmTest = open( "test.txt" ) for strLine in istmTest: print( strLine ) for strLine in open( "test.txt" ): print( strLine ) someFunctionThatReadsInput( open( "test.txt" ) ) for strLine in open( "test.txt" ): if strLine[1] == "a": sys.stdout.write( strLine ) for strLine in open( "test.txt" ): print( strLine.rstrip( ) ) "no whitespace, nothing happens".rstrip( ) "one newline gets nuked\n".rstrip( ) "many newlines, all gone\n\n\n\n".rstrip( ) "but so is everything else: \t \n ".rstrip( ) iGene = 0 for strLine in open( "transcripts.txt" ): iGene += 1 astrExons = strLine.rstrip( ).split( "," ) for iExon in xrange( len( astrExons ) ): if astrExons[iExon].find( "GATACAACG" ) >= 0: print( "Found: gene " + str(iGene) + ", exon " + str(iExon + 1) ) iWidth = 4 astrIDs = ["one", "two", "three"] astrSeqs = ["AACGTAATGCAATAA", "GCTTTGGC", "GCAGAATTAAT"] >one AACG TAAT GCAA TAA >two GCTT TGGC >three GCAG AATT AAT>one AACGTA ATGCAA TAA >two GCTTTG GC >three GCAGAA TTAAT def writeFasta( ostm, iWidth, astrIDs, astrSeqs ): pass writeFasta( sys.stdout, 60, [], [] ) [["one", "two", "three"], ["AACGTAATGCAATAA", "GCTTTGGC", "GCAGAATTAAT"]] def readFasta( istm ): return [] strMotif = "TAA" ["one", "three"] strAnswer = "" strAnswer = ""