def info_seq(arg): seq = Seq(arg) length = seq.len() count_a = seq.count_base("A") a_percent = (count_a * 100) / length count_c = seq.count_base("C") c_percent = (count_c * 100) / length count_g = seq.count_base("G") g_percent = (count_g * 100) / length count_t = seq.count_base("T") t_percent = (count_t * 100) / length res = f"""Sequence: {seq} \n Total length: {length} \n A: {round(count_a, 2)} ({a_percent}) \n C: {round(count_c, 2)} ({c_percent}) \n G: {round(count_g, 2)} ({g_percent}) \n T: {round(count_t, 2)} ({t_percent})""" return res
def info_func(s): s = Seq(s) l = s.length() ac = s.count_base("A") tc = s.count_base("T") cc = s.count_base("C") gc = s.count_base("G") resp = f"""Sequence: {s} Total length: {l} A: {ac} ({round((ac/l)*100)})% C: {cc} ({round((cc/l)*100)})% T: {tc} ({round((tc/l)*100)})% G: {gc} ({round((gc/l)*100)})%""" return resp
def INFO(seq): seq = Seq(seq) lenS = seq.len() A_COUNTER = seq.count_base('A') G_COUNTER = seq.count_base('G') C_COUNTER = seq.count_base('C') T_COUNTER = seq.count_base('T') A_PER = 100 * A_COUNTER / lenS G_PER = 100 * G_COUNTER / lenS C_PER = 100 * C_COUNTER / lenS T_PER = 100 * T_COUNTER / lenS RES = f"Sequence: {seq}\nTotal length: {lenS}\nA: {A_COUNTER} ({A_PER}%)\nC: {C_COUNTER} ({C_PER}%)\nG: {G_COUNTER} ({G_PER}%)\nT: {T_COUNTER} ({T_PER}%)\n" return RES
def INFO(SEQ): # First convert the string into an object Seq, the using th class functions calculate the info with count_base and # len s = Seq(SEQ) length = s.len() counter_A = s.count_base('A') perc_A = (100 * counter_A / length) counter_C = s.count_base('C') perc_C = (100 * counter_C / length) counter_G = s.count_base('G') perc_G = (100 * counter_G / length) counter_T = s.count_base('T') perc_T = (100 * counter_T / length) INFO = f"Sequence: {s},\n,Total length: {length},A: {counter_A} ({perc_A}%),\n,C: {counter_C} ({perc_C}%),\n,G: {counter_G} ({perc_G}%),\n,T: {counter_T} ({perc_T}%)" return INFO
def info_command(seq): real_seq = Seq(seq) length = real_seq.len() count_A = real_seq.count_base("A") count_C = real_seq.count_base("C") count_G = real_seq.count_base("G") count_T = real_seq.count_base("T") per_A = 100 * (count_A / length) per_C = 100 * (count_C / length) per_G = 100 * (count_G / length) per_T = 100 * (count_T / length) response_info = f"""Total length {real_seq}: {length} A : {count_A} ({per_A} %) C : {count_C} ({per_C} %) G : {count_G} ({per_G} %) T : {count_T} ({per_T} %)""" return response_info
def info(seq): s = Seq(seq) l = Seq.len(s) a = [Seq.count_base(s, "A"), Seq.count_base(s, "A") * 100 / l] c = [Seq.count_base(s, "C"), Seq.count_base(s, "C") * 100 / l] g = [Seq.count_base(s, "G"), Seq.count_base(s, "G") * 100 / l] t = [Seq.count_base(s, "T"), Seq.count_base(s, "T") * 100 / l] return "Sequence: " + str(s) + "\nTotal length: " + str(l) + "\nA: " + str( a[0]) + " (" + str(round(a[1], 1)) + "%)" + "\nC: " + str( c[0]) + " (" + str(round(c[1], 1)) + "%)" + "\nG: " + str( g[0]) + " (" + str(round(g[1], 1)) + "%)" + "\nT: " + str( t[0]) + " (" + str(round(t[1], 1)) + "%)"
def info_cmd(strseq): s = Seq(strseq) sl = s.len() ca = s.count_base('A') pa = "{:.1f}".format(100 * ca / sl) 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) resp = f"""Sequence: {s} Total length: {sl} A: {ca} ({pa}%) C: {cc} ({pc}%) G: {cg} ({pg}%) T: {ct} ({pt}%)""" return resp
def info_cmd(strseq): s = Seq(strseq) slen = s.len() bases = ['A', 'C', 'G', 'T'] response = f"Sequence: {s}\n" response += f"Total length: {slen}\n" for element in bases: count = s.count_base(element) percentage = round(100 * count / slen, 1) response += f"{element}: {count} ({percentage}%) \n" return response
def info_cmd(strseq): # -- Create the object sequence from the string s = Seq(strseq) sl = s.len() ca = s.count_base('A') sa = "{:.1f}".format(100 * ca / sl) cc = s.count_base('C') sc = "{:.1f}".format(100 * cc / sl) cg = s.count_base('G') sg = "{:.1f}".format(100 * cg / sl) ct = s.count_base('T') st = "{:.1f}".format(100 * ct / sl) resp = f"""Sequence: {s} Total length: {sl} A: {ca} ({sa}%) C: {cc} ({sc}%) G: {cg} ({sg}%) T: {ct} ({st}%)""" return resp
def info(seq): s = Seq(seq) result = "Sequence: " + str(s) length = Seq.len(s) result += "\nTotal length: " + str(length) a = [Seq.count_base(s, "A"), Seq.count_base(s, "A") * 100 / length] result += "\nA: " + str(a[0]) + " (" + str(round(a[1], 1)) + "%)" c = [Seq.count_base(s, "C"), Seq.count_base(s, "C") * 100 / length] result += "\nC: " + str(c[0]) + " (" + str(round(c[1], 1)) + "%)" g = [Seq.count_base(s, "G"), Seq.count_base(s, "G") * 100 / length] result += "\nG: " + str(g[0]) + " (" + str(round(g[1], 1)) + "%)" t = [Seq.count_base(s, "T"), Seq.count_base(s, "T") * 100 / length] result += "\nT: " + str(t[0]) + " (" + str(round(t[1], 1)) + "%)" context = {"seq": s, "op": "Info", "res": result} content = read_template("./html/operation.html").render(context=context) return content
def info_cmd(strseq): """INFO seq returns: The string with the information """ # -- Create the object sequence from the string s = Seq(strseq) sl = s.len() ca = s.count_base('A') pa = "{:.1f}".format(100 * ca / sl) 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) resp = f"""Sequence: {s} Total length: {sl} A: {ca} ({pa}%) C: {cc} ({pc}%) G: {cg} ({pg}%) T: {ct} ({pt}%)""" return resp
def info(sequence): useful_seq = Seq(sequence) complete_nuc_info = "" seq_len = f"Total length: {len(sequence)}\n" complete_nuc_info += seq_len A, C, T, G = useful_seq.count_base() context = {"operation": "INFO", "sequence": sequence} nucleotides_list = [A, C, T, G] nucleotides_names = ["A", "C", "T", "G"] for i in range(0, 4): nuc_info = f"{nucleotides_names[i]}: {nucleotides_list[i]} ({(nucleotides_list[i] * 100) / len(sequence)}%)\n" complete_nuc_info += nuc_info context["sequence_modified"] = complete_nuc_info contents = read_template_html_file("./HTML_FILES/operation.html").render( context=context) return contents
def info(client_socket, sequence): print(Fore.GREEN + "INFO") complete_nuc_info = "" useful_seq = Seq(sequence) A, C, T, G = useful_seq.count_base() seq_info = f"Sequence: {sequence}\n" complete_nuc_info += seq_info seq_len = f"Total length: {len(sequence)}\n" complete_nuc_info += seq_len nucleotides_list = [A, C, T, G] nucleotides_names = ["A", "C", "T", "G"] for i in range(0, 4): nuc_info = f"{nucleotides_names[i]}: {nucleotides_list[i]} ({(nucleotides_list[i] * 100) / len(sequence)}%)\n" complete_nuc_info += nuc_info print(complete_nuc_info) client_socket.send(complete_nuc_info.encode())
def info(sequence): list_bases = ["A", "C", "T", "G"] sequence = Seq(sequence.replace('"', "")) t_l = Seq.len(sequence) count_list = [] percentage_list = [] for base in list_bases: count_list.append(sequence.count_base(base)) for i in range(0, len(count_list)): percentage_list.append(count_list[i] * 100 / t_l) result = { "length": t_l, "bases": { "A": str(count_list[0]) + " (" + str(percentage_list[0]) + "%)", "C": str(count_list[1]) + " (" + str(percentage_list[1]) + "%)", "T": str(count_list[2]) + " (" + str(percentage_list[2]) + "%)", "G": str(count_list[3]) + " (" + str(percentage_list[3]) + "%)" } } context = {'Sequence': sequence, 'Operation': 'Info', 'Result': result} contents = read_template_html_file('./html/operation.html').render( context=context) return contents
# Create a variable with the data, # form the JSON received gene = json.loads(data1) termcolor.cprint("Gene", 'green', end="") print(f": {gene_user}") termcolor.cprint("Description", 'green', end="") print(f": {gene['desc']}") gene_str = gene['seq'] # Create the object sequence from the string s = Seq(gene_str) sl = s.len() ca = s.count_base('A') pa = "{:.1f}".format(100 * ca / sl) 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}%)")
def do_GET(self): termcolor.cprint(self.requestline, 'green') req_line = self.requestline.split()[1] verb = req_line.split("?")[0] # index if verb == "/": contents = Path('index.html').read_text() self.send_response(200) # 1) List of species in the genome database elif verb == "/listSpecies": limit = req_line.split("=")[1] try: info = info_json("info/species?")["species"] # here we get the all the species contents = document("LIST OF SPECIES IN THE BROWSER", "plum") # we compute the html response contents += f"""<h4>The total number of species in ensembl is: 286</h4><p>""" contents += f"""<h4>The limit you have selected is: {limit}</h4><p>""" if limit == "": # if the client does not enter anything contents += f"""<h4>The names of the species are:</h4>""" for element in info: contents += f"""<p> • {element["display_name"]}</p>""" # get the species name self.send_response(200) elif 286 >= int(limit) >= 0: # if the client enter a valid limit counter = 0 contents += f"""<h4>The names of the species are:</h4>""" for element in info: if counter < int(limit): # get the correct number of species for the limit contents += f"""<p> • {element["display_name"]}</p>""" # we just ger the specie's name counter += 1 self.send_response(200) else: # if the client enter a value out of range contents = Path('Error.html').read_text() self.send_response(404) except ValueError: # if the client enter a word contents = Path('Error.html').read_text() self.send_response(404) contents += f"""<a href="/">Main page</a></body></html>""" # 2) Information about the karyotype elif verb == "/karyotype": specie = req_line.split("=")[1] list_species = info_json("info/species?")["species"] # we get all the species contents = "" for dictionary in list_species: if specie in dictionary.values(): # check if the specie entered by the client is valid info = info_json("info/assembly/" + specie + "?")["karyotype"] # get the karyotype of the species contents = document("KARYOTYPE OF A SPECIFIC SPECIES", "plum") # we compute the html response contents += f"""<h4> The names of the {specie}'s chromosomes are: </h4>""" for element in info: contents += f"""<p> • {element}</p>""" self.send_response(200) break else: # if the species does not exist contents = Path('Error.html').read_text() self.send_response(404) contents += f"""<a href="/">Main page</a></body></html>""" # 3) Chromosome Length elif verb == "/chromosomeLength": number = req_line.split("=")[2] values = req_line.split("=")[1] specie = values.split("&")[0] list_species = info_json("info/species?")["species"] # we get all the species contents = "" for dictionary in list_species: if specie in dictionary.values(): # check if the specie entered by the client is valid info = info_json("info/assembly/" + specie + "?")["top_level_region"] # get all the lengths for element in info: if element["name"] == number: # check if the chromosome is valid # we compute the html response contents = document("LENGTH OF A SPECIFIC CHROMOSOME", "plum") contents += f"""<h4> The length of the chromosomes {number} is: </h4>""" contents += f"""<p> {element["length"]} </p>""" self.send_response(200) break else: # if the chromosome does not exist contents = Path('Error.html').read_text() self.send_response(404) break else: # if the chromosome does not exist contents = Path('Error.html').read_text() self.send_response(404) contents += f"""<a href="/">Main page</a></body></html>""" # 4) Sequence of a human gene elif verb == "/geneSeq": value = req_line.split("=")[1] if value == "": # if the client does not enter anything contents = Path('Error.html').read_text() self.send_response(404) else: try: # if the value is a number it prints an error value = int(value) contents = Path('Error.html').read_text() self.send_response(404) except ValueError: try: # if the value is not a number gene_id = info_json(f"/xrefs/symbol/homo_sapiens/{value}?")[0]["id"] # get the id seq = info_json(f"/sequence/id/{gene_id}?") # get all the information about the gene contents = document("GENE SEQUENCE", "plum") # we compute the html response contents += f'<p> The sequence of gene {value} is: </p>' # get the sequence of the id contents += f'<textarea rows = "30" cols = "150"> {seq["seq"]} </textarea>' self.send_response(200) except IndexError: # if the gene does not exist contents = Path('Error.html').read_text() self.send_response(404) contents += f"""<a href="/">Main page</a></body></html>""" # 5) Information about a human gene elif verb == "/geneInfo": value = req_line.split("=")[1] if value == "": # if the client does not enter anything contents = Path('Error.html').read_text() self.send_response(404) else: try: # if the value is not a number value = int(value) contents = Path('Error.html').read_text() self.send_response(404) except ValueError: try: gene_id = info_json(f"/xrefs/symbol/homo_sapiens/{value}?")[0]["id"] # get the id seq_info = info_json(f"/lookup/id/{gene_id}?") # get all the information about the gene contents = document("SEQUENCE INFORMATION", "plum") # we compute the html response contents += f'<h4> The sequence of gene {value} is: </h4>' contents += f'<p> The start point is: {seq_info["start"]} </p>' contents += f'<p> The end point is: {seq_info["end"]} </p>' contents += f'<p> The length is: {seq_info["end"] - seq_info["start"]} </p>' contents += f'<p> The id is: {seq_info["id"]} </p>' contents += f'<p> The chromose is: {seq_info["seq_region_name"]} </p>' self.send_response(200) except IndexError: # if the gene does not exist contents = Path('Error.html').read_text() self.send_response(404) contents += f"""<a href="/">Main page</a></body></html>""" # 6) Total length and % of bases os a human gene elif verb == "/geneCalc": value = req_line.split("=")[1] if value == "": # if the client does not enter anything contents = Path('Error.html').read_text() self.send_response(404) else: try: # if the value is not a number value = int(value) contents = Path('Error.html').read_text() self.send_response(404) except ValueError: try: gene_id = info_json(f"/xrefs/symbol/homo_sapiens/{value}?")[0]["id"] # get the id sequence = info_json(f"/sequence/id/{gene_id}?")["seq"] # get the sequence of the gene seq = Seq(sequence) contents = document("SEQUENCE CALCULATIONS", "plum") # we compute the html response contents += f'<h4> Calculations over the introduced gene {value}: </h4>' contents += f'<p> Total length of this gene is: {seq.len()}</p>' # calculate the length contents += f'<p> Percentage of the bases is: </p>' for base in bases: # calculate the count and the percentage od the sequence count = seq.count_base(base) percentage = round(seq.count_base(base) * (100 / seq.len()), 2) contents += f'<p> {base}: {count} ({percentage}%) </p>' self.send_response(200) except IndexError: # if the gene does not exist contents = Path('Error.html').read_text() self.send_response(404) contents += f"""<a href="/">Main page</a></body></html>""" # 7) List of genes located in a chromosome elif verb == "/geneList": value = req_line.split("?")[1] chromo_number, start_number, end_number = value.split("&") chromo = chromo_number.split("=")[1] start = start_number.split("=")[1] end = end_number.split("=")[1] if chromo == "" or start == "" or end == "": # if the client does not enter anything contents = Path('Error.html').read_text() self.send_response(404) else: try: start = int(start) # check if the start point is a number end = int(end) # check if the start point is a number # check if the chromosome is human if chromo in ["x", "X", "y", "Y", "MT"] or 1 <= int(chromo) <= 22: # get the genes inside that positions genes = info_json(f"/overlap/region/human/{chromo}:{start}-{end}?feature=gene;") try: contents = document("GENE LIST", "plum") # we compute the html response contents += f"<h4>Genes located in the chromosome {chromo} from {start} to {end} positions:</h4>" for list_gene in genes: contents += f'<p> - {list_gene["external_name"]}</p>' self.send_response(200) except TypeError: # if the start or end points are no valid contents = Path('Error.html').read_text() self.send_response(404) else: # if the chromosome is not one of the human contents = Path('Error.html').read_text() self.send_response(404) except ValueError: # if something entered by the client is not a number contents = Path('Error.html').read_text() self.send_response(404) contents += f"""<a href="/">Main page</a></body></html>""" else: contents = Path('Error.html').read_text() self.send_response(404) self.send_header('Content-Type', 'text/html') self.send_header('Content-Length', len(str.encode(contents))) self.end_headers() self.wfile.write(str.encode(contents)) return
r1 = conn.getresponse() print(f"Response received!: {r1.status} {r1.reason}\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)
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') #green request line req_line = self.requestline.split( ' ') #splits the request line (by the spaces) arguments = (req_line[1]).split( "?" ) #We get the first request line and then the path, goes after /. We get the arguments that go after the ? first_argument = arguments[0] #sets the first argument contents = Path('Error.html').read_text() #no argument --> error form self.send_response(404) #--------------------------------------------MAIN PROGRAM-------------------------------------------- try: # --------------------------------------------only / -------------------------------------------- if first_argument == "/": #return an HTML page with the forms for accessing to all the previous services contents = Path('index.html').read_text( ) #contents displayed in index.html self.send_response(200) #--------------------------------------------listSpecies-------------------------------------------- elif first_argument == '/listSpecies': #part 2, list species --> html form list the names of all the species available in the database contents = f"""<!DOCTYPE html> <html lang = "en"> <head> <meta charset = "utf-8" > <title>List of species in the browser</title > </head > <body style="background-color:rgb(255,255,182)"> <h1 style="color:rgb(21,105,150);"> List of species in the genome database</h1> <p style="color:rgb(21,105,150);"><b>The total number of species in ensembl is: 286</b></p> """ #Get the arguments after the ? get_value = arguments[1] #We need the value of the index --> position of the sequence seq_n = get_value.split('?') #splits the argument by the ? seq_name, index = seq_n[0].split("=") #splits by the = # menu of iteration to chose the path to act when the limit is inputed if index == "": #no index is inputed --> all list must be printed index = "286" if index == str: #this module works as an exception for when a string is inputed contents = """<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Error</title> </head> <body style="background-color:rgb(255,255,182)"> <h1>ERROR</h1> <p> Limit must be a number </p> <a href="/"> Main page </a> </p> </body> </html>""" index = int(index) if index <= 0: #index less or equal to 0 --> error contents = """<!DOCTYPE html> <html lang="en" dir="ltr"> <head> <meta charset="utf-8"> <title>ERROR</title> </head> <body style="background-color: red;"> <h1>ERROR</h1> <p>Resource not available</p> <p>Your search's limit is equal or less than 0</p> </body> </html>""" if index > 0: #index more than 0 --> normal execution of the program #html to print the total numbers of species selected contents += f"""<p>The number of species you selected are: {index} </p>""" #now program starts, gets the requested limit and ... endpoint = 'info/species' #stablishes the endpoint and its parameters for the request parameters = '?content-type=application/json' request = endpoint + parameters try: conn.request("GET", request) #connection request except ConnectionRefusedError: #exception for connection error print("ERROR! Cannot connect to the Server") exit() #----------------------Main program of listSpecies------------------------ # -- Read the response message from the server response = conn.getresponse() # -- Read the response's body body = response.read().decode( 'utf_8' ) #utf_8 to admit all characters in the response limit_list = [] #list to save all species within the limit body = json.loads( body) #loads is a json method to read json response limit = body["species"] #json.loads(species) if index > len( limit): #if there are more species than the limit contents = f"""<!DOCTYPE html> <html lang = "en"> <head> <meta charset = "utf-8" > <title>ERROR</title > </head> <body style="background-color:rgb(255,255,182)"> <p>ERROR LIMIT OUT OF RANGE</p> <p> Introduce a valid limit</p> <a href="/">Main page</a></body></html>""" else: for element in limit: #iteration to get all the species within the limit limit_list.append( element["display_name"] ) #appends each element to the list if len(limit_list) == index: contents += f"""<p>The species are: </p>""" for specie in limit_list: #iteration to print all the species in the limit list contents += f"""<p> - {specie} </p>""" contents += f"""<a href="/">Main page</a></body></html>""" #link to return to main page # --------------------------------------------karyotype-------------------------------------------- elif first_argument == '/karyotype': #part3, returns the names of the cromosomes of the chosen species contents = f"""<!DOCTYPE html> <html lang = "en"> <head> <meta charset = "utf-8"> <title> Karyotype </title > </head > <body style="background-color:rgb(255,255,182)"> """ try: # Get the arguments after the ? get_value = arguments[1] # We get the seq index and name specie = get_value.split('?') # splits by the ? specie_method, name_sp = specie[0].split( "=") # splits by the = full_name = "" #we initialize the variable to keep doble or more word names for n in range( 0, len(name_sp) ): #we iterate to inlude all the characters of the entire name (all words combined with +) if name_sp[ n] == "+": #when we get a + we create a space with "%20" in the url to be able to search it in the database as a 2 (or more) word species full_name += "%20" else: full_name += name_sp[ n] #in case its a one word species endpoint = 'info/assembly/' #stablishes the endpoint and its parameters for the request parameters = '?content-type=application/json' request = endpoint + full_name + parameters try: conn.request("GET", request) #connection request except ConnectionRefusedError: #exception for connection error print("ERROR! Cannot connect to the Server") exit() # ----------------------Main program of karyotype------------------------ # -- Read the response message from the server response = conn.getresponse() # -- Read the response's body body = response.read().decode( "utf-8" ) #utf_8 to admit all characters in the response body = json.loads( body) #loads is a json method to read json response karyotype_data = body[ "karyotype"] #list to save all the names full_name = full_name.replace( "%20", " " ) #to print it with a space between both words (in double word species) contents += f"""<h2 style="color:rgb(21,105,150);"> The names of the {full_name} chromosomes are:</h2>""" for chromosome in karyotype_data: #iteration to print all the chromosomes names contents += f"""<p> - {chromosome} </p>""" contents += f"""<a href="/">Main page </a></body></html>""" #link to return to main page except KeyError: #exception in case no value or an incorrect format value is inputed contents = f"""<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Error</title> </head> <body style="background-color:rgb(255,255,182)"> <h1>ERROR</h1> <p> Selected specie's karyotype information is not available </p> <p><a href="/karyotype?Specie={full_name}">Check if your specie is in our database</a><br><br> <p> Introduce a specie in the database to find its karyotype </p> <a href="/"> Main page </a> </p> </body> </html>""" # --------------------------------------------Cromosome length-------------------------------------------- elif first_argument == "/chromosomeLength": #part4, Return the Length of the chromosome named "chromo" of the given specie try: # We get the arguments that go after the ?, it will get us the SPECIE&CHROMOSOME pair = arguments[1] # We have to separate both the species name and the chromo index inputed pairs = pair.split('&') #splits by the & specie_name, specie = pairs[0].split( "=") #having pair[0] as the species name chromosome_index, chromosome = pairs[1].split( "=") #having pair[1] as the species name #html form for when no chromosome index is inputed contents = f"""<!DOCTYPE html> <html lang = "en"> <head> <meta charset = "utf-8" > <title>ERROR</title > </head> <body style="background-color:rgb(255,255,182)"> <p>ERROR INVALID VALUE</p> <p> Introduce a valid integer value for chromosome of this species</p> <a href="/">Main page</a></body></html>""" full_name = "" # we initialize the variable to keep doble or more word names for n in range( 0, len(specie) ): # we iterate to inlude all the characters of the entire name (all words combined with +) if specie[ n] == "+": # when we get a + we create a space with "%20" in the url to be able to search it in the database as a 2 (or more) word species full_name += "%20" else: full_name += specie[ n] # in case its a one word species endpoint = 'info/assembly/' # stablishes the endpoint and its parameters for the request parameters = '?content-type=application/json' request = endpoint + full_name + parameters try: conn.request("GET", request) #connection request except ConnectionRefusedError: #exception for connection error print("ERROR! Cannot connect to the Server") exit() # ----------------------Main program of chromosome length------------------------ # -- Read the response message from the server response = conn.getresponse() # -- Read the response's body body = response.read().decode( 'utf-8' ) #utf_8 to admit all characters in the response body = json.loads( body) #loads is a json method to read json response chromosome_data = body[ "top_level_region"] #list to save all the chromosomes specie = specie.replace( "+", " " ) #to print it with a space between both words (in double word species) for chromo in chromosome_data: #iteration to get all the chromosomes within the list of data if chromo["name"] == str( chromosome): #to print the cromosome length length = chromo["length"] contents = f"""<!DOCTYPE html><html lang = "en"><head><meta charset = "utf-8" ><title> Length Chromosome</title > </head ><body style="background-color:rgb(255,255,182)"><h2 style="color:rgb(21,105,150);"> The length of the '{chromosome}' {specie} chromosome is: {length}</h2><a href="/"> Main page</a""" except KeyError: #exception in case no value or an incorrect format value is inputed contents = f"""<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Error</title> </head> <body style="background-color:rgb(255,255,182)"> <h1>ERROR</h1> <p> Selected specie's cromosome length information is not available </p> <p><a href="/karyotype?Specie={full_name}">Check if your specie is in our database</a><br><br> <p> Introduce a specie in the database (with a proper chromosome) to find its length information </p> <a href="/"> Main page </a> </p> </body> </html>""" # --------------------------------------------gene Seq-------------------------------------------- elif first_argument == "/geneSeq": #Return the sequence of a given human gene contents = f"""<!DOCTYPE html> <html lang = "en"> <head> <meta charset = "utf-8"> <title> Gene Sequence </title> </head> <body style="background-color:rgb(255,255,182)"> <h1 style="color:rgb(225, 141, 27)"> Gene Sequence</h1>""" try: # We get the arguments that go after the ? get_value = arguments[1] # We get the seq index, after we have a couple of elements, the one which we need is the value of the index # position of the sequence seq_n = get_value.split('?') #splits the argument by the ? seq_name, name_seq = seq_n[0].split( "=") # #splits by the = --> name of the gene inputed contents += f"""<p style="color:rgb(225, 141, 27)"> The sequence of gene {name_seq} is: </p>""" #html to print the gene name first_endpoint = "xrefs/symbol/homo_sapiens/" #first endpoint = homosapiens --> human gene parameters = '?content-type=application/json' first_request = first_endpoint + name_seq + parameters try: conn.request("GET", first_request) #connection request except ConnectionRefusedError: #exception for connection error print("ERROR! Cannot connect to the Server") exit() # ----------------------Main program of geneSeq------------------------ # -- Read the response message from the server response = conn.getresponse() # -- Read the response's body body = response.read().decode() body = json.loads( body) #loads is a json method to read json response id_gene = body[ 0] #to get the id of the gene (first column of body) #json.loads(id) id_gene = id_gene["id"] second_endpoint = "sequence/id/" #to get specifically the sequence of the gene we id'd second_request = second_endpoint + id_gene + parameters try: conn.request("GET", second_request) #connection request except ConnectionRefusedError: #exception for connection error print("ERROR! Cannot connect to the Server") exit() # -- Read the second response message from the server second_response = conn.getresponse() # -- Read the second response's body second_body = second_response.read().decode("utf-8") second_body = json.loads( second_body ) #loads is a json method to read json response sequence = second_body["seq"] #gene sequence asked contents += f"""<p>{sequence}</p><a href="/">Main page</a></body></html>""" #print the sequence on screen except KeyError: #exception in case no value or an incorrect format value is inputed contents = """<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Error</title> </head> <body style="background-color:rgb(255,255,182)"> <h1>ERROR</h1> <p> Selected specie's gene sequence information is not available </p> <p> Introduce a valid human gene to find its sequence information </p> <a href="/"> Main page </a> </p> </body> </html>""" # --------------------------------------------gene Info------------------------------------------- elif first_argument == "/geneInfo": #Return information about a human gene: start, end, Length, id and Chromose contents = f"""<!DOCTYPE html> <html lang = "en"> <head> <meta charset = "utf-8" > <title>Gene Information</title> </head> <body style="background-color:rgb(255,255,182)">""" try: # We get the arguments that go after the ? get_value = arguments[1] # We get the seq index, after we have a couple of elements, the one which we need is the value of the index # position of the sequence seq_n = get_value.split('?') #splits the argument by the ? seq_name, name_seq = seq_n[0].split( "=") # #splits by the = --> name of the gene inputed contents += f"""<p style="color:rgb(225, 141, 27)"> Information of gene {name_seq} is: </p>""" #html to print the gene name first_endpoint = "xrefs/symbol/homo_sapiens/" #first endpoint = homosapiens --> human gene parameters = '?content-type=application/json' first_request = first_endpoint + name_seq + parameters try: conn.request("GET", first_request) #connection request except ConnectionRefusedError: print("ERROR! Cannot connect to the Server" ) #exception for connection error exit() # ----------------------Main program of geneInfo------------------------ # -- Read the response message from the server response = conn.getresponse() # -- Read the response's body body = response.read().decode() body = json.loads( body) #loads is a json method to read json response id_gene = body[ 0] #to get the id of the gene (first column of body) #json.loads(id) id_gene = id_gene["id"] second_endpoint = "lookup/id/" #to get specifically the info of the sequence of the gene we id'd second_request = second_endpoint + id_gene + parameters try: conn.request("GET", second_request) #connection request except ConnectionRefusedError: #exception for connection error print("ERROR! Cannot connect to the Server") exit() # -- Read the second response message from the server second_response = conn.getresponse() # -- Read the response's body second_body = second_response.read().decode() second_body = json.loads( second_body ) #loads is a json method to read json response length = int(second_body["end"]) - int( second_body["start"]) #measure the length of the gene #prints the data of the starting point, end, length, region name and gene id contents += f"""<p> -Starts at: {second_body ["start"]} </p><p> -Ends at: {second_body ["end"]} </p> <p> -Length is: {length}</p> <p> -ID is at: {id_gene} </p> <p> Located in chromosome: {second_body ["seq_region_name"]} </p> <a href="/">Main page</a></body></html>""" except KeyError: #exception in case no value or an incorrect format value is inputed contents = """<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Error</title> </head> <body style="background-color:rgb(255,255,182)"> <h1>ERROR</h1> <p> Selected specie's gene information is not available </p> <p> Introduce a valid human gene to find its information </p> <a href="/"> Main page </a> </p> </body> </html>""" # --------------------------------------------gene Calc-------------------------------------------- elif first_argument == "/geneCalc": #Return the names of the genes located in the chromosome "chromo" from the start to end positions contents = f"""<!DOCTYPE html> <html lang = "en"> <head> <meta charset = "utf-8"> <title> Gene Calculations</title> </head> <body style="background-color:rgb(255,255,182)"> <h1 style="color:rgb(225, 141, 27)">Gene calculations</h1>""" try: # We get the arguments that go after the ? get_value = arguments[1] # We get the seq index, after we have a couple of elements, the one which we need is the value of the index # position of the sequence seq_n = get_value.split('?') #splits the argument by the ? seq_name, name_seq = seq_n[0].split( "=") #splits by the = --> name of the gene inputed first_endpoint = "xrefs/symbol/homo_sapiens/" #first endpoint = homosapiens --> human gene parameters = '?content-type=application/json' first_request = first_endpoint + name_seq + parameters try: conn.request("GET", first_request) #connection request except ConnectionRefusedError: print("ERROR! Cannot connect to the Server" ) #exception for connection error exit() # ----------------------Main program of geneCalc------------------------ # -- Read the response message from the server response = conn.getresponse() # -- Read the response's body body = response.read().decode('utf-8') body = json.loads( body) #loads is a json method to read json response id_gene = body[ 0] #to get the id of the gene (first column of body) #json.loads(id) id_gene = id_gene["id"] second_endpoint = "sequence/id/" #to get specifically the info of the sequence of the gene we id'd second_request = second_endpoint + id_gene + parameters try: conn.request("GET", second_request) #connection request except ConnectionRefusedError: print("ERROR! Cannot connect to the Server" ) #exception for connection error exit() # -- Read the second response message from the server second_response = conn.getresponse() # -- Read the second response's body second_body = second_response.read().decode() second_body = json.loads(second_body) sequence = Seq( second_body["seq"]) #gets the sequence of the gene contents += f"""<p> The length of gene {name_seq} is: {sequence.len()} </p>""" list_of_bases = ["A", "C", "G", "T"] for base in list_of_bases: perc_base = round( sequence.count_base(base) * 100 / sequence.len(), 2) contents += f"""<p> {base} : {sequence.count_base(base)} ({perc_base}%) </p>""" contents += f"""<a href="/">Main page</a></body></html>""" except KeyError: #exception in case no value or an incorrect format value is inputed contents = """<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Error</title> </head> <body style="background-color:rgb(255,255,182)"> <h1>ERROR</h1> <p> Selected specie's gene calculations information is not available </p> <p> Introduce a valid human gene to find its calculations information </p> <a href="/"> Main page </a> </p> </body> </html>""" # --------------------------------------------gene geneList-------------------------------------------- elif first_argument == "/geneList": #Return the names of the genes located in the chromosome "chromo" from the start to end positions contents = f"""<!DOCTYPE html> <html lang = "en"> <head> <meta charset = "utf-8"> <title> Gene List</title> </head> <body style="background-color:rgb(255,255,182)"> <h1 style="color:rgb(225, 141, 27)">Gene List</h1>""" try: # We get the arguments that go after the ? get_value = arguments[1] # We get the seq index, after we have a couple of elements, the one which we need is the value of the index # position of the sequence pairs = get_value.split('&') #splits by the & chromo_value, chromo = pairs[0].split( "=") #having pair[0] as the species name chromosome_start, start = pairs[1].split( "=") #chromosome start (pair[1] column) chromosome_end, end = pairs[2].split( "=") #chromosome end (pair[2] column) contents += f"""<p style="color:rgb(225, 141, 27)"> List of genes of the chromosome {chromo}, which goes from {start} to {end}: </p>""" endpoint = "overlap/region/human/" # first endpoint --> human parameters = '?feature=gene;content-type=application/json' request = endpoint + chromo + ":" + start + "-" + end + parameters #request line try: conn.request("GET", request) #connection request except ConnectionRefusedError: print("ERROR! Cannot connect to the Server" ) #exception for connection error exit() # ----------------------Main program of geneList------------------------ # -- Read the response message from the server response = conn.getresponse() # -- Read the response's body body = response.read().decode( "utf-8" ) #utf_8 to admit all characters in the response body = json.loads(body) for element in body: #iteration to print all the elements of the chromosome within the chosen limits print(element["external_name"]) contents += f"""<p>{element["external_name"]}</p>""" contents += f"""<a href="/">Main page</a></body></html>""" except KeyError: #exception in case no value or an incorrect format value is inputed contents = """<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Error</title> </head> <body style="background-color:rgb(255,255,182)"> <h1>ERROR</h1> <p> Selected specie's gene interval sequence information is not available </p> <p> Introduce a valid human gene to find its interval sequence information </p> <a href="/"> Main page </a> </p> </body> </html>""" # Open the form1.html file # Read the index from th # Define the content-type header: if 'json=1' in req_line: self.send_header('Content-Type', 'application/json') self.send_header('Content-Length', len(str.encode(contents))) else: 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 except (KeyError, ValueError, IndexError, TypeError): contents = Path('error.html').read_text()
def do_GET(self): """This method is called whenever the client invokes the GET method in the HTTP protocol request""" termcolor.cprint(self.requestline, "green") req_line = self.requestline.split(' ') path = req_line[1] arguments = path.split('?') option = arguments[0] content = Path('error.html').read_text() cod = 404 if option == "/": content = Path('form-4.html').read_text() cod = 200 elif option == "/ping": content = """<!DOCTYPE html><html lang = "en"><head><meta charset = "utf-8" ><title> Ping </title ></head > <body><h2> PING OK!</h2><p> The SEQ2 server in running.... </p><a href="/">Main page</a></body></html>""" cod = 200 elif option == "/get": get_seq = arguments[1] seq = get_seq.split('?') name, seq_index = seq[0].split("=") seq_index = int(seq_index) seq = LIST_SEQ[seq_index] content = f"""<!DOCTYPE html><html lang = "en"><head><meta charset = "utf-8" ><title> Get </title ></head > <body><h2> Sequence number {seq_index}</h2><p> {seq} </p><a href="/">Main page</a></body></html>""" cod = 200 elif option == "/gene": get_gene = arguments[1] couple = get_gene.split('?') name, gene = couple[0].split("=") seq = Seq() filename = FOLDER + gene + TXT seq = Seq(seq.read_fasta(filename)) gene = str(seq) content = f"""<!DOCTYPE html><html lang = "en"><head><meta charset = "utf-8" ><title> Gene </title ></head> <body><h2> Gene: {gene}</h2><textarea readonly rows="20" cols="80"> {gene_seq} </textarea><br><br> <a href="/">Main page</a></body></html>""" cod = 200 elif option == "/operation": operation = arguments[1] couple = operation.split('&') name_s, seq = couple[0].split("=") name_o, op = couple[1].split("=") seq = Seq(seq) if op == "comp": result = seq.complement() elif op == "rev": result = seq.reverse() else: l = seq.len() A_COUNTER = seq.count_base('A') C_COUNTER = seq.count_base('C') G_COUNTER = seq.count_base('G') T_COUNTER = seq.count_base('T') A_PER = 100 * A_COUNTER / l C_PER = 100 * C_COUNTER / l G_PER = 100 * G_COUNTER / l T_PER = 100 * T_COUNTER / l result = f"""<p>Total length: {l}</p><p>A: {A_COUNTER} ({A_PER}%)</p><p>G: {C_COUNTER} ({C_PER}%) </p><p>C: {G_COUNTER} ({G_PER}%)</p><p>T: {T_COUNTER} ({T_PER}%)</p>""" content = f"""<!DOCTYPE html><html lang = "en"><head><meta charset = "utf-8" ><title> Operation </title > </head ><body><h2> Sequence </h2><p>{seq}</p><h2> Operation: </h2><p>{op}</p><h2> Result: </h2><p>{result} </p><br><br><a href="/">Main page</a></body></html>""" cod = 200 # Generating the response message self.send_response(cod) # -- Status line: OK! # Define the content-type header: self.send_header('Content-Type', 'text/html') self.send_header('Content-Length', len(str.encode(content))) # The header is finished self.end_headers() # Send the response message self.wfile.write(str.encode(content)) return
r1 = conn.getresponse() print(f"Response received!: {r1.status} {r1.reason}\n") data1 = r1.read().decode() gene = json.loads(data1) termcolor.cprint("Gene", 'green', end="") print(f": {name}") termcolor.cprint("Description", 'green', end="") print(f": {gene['desc']}") genestr = gene['seq'] s = Seq(genestr) length = s.len() termcolor.cprint("Total length", 'green', end="") print(f": {length}") for e in BASES: count = s.count_base(e) percentage = round(s.count_base(e) * (100 / s.len()), 2) termcolor.cprint(f"{e}", 'blue', end="") print(f": {count} ({percentage}%)") dictionary = s.count() list_values = list(dictionary.values()) max_base = max(list_values) termcolor.cprint("Most frequent Base", 'green', end="") print(f": {BASES[list_values.index(max_base)]}")
console = json.loads(data1) 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)])
def do_GET(self): termcolor.cprint(self.requestline, 'green') request = self.requestline.split(' ') raw_arg = request[1] clean_arg = raw_arg.split('?') argument = clean_arg[0] if argument == '/': contents = Path('form-4.html').read_text() elif argument == '/ping': contents = f""" <!DOCTYPE html> <html lang = "en"> <head> <meta charset = "utf-8" > <title> PING </title > </head > <body> <h2> PING OK!</h2> <p> The SEQ2 server is running </p> <a href="/">Main page</a> </body> </html> """ elif argument == '/get': argument1 = clean_arg[1].split('&') name, value = argument1[0].split('=') value = int(value) sequence = Sequences[value] contents = f""" <!DOCTYPE html> <html lang = "en"> <head> <meta charset = "utf-8" > <title> GET </title > </head > <body> <h2> Sequence number {value}</h2> <p> {sequence} </p> <a href="/">Main page</a> </body> </html> """ elif argument == '/gene': argument1 = clean_arg[1].split('&') name, gene = argument1[0].split('=') sequence = Seq() sequence.read_fasta(FOLDER + gene + EXT) gene_str = str(sequence) contents = f""" <!DOCTYPE html> <html lang = "en"> <head> <meta charset = "utf-8" > <title> GENE </title > </head > <body> <h2> Gene: {gene}</h2> <textarea readonly rows="20" cols="80"> {gene_str} </textarea> <br> <br> <a href="/">Main page</a> </body> </html>""" elif argument == '/operation': argument1 = clean_arg[1].split('&') name, sequence = argument1[0].split("=") name, operation = argument1[1].split("=") seq = Seq(sequence) if operation == 'comp': result = seq.complement() elif operation == 'rev': result = seq.reverse() else: length = seq.len() num_bases = [] perc_bases = [] for element in bases: counter_bases = seq.count_base(element) percentage = round((seq.count_base(element) * 100 / seq.len()), 2) num_bases.append(counter_bases) perc_bases.append(percentage) result = f""" <p>Total length: {length}</p> <p>A: {num_bases[0]} ({perc_bases[0]}%)</p> <p>C: {num_bases[1]} ({perc_bases[1]}%)</p> <p>G: {num_bases[2]} ({perc_bases[2]}%)</p> <p>T: {num_bases[3]} ({perc_bases[3]}%)</p>""" contents = f""" <!DOCTYPE html> <html lang = "en"> <head> <meta charset = "utf-8" > <title> OPERATION </title > </head > <body> <h2>Sequence</h2> <p>{seq}</p> <h2>Operation</h2> <p>{operation}</p> <h2>Result</h2> <p>{result}</p> <br> <br> <a href="/">Main page</a> </body> </html>""" else: contents = Path('Error.html').read_text() self.send_response(200) self.send_header('Content-Type', 'text/html') self.send_header('Content-Length', len(str.encode(contents))) self.end_headers() self.wfile.write(str.encode(contents)) return
def do_GET(self): # Print the request line termcolor.cprint(self.requestline, 'green') path = self.requestline.split()[1] arguments = path.split('?') firts_argument = arguments[0] try: if firts_argument == "/": contents = Path("main-page.html").read_text() self.send_response(200) elif firts_argument == "/listSpecies": second_argument = arguments[1] new_argument = second_argument.split("&") data = client_get_species( "info/species?content-type=application/json")["species"] if len(new_argument) == 2: parameter, json = second_argument.split("&") third_argument = parameter.split("=")[1] if json == "json=1": new_list = [] counter = 0 if third_argument == "": for element in data: new_list.append(element["display_name"]) counter += 1 contents = list_species(third_argument, new_list) elif int(third_argument) <= 267: for element in data: if counter < int(third_argument): new_list.append(element["display_name"]) counter += 1 contents = list_species(third_argument, new_list) self.send_response(200) else: contents = Path('Error.json').read_text() self.send_response(404) elif len(new_argument) == 1: third_argument = second_argument.split("=")[1] contents = html_file("lightblue", "List of species") contents += f"""<p>Total number of species is 267</p>""" if third_argument == "": contents += f""" <p>The limit you have selected is:{267} </p> <p>The names of the species are:</p>""" for element in data: contents += f"""<p> · {element["display_name"]} </p>""" elif 267 >= int(third_argument): contents += f""" <p>Total number of species is: 267 </p> <p>The limit you have selected is: {third_argument}</p> <p>The names of the species are:</p>""" count = 0 for element in data: if count < int(third_argument): contents += f'''<p> · {element["display_name"]}</p>''' count += 1 else: contents += f"""<p>Total number of species is: 267 </p> <p>The limit you have selected is:{third_argument}</p> <p>The names of the species are:</p>""" for element in data: contents += f"""<p> · {element["display_name"]} </p>""" self.send_response(200) elif firts_argument == "/karyotype": second_argument = arguments[1] new_argument = second_argument.split("&") if len(new_argument) == 2: parameter, json = second_argument.split("&") third_argument = parameter.split("=")[1] data = client_get_species( f"/info/assembly/{third_argument}?content-type=application/json" ) if json == "json=1": contents_1 = data["karyotype"] new_list = [] for element in contents_1: new_list.append(element) contents = karyotype_dic(new_list) self.send_response(200) else: contents = Path('Error.json').read_text() self.send_response(404) elif len(new_argument) == 1: third_argument = second_argument.split("=")[1] species = client_get_species( "info/assembly/" + third_argument + "?content-type=application/json")["karyotype"] contents = html_file("pink", "Name of chromosomes:") contents += f"""<p>The names of the chromosomes are: </p>""" for element in species: contents += f"""<p> · {element} </p>""" self.send_response(200) elif firts_argument == "/chromosomeLength": second_argument = arguments[1] new_argument = second_argument.split("&") if len(new_argument) == 3: specie, chromosome, json = second_argument.split("&") specie_1 = specie.split("=")[1] chromo = chromosome.split("=")[1] species = client_get_species( "info/assembly/" + specie_1 + "?content-type=application/json") if json == "json=1": info = species["top_level_region"] contents = dic_chromosomeLength(0) for element in info: if element["name"] == chromo: length = element["length"] contents = dic_chromosomeLength(length) self.send_response(200) else: contents = Path('Error.json').read_text() self.send_response(404) elif len(new_argument) == 2: specie, chromosome = second_argument.split("&") specie_1 = specie.split("=")[1] chromo = chromosome.split("=")[1] species = client_get_species( "info/assembly/" + specie_1 + "?content-type=application/json") info = species["top_level_region"] contents = html_file("plum", "Chromosome length") for element in info: if element["coord_system"] == "chromosome": if element["name"] == chromo: contents += f"""<p> The length of the chromosome is: {element["length"]} </p>""" self.send_response(200) elif firts_argument == "/geneSeq": second_argument = arguments[1] new_argument = second_argument.split("&") if len(new_argument) == 2: parameter, json = second_argument.split("&") gene = parameter.split("=")[1] id_gen = client_get_species( f"""/xrefs/symbol/homo_sapiens/{gene}?content-type=application/json""" )[0]["id"] data = client_get_species( f"""/sequence/id/{id_gen}?content-type=application/json""" ) if json == "json=1": contents = dict_geneSeq(data["seq"]) self.send_response(200) else: contents = Path('Error.json').read_text() self.send_response(404) elif len(new_argument) == 1: gene = second_argument.split("=")[1] id_gen = client_get_species( f"""/xrefs/symbol/homo_sapiens/{gene}?content-type=application/json""" )[0]["id"] data = client_get_species( f"""/sequence/id/{id_gen}?content-type=application/json""" ) contents = html_file("aquamarine", "Sequence of a human gene") contents += f'<p> The sequence of gene {gene} is: </p>' contents += f'<textarea rows = "100" "cols = 500"> {data["seq"]} </textarea>' self.send_response(200) elif firts_argument == "/geneInfo": second_argument = arguments[1] new_argument = second_argument.split("&") if len(new_argument) == 2: parameter, json = second_argument.split("&") gene = parameter.split("=")[1] id_gen = client_get_species( f"""/xrefs/symbol/homo_sapiens/{gene}?content-type=application/json""" )[0]["id"] data = client_get_species( f"""/lookup/id/{id_gen}?content-type=application/json""" ) if json == "json=1": length = data["end"] - data["start"] contents = dict_geneInfo(data["start"], data["end"], length, data["id"], data["seq_region_name"]) self.send_response(200) else: contents = Path('Error.json').read_text() self.send_response(404) elif len(new_argument) == 1: gene = second_argument.split("=")[1] id_gen = \ client_get_species(f"""/xrefs/symbol/homo_sapiens/{gene}?content-type=application/json""")[0]["id"] data = client_get_species( f"""/lookup/id/{id_gen}?content-type=application/json""" ) contents = html_file("palevioletred", "Information of a human gene") contents += f'<p> The gene is on chromosome {data["seq_region_name"]} </p>' contents += f'<p> The start of the gene is: {data["start"]} </p>' contents += f'<p> The end of the gene is: {data["end"]}</p>' contents += f'<p> The length of the gene is: {data["end"] - data["start"]}</p>' contents += f'<p> The identification of the gene is: {id_gen}</p>' self.send_response(200) elif firts_argument == "/geneCalc": second_argument = arguments[1] new_argument = second_argument.split("&") if len(new_argument) == 2: parameter, json = second_argument.split("&") gene = parameter.split("=")[1] id_gen = client_get_species( f"""/xrefs/symbol/homo_sapiens/{gene}?content-type=application/json""" )[0]["id"] data = client_get_species( f"""/sequence/id/{id_gen}?content-type=application/json""" ) sequence = Seq(data["seq"]) if json == "json=1": bases_list = [] for base in list_bases: bases_list.append(sequence.count_base(base)[1]) contents = dict_geneCalc(sequence.len(), bases_list) self.send_response(200) else: contents = Path('Error.json').read_text() self.send_response(404) elif len(new_argument) == 1: self.send_response(200) gene = second_argument.split("=")[1] id_gen = \ client_get_species(f"""/xrefs/symbol/homo_sapiens/{gene}?content-type=application/json""")[0]["id"] data = client_get_species( f"""/sequence/id/{id_gen}?content-type=application/json""" ) sequence = data["seq"] gene_seq = Seq(sequence) contents = html_file( "thistle", "Length and percentage of basis of the gene") contents += f"The total lenght of the sequence is: {gene_seq.len()}" for element in list_bases: contents += f"<p>Base {element}: Percentage: ({gene_seq.count_base(element)[1]}%)</p>" elif firts_argument == "/geneList": second_argument = arguments[1] new_argument = second_argument.split("&") if len(new_argument) == 4: first, second, third, json = second_argument.split("&") chromo = first.split("=")[1] start = second.split("=")[1] end = third.split("=")[1] data = client_get_species( "overlap/region/human/" + chromo + ":" + start + "-" + end + "?feature=gene;content-type=application/json") if json == "json=1": new_list = [] for gene in data: new_list.append(gene["external_name"]) contents = dict_geneList(new_list) self.send_response(200) else: contents = Path('Error.json').read_text() self.send_response(404) elif len(new_argument) == 3: self.send_response(200) first, second, third = second_argument.split("&") chromo = first.split("=")[1] start = second.split("=")[1] end = third.split("=")[1] data = client_get_species( "overlap/region/human/" + chromo + ":" + start + "-" + end + "?feature=gene;content-type=application/json") contents = html_file("moccasin", "List of human genes") for gene in data: contents += f"<p> · {gene['external_name']} </p>" except (KeyError, ValueError, IndexError, TypeError): contents = Path('Error.html').read_text() self.send_response(404) endpoints = [ "/", "/listSpecies", "/karyotype", "/chromosomeLength", "/geneSeq", "/geneInfo", "/geneCalc", "/geneList" ] if firts_argument in endpoints: if "json" in path: type = "application/json" else: type = "text/html" self.send_header('Content-Type', type) self.send_header('Content-Length', len(str.encode(contents))) self.end_headers() self.wfile.write(str.encode(contents)) return
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') # Analize the request line req_line = self.requestline.split(' ') # Get the path. It always start with the / symbol path = req_line[1] # Read the arguments arguments = path.split('?') # Get the verb. It is the first argument verb = arguments[0] # -- Content type header # -- Both, the error and the main page are in HTML contents = Path('Error.html').read_text() error_code = 404 if verb == "/": # Open the form1.html file # Read the index from the file contents = Path('form-4.html').read_text() error_code = 200 elif verb == "/ping": contents = """ <!DOCTYPE html> <html lang = "en"> <head> <meta charset = "utf-8" > <title> PING </title > </head > <body> <h2> PING OK!</h2> <p> The SEQ2 server in running... </p> <a href="/">Main page</a> </body> </html> """ error_code = 200 elif verb == "/get": # -- Get the argument to the right of the ? symbol pair = arguments[1] # -- Get all the pairs name = value pairs = pair.split('&') # -- Get the two elements: name and value name, value = pairs[0].split("=") n = int(value) # -- Get the sequence seq = SEQ_GET[n] # -- Generate the html code contents = f""" <!DOCTYPE html> <html lang = "en"> <head> <meta charset = "utf-8" > <title> GET </title > </head > <body> <h2> Sequence number {n}</h2> <p> {seq} </p> <a href="/">Main page</a> </body> </html> """ error_code = 200 elif verb == "/gene": # -- Get the argument to the right of the ? symbol pair = arguments[1] # -- Get all the pairs name = value pairs = pair.split('&') # -- Get the two elements: name and value name, gene = pairs[0].split("=") s = Seq() s.read_fasta(FOLDER + gene) gene_str = str(s) # -- Generate the html code contents = f""" <!DOCTYPE html> <html lang = "en"> <head> <meta charset = "utf-8" > <title> GENE </title > </head > <body> <h2> Gene: {gene}</h2> <textarea readonly rows="20" cols="80"> {gene_str} </textarea> <br> <br> <a href="/">Main page</a> </body> </html> """ error_code = 200 elif verb == "/operation": # -- Get the argument to the right of the ? symbol pair = arguments[1] # -- Get all the pairs name = value pairs = pair.split('&') # -- Get the two elements: name and value name, seq = pairs[0].split("=") # -- Get the two elements of the operation name, op = pairs[1].split("=") # -- Create the sequence s = Seq(seq) if op == "comp": result = s.complement() elif op == "rev": result = s.reverse() else: length = s.len() count_bases = [] count_percentages = [] for e in bases: count = s.count_base(e) percentage = round(s.count_base(e) * (100 / s.len()), 2) count_bases.append(count) count_percentages.append(percentage) result = f""" <p>Total length: {length}</p> <p>A: {count_bases[0]} ({count_percentages[0]}%)</p> <p>C: {count_bases[1]} ({count_percentages[1]}%)</p> <p>G: {count_bases[2]} ({count_percentages[2]}%)</p> <p>T: {count_bases[3]} ({count_percentages[3]}%)</p>""" contents = f""" <!DOCTYPE html> <html lang = "en"> <head> <meta charset = "utf-8" > <title> OPERATION </title > </head > <body> <h2> Sequence </h2> <p>{seq}</p> <h2> Operation: </h2> <p>{op}</p> <h2> Result: </h2> <p>{result}</p> <br> <br> <a href="/">Main page</a> </body> </html> """ error_code = 200 # Generating the response message self.send_response(error_code) # -- Status line: OK! # 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
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') # Analize the request line req_line = self.requestline.split(' ') # Take the path info path = req_line[1] # Separate the path from the rest of the request line arguments = path.split('?') # we take the service names from the path action = arguments[0] # -- Content type header # -- Both, the error and the main page are in HTML contents = Path('Error.html').read_text() error_code = 404 if action == "/": # Open the form1.html file # Read the index from the file contents = Path('form-4.html').read_text() error_code = 200 elif action == "/ping": contents = """ <!DOCTYPE html> <html lang = "en"> <head> <meta charset = "utf-8" > <title> PING </title > </head > <body> <h2> PING OK!</h2> <p> The SEQ2 server in running... </p> <a href="/">Main page</a> </body> </html> """ error_code = 200 elif action == "/get": # -- Get the argument to the right of the ? symbol pair = arguments[1] # -- Get all the pairs name = value pairs = pair.split('&') # -- Get the two elements: name and value name, value = pairs[0].split("=") n = int(value) # -- Get the sequence seq = SEQ_GET[n] # -- Generate the html code contents = f""" <!DOCTYPE html> <html lang = "en"> <head> <meta charset = "utf-8" > <title> GET </title > </head > <body> <h2> Sequence number {n}</h2> <p> {seq} </p> <a href="/">Main page</a> </body> </html> """ error_code = 200 elif action == "/gene": # -- Get the argument to the right of the ? symbol pair = arguments[1] # -- Get all the pairs name = value pairs = pair.split('&') # -- Get the two elements: name and value name, gene = pairs[0].split("=") s = Seq() s.read_fasta(FOLDER + gene + EXT) gene_str = str(s) # -- Generate the html code contents = f""" <!DOCTYPE html> <html lang = "en"> <head> <meta charset = "utf-8" > <title> GENE </title > </head > <body> <h2> Gene: {gene}</h2> <textarea readonly rows="20" cols="80"> {gene_str} </textarea> <br> <br> <a href="/">Main page</a> </body> </html> """ error_code = 200 elif action == "/operation": # -- Get the argument to the right of the ? symbol pair = arguments[1] # -- Get all the pairs name = value pairs = pair.split('&') # -- Get the two elements: name and value name, seq = pairs[0].split("=") # -- Get the two elements of the operation name, op = pairs[1].split("=") # -- Create the sequence s = Seq(seq) if op == "comp": result = s.complement() elif op == "rev": result = s.reverse() else: sl = s.len() ca = s.count_base('A') pa = "{:.1f}".format(100 * ca / sl) 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) result = f""" <p>Total length: {sl}</p> <p>A: {ca} ({pa}%)</p> <p>C: {cc} ({pc}%)</p> <p>G: {cg} ({pg}%)</p> <p>T: {ct} ({pt}%)</p>""" contents = f""" <!DOCTYPE html> <html lang = "en"> <head> <meta charset = "utf-8" > <title> OPERATION </title > </head > <body> <h2> Sequence </h2> <p>{seq}</p> <h2> Operation: </h2> <p>{op}</p> <h2> Result: </h2> <p>{result}</p> <br> <br> <a href="/">Main page</a> </body> </html> """ error_code = 200 # Generating the response message self.send_response(error_code) # -- Status line: OK! # 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
# -- Create a variable with the data, # -- form the JSON received gene = json.loads(body) print("Gene", end="") print(":", colored(name, "green")) print("Description", end="") print(":", colored(gene['desc'], "green")) body = gene['seq'] # -- Create the object sequence from the string # We use class Seq for using its functions seq = Seq(body) length = seq.len() counter_a = seq.count_base('A') counter_g = seq.count_base('G') counter_c = seq.count_base('C') counter_t = seq.count_base('T') perc_a = 100 * counter_a / length perc_g = 100 * counter_g / length perc_c = 100 * counter_c / length perc_t = 100 * counter_t / length print("Total lengh", end="") print(":", colored(length, "green")) print("A", end="") print(":", colored(counter_a, "green"), colored(perc_a, "blue"), "%") print("C", end="") print(":", colored(counter_c, "green"), colored(perc_c, "blue"), "%")
print(f"{gene}") termcolor.cprint("Description :", "green", end=" ") print(f"{gene_info['desc']}") sequence = Seq(gene_info['seq']) termcolor.cprint("Total length: ", "green", end=" ") print(f"{sequence.len()}") bases = ['A', 'C', 'G', 'T'] for base in bases: termcolor.cprint(base, "blue", end=" ") print(f": {sequence.count_base(base)}", end=" ") percentage_base = "{:.1f}".format( sequence.count_base(base) / sequence.len() * 100) print(f"({percentage_base} %)") max = 0 max_base = "" for base in bases: if sequence.count_base(base) > int(max): max = sequence.count_base(base) max_base = base print("Most frequent base: ", {max_base}) except ConnectionRefusedError: print("ERROR! Cannot connect to the Server") exit()
from Seq1 import Seq print("Practice 1, Exercise 5") s1 = Seq() s2 = Seq("GATCGG") s3 = Seq("Invalid sequence") validbases = ["A", "T", "G", "C"] print("Sequence 1: (Length : ", s1.len(), ")", s1) for element in validbases: print(element + ":", s1.count_base(element), end=", ") print("\nSequence 2: (Length : ", s2.len(), ")", s2) for element in validbases: print(element + ":", s2.count_base(element), end=", ") print("\nSequence 3: (Length : ", s3.len(), ")", s3) for element in validbases: print(element + ":", s3.count_base(element), end=", ")
# -- Read the response's body data1 = r1.read().decode("utf-8") # -- Create a variable with the data, # -- form the JSON received response = json.loads(data1) termcolor.cprint(f"Gene: ", "green", end="") print(id) termcolor.cprint(f"Description: ", "green", end="") print(response['desc']) seq_bases = response['seq'] sequence = Seq(seq_bases) termcolor.cprint(f"Total length: ", "green", end="") print(sequence.len()) for base in bases: count = sequence.count_base(base) percentage = round( sequence.count_base(base) * (100 / sequence.len()), 2) termcolor.cprint(f"{base}", 'blue', end="") print(f": {count} ({percentage}%)") d = sequence.seq_count() ll = list(d.values()) m = max(ll) termcolor.cprint("Most frequent Base:", 'green', end="") print(f"{bases[ll.index(m)]}")
content = answer.read().decode() # -- Create a variable with the data, # -- form the JSON received gene = json.loads(content) termcolor.cprint("Gene:", "green", end="") print(g) termcolor.cprint("Description:", "green", end="") print(gene["desc"]) GENE_SEQ = gene["seq"] seq = Seq(GENE_SEQ) l = seq.len() A_COUNTER = seq.count_base('A') C_COUNTER = seq.count_base('C') G_COUNTER = seq.count_base('G') T_COUNTER = seq.count_base('T') A_PER = 100 * A_COUNTER / l C_PER = 100 * C_COUNTER / l G_PER = 100 * G_COUNTER / l T_PER = 100 * T_COUNTER / l print(f"""Total length: {l} A: {A_COUNTER} ({A_PER}%) G: {G_COUNTER} ({G_PER}%) C: {C_COUNTER} ({C_PER}%) T: {T_COUNTER} ({T_PER}%)""") DICT_VALUES = seq.count()