Advertisement
Azkiin

Python programs

Jun 17th, 2019
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.71 KB | None | 0 0
  1. # Build a Translator
  2.  
  3. def translate(phrase):
  4. translation = ""
  5. for letter in phrase:
  6. if letter in "AEIOUaeiou":
  7. if letter.isupper():
  8. translation = translation + "K"
  9. else:
  10. translation = translation + "k"
  11. else:
  12. translation = translation + letter
  13. return translation
  14.  
  15. print(translate(input("enter a phrase")))
  16.  
  17. # Guessing game
  18.  
  19. secret_word = "giraffe"
  20. guess = ""
  21. guess_count = 0
  22. guess_limit = 3
  23. out_of_guesses = False
  24.  
  25. while guess != secret_word and not (out_of_guesses):
  26. if guess_count < guess_limit:
  27. guess = input ("Enter guess:")
  28. guess_count += 1
  29. else:
  30. out_of_guesses = True
  31.  
  32. if out_of_guesses:
  33. print("You lose")
  34. else:
  35. print("You win")
  36.  
  37. # Caculator
  38.  
  39. num1 = float(input("Enter first number:"))
  40. op = input("Enter operator:")
  41. num2 = float(input("Enter second number:"))
  42.  
  43. if op == "+":
  44. print(num1 + num2)
  45. elif op == "-":
  46. print (num1 - num2)
  47. elif op == "/":
  48. print (num1 / num2)
  49. elif op == "*":
  50. print (num1 * num2)
  51. else:
  52. print ("Invalid operator")
  53.  
  54. # Lengte van de uitgeschreven input (voorbeeld primer sequentie)
  55.  
  56. Seq_primer = input("Enter primer sequence:")
  57. print("length nucleotide primer sequence is:", len(Seq_primer), "nucleotides long")
  58.  
  59. # code die nummers in succesie verteld en een eindantwoord heeft
  60.  
  61. Leeftijd = 0
  62. while Leeftijd <= 26:
  63. print(Leeftijd)
  64. Leeftijd += 1
  65. if Leeftijd == 27:
  66. print("Jeetje wat oud")
  67.  
  68. # DNA to RNA
  69.  
  70. def DNA_to_RNA(DNA):
  71. DNA = DNA.upper()
  72. RNA = DNA.replace("T","U")
  73. return RNA
  74.  
  75. print(DNA_to_RNA(input("DNA sequence:")))
  76.  
  77. # RNA to DNA
  78.  
  79. def RNA_to_DNA(RNA):
  80. RNA = RNA.upper()
  81. DNA = RNA.replace("U","T")
  82. return DNA
  83.  
  84. print(RNA_to_DNA(input("RNA sequence:")))
  85.  
  86. # Reverse complement DNA
  87.  
  88. DNA = input("Please enter your DNA sequence")
  89.  
  90. DNA_upper = DNA.upper()
  91. DNA_upper_reversed = DNA_upper[::-1]
  92. for i in DNA_upper_reversed:
  93. if(i == "A"):print ("T", end ="")
  94. elif (i == "T"):print ("A", end ="")
  95. elif (i == "C"):print("G", end ="")
  96. elif (i == "G"):print("C", end ="")
  97.  
  98. # Nucleotide counter
  99.  
  100. DNA = input("Enter DNA sequence:")
  101.  
  102. Upper_DNA = DNA.upper()
  103. A = Upper_DNA.count("A")
  104. T = Upper_DNA.count("T")
  105. C = Upper_DNA.count("C")
  106. G = Upper_DNA.count("G")
  107. print("The number of A in your DNA sequence is:", A)
  108. print("The number of T in your DNA sequence is:", T)
  109. print("The number of C in your DNA sequence is:", C)
  110. print("The number of G in your DNA sequence is:", G)
  111. print("The GC ratio of your DNA sequence is:",(G+C)/(G+C+T+A)*100, "procent!")
  112.  
  113. # Digestion enzym code
  114.  
  115. DNA0 = input("Enter DNA sequence:")
  116. seq = DNA0.upper()
  117. enz0 = input("Enter the enzyme name:")
  118. enz = enz0.upper()
  119.  
  120. y= seq[0]
  121. y_minus = 0
  122.  
  123. restriction_site = ""
  124. restriction_site = 0
  125. fragment = ""
  126. enz = ""
  127.  
  128. def digestion (seq,enz):
  129. if (enz == "XBAI"): recognition_site = "TCTAGA"
  130. elif (enz == "ECORI"): recognition_site = "GAATTC"
  131. else:
  132. print("Enzym niet herkend")
  133. fragment = (seq.split(recognition_site))
  134. print("Upon digesting your DNA with "+ enz +", the following DNA fragments")
  135. for No_fr, i in enumerate(fragment):
  136. print(No_fr,i, end= "\n")
  137. str_regDNA0 = "(_^_)".join(map(str,fragment))
  138. str_regDNA1 = str(str_regDNA0[0:-1])
  139. print("\nThe map of your DNA sequence after this digestion would look like this")
  140. return(str_regDNA1)
  141.  
  142. digestion(DNA0, enz0)
  143.  
  144. # Module DNA naar AMINOZUUR
  145. userDNA= str(input("Enter your DNA sequence:"))
  146. Codon_triplet_data = ([userDNA[start : start+3]for start in range(0,len(userDNA),3)])
  147.  
  148. AA_dictionary = {
  149. 'ATA': 'I', 'ATC': 'I', 'ATT': 'I', 'ATG': 'M',
  150. 'ACA': 'T', 'ACC': 'T', 'ACG': 'T', 'ACT': 'T',
  151. 'AAC': 'N', 'AAT': 'N', 'AAA': 'K', 'AAG': 'K',
  152. 'AGC': 'S', 'AGT': 'S', 'AGA': 'R', 'AGG': 'R',
  153. 'CTA': 'L', 'CTC': 'L', 'CTG': 'L', 'CTT': 'L',
  154. 'CCA': 'P', 'CCC': 'P', 'CCG': 'P', 'CCT': 'P',
  155. 'CAC': 'H', 'CAT': 'H', 'CAA': 'Q', 'CAG': 'Q',
  156. 'CGA': 'R', 'CGC': 'R', 'CGG': 'R', 'CGT': 'R',
  157. 'GTA': 'V', 'GTC': 'V', 'GTG': 'V', 'GTT': 'V',
  158. 'GCA': 'A', 'GCC': 'A', 'GCG': 'A', 'GCT': 'A',
  159. 'GAC': 'D', 'GAT': 'D', 'GAA': 'E', 'GAG': 'E',
  160. 'GGA': 'G', 'GGC': 'G', 'GGG': 'G', 'GGT': 'G',
  161. 'TCA': 'S', 'TCC': 'S', 'TCG': 'S', 'TCT': 'S',
  162. 'TTC': 'F', 'TTT': 'F', 'TTA': 'L', 'TTG': 'L',
  163. 'TAC': 'Y', 'TAT': 'Y', 'TAA': '*', 'TAG': '*',
  164. 'TGC': 'C', 'TGT': 'C', 'TGA': '*', 'TGG': 'W', }
  165.  
  166. for codon in Codon_triplet_data:
  167. for item in AA_dictionary:
  168. if codon == item:
  169. print (AA_dictionary[item],end = "")
  170.  
  171.  
  172. # Working with strings
  173. phrase = "Giraffe Academy"
  174. print(phrase.lower())
  175. print(phrase + " is cool")
  176. print (phrase.isupper())
  177. print(phrase.upper().isupper())
  178. print(len(phrase))
  179. print(phrase[0])
  180. print(phrase.index("Acad"))
  181. print(phrase.replace("Giraffe", "Elephant"))
  182.  
  183. #Working with numbers
  184. print(3 + 4.5)
  185. print(3*(4+5))
  186. print(10 % 3)
  187.  
  188. from math import *
  189. my_num = -4
  190. print(my_num)
  191. print(max(4,6))
  192. print(min(4,6))
  193. print(round(5.768))
  194. print(floor(4.7))
  195. print(ceil(3.2))
  196. print(sqrt(36))
  197.  
  198. #If statements
  199.  
  200. Is_male = True
  201. Is_tall = False
  202.  
  203. if Is_male and Is_tall:
  204. print("you are a tall male")
  205. elif Is_male and not Is_tall:
  206. print("you are a short male")
  207. elif not Is_male and Is_tall:
  208. print("you are not a male but are tall")
  209. else:
  210. print("you are either not male and not tall")
  211.  
  212. # Dictionary
  213.  
  214. Monthconversions = {
  215. "Jan":"January",
  216. "Feb":"February",
  217. "Mar": "March",
  218. }
  219.  
  220. print(Monthconversions["Feb"])
  221.  
  222. #For loop
  223.  
  224. friends = ["jim","KAREN","kevin"]
  225.  
  226. for friend in friends:
  227. if friend.isupper():
  228. print ("Big Friend")
  229. else:
  230. print ("Small friend")
  231.  
  232. for index in range(5): # range means numbers
  233. if index == 0:
  234. print ("first")
  235. elif index == 1:
  236. print ("second")
  237. elif index == 2:
  238. print ("third")
  239. elif index == 3:
  240. print ("fourth")
  241. else:
  242. print ("last")
  243.  
  244. #Exponent Function
  245.  
  246. def raise_to_power(base_num, pow_num):
  247. result = 1
  248. for index in range(pow_num):
  249. result = result * base_num
  250. return result
  251.  
  252. print(raise_to_power(3,6))
  253.  
  254. # Biopython modules Chapter 3
  255.  
  256. # GC Counting with Biopython
  257. from Bio.Seq import Seq
  258. from Bio.Alphabet import IUPAC
  259. from Bio.SeqUtils import GC
  260. my_seq = Seq(input("ENTER DNA SEQUENCE:"), IUPAC.unambiguous_dna)
  261. print(GC(my_seq))
  262.  
  263. # counting elements in Sequence
  264.  
  265. from Bio.Seq import Seq
  266. from Bio.Alphabet import IUPAC
  267. my_seq = Seq(input("ENTER DNA SEQUENCE"), IUPAC.unambiguous_dna)
  268. for index, letter in enumerate(my_seq):
  269. print("%i %s" % (index, letter))
  270. print(len(my_seq)) # print de totale lengte van de sequentie
  271. print(my_seq[0]) # print een exacte aangegeven element uit
  272.  
  273. # Sequence slicing
  274.  
  275. from Bio.Seq import Seq
  276. from Bio.Alphabet import IUPAC
  277. my_seq = Seq("GATCGATGGGCCTATATAGGATCGAAAATCGC", IUPAC.unambiguous_dna)
  278. print(my_seq[2:12])
  279.  
  280. # Adding sequences
  281.  
  282. from Bio.Seq import Seq
  283. from Bio.Alphabet import generic_nucleotide
  284. from Bio.Alphabet import IUPAC
  285. nuc_seq = Seq(input("Enter first sequence"), generic_nucleotide)
  286. dna_seq = Seq(input("Enter second sequence"), IUPAC.unambiguous_dna)
  287. print(nuc_seq + dna_seq)
  288.  
  289. from Bio.Seq import Seq
  290. from Bio.Alphabet import generic_dna
  291. list_of_seqs = [Seq("ACGT", generic_dna), Seq("AACC", generic_dna), Seq("GGTT", generic_dna)]
  292. sum(list_of_seqs, Seq("", generic_dna))
  293.  
  294. # changing case
  295.  
  296. from Bio.Seq import Seq
  297. from Bio.Alphabet import generic_dna
  298. dna_seq = Seq("acgtACGT", generic_dna)
  299. print(dna_seq.upper())
  300. print(dna_seq.lower())
  301.  
  302. # Sequences in (reverse) complements
  303.  
  304. from Bio.Seq import Seq
  305. from Bio.Alphabet import IUPAC
  306. my_seq = Seq("GAAAAAC", IUPAC.unambiguous_dna)
  307. print(my_seq.complement())
  308. print(my_seq.reverse_complement())
  309.  
  310. # TRANSCRIPTION
  311.  
  312. # DNA TO mRNA
  313.  
  314. from Bio.Seq import Seq
  315. from Bio.Alphabet import IUPAC
  316. coding_dna = Seq("", IUPAC.unambiguous_dna)
  317. template_dna = coding_dna.reverse_complement()
  318. print(template_dna.reverse_complement().transcribe())
  319.  
  320. # mRNA to DNA
  321.  
  322. from Bio.Seq import Seq
  323. from Bio.Alphabet import IUPAC
  324. messenger_rna = Seq("AUGGCCAUUGUAAUGGGCCGCUGAAAGGGUGCCCGAUAG", IUPAC.unambiguous_rna)
  325. print(messenger_rna.back_transcribe())
  326.  
  327. # TRANSLATION
  328.  
  329. # mRNA to Protein
  330.  
  331. from Bio.Seq import Seq
  332. from Bio.Alphabet import IUPAC
  333. messenger_rna = Seq("AUGGCCAUUGUAAUGGGCCGCUGAAAGGGUGCCCGAUAG", IUPAC.unambiguous_rna)
  334. print(messenger_rna.translate())
  335.  
  336. # DNA to protein
  337.  
  338. from Bio.Seq import Seq
  339. from Bio.Alphabet import IUPAC
  340. coding_dna = Seq("ATGGCCATTGTAATGGGCCGCTGAAAGGGTGCCCGATAG", IUPAC.unambiguous_dna)
  341. print(coding_dna.translate())
  342.  
  343. # Gene translation
  344.  
  345. from Bio.Seq import Seq
  346. from Bio.Alphabet import generic_dna
  347. gene = Seq("GTGAAAAAGATGCAATCTATCGTACTCGCACTTTCCCTGGTTCTGGTCGCTCCCATGGCA" + \
  348. "GCACAGGCTGCGGAAATTACGTTAGTCCCGTCAGTAAAATTACAGATAGGCGATCGTGAT" + \
  349. "AATCGTGGCTATTACTGGGATGGAGGTCACTGGCGCGACCACGGCTGGTGGAAACAACAT" + \
  350. "TATGAATGGCGAGGCAATCGCTGGCACCTACACGGACCGCCGCCACCGCCGCGCCACCAT" + \
  351. "AAGAAAGCTCCTCATGATCATCACGGCGGTCATGGTCCAGGCAAACATCACCGCTAA", generic_dna)
  352. print(gene.translate(table="Bacterial"))
  353.  
  354. # Translation tables
  355.  
  356. from Bio.Data import CodonTable
  357. standard_table = CodonTable.unambiguous_dna_by_name["Standard"]
  358. mito_table = CodonTable.unambiguous_dna_by_name["Vertebrate Mitochondrial"]
  359. print(standard_table)
  360. print(standard_table.start_codons)
  361.  
  362. # Comparing objects
  363.  
  364. from Bio.Seq import Seq
  365. from Bio.Alphabet import IUPAC
  366. seq1 = Seq(input("Enter DNA sequence 1"), IUPAC.unambiguous_dna)
  367. seq2 = Seq(input("Enter DNA sequence 2"), IUPAC.ambiguous_dna)
  368. print(str(seq1) == str(seq2))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement