# Computing Sum, Mean, Variance, Standard Deviation, Coefficient of Variation, Smallest, Biggest, Median, Range, and Mode using C++

This is a programming tutorial for computing sum, mean, variance, standard deviation, coefficient of variation, smallest number, biggest number, median, range, and mode of n numbers using C++. In this tutorial, I have used Dev C++ v5.11 software for compiling the C++ program.

The formula for calculating sum, mean, variance, standard deviation, coefficient of variation, smallest number, biggest number, median, range, and mode of set of n numbers is given below:

Description Formula
Number of Samples n
Sum (Total) $\sum\limits_{i=1}^{n}{{{x}_{i}}}={{x}_{1}}+{{x}_{2}}+...+{{x}_{n}}$
Mean (Average) $\bar{x}=\frac{\sum\limits_{i=1}^{n}{{{x}_{i}}}}{n}$
Variance ${{\operatorname{var}}_{x}}=\frac{\sum\limits_{i=1}^{n}{{{({{x}_{i}}-\bar{x})}^{2}}}}{n-1}$
Standard Deviation ${{\sigma }_{x}}=\sqrt{\frac{\sum\limits_{i=1}^{n}{{{({{x}_{i}}-\bar{x})}^{2}}}}{n-1}}$
Coefficient of Variance $c{{v}_{x}}=\frac{{{\sigma }_{x}}}{{\bar{x}}}\times 100%$
Minimum (Smallest Number) x1
Maximum (Biggest Number) xn
Median (Middle) $m{{d}_{x}}={{x}_{\left( \frac{n-1}{2} \right)}},\ if\ n\ is\ odd$
$m{{d}_{x}}=\frac{{{x}_{\left( \frac{n}{2} \right)}}+{{x}_{\left( \frac{n}{2}+1 \right)}}}{2},\ if\ n\ is\ even$
Mode (Most Common) frequencymax

#### Source Code

// Sum, Mean, Variance, Standard Deviation, Coefficient of Variation, Smallest, Biggest, Median, Range, and Mode.
#include <iostream>
#include <sstream>
#include <string>
#include <conio.h>
#include <math.h>
#include <iomanip>
using namespace std;
string n2s(double y)
{
std::stringstream p;
std::string q;
p << y;
p >> q;
return q;
}
int main()
{
int i, j, n, x, max, mn;
double t, a[20], b[20], s[20], s1, z, m, cv, dev, var, sum, mean, median, range, sd;
string mode;
sum = x = dev = max = mn = range = 0;
mode = "";

cout << "\nSum, Mean, Variance, Standard Deviation, Coefficient of Variation, Smallest, Biggest, Median, Range, and Mode\n\n";
cout << "\nHow many numbers? ";
cin >> n;
cout << "\nEnter the numbers: ";
for (i = 0; i < n; i++) {
cin >> a[i];
s[i] = a[i];
sum += a[i];
}
mean = sum / n;
system("cls");
cout << "\nInput Numbers: ";
for (i = 0; i < n; i++) {
i < n-1 ? cout << a[i] << ", " : cout << "and " << a[i] << endl;
dev += pow((a[i] - mean), 2);
}
var = dev / (n - 1);
sd = sqrt(var);
cv = (sd / mean) * 100;
for (i = 0; i < n; i++) {
for (j = i + 1; j < n; j++) {
if (s[i] > s[j]) {
t = s[i];
s[i] = s[j];
s[j] = t;
}
}
}
s1 = s[0];
cout << "\nSorted Numbers: ";
for(i = 0; i < n; i++) {
i < n-1 ? cout << s[i] << ", " : cout << "and " << s[i] << endl;
b[i+1] = s[i];
}
range = s[n-1] - s[0];
cout << "\n" << setw(29) << "Total Numbers : " << n << endl;
cout << "\n" << setw(29) << "Sum (Total) : " << sum << endl;
cout << "\n" << setw(29) << "Mean (Average) : " << mean << endl;
cout << "\n" << setw(29) << "Variance (\xE5\xFD) : " << var << endl;
cout << "\n" << setw(29) << "Standard Deviation : " << sd << endl;
cout << "\n" << setw(29) << "Coefficient of Variation : " << cv << " \%" << endl;
if (n%2 == 0) {
median = (b[n/2] + b[(n/2)+1]) / 2;
} else {
median = b[(n+1)/2];
}
cout << "\n" << setw(29) << "Smallest Number : " << s[0] << endl;
cout << "\n" << setw(29) << "Biggest Number : " << s[n-1] << endl;
cout << "\n" << setw(29) << "Median (Middle) : " << median << endl;
cout << "\n" << setw(29) << "Range (Biggest - Smallest) : " << range << endl;
cout << "\n" << setw(29) << "Mode (Most Common) : ";
for (i = 0; i <= n; i++) {
z = s[i];
if (z == s1) {
x += 1;
if (x > max) {
m = s1;
max = x;
mn++;
mode = n2s(m);
}
} else {
if (x == max &&  m != s1) {
mode += ", " + n2s(s1);
mn++;
}
s1 = z;
x = 1;
}
}
if (n == mn) {
cout << "No Mode Found" << endl;
} else {
cout << mode << endl;
}
getch();
}


# Simple Server and Client Chat using Python

In Python language, socket (or network socket) is a module used to communicate between two computers. It provides two types of interface to access the network, namely low-level (platform dependent connections — Example: Telnet) and high-level (application dependent connections — Example: HTTP, FTP, SMTP, etc.). This is a simple tutorial to establish the low-level socket connection between server and client to communicate messages using the TCP/IP protocol.

### Server and Client Chat

In this tutorial, I have used two scripts server.py to serve data by sending an invitation to the client, and client.py to receive data on acceptance of the invitation. After accepting the invitation, both server and client share messages mutually.

#### — Server —

An server script performs the sequence of functions such as socket(), bind(), listen(), and accept() (repeats for more than one client) to communicate with the client. The description of each functions used in the server script are given bellow:

• socket() – creates a socket using the address family, socket type and protocol
• bind() – binds the socket to the given address (host name, and port number pair)
• listen() – enables a server to accept connections from the client(s)
• accept() – waits and accepts connection request from the client(s)
• gethostname() – retrieves host name of the machine
• gethostbyname() – translates a host name to IPv4 format address
• recv() – receives message sent through TCP
• decode() – decodes the message using the codec
• send() – sends message sent through TCP
For more details on methods, functions, module, and objects, refer to the official site of Python (python.org).

#### Source Code

# server.py
import time, socket, sys

print("\nWelcome to Chat Room\n")
print("Initialising....\n")
time.sleep(1)

s = socket.socket()
host = socket.gethostname()
ip = socket.gethostbyname(host)
port = 1234
s.bind((host, port))
print(host, "(", ip, ")\n")
name = input(str("Enter your name: "))

s.listen(1)
print("\nWaiting for incoming connections...\n")

s_name = conn.recv(1024)
s_name = s_name.decode()
print(s_name, "has connected to the chat room\nEnter [e] to exit chat room\n")
conn.send(name.encode())

while True:
message = input(str("Me : "))
if message == "[e]":
message = "Left chat room!"
conn.send(message.encode())
print("\n")
break
conn.send(message.encode())
message = conn.recv(1024)
message = message.decode()
print(s_name, ":", message)


#### — Client —

An client script performs the sequence of functions such as socket(), and connect() to communicate with the server. The description of each functions used in the server script are given bellow:

• socket() – creates a socket using the address family, socket type and protocol
• connect() – connects to a server socket at address
For more details on methods, functions, module, and objects, refer to the official site of Python (python.org).

#### Source Code

# client.py
import time, socket, sys

print("\nWelcome to Chat Room\n")
print("Initialising....\n")
time.sleep(1)

s = socket.socket()
host = socket.gethostname()
ip = socket.gethostbyname(host)
port = 1234
s.bind((host, port))
print(host, "(", ip, ")\n")
name = input(str("Enter your name: "))

s.listen(1)
print("\nWaiting for incoming connections...\n")

s_name = conn.recv(1024)
s_name = s_name.decode()
print(s_name, "has connected to the chat room\nEnter [e] to exit chat room\n")
conn.send(name.encode())

while True:
message = input(str("Me : "))
if message == "[e]":
message = "Left chat room!"
conn.send(message.encode())
print("\n")
break
conn.send(message.encode())
message = conn.recv(1024)
message = message.decode()
print(s_name, ":", message)


### Implementation Steps

1. Run the server.py script in Python application.

2. Note down the local IP address and pass it to the clients (invitation). Alternatively, you can also get the local IP address through online using L-IP tool.

3. Run the client.py script in Python application using the local IP address sent by the server (accept the invitation).

4. Sent and/or receive messages from the server/client mutually.

# Retrieving Windows Product Key using VB Script

In this article, I present the simple and safe method to retrieve product key of Windows operating system using VB Script. The VB Script reads the value of Windows product from the Registry Editor (regedit) and translates it to a formatted product key (25 alphanumeric characters). Also, it creates the backup of the product information to the local drive (Desktop).

### Instructions

Step 1: Create a VB Script file “WinProductKey.vbs” using any ASCII text editor and enter the following codes.

#### Source Code

Dim objshell, path, DigitalID
Set objshell = CreateObject("WScript.Shell")
'Set registry key path
Path = "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\"
'Registry key value
Dim ProductName, ProductID, ProductKey, ProductData
'Get ProductName, ProductID, ProductKey
ProductName = "Product Name: " & objshell.RegRead(Path & "ProductName")
ProductID = "Product ID: " & objshell.RegRead(Path & "ProductID")
ProductKey = "Product Key: " & ConvertToKey(DigitalID)
ProductData = ProductName  & vbNewLine & ProductID  & vbNewLine & ProductKey
'Show messbox if save to a file
If vbYes = MsgBox(ProductData & vblf & vblf & "Do you want to save it to Desktop?", vbYesNo, "Microsoft OS Product Key Information") then
Save ProductData
End If

'Convert Binary to Characters
Function ConvertToKey(Key)
Const KeyOffset = 52
Dim Maps, i, j, Current, KeyOutput, Last, keypart1, insert
i = 24
Maps = "BCDFGHJKMPQRTVWXY2346789"
Do
Current = 0
j = 14
Do
Current = Current* 256
Current = Key(j + KeyOffset) + Current
Key(j + KeyOffset) = (Current \ 24)
Current = Current Mod 24
j = j -1
Loop While j >= 0
i = i -1
KeyOutput = Mid(Maps, Current+1, 1) & KeyOutput
Last = Current
Loop While i >= 0
keypart1 = Mid(KeyOutput, 2, Last)
insert = "N"
KeyOutput = Replace(KeyOutput, keypart1, keypart1 & insert, 2, 1, 0)
If Last = 0 Then KeyOutput = insert & KeyOutput
ConvertToKey = Mid(KeyOutput, 1, 5) & "-" & Mid(KeyOutput, 6, 5) & "-" & Mid(KeyOutput, 11, 5) & "-" & Mid(KeyOutput, 16, 5) & "-" & Mid(KeyOutput, 21, 5)
End Function

'Save the data to a file
Function Save(Data)
Dim fso, fName, txt, objshell, UserName
Set objshell = CreateObject("wscript.shell")
'Get current user name
'Create a text file on desktop
fName = "C:\Users\" & UserName & "\Desktop\WinKeyInfo.txt"
Set fso = CreateObject("Scripting.FileSystemObject")
Set txt = fso.CreateTextFile(fName)
txt.Writeline Data
txt.Close
End Function

Step 2: Double-click the “WinProductKey.vbs” file to run the VB Script (image shown at top).

Step 3: Click “Yes” button present near the prompt (“Do you want to save it to Desktop?”), to backup the information to the Desktop.

# Installing Latest Apache Web Server on Windows 10

This tutorial describes step-by-step instructions to install the latest Apache HTTP Server software (latest stable version is 2.4.32, as on date 18 March 2018) on Windows 10 operating system.

Apache HTTP Server (“Apache” and “httpd”) was launched in 1995 and it has been the most popular web server on the Internet since April 1996. The Apache HTTP Server Project is a collaborative software development effort aimed at creating a robust, commercial-grade, featured, and freely-available source code implementation of an HTTP (Web) server. This project is part of the Apache Software Foundation.

The latest released version of Apache HTTP Server Project is Apache HTTP Server 2.4.29 (as on date 18 March 2018). This version of Apache is the latest GA release of the new generation 2.4.x branch of Apache HTTPD and represents fifteen years of innovation by the project, and is recommended over all previous releases.

The Apache HTTP Server Project itself does not provide binary releases of software, only source code. Individual contributors may provide binary packages as a convenience, but it is not a release deliverable. If you cannot able to compile the Apache HTTP Server yourself, you can obtain a binary package from numerous binary distributions available on the Internet. The final and official binary release of Apache HTTP Server software is Apache HTTP Server 2.2.25, which can be download from https://archive.apache.org/dist/httpd/binaries/win32/.

There are popular options suggested by Apache for deploying Apache HTTPD, and, optionally, PHP and MySQL, on Microsoft Windows, include:

For more details on Apache installation in windows OS, refer to the official site of Apache (apache.org).

### Step-by-Step Instruction

2. Decompress the downloaded file (“httpd-2.4.32-o102n-x64-vc14.zip”) and copy the folder “Apache24” to the “C:” drive.

The “Apache24” directory consists of major sub-directories such as “bin” (binary files), “htdocs” (web pages), “conf” (configuration files), “cgi-bin” (CGI server scripts), “error” (web template files for displaying server errors), “icons” (Apache server web page theme images), and other sub-directories/help files.

3. Open the “bin” sub-directory and run the “ApacheMonitor” binary with administrator privileges.

4. If you have done step 3 successfully, the “ApacheMonitor” icon appears in the notification area (bottom-right corner of the taskbar). Right-click the icon and select “Open Apache Monitor” from the menu.

5. Select “Apache2.4” from the “Service Status:” and click “Start” button to run the Apache HTTP Server. If the process is successful, a small green icon appears near the list “Apache2.4” in the “Service Status:”. Click “OK” button to hide the window.

6. Open the web browser and enter the IP “http://127.0.0.1“ or URL “http://localhost” in the address bar to open the test page of Apache HTTP Server. You will be greeted by the following page.

We can also test SSL (HTTPS) by entering the IP to “https://127.0.0.1” or URL “https://localhost” in the address bar to open the test page of Apache HTTP Server with SSL enabled.

# Simple WebServer using Python

This is a simple example to develop your own Python web server used for application development or testing Python script and should not be used on a public network. It works similar to the Apache web server with limited functionalities and not intended to be a full-featured web server. This tutorial demonstrates just to activate the built-in web server through a batch file.

### Instructions

For developing a Python web server, we simply need Python software alone. The batch file (.bat) was created to activate the built-in web server, automatically open the default web browser with custom folder as root directory (www), and minimize the command line window while running the Python script.

Step 2: Create a new folder named www (optional, your path to the webpages). Also, create a new file index.html in the www folder with the following line “<h1>It Works!</h1>” and save it.

Step 3: Create an batch file PythonServer.bat in the www folder using any ASCII text editor and enter the following commands.

START /min python -S localhost 80 START "" http://localhost

Step 4: Double-click PythonServer.bat file to run web server and launch the web browser.

# Simple WebServer using PHP

This is a simple example to design your own PHP web server used for application development or testing PHP script and should not be used on a public network. It works similar to the Apache web server with limited functionalities and not intended to be a full-featured web server. This tutorial demonstrates just to activate the built-in web server through a batch file.

### Instructions

For designing a PHP web server, we simply need PHP software alone. The batch file(.bat) was created to activate the built-in web server, automatically open the default web browser with custom folder as root directory (www), and minimize the command line window while running the PHP script.

Step 2: Create a new folder www in the extracted folder. Also, create a new file index.html in the www folder with the following line “<h1>It Works!</h1>” and save it.

Step 3: Create an batch file PHPServer.bat in the extracted folder using any ASCII text editor and enter the following commands.

START /min php -S localhost:88 -t www/ START "" http://localhost:88

Step 4: Double-click PHPServer.bat file to run web server and launch the web browser.

# Frequency Plot of Protein Sequence using PHP and R

A frequency plot is a graphical data analysis technique for summarizing the distributional information of a variable. The response variable is divided into equal sized intervals (or bins). The number of occurrences of the response variable is calculated for each bin. In this tutorial, the number of occurrences of each amino acids in the protein sequence (response variable) is calculated and sorted in ascending order.

The frequency plot then consists of:

Vertical Axis = Amino acids
Horizontal Axis = Frequencies of the amino acids

There are 4 types of frequency plots:

1. Frequency plot (absolute counts);
2. Relative frequency plot (convert counts to proportions);
3. Cumulative frequency plot;
4. Cumulative relative frequency plot.

The frequency plot and the histogram have the same information except the frequency plot has lines connecting the frequency values, whereas the histogram has bars at the frequency values.

### Frequency plot using PHP and R

In this tutorial, the programming language R, PHP, and BioConductor packages SeqinR & Biostrings are used to generate a frequency plot from the protein sequence. SeqinR is used to read or manipulate sequences, and Biostrings is used to convert sequence to array. The PHP language is used to execute Rscript at background using exec() function of PHP and the image generated through R (Rscript) is retrieved and displayed through the IMG HTML tag. The execution process acts similar to PHP/CGI or Perl/CGI. For generating a frequency plot, we need a protein sequence in .fasta|.fas file format as input. A simple protocol for generating a frequency plot is given below:

Step 3: Create an R script as given bellow using an ASCII editor (Eg. Notepad) and save it with .R file extension. Here args function is used to get path of the FASTA file formatted protein sequence through command line.

#### R Source Code (Freq.R)

args <- commandArgs(TRUE) fas_file <- args[1] library("seqinr") library("Biostrings") seqfile <- read.fasta(file = fas_file) fastaseq <- seqfile[[1]] seqstring <- c2s(fastaseq) seqstring <- toupper(seqstring) seqchar <- s2c(seqstring) tab <- table(seqchar) taborder <- tab[order(tab)] names(taborder) <- aaa(names(taborder)) png(filename="freq.png", width=500, height=500) dotchart(taborder, pch=19, main="Frequency of Amino Acids", xlab="Frequency", ylab="Amino Acid") dev.off()

Step 4: Create an PHP program to get protein sequence and execute the Rscript in commandline using exec() function. Example, exec("\"C:\Program Files\R\R-3.3.1\bin\Rscript.exe\" Freq.R input"); #### PHP Source Code (Freq.PHP) <!DOCTYPE html"> <html xmlns="https://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Frequency Plot using PHP and R</title> <style type="text/css"> html { box-sizing: border-box; -webkit-text-size-adjust: 100%; -webkit-font-smoothing: antialiased; } html, body { height: 100%; margin: 0px; padding: 0px; } .form{ position: absolute; top: 50%; left: 50%; width: 510px; height: 320px; margin-top: -160px; /* Half the height */ margin-left: -255px; /* Half the width */ vertical-align: middle; border: 1px solid blue; box-shadow: 0px 0px 3px #ccc, 0 10px 15px #eee inset; border-radius: 2px; font-family: "Times New Roman", Georgia, Serif; } .output{ position: absolute; top: 50%; left: 50%; width: 512px; height: 584px; margin-top: -292px; /* Half the height */ margin-left: -256px; /* Half the width */ vertical-align: middle; border: 1px solid blue; font-size: 12px; box-shadow: 0px 0px 3px #ccc, 0 10px 15px #eee inset; border-radius: 2px; } .space { padding: 10px; } .effect { border: 1px solid #aaa; box-shadow: 0px 0px 3px #ccc, 0 10px 15px #eee inset; border-radius: 2px; } .heffect { border: 0; height: 1px; background: blue; } a { text-decoration: none; } </style> </head> <body> <?php if(isset(_POST['submit'])) { function img2data($image) {$type = pathinfo($image, PATHINFO_EXTENSION);$data = file_get_contents($image); return 'data:image/' .$type . ';base64,' . base64_encode($data); }$input = pathinfo($_FILES['seqfile']['tmp_name'], PATHINFO_FILENAME) . ".fasta"; move_uploaded_file($_FILES['seqfile']['tmp_name'], $input); exec("\"C:\Program Files\R\R-3.3.1\bin\Rscript.exe\" Freq.R$input"); $s = img2data("freq.png"); unlink("freq.png"); unlink($input); ?> <div class='output'> <h2 style="text-align: center;">Frequency Plot using PHP and R</h2> <hr class='heffect' /> <div class='space'><?php print "<img src='" . s . "' />"; ?></div> </div> <?php } else { ?> <div class="form"> <h3 style="text-align: center;">Frequency Plot using PHP and R</h3> <hr class="heffect" /> <form action="" method="post" name="freqform" enctype= "multipart/form-data"> <p style="padding: 0 10px 0 10px;"> Upload protein sequence (<i>fasta file</i>) <input class="effect" type="file" name="seqfile" /> </p> <p style="padding: 0 10px 0 10px;"> <input class="effect" type="reset" name="reset" value="Reset" /> <input class="effect" type="submit" name="submit" value="Generate Plot" /> </p> </form> </div> <?php } ?> </body> </html> #### Program Output ## Friday, September 29, 2017 # Local NCBI BLAST+ in WebServer - Easy Steps This is a simple tutorial which explains how to design your own web interface for NCBI BLAST+ to perform local and online database search using PHP in webserver. The PHP library locBLAST executes the NCBI BLAST+ programs using exec() function through passing parameters from the HTML form fields. In locBLAST, two list boxes were used to select program & database, and text area & file upload is used to input query sequence in the FASTA file format. A FASTA file validation function is included to validate the query sequence before executing the BLAST programs. The locBLAST PHP library and test database files were freely available at GitHub. ### Requirements for locBLAST Setup In this tutorial, I have given a brief explanation about embedding the latest NCBI BLAST+ (the latest version of NCBI BLAST+ as on September 29, 2017 is 2.60.) in any PHP enabled web server. The latest version of NCBI BLAST+ (standalone command-line BLAST programs) can be downloaded from the FTP server of NCBI (ftp://ftp.ncbi.nih.gov/blast/executables/blast+/LATEST). A simple PHP supporting web server is enough to run the local BLAST program through the web browser. Follow the steps given below to setup and run the locBLAST program. 1. Download the compressed NCBI BLAST+ software (I have downloaded ncbi-blast-2.6.0+-x64-win64.tar.gz for Windows 64-bit OS) from the NCBI FTP server. 2. Create a folder named locBLAST in the web directory (usually named htdocs, www, or wwwroot). 3. Decompress ncbi-blast-2.6.0+-x64-win64.tar.gz file and copy all the files present in the bin folder to locBLAST directory. 4. Download index.php and db folder from GitHub and copy to locBLAST directory. 5. Open the URL http://localhost/locBLAST in the web browser to run locBLAST program. 6. Click the hyperlink DEMO and Search button to test local database search. The default parameters will generate an HTML file formatted BLAST search report. The form fields in locBLAST program can be extended with custom form fields to perform advanced search. Moreover, desired database files can be downloaded from the NCBI FTP directory (ftp://ftp.ncbi.nih.gov) and copied to the db directory to perform offline database search. ### Custom Database Creation We can also create custom database using makeblastdb program present in the locBLAST directory. To create a custom database, copy the FASTA file formatted multiple sequence file to the db directory. Then open the db directory and press ALT key + Right mouse click to open command-line window directing to the current directory. Enter the command “makeblastdb.exe -in db/pdt.fas -out db/pdt -dbtype prot -title PDTDB” to create a custom database named PDTDB (already included in the db folder). The FASTA file formatted multiple sequence file (pdt.fas)is given below: >pdt|pdtdbt00001|pdb|1OKE|A/B MRCIGISNRDFVEGVSGGSWVDIVLEHGSCVTTMAKNKPTLDFELIKTEAKQPATLRKYC IEAKLTNTTTESRCPTQGEPTLNEEQDKRFVCKHSMVDRGWGNGCGLFGKGGIVTCAMFT CKKNMEGKIVQPENLEYTVVITPHSGEEHAVGNDTGKHGKEVKITPQSSITEAELTGYGT VTMECSPRTGLDFNEMVLLQMKDKAWLVHRQWFLDLPLPWLPGADTQGSNWIQKETLVTF KNPHAKKQDVVVLGSQEGAMHTALTGATEIQMSSGNLLFTGHLKCRLRMDKLQLKGMSYS MCTGKFKVVKEIAETQHGTIVIRVQYEGDGSPCKIPFEIMDLEKRHVLGRLITVNPIVTE KDSPVNIEAEPPFGDSYIIIGVEPGQLKLNWFKK >pdt|pdtdbt00002|pdb|4O6B|A/B AHHHHHHSSGVDLGTENLYFQSNADSGCVVSWKNKELKCGSGIFITDNVHTWTEQYKFQP ESPSKLASAI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GVV DATTFQSVCYQYVDTLYPGFEGTEMWNPNRELSEDCLYLNVWTPYPRPTSPTPVLVWIYG GGFYSGASSLDVYDGRFLVQAERTVLVSMNYRVGAFGFLALPGSREAPGNVGLLDQRLAL QWVQENVAAFGGDPTSVTLFGESAGAASVGMHLLSPPSRGLFHRAVLQSGAPNGPWATVG MGEARRRATQLAHLVGCPPGGTGGNDTELVACLRTRPAQVLVNHEWHVLPQESVFRFSFV PVVDGDFLSDTPEALINAGDFHGLQVLVGVVKDEGSYFLVYGAPGFSKDNESLISRAEFL AGVRVGVPQVSDLAAEAVVLHYTDWLHPEDPARLREALSDVVGDHNVVCPVAQLAGRLAA QGARVYAYVFEHRASTLSWPLWMGVPHGYEIEFIFGIPLDPSRNYTAEEKIFAQRLMRYW ANFARTGDPNEPRDPKAPQWPPYTAGAQQYVSLDLRPLEVRRGLRAQACAFWNRFLPKLL SAT ### Brief Video Tutorial ## Tuesday, September 26, 2017 # Extracting Multiple FASTA Sequences using PHP This is a simple tutorial which explains how to safely extract multiple sequences from a FASTA file using PHP script. I have used four functions to perform different tasks: read_fas_file() - to check invalid file, fas_check() - to check FASTA file format, get_seq() - to retrieve sequence and sequence name pairs, and fas_get() - to extract and display multiple FASTA file formatted sequences. The full source code and multiple protein sequences in FASTA file format used in this tutorial is given below. ### Source Code function read_fas_file(x) { // Check for Empty File
if (!file_exists($x)) { print "File Not Exist!!"; exit(); } else {$fh = fopen($x, 'r'); if (filesize($x) == 0) {
print "File is Empty!!";
fclose($fh); exit(); } else {$f = fread($fh, filesize($x));
fclose($fh); return$f;
}
}
}

function fas_check($x) { // Check FASTA File Format$gt = substr($x, 0, 1); if ($gt != ">") {
print "Not FASTA File!!";
exit();
} else {
return $x; } } function get_seq($x) { // Get Sequence and Sequence Name
$fl = explode(PHP_EOL,$x);
$sh = trim(array_shift($fl));
if($sh == null) {$sh = "UNKNOWN SEQUENCE";
}
$fl = array_filter($fl);
$seq = ""; foreach($fl as $str) {$seq .= trim($str); }$seq = strtoupper($seq);$seq = preg_replace("/[^ACDEFGHIKLMNPQRSTVWY]/i", "", $seq); if ((count($fl) < 1) || (strlen($seq) == 0)) { print "Sequence is Empty!!"; exit(); } else { return array($sh, $seq); } } function fas_get($x) { // Read Multiple FASTA Sequences
$gtr = substr($x, 1);
$sqs = explode(">",$gtr);
if (count($sqs) > 1) { foreach ($sqs as $sq) {$spair = get_seq($sq);$spairs[$spair[0]] =$spair[1];
}
return $spairs; } else {$spair = get_seq($gtr); return array($spair[0] => $spair[1]); } }$file = "pdt.fas";
$content = read_fas_file($file);
$fasta = fas_check($content);
$seq = fas_get($fasta);
foreach($seq as$x => $y) { print$x . "\n" . \$y;
print "\n\n";
}

### FASTA File (pdt.fas)

>pdt|pdtdbt00001|pdb|1OKE|A/B
MRCIGISNRDFVEGVSGGSWVDIVLEHGSCVTTMAKNKPTLDFELIKTEAKQPATLRKYC
IEAKLTNTTTESRCPTQGEPTLNEEQDKRFVCKHSMVDRGWGNGCGLFGKGGIVTCAMFT
CKKNMEGKIVQPENLEYTVVITPHSGEEHAVGNDTGKHGKEVKITPQSSITEAELTGYGT
KNPHAKKQDVVVLGSQEGAMHTALTGATEIQMSSGNLLFTGHLKCRLRMDKLQLKGMSYS
MCTGKFKVVKEIAETQHGTIVIRVQYEGDGSPCKIPFEIMDLEKRHVLGRLITVNPIVTE
KDSPVNIEAEPPFGDSYIIIGVEPGQLKLNWFKK
>pdt|pdtdbt00002|pdb|4O6B|A/B
ESPSKLASAIQKAHEEGICGIRSVTRLENLMWKQITPELNHILSENEVKLTIMTGDIKGI
MQAGKRSLRPQPTELKYSWKTWGKAKMLSTESHNQTFLIDGPETAECPNTNRAWNSLEVE
FIEVKNCHWPKSHTLWSNGVLESEMIIPKNLAGPVSQHNYRPGYHTQITGPWHLGKLEMD
FDFCDGTTVVVTEDCGNRGPSLRTTTASGKLITEWCCRSCTLPPLRYRGEDGCWYGMEIR
PLKEKEENLVNSLVTA
>pdt|pdtdbt00003|pdb|2IOK|A/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QAHEQRLQEVEAEVAATGTYQLRESELVFGAKQAWRNAPRCV
GRIQWGKLQVFDARDCRSAQEMFTYICNHIKYATNRGNLRSAITVFPQRCPGRGDFRIWN
SQLVRYAGYRQQDGSVRGDPANVEITELCIQHGWTPGNGRFDVLPLLLQAPDEPPELFLL
PPELVLEVPLEHPTLEWFAALGLRWYALPAVSNMLLEIGGLEFPAAPFSGWYMSTEIGTR
NLCDPHRYNILEDVAVCMDLDTRTTSSLWKDKAAVEINVAVLHSYQLAKVTIVDHHAATA
KGTGITR
>pdt|pdtdbt00008|pdb|4PQE|A
EGREDAELLVTVRGGRLRGIRLKTPGGPVSAFLGIPFAEPPMGPRRFLPPEPKQPWSGVV
DATTFQSVCYQYVDTLYPGFEGTEMWNPNRELSEDCLYLNVWTPYPRPTSPTPVLVWIYG
GGFYSGASSLDVYDGRFLVQAERTVLVSMNYRVGAFGFLALPGSREAPGNVGLLDQRLAL
QWVQENVAAFGGDPTSVTLFGESAGAASVGMHLLSPPSRGLFHRAVLQSGAPNGPWATVG
MGEARRRATQLAHLVGCPPGGTGGNDTELVACLRTRPAQVLVNHEWHVLPQESVFRFSFV
PVVDGDFLSDTPEALINAGDFHGLQVLVGVVKDEGSYFLVYGAPGFSKDNESLISRAEFL
AGVRVGVPQVSDLAAEAVVLHYTDWLHPEDPARLREALSDVVGDHNVVCPVAQLAGRLAA
QGARVYAYVFEHRASTLSWPLWMGVPHGYEIEFIFGIPLDPSRNYTAEEKIFAQRLMRYW
ANFARTGDPNEPRDPKAPQWPPYTAGAQQYVSLDLRPLEVRRGLRAQACAFWNRFLPKLL
SAT

# Simple Show/Hide Text using JavaScript

This is a simple tutorial which explains how to show or hide lines in a paragraph using JavaScript. The concept is to split the lines in the paragraph into two sections and show/hide the second section by clicking a hyperlinked text (to create a toggle effect). By default, the first section in the paragraph is set to always visible, and a hyperlink at the end of the line with onclick action to display the second section. Similarly, a hyperlink at the end of the line of the second section with onclick action is displayed after a mouse click event to hide the second section.

### Example

This is a simple tutorial which explains how to show or hide lines in a paragraph using JavaScript. <a style='text-decoration: none' id='show_line' href='javascript:void(0)' onclick="document.getElementById('more').style.display = 'inline'; document.getElementById('hide_line').style.display = 'inline'; document.getElementById('show_line').style.display = 'none';">show more</a><span id='more' style='display: none;'>The concept is to split the lines in the paragraph into two sections and show/hide the <i>second section</i> by clicking a hyperlinked text (to create a toggle effect). By default, the <i>first section</i> in the paragraph is set to always visible, and a hyperlink at the end of the line with <kbd>onclick</kbd> action to display the <i>second section</i>. Similarly, a hyperlink at the end of the line of the <i>second section</i> with <kbd>onclick</kbd> action is displayed after a mouse click event to hide the <i>second section</i>. </span><a style='text-decoration: none; display: none' id='hide_line' href='javascript:void(0)' onclick="document.getElementById('more').style.display = 'none'; document.getElementById('show_line').style.display = 'inline'; document.getElementById('hide_line').style.display = 'none';">hide less</a>

#### Demo

This is a simple tutorial which explains how to show or hide lines in a paragraph using JavaScript. show more