from Bio.SeqUtils import gc_fraction

from Bio.SeqUtils import MeltingTemp

from Bio.Seq import Seq

# Given DNA sequence
sequence = Seq("AAGCTATGGTGCTCTGTCCCATCACGCGGACCCGTGCACGCTGACGTTACTCCATAATCATGACGCTTACATTTCCGCAAGGCGCCCAATAGCCTTAGCTTGCCTCTGGACCCTACCTAAACTACTGCTGGGGAAATCCGAAAGGGCTGTAGGGGTGGGAGAACGCGTAGACGGTTCTCGTCTAGGTGCGGGTTATGAGTTCCGGGACGCCGGTGAAACCGGTAAGGGCTCCATGATATATAGGGAGGTGCAACTCTCTAAGACTCCACACCCACACTGGTAAAGAATTACCACTGCCCAATGGGCGGGCGGGAATTAGAACGAAGCATGCGTCCTTAATACGGGGAATGAATGATATGATGAACATAGGGCGTGTTACGTATACACACCCTATACCGAGTATTTGTTGCGCCTTCTCTTCAATGCACTGTCGTTGAACAGTTCCTATACTACGTGTGTGCCGAACCCACGATCCTCGTACCCCCTCCCCGGGACATCAG")
#sequence = Seq("AAGCTATGGT GCTCTGTCCC ATCACGCGGA CCCGTGCACG CTGACGTTAC TCCATAATCA TGACGCTTAC ATTTCCGCAA GGCGCCCAAT AGCCTTAGCT TGCCTCTGGA CCCTACCTAA ACTACTGCTG GGGAAATCCG AAAGGGCTGT AGGGGTGGGA GAACGCGTAG ACGGTTCTCG TCTAGGTGCG GGTTATGAGT TCCGGGACGC CGGTGAAACC GGTAAGGGCT CCATGATATA TAGGGAGGTG CAACTCTCTA AGACTCCACA CCCACACTGG TAAAGAATTA CCACTGCCCA ATGGGCGGGC GGGAATTAGA ACGAAGCATG CGTCCTTAAT ACGGGGAATG AATGATATGA TGAACATAGG GCGTGTTACG TATACACACC CTATACCGAG TATTTGTTGC GCCTTCTCTT CAATGCACTG TCGTTGAACA GTTCCTATAC TACGTGTGTG CCGAACCCAC GATCCTCGTA CCCCCTCCCC GGGACATCAG")
# 1. Calculate the frequency of each nucleotide base (A, T, C, G)
nucleotide_freq = {'A': sequence.count('A'), 'T': sequence.count('T'), 'C': sequence.count('C'), 'G': sequence.count('G')}
print("Frequency of each nucleotide base:", nucleotide_freq)

# 2. Give the number of nucleotides in the sequence
num_nucleotides = len(sequence)
print("Number of nucleotides:", num_nucleotides)

# 3. What is the length of the sequence?
sequence_length = len(sequence.replace(" ", ""))
print("Length of the sequence:", sequence_length)

# 4. Calculate the GC content of the DNA sequence file
gc_content = 100 * (sequence.count('G') + sequence.count('C')) / len(sequence)
gc_content1 = gc_fraction(sequence) * 100
print("GC content of the sequence:", gc_content)
print("GC content of the sequence:", gc_content1)

# 5. Give the reverse complement of the first line of the sequence
first_line = Seq("AAGCTATGGT")
reverse_complement = first_line.reverse_complement()
complement = first_line.complement()
print("Sequence of the first line:", first_line)
print("Complement of the first line:", complement)
print("Reverse complement of the first line:", reverse_complement)

# 6. Count each codon (3-nucleotide sequences) in the file
codon_count = {}
for i in range(0, len(sequence), 3):
    codon = sequence[i:i+3].replace(" ", "")
    codon_count[codon] = codon_count.get(codon, 0) + 1
print("Codon count:", codon_count)

# 7. What is the melting temperature (Tm) of the sequence?
#melting_temperature = Seq(sequence.replace(" ", "")).melting_temperature()
melting_temperature1 = MeltingTemp.Tm_Wallace(sequence)
melting_temperature2 = MeltingTemp.Tm_GC(sequence)
melting_temperature3 = MeltingTemp.Tm_NN(sequence)
print("Melting temperature of the sequence by using the Rule of thumb:", melting_temperature1)
print("Melting temperature of the sequence by using the GC melting temperature:", melting_temperature2)
print("Melting temperature of the sequence by using the Nearest-neighbor melting temperature:", melting_temperature3)

# 8. Create a text file containing a list of 5 DNA sequences in Python.
# Write a Python script to read the file and print the length of each sequence
with open("dna_sequences.txt", "w") as file:
    sequences = ["AAGCTATGGT", "GCTCTGTCCC", "ATCACGCGGA", "CCCACACTGG", "TAAAGAATTA"]
    for seq in sequences:
        file.write(seq + "\n")

# Read the file and print the length of each sequence
with open("dna_sequences.txt", "r") as file:
    for line in file:
        print("Length of sequence:", len(line.strip()))

 

Python Online Compiler

Write, Run & Share Python code online using OneCompiler's Python online compiler for free. It's one of the robust, feature-rich online compilers for python language, supporting both the versions which are Python 3 and Python 2.7. Getting started with the OneCompiler's Python editor is easy and fast. The editor shows sample boilerplate code when you choose language as Python or Python2 and start coding.

Taking inputs (stdin)

OneCompiler's python online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample python program which takes name as input and print your name with hello.

import sys
name = sys.stdin.readline()
print("Hello "+ name)

About Python

Python is a very popular general-purpose programming language which was created by Guido van Rossum, and released in 1991. It is very popular for web development and you can build almost anything like mobile apps, web apps, tools, data analytics, machine learning etc. It is designed to be simple and easy like english language. It's is highly productive and efficient making it a very popular language.

Tutorial & Syntax help

Loops

1. If-Else:

When ever you want to perform a set of operations based on a condition IF-ELSE is used.

if conditional-expression
    #code
elif conditional-expression
    #code
else:
    #code

Note:

Indentation is very important in Python, make sure the indentation is followed correctly

2. For:

For loop is used to iterate over arrays(list, tuple, set, dictionary) or strings.

Example:

mylist=("Iphone","Pixel","Samsung")
for i in mylist:
    print(i)

3. While:

While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.

while condition  
    #code 

Collections

There are four types of collections in Python.

1. List:

List is a collection which is ordered and can be changed. Lists are specified in square brackets.

Example:

mylist=["iPhone","Pixel","Samsung"]
print(mylist)

2. Tuple:

Tuple is a collection which is ordered and can not be changed. Tuples are specified in round brackets.

Example:

myTuple=("iPhone","Pixel","Samsung")
print(myTuple)

Below throws an error if you assign another value to tuple again.

myTuple=("iPhone","Pixel","Samsung")
print(myTuple)
myTuple[1]="onePlus"
print(myTuple)

3. Set:

Set is a collection which is unordered and unindexed. Sets are specified in curly brackets.

Example:

myset = {"iPhone","Pixel","Samsung"}
print(myset)

4. Dictionary:

Dictionary is a collection of key value pairs which is unordered, can be changed, and indexed. They are written in curly brackets with key - value pairs.

Example:

mydict = {
    "brand" :"iPhone",
    "model": "iPhone 11"
}
print(mydict)

Supported Libraries

Following are the libraries supported by OneCompiler's Python compiler

NameDescription
NumPyNumPy python library helps users to work on arrays with ease
SciPySciPy is a scientific computation library which depends on NumPy for convenient and fast N-dimensional array manipulation
SKLearn/Scikit-learnScikit-learn or Scikit-learn is the most useful library for machine learning in Python
PandasPandas is the most efficient Python library for data manipulation and analysis
DOcplexDOcplex is IBM Decision Optimization CPLEX Modeling for Python, is a library composed of Mathematical Programming Modeling and Constraint Programming Modeling