<jupyter_start><jupyter_text>**Bioinformatics with Jupyter Notebooks for WormBase:** **Analyses 5 - Multiple Sequence Alignment**Welcome to the ninth jupyter notebook in the WormBase tutorial series. Over this series of tutorials, we will write code in Python that allows us to retrieve and perform simple analyses with data available on the WormBase sites.This tutorial will deal with performing multiple sequence alignment of your data using several different algorithms. This is not limited to only data from WormBase but the examples in the tutorial use worm data.Let's get started! As always, we start by importing any libraries that are required for the tutorials.<jupyter_code>import requests
import sys
import json
import xml.dom.minidom<jupyter_output><empty_output><jupyter_text>Read in the input data of multiple sequences from a fasta file<jupyter_code>sequences = open("data/mutltiple_fasta.fa")
sequences = sequences.read()
print(sequences)<jupyter_output><empty_output><jupyter_text>Clustal OmegaClustal Omega is an MSA tool that uses seeded guide trees and HMM profile-profile techniques to generate alignments that is suitable for medium-large alignments.<jupyter_code>server = 'https://www.ebi.ac.uk/Tools/services/rest/clustalo/'<jupyter_output><empty_output><jupyter_text>We can first explore which parameters can be used for multiple sequence alignment using the API. Out of these, only email, and input sequences are mandatory fields in most cases!<jupyter_code>request = requests.get(server + 'parameters')

if not request.ok:
  request.raise_for_status()
  sys.exit() 

parameters = xml.dom.minidom.parseString(request.text)
parameters = parameters.toprettyxml()

print(parameters)<jupyter_output><empty_output><jupyter_text>Now, we know the different parameters that can be used for running the alignment. We can then find out the details of each of these parameters, the type of data accepted, and the permissible values. You can change the parameter variable to whichever parameter you want to find details about.<jupyter_code>parameter = 'guidetreeout'
request = requests.get(server + 'parameterdetails/' + parameter)

if not request.ok:
  request.raise_for_status()
  sys.exit() 

parameter_details = xml.dom.minidom.parseString(request.text)
parameter_details = parameter_details.toprettyxml()

print(parameter_details)<jupyter_output><empty_output><jupyter_text>Now, we can request the API and run the alignment. Here, multiple parameters can be specified based on the details from the cells above. For Clustal Omega, the mandatory fields are - email and sequence. And after running the alignment, we can get the job id for getting the status of the job and also the results of the alignment.<jupyter_code>request = requests.post(server + 'run', 
                        headers = {"Content-Type" : "application/x-www-form-urlencoded", "Accept" : "text/plain"}, 
                        data = {"email" : "<EMAIL>", "sequence" : sequences })

if not request.ok:
  request.raise_for_status()
  sys.exit() 

print(request.text)
jobid = request.text<jupyter_output><empty_output><jupyter_text>It is easy to check the status of our job. Just run the code in the next cell.<jupyter_code>request = requests.get(server + 'status/' + jobid)
print(request.text)<jupyter_output><empty_output><jupyter_text>The API gives different data as results. You can use the next cell to understand the different result types and which ones you need to output.<jupyter_code>request = requests.get(server + 'resulttypes/' + jobid)

resulttypes = xml.dom.minidom.parseString(request.text)
resulttypes = resulttypes.toprettyxml()

print(resulttypes)<jupyter_output><empty_output><jupyter_text>Assign the kind of output you would want to display and then request the API to return it. You can change the outtype variable to any of the identifiers mentioned in the previous output.<jupyter_code>outtype = 'aln-clustal_num' 
request = requests.get(server + 'result/' + jobid + '/' + outtype)

print(request.text)
outtype = 'phylotree' 
request = requests.get(server + 'result/' + jobid + '/' + outtype)

print(request.text)
outtype = 'pim'
request = requests.get(server + 'result/' + jobid + '/' + outtype)

print(request.text)<jupyter_output><empty_output><jupyter_text>And you are done!We will check out some more tutorials, based on the other alignment algorithms!  KALIGNKALIGN is a very fast MSA tool that concentrates on local regions that is suitable for large alignments.<jupyter_code>server = 'https://www.ebi.ac.uk/Tools/services/rest/kalign/'
request = requests.get(server + 'parameters')

if not request.ok:
  request.raise_for_status()
  sys.exit() 

parameters = xml.dom.minidom.parseString(request.text)
parameters = parameters.toprettyxml()

print(parameters)
parameter = 'gapext'
request = requests.get(server + 'parameterdetails/' + parameter)

if not request.ok:
  request.raise_for_status()
  sys.exit() 

parameter_details = xml.dom.minidom.parseString(request.text)
parameter_details = parameter_details.toprettyxml()

print(parameter_details)
request = requests.post(server + 'run', 
                        headers = {"Content-Type" : "application/x-www-form-urlencoded", "Accept" : "text/plain"}, 
                        data = {"email" : "<EMAIL>", "sequence" : sequences, "stype" : "dna"})

if not request.ok:
  request.raise_for_status()
  sys.exit() 

print(request.text)
jobid = request.text
request = requests.get(server + 'status/' + jobid)
print(request.text)
request = requests.get(server + 'resulttypes/' + jobid)

resulttypes = xml.dom.minidom.parseString(request.text)
resulttypes = resulttypes.toprettyxml()

print(resulttypes)
outtype = 'out' 
request = requests.get(server + 'result/' + jobid + '/' + outtype)

print(request.text)
outtype = 'aln-clustalw' 
request = requests.get(server + 'result/' + jobid + '/' + outtype)

print(request.text)
outtype = 'phylotree' 
request = requests.get(server + 'result/ '+ jobid + '/' + outtype)

print(request.text)
outtype = 'pim'
request = requests.get(server + 'result/' + jobid + '/' + outtype)

print(request.text)<jupyter_output><empty_output><jupyter_text>MAFFTMAFFT is an MSA tool that uses Fast Fourier Transforms that is suitable for medium-large alignments.<jupyter_code>server = 'https://www.ebi.ac.uk/Tools/services/rest/mafft/'
request = requests.get(server + 'parameters')

if not request.ok:
  request.raise_for_status()
  sys.exit() 

parameters = xml.dom.minidom.parseString(request.text)
parameters = parameters.toprettyxml()

print(parameters)
parameter = 'gapext'
request = requests.get(server + 'parameterdetails/' + parameter)

if not request.ok:
  request.raise_for_status()
  sys.exit() 

parameter_details = xml.dom.minidom.parseString(request.text)
parameter_details = parameter_details.toprettyxml()

print(parameter_details)
request = requests.post(server + 'run', 
                        headers = {"Content-Type" : "application/x-www-form-urlencoded", "Accept" : "text/plain"}, 
                        data = {"email" : "<EMAIL>", "sequence" : sequences })

if not request.ok:
  request.raise_for_status()
  sys.exit() 

print(request.text)
jobid = request.text
request = requests.get(server + 'status/' + jobid)
print(request.text)
request = requests.get(server + 'resulttypes/' + jobid)

resulttypes = xml.dom.minidom.parseString(request.text)
resulttypes = resulttypes.toprettyxml()

print(resulttypes)
outtype = 'out'
request = requests.get(server + 'result/' + jobid + '/' + outtype)

print(request.text)
outtype = 'phylotree' 
request = requests.get(server + 'result/' + jobid + '/' + outtype)

print(request.text)
outtype = 'pim'
request = requests.get(server + 'result/' + jobid + '/' + outtype)

print(request.text)<jupyter_output><empty_output><jupyter_text>MUSCLEMUSCLE is an accurate MSA tool that is especially good with proteins. It is suitable for medium alignments.<jupyter_code>server = 'https://www.ebi.ac.uk/Tools/services/rest/muscle/'
request = requests.get(server + 'parameters')

if not request.ok:
  request.raise_for_status()
  sys.exit() 

parameters = xml.dom.minidom.parseString(request.text)
parameters = parameters.toprettyxml()

print(parameters)
parameter = 'tree'
request = requests.get(server + 'parameterdetails/' + parameter)

if not request.ok:
  request.raise_for_status()
  sys.exit() 

parameter_details = xml.dom.minidom.parseString(request.text)
parameter_details = parameter_details.toprettyxml()

print(parameter_details)
request = requests.post(server + 'run', 
                        headers = {"Content-Type" : "application/x-www-form-urlencoded", "Accept" : "text/plain"}, 
                        data = {"email" : "<EMAIL>", "sequence" : sequences})

if not request.ok:
  request.raise_for_status()
  sys.exit() 

print(request.text)
jobid = request.text
request = requests.get(server + 'status/' + jobid)
print(request.text)
request = requests.get(server + 'resulttypes/' + jobid)

resulttypes = xml.dom.minidom.parseString(request.text)
resulttypes = resulttypes.toprettyxml()

print(resulttypes)
outtype = 'out'
request = requests.get(server + 'result/' + jobid + '/' + outtype)

print(request.text)
outtype = 'aln-clustalw'
request = requests.get(server + 'result/' + jobid + '/' + outtype)

print(request.text)
outtype = 'phylotree'
request = requests.get(server + 'result/' + jobid + '/' + outtype)

print(request.text)
outtype = 'pim' 
request = requests.get(server + 'result/' + jobid + '/' + outtype)

print(request.text)<jupyter_output><empty_output><jupyter_text>PRANKPRANK is a phylogeny-aware multiple sequence alignment program which makes use of evolutionary information to help place insertions and deletions.<jupyter_code>server = 'https://www.ebi.ac.uk/Tools/services/rest/prank/'
request = requests.get(server + 'parameters')

if not request.ok:
  request.raise_for_status()
  sys.exit() 

parameters = xml.dom.minidom.parseString(request.text)
parameters = parameters.toprettyxml()

print(parameters)
parameter = 'gap_extension'
request = requests.get(server + 'parameterdetails/' + parameter)

if not request.ok:
  request.raise_for_status()
  sys.exit() 

parameter_details = xml.dom.minidom.parseString(request.text)
parameter_details = parameter_details.toprettyxml()

print(parameter_details)
request = requests.post(server + 'run', 
                        headers = {"Content-Type" : "application/x-www-form-urlencoded", "Accept" : "text/plain"}, 
                        data = {"email" : "<EMAIL>", "sequence" : sequences})

if not request.ok:
  request.raise_for_status()
  sys.exit() 

print(request.text)
jobid = request.text
request = requests.get(server + 'status/' + jobid)
print(request.text)
request = requests.get(server + 'resulttypes/' + jobid)

resulttypes = xml.dom.minidom.parseString(request.text)
resulttypes = resulttypes.toprettyxml()

print(resulttypes)
outtype = 'out'
request = requests.get(server + 'result/' + jobid + '/' + outtype)

print(request.text)
outtype = 'aln-hsaml'
request = requests.get(server + 'result/' + jobid + '/' + outtype)

print(request.text)
outtype = 'tree'
request = requests.get(server + 'result/' + jobid + '/' + outtype)

print(request.text)<jupyter_output><empty_output><jupyter_text>T-COFFEET-COFFEE is a consistency-based MSA tool that attempts to mitigate the pitfalls of progressive alignment methods. This is suitable for small alignments.<jupyter_code>server = 'https://www.ebi.ac.uk/Tools/services/rest/tcoffee/'
request = requests.get(server + 'parameters')

if not request.ok:
  request.raise_for_status()
  sys.exit() 

parameters = xml.dom.minidom.parseString(request.text)
parameters = parameters.toprettyxml()

print(parameters)
parameter = 'order'
request = requests.get(server + 'parameterdetails/' + parameter)

if not request.ok:
  request.raise_for_status()
  sys.exit() 

parameter_details = xml.dom.minidom.parseString(request.text)
parameter_details = parameter_details.toprettyxml()

print(parameter_details)
request = requests.post(server + 'run', 
                        headers = {"Content-Type" : "application/x-www-form-urlencoded", "Accept" : "text/plain"}, 
                        data = {"email" : "<EMAIL>", "sequence" : sequences})

if not request.ok:
  request.raise_for_status()
  sys.exit() 

print(request.text)
jobid = request.text
request = requests.get(server + 'status/' + jobid)
print(request.text)
request = requests.get(server + 'resulttypes/' + jobid)

resulttypes = xml.dom.minidom.parseString(request.text)
resulttypes = resulttypes.toprettyxml()

print(resulttypes)
outtype = 'out'
request = requests.get(server + 'result/' + jobid + '/' + outtype)

print(request.text)
outtype = 'aln-clustalw'
request = requests.get(server + 'result/' + jobid + '/' + outtype)

print(request.text)
outtype = 'tree'
request = requests.get(server + 'result/' + jobid + '/' + outtype)

print(request.text)
outtype = 'phylotree'
request = requests.get(server + 'result/' + jobid + '/' + outtype)

print(request.text)
outtype = 'pim' 
request = requests.get(server + 'result/' + jobid + '/' + outtype)

print(request.text)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12430497372229415, 'fraction_numerical': 7.616726331022927e-05, 'mean_word_length': 4.781594011448701, 'pattern_counts': {'":': 0, '<': 63, '<?xml version=': 0, '>': 63, 'https://': 6, 'lorem ipsum': 0, 'www.': 12, 'xml': 37}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '14', 'dir': 'jupyter-structured-clean-dedup', 'id': '523545', 'license': 'MIT', 'n_tokens_mistral': 4153, 'n_tokens_neox': 4049, 'n_words': 1021, 'path': 'Tutorial-09-analysis-multiple-alignment.ipynb', 'repo_name': 'WormBase/gsoc-2021-bioinformatics-notebooks', 'size': '13183'}