Thursday, March 14, 2019

Python script to check if DNA sequence is palindrome or reverse palindrome

Check if a DNA sequence is palindrome or reverse palindrome:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Mar 12 04:18:04 2019

@author: milospjanic
"""
# Program to check if a string
#  is palindrome or not
def reverse(Pattern):
    revcomp = []
    x = len(Pattern)
    for i in Pattern:
        x = x - 1
        revcomp.append(Pattern[x])
    return ''.join(revcomp)


def compliment(Nucleotide):
    comp = []
    for i in Nucleotide:
        if i == "t":
            comp.append("a")
        if i == "a":
            comp.append("t")
        if i == "g":
            comp.append("c")
        if i == "c":
            comp.append("g")

    return ''.join(comp)


my_str = str(input("Enter a sequence: "))
# change this value for a different output
#my_str = 'aIbohPhoBiA'

# make it suitable for caseless comparison
my_str = my_str.casefold()

# reverse the string
comp = compliment (my_str)

rev = reverse (my_str)

revcomp = compliment (reverse (my_str))

print("Complement:",comp) 

print("Reversed:",rev) 

print("Reversed Complement:",revcomp) 


# check if the string is equal to its reverse

if list(my_str) == list(rev):
   print(my_str, "is palindrome")
else:
   if list(my_str) == list(revcomp):
       print(my_str, "is reverse palindrome")
   
   else:
       print(my_str, "is not a palindrome")
   

Python script for Fibonacci sequence using while loop or recursion

Python script for Fibonacci sequence using while loop or recursion.

Using recursive function:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Mar 12 05:32:45 2019

@author: milospjanic
"""

# Python program to display the Fibonacci sequence up to n-th term using recursive functions

def recur_fibo(n):
   """Recursive function to
   print Fibonacci sequence"""
   if n <= 1:
       return n
   else:
       return(recur_fibo(n-1) + recur_fibo(n-2))

# Change this value for a different result
#nterms = 10

# uncomment to take input from the user
nterms = int(input("How many terms? "))

# check if the number of terms is valid
if nterms <= 0:
   print("Plese enter a positive integer")
else:
   print("Fibonacci sequence:")
   for i in range(nterms):```
       print(recur_fibo(i), end=" , ")


Using while loop:


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Mar 12 05:22:28 2019

@author: milospjanic
"""

# Program to display the Fibonacci sequence up to n-th term where n is provided by the user

# change this value for a different result
#nterms = 10

# uncomment to take input from the user
nterms = int(input("How many terms? "))

# first two terms
n1 = 0
n2 = 1
count = 0

# check if the number of terms is valid
if nterms <= 0:
   print("Please enter a positive integer")
elif nterms == 1:
   print("Fibonacci sequence upto",nterms,":")
   print(n1)
else:
   print("Fibonacci sequence upto",nterms,":")
   while count < nterms:
       print(n1,end=" , ")
       n3 = n1 + n2
       # update values
       n1 = n2
       n2 = n3
       count += 1