def sequence_information( bases ): # created this function in order to return information about a sequence seq_info = Seq(bases) min_count = 0 most_frecuent_base = "" tc.cprint("Total length:", 'green', end=' ') print(seq_info.len()) for base, count in seq_info.count().items(): # .items() returns the base and the count for each base percentage = round(count / seq_info.len() * 100, 2) # round(,2) only shows to decimal numbers tc.cprint(f"{base}:", 'blue', end=' ') print(f" {count} ({percentage}%)") # here we are looking for the most frequent base if min_count < count: min_count = count most_frecuent_base = base # print the most frecuent base tc.cprint("Most frecuent Base:", 'green', end=' ') print(most_frecuent_base)
def sequence_information(sequence): # See Seq1.py for information about the class Seq seq_info = Seq(sequence) length_sequence = seq_info.len() bases_information = { } # This is a dictionary with the count a percentage for each base # Bases_information = [(A,817,25),(T,928,40)....] for base, count in seq_info.count().items(): # .items() returns the base and the count for each base percentage = round(count / seq_info.len() * 100, 2) keys = ['Count', 'Fraction'] base_count = count base_percentage = f'{percentage}%' dict_base = dict(zip(keys, [base_count, base_percentage ])) # Dictionary with information of a base dict_bases = { base: dict_base } # Dictionary with the information of all the bases bases_information.update(dict_bases) return length_sequence, bases_information
def info(sequence): seq = Seq(sequence) result = f"Total length: {seq.len()}<br><br>" for base, count in seq.count().items(): result += f"{base}: {count} ({seq.percentage_base(base)}%)<br><br>" context = {"sequence": seq, "operation": "info", "result": result} contents = read_template_html_file("./html/operation.html").render( context=context) return contents
def info(cs, seq): termcolor.cprint("INFO", "yellow") seq = Seq(seq) len_seq = Seq.len(seq) count_seq = Seq.count(seq) response = "Sequence: " + str(seq) + "\nTotal length: " + str(len_seq) + seq.percentage() print(response) cs.send(response.encode()) cs.close()
def info(cs, arg): print_colored("INFO", "yellow") seq_info = Seq(arg) lenght = len(arg) count_bases = "" for base, count in seq_info.count().items(): bases_info = str(base) + ": " + str(count) + " (" + str(round(count / lenght * 100, 1)) + "%)" + "\n" count_bases += bases_info response = ("Sequence: " + str(arg) + "\n" + "Total length: " + str(lenght) + "\n" + count_bases) print(response) cs.send(str(response).encode())
def info(cs, argument): seq_info = Seq(argument) count_bases_string = "" for base, count in seq_info.count().items(): s_base = str(base) + ": " + str(count) + " (" + str( round(count / seq_info.len() * 100, 2)) + "%)" + "\n" count_bases_string += s_base response = ("Sequence: " + "\n" + str(seq_info) + "\n" + "Total length: " + str(seq_info.len()) + "\n" + count_bases_string) print_colored(response, 'blue') cs.send(str(response).encode())
def sequence_information_html(sequence): seq_info = Seq(sequence) count_bases_string = "" for base, count in seq_info.count().items(): percentage = round(count / seq_info.len() * 100, 2) s_base = f'{base}: {count} ({percentage}%)</br>' count_bases_string += s_base reponse_info = ( f'<textarea readonly rows = "20" cols = "80">{seq_info}</textarea>' + " <br>" + "Total length: " + str(seq_info.len()) + "<br>" + count_bases_string) return reponse_info
def info_seq(in_msg): get = Seq(spl_in(in_msg)) f_msg = f'Sequence:{get}\n' len_seq = get.len() f_msg += f'Total length: {len_seq}\n' count_seq = get.count() for k, v in count_seq.items(): per = percentage(v, len_seq) cod = f'{k}: {v} ({per}%)\n' f_msg += cod response = f_msg print(response) return cs.send(str.encode(response))
def bases(cs, argument): print_color("INFO", "yellow") seq = Seq(argument) counting_bases = "" for base, number_of_bases in seq.count().items(): info = str(base) + ": " + str(number_of_bases) + " " + "(" + str( round(number_of_bases / len(argument) * 100, 1)) + "%" + ")" + "\n" counting_bases += info response = "Sequence: " + str(argument) + "\n" "Length: " + str( len(argument)) + "\n" + str(counting_bases) # print("Sequence: " + str(argument) + "\n" + "Total length: " + str(len(argument)) + "\n" + str(seq.count())) print(response) cs.send(str(response).encode())
def info(sequence): seq_info = Seq(sequence) lenght = len(sequence) count_bases = "" for base, count in seq_info.count().items(): bases_info = str(base) + ": " + str(count) + " (" + str(round(count / lenght * 100, 1)) + "%)" + "\n" count_bases += bases_info calculation = ("Total length: " + str(lenght) + "\n" + count_bases) context = { "operation_contents": sequence, "operation_name": "Info", "operation_result": calculation } contents = read_template_html_file("./html/operation.html").render(context=context) return contents
def seq_information(bases): seq_info = Seq(bases) # Use de Seq() class here min_count = 0 most_frecuent_base = "" for base, count in seq_info.count().items(): # .items() returns the base and the count for each base , then we print percentage = round(count / seq_info.len() * 100, 2) # round(,2) only shows to decimal numbers tc.cprint(f"{base}:", 'blue', end=' ') print(f" {count} ({percentage}%)") # here we look for the most frecuent base if min_count < count: min_count = count most_frecuent_base = base
def info(sequence): termcolor.cprint('INFO', 'green') s = Seq(sequence) info_dict = s.count() response =f"""Total length {len(sequence)} A: {info_dict['A'][0]} ({info_dict['A'][1]}) C: {info_dict['C'][0]} ({info_dict['C'][1]}) G: {info_dict['A'][0]} ({info_dict['G'][1]}) T: {info_dict['T'][0]} ({info_dict['T'][1]})""" context = { 'sequence': sequence, 'information': response, 'operation': 'info' } contents = read_template_html_file('./html/form-4.html').render(context=context) return contents
def sequence_information(bases): seq_info = Seq(bases) min = 0 max = ' ' termcolor.cprint('Total length :', 'green', end='') print(seq_info.len()) for base, count in seq_info.count().items(): percentage = round(count / seq_info.len() * 100, 2) termcolor.cprint(f"{base}", 'blue', end=' ') print(f"{count} ({percentage}%") if min < count: min = count max = base termcolor.cprint('Most frequent base', 'green', end=' ') print(max)
def information(bases): seq_info = Seq(bases) minimum = 0 freq_base = "" termcolor.cprint('Total length: ', 'green', end=' ') print(seq_info.len()) for base, counter in seq_info.count().items(): percentage = round(counter / seq_info.len() * 100, 1) termcolor.cprint(f"{base}:", 'blue', end=" ") print(f" {counter} ({percentage}%)") if minimum < counter: minimum = counter freq_base = base termcolor.cprint('Most frequent base: ', 'green', end=' ') print(freq_base)
def sequence_information(bases): seq_info = Seq(bases) min_count = 0 most_frecuent_base = "" tc.cprint("Total length:", 'green', end=' ') print(seq_info.len()) for base, count in seq_info.count().items(): # -- Returns the base and count from each .item() percentage = round(count / seq_info.len() * 100, 2) tc.cprint(f"{base}:", 'blue', end=' ') print(f" {count} ({percentage}%)") # -- Most frequent bases if min_count < count: min_count = count most_frecuent_base = base tc.cprint("Most frequent Base:", 'green', end=' ') print(most_frecuent_base)
def bases(sequence): print_color("INFO", "yellow") seq = Seq(sequence) counting_bases = "" for base, number_of_bases in seq.count().items(): info = str(base) + ": " + str(number_of_bases) + " " + "(" + str( round(number_of_bases / len(sequence) * 100, 1)) + "%" + ")" + "\n" counting_bases += info context = { "sequence": sequence, "operation": "Info", "result": "Total length: " + str(len(sequence)) + " " + "\n" + str(counting_bases) } contents = read_template_html_file("./html/operations.html").render( context=context) return contents
from Seq1 import Seq print('-----Exercise 8------') s1 = Seq('ACTGA') s2 = Seq() s3 = Seq('Invalid Sequence') bases = ['A', 'C', 'T', 'G'] print(f'Sequence 1 : Length : {s1.len()} {s1}') print('\tBases ;', s1.count()) print('\tRev :', s1.reverse()) print('\tComp:', s1.complement()) print(f'\nSequence 2 : Length : {s2.len()} {s2}') print('\tBases ;', s2.count()) print('\tRev :', s2.reverse()) print('\tComp:', s2.complement()) print(f'\nSequence 3 : Length : {s3.len()} {s3}') print('\tBases ;', s3.count()) print('\tRev :', s3.reverse()) print('\tComp:', s3.complement())
from Seq1 import Seq print("-----| Practice 1, Exercise 10 |------") folder = "../Session-04/" bases = ['A', 'C', 'T', 'G'] genes = ['U5', 'ADA', 'FRAT1', 'FXN', 'RNU6_269P'] for file in genes: s = Seq() sequence = s.read_fasta(folder + file + ".txt") dict_bases = s.count() min_value = 0 greater_number_base = '' for base, value in dict_bases.items(): while value > min_value: min_value = value greater_number_base = base print('Gene ', file, ': Most frequent Base: ', greater_number_base)
cc = s.count_base('C') pc = "{:.1f}".format(100 * cc / sl) cg = s.count_base('G') pg = "{:.1f}".format(100 * cg / sl) ct = s.count_base('T') pt = "{:.1f}".format(100 * ct / sl) termcolor.cprint("Total length", 'green', end="") print(f": {sl}") termcolor.cprint("A", 'blue', end="") print(f": {ca} ({pa}%)") termcolor.cprint("C", 'blue', end="") print(f": {cc} ({pc}%)") termcolor.cprint("G", 'blue', end="") print(f": {cg} ({pg}%)") termcolor.cprint("T", 'blue', end="") print(f": {ct} ({pt}%)") # Dictionary with the values d = s.count() # Create a list with all the values ll = list(d.values()) # Calculate the maximum m = max(ll) # Print the base termcolor.cprint("Most frequent Base", 'green', end="") print(f": {BASES[ll.index(m)]}")
except IndexError: service = msg # PING command if "PING" == service: response = "OK!\n" # Return response OK!! # GET command elif "GET" == service: response = seq[int(seq1)] elif "INFO" == service: seq_info = Seq(seq1) # seq1 is the sequence count_bases_string = "" for base, count in seq_info.count().items(): s_base = str(base) + ": " + str(count) + " (" + str( round(count / seq_info.len() * 100, 2)) + "%)" + "\n" count_bases_string += s_base response = ("Sequence: " + str(seq_info) + "\n" + "Total length: " + str(seq_info.len()) + "\n" + count_bases_string) elif "COMP" == service: complementary = Seq(seq1) response = complementary.complement() + "\n" elif "REV" == service: reverse = Seq(seq1) response = reverse.reverse() + "\n"
data1 = r1.read().decode() gene = json.loads(data1) termcolor.cprint(f"Gene: ", "yellow", end="") print(f"{namegene}") termcolor.cprint("Description: ", "yellow", end="") print(f"{gene['desc']}" ) #see esembl page for desc and seq (especial functions) sequence = gene['seq'] s = Seq(sequence) l = s.length() ac = s.count_base("A") tc = s.count_base("T") cc = s.count_base("C") gc = s.count_base("G") termcolor.cprint("Total length: ", "yellow", end="") print(l) resp = f""" A: {ac} ({round((ac / l) * 100)})% C: {cc} ({round((cc / l) * 100)})% T: {tc} ({round((tc / l) * 100)})% G: {gc} ({round((gc / l) * 100)})%""" print(resp) dictionary = s.count() listvalues = list(dictionary.values()) maxvalue = max(listvalues) termcolor.cprint("The most frequent base is: ", "yellow", end="") print(f"{BASES[listvalues.index(maxvalue)]}") #look for the max base
from Seq1 import Seq print('-----| Practice 1, Exercise 10 |------') FOLDER = "../Session_04/" GENES = ["U5", "ADA", "FRAT1", "FXN", "RNU6_269P"] EXT = ".txt" BASES = ['A', 'T', 'C', 'G'] for gene in GENES: sequence = Seq().read_fasta(FOLDER + gene + EXT) dictionary = sequence.count() lista = list(dictionary.values()) maximo = max(lista) print(f"Gene {gene}: Most frequent Base: {BASES[lista.index(maximo)]}")
from Seq1 import Seq BASES = ["A", "C", "T", "G"] FOLDER = "../Session-04/" FILENAME = "U5.txt" s = Seq() DNA_FILE = FOLDER + FILENAME s1 = Seq(s.read_fasta(DNA_FILE)) #!!!!!!! print(f"Sequence: (Length {s1.length()}){s1}") print(s1.count()) print(f"Reversed: {s1.seq_reverse()}") print(f"Complement: {s1.seq_complement()}")
from Seq1 import Seq # Import our Seq class print("-----| Practice 1, Exercise 9 |------") folder = "../Session-04/" filename = folder + 'U5.txt' s = Seq() # Create a Seq object s.read_fasta(filename) print(f"Sequence : (Length: {s.len()}) {s}") print("\tBases: ", s.count()) print("\tRev: ", s.reverse()) print("\tComp:", s.complement())
cc = seq.count_base('C') pc = "{:.1f}".format(100 * cc / sl) cg = seq.count_base('G') pg = "{:.1f}".format(100 * cg / sl) ct = seq.count_base('T') pt = "{:.1f}".format(100 * ct / sl) termcolor.cprint("Total lengh", 'green', end="") print(f": {sl}") termcolor.cprint("A", 'blue', end="") print(f": {ca} ({pa}%)") termcolor.cprint("C", 'blue', end="") print(f": {cc} ({pc}%)") termcolor.cprint("G", 'blue', end="") print(f": {cg} ({pg}%)") termcolor.cprint("T", 'blue', end="") print(f": {ct} ({pt}%)") # -- Dictionary with the values d = seq.count() # -- Create a list with all the values ll = list(d.values()) # -- Calculate the maximum m = max(ll) # -- Print the base termcolor.cprint("Most frequent Base", 'green', end="") print(f": {BASES[ll.index(m)]}")
sequence = console['id'] termcolor.cprint('GENE:', 'green') print(input_name) termcolor.cprint("Description:", 'green') print(console['desc']) termcolor.cprint('Bases:', 'green') print(console['seq']) sequence = Seq(console['seq']) termcolor.cprint('Total length:', 'green') print(sequence.len()) termcolor.cprint('A:', 'blue') print(sequence.count_base('A')) print('(', sequence.count_base('A') * 100 / sequence.len(), '%)') termcolor.cprint('C:', 'blue') print(sequence.count_base('C')) print('(', sequence.count_base('C') * 100 / sequence.len(), '%)') termcolor.cprint('G:', 'blue') print(sequence.count_base('G')) print('(', sequence.count_base('G') * 100 / sequence.len(), '%)') termcolor.cprint('T:', 'blue') print(sequence.count_base('T')) print('(', sequence.count_base('T') * 100 / sequence.len(), '%)') total_bases = list(sequence.count().values()) maximum = max(total_bases) termcolor.cprint("Most frequent base: ", 'green') print(bases[total_bases.index(maximum)])
# -- Create a variable with the data, # -- form the JSON received resp = json.loads(dat) termcolor.cprint('Gene', 'green', end='') print(f': {gene_n}') termcolor.cprint('Description', 'green', end='') print(f": {resp['desc']}") g_str = resp['seq'] s = Seq(g_str) str_len = s.len() Num = s.count() Per_A = 100 * int(Num['A']) / str_len Per_C = 100 * int(Num['C']) / str_len Per_T = 100 * int(Num['T']) / str_len Per_G = 100 * int(Num['G']) / str_len termcolor.cprint('Total Length', 'green', end='') print(f": {str_len}") termcolor.cprint('A', 'blue', end='') print(f': {Num["A"]} ({Per_A}%)') termcolor.cprint('C', 'blue', end='') print(f': {Num["C"]} ({Per_C}%)') termcolor.cprint('T', 'blue', end='')
from Seq1 import Seq s1 = Seq() s2 = Seq("ACTGTCCTG") s3 = Seq("Incorrect sequence") print(f"\nSequence {str(0)}: (Length: {str(s1.len())}) {str(s1)}") print(" Bases: ", s1.count()) print(" Rev: ", s1.reverse()) print(f"Sequence {str(1)}: (Length: {str(s2.len())}) {str(s2)}") print(" Bases: ", s2.count()) print(" Rev: ", s2.reverse()) print(f"Sequence {str(2)}: (Length: {str(s3.len())}) {str(s3)}") print(" Bases: ", s3.count()) print(" Rev: ", s3.reverse())
def do_GET(self): """This method is called whenever the client invokes the GET method in the HTTP protocol request""" # Print the request line termcolor.cprint(self.requestline, 'green') if self.path == "/": file = "form-4.html" contents = Path(file).read_text() error = 200 elif "/ping" in self.path: html = "<h1>PING OK!</h1><p>The SEQ2 server is running...</p>" contents = html_response( "PING", html, 'orange') # En title va PING y en body el resto error = 200 elif "/get" in self.path: seq_list = [ "TGTGAACATTCTGCACAGGTCTCTGGCTGCGCCTGGGCGGGTTTCTT", "CAGGAGGGGACTGTCTGTGTTCTCCCTCCCTCCGAGCTCCAGCCTTC", "CTCCCAGCTCCCTGGAGTCTCTCACGTAGAATGTCCTCTCCACCCC", "GAACTCCTGCAGGTTCTGCAGGCCACGGCTGGCCCCCCTCGAAAGT", "CTGCAGGGGGACGCTTGAAAGTTGCTGGAGGAGCCGGGGGGAA" ] sequence_number = int(argument_command(self.path)) sequence = seq_list[sequence_number] html = "<h1>Sequence number " + str( sequence_number) + "</h1> <p>" + sequence + "</p>" contents = html_response("GET", html, 'pink') error = 200 # -- Status line: OK! elif "/gene" in self.path: gene = argument_command(self.path) s = Seq() s.read_fasta("../Session-04/" + gene + ".txt") html = "<h1>Gene Sequence: " + gene + '</h1> <textarea readonly rows = "20" cols = "80">' + str( s) + '</textarea>' contents = html_response("GENE", html, 'yellow') error = 200 elif "/operation" in self.path: requests = self.path.split("&") sequence = argument_command(requests[0]) op = argument_command(requests[1]) seq = Seq(sequence) bases = ['A', 'C', 'T', 'G'] for b in sequence: if b in bases: if "info" == op: count_bases_string = "" for base, count in seq.count().items(): s_base = str(base) + ": " + str( count) + " (" + str( round(count / seq.len() * 100, 2)) + "%)" + "<br>" count_bases_string += s_base response_info = ("Sequence: " + str(seq) + " <br>" + "Total length: " + str(seq.len()) + "<br>" + count_bases_string) html_operation = "<h1>Operation:</h1><p>Information</p>" html_result = "<h1>Result:</h1>" + "<p>" + response_info + "</p>" color = 'lightblue' elif "comp" == op: response_comp = seq.complement() + "\n" html_operation = "<h1>Operation:</h1><p>Complementary sequence</p>" html_result = "<h1>Result:</h1>" + "<p>" + response_comp + "</p>" color = 'lightgrey' elif "rev" == op: response_rev = seq.reverse() + "\n" html_operation = "<h1>Operation:</h1><p>Reverse sequence</p>" html_result = "<h1>Result:</h1>" + "<p>" + response_rev + "</p>" color = 'lightgreen' html_sequence = "<h1>Sequence:</h1>" + "<p>" + sequence + "</p>" html = html_sequence + html_operation + html_result contents = html_response("OPERATION", html, color) error = 200 else: file = "Error-1.html" contents = Path(file).read_text() error = 404 else: file = "Error-1.html" contents = Path(file).read_text() error = 404 # -- Status line: ERROR NOT FOUND self.send_response(error) # Generating the response message # Define the content-type header: self.send_header('Content-Type', 'text/html') self.send_header('Content-Length', len(str.encode(contents))) # The header is finished self.end_headers() # Send the response message self.wfile.write(str.encode(contents)) return
from Seq1 import Seq # Null sequence s1 = Seq() # valid sequence s2 = Seq("ACTGA") # invalid sequence s3 = Seq("Invalid sequence") print("Sequence 1: (Length: " + str(Seq.len(s1)) + ") ", s1) print("\tBases:", Seq.count(s1)) print("\tRev: ", Seq.reverse(s1)) print("\tComp: ", Seq.complement(s1)) print("Sequence 2: (Length: " + str(Seq.len(s2)) + ") ", s2) print("\tBases:", Seq.count(s2)) print("\tRev: ", Seq.reverse(s2)) print("\tComp: ", Seq.complement(s2)) print("Sequence 3: (Length: " + str(Seq.len(s3)) + ") ", s3) print("\tBases:", Seq.count(s3)) print("\tRev: ", Seq.reverse(s3)) print("\tComp: ", Seq.complement(s3))