#!/usr/bin/perl use strict; # sort_mol2_bonds.pl - a script to reorder the listing in a .mol2 @<TRIPOS>BOND # section so that the following conventions are preserved: # 1. Atoms on each line are in increasing order (e.g. 1 2 not 2 1) # 2. The bonds appear in order of ascending atom number # 3. For bonds involving the same atom in the first position, the bonds appear # in order of ascending second atom # # Written by: Justin Lemkul ([email protected]) # # Distributed under the GPL-3.0 license unless (scalar(@ARGV)==2) { die "Usage: perl sort_mol2_bonds.pl input.mol2 output.mol2\n"; } my $input = $ARGV[0]; my $output = $ARGV[1]; open(IN, "<$input") || die "Cannot open $input: $!\n"; my @in = <IN>; close(IN); # test for header lines that some scripts produce unless($in[0] =~ /TRIPOS/) { die "Nonstandard header found: $in[0]. Please delete header lines until the TRIPOS molecule definition.\n"; } open(OUT, ">$output") || die "Cannot open $output: $!\n"; # get number of atoms and number of bonds from mol2 file my @tmp = split(" ", $in[2]); my $natom = $tmp[0]; my $nbond = $tmp[1]; # check print "Found $natom atoms in the molecule, with $nbond bonds.\n"; # print out everything up until the bond section my $i=0; while (!($in[$i] =~ /BOND/)) { print OUT $in[$i]; $i++; } # print the bond section header line to output print OUT $in[$i]; $i++; # read in the bonds and sort them my $bondfmt = "%6d%6d%6d%5s\n"; my @tmparray; # sort the bonds - e.g. the one that has the # lowest atom number in the first position and then the # lowest atom number in the second position (swap if necessary) for (my $j=0; $j<$nbond; $j++) { my @tmp = split(" ", $in[$i+$j]); # parse atom numbers my $ai = $tmp[1]; my $aj = $tmp[2]; # reorder if second atom number < first if ($aj < $ai) { $ai = $tmp[2]; $aj = $tmp[1]; } # store new lines in a temporary array $tmparray[$j] = sprintf($bondfmt, $tmp[0], $ai, $aj, $tmp[3]); } # loop over tmparray to find each atom number my $nbond = 0; for (my $x=1; $x<=$natom; $x++) { my @bondarray; my $ntmp = scalar(@tmparray); for (my $b=0; $b<$ntmp; $b++) { my @tmp = split(" ", $tmparray[$b]); if ($tmp[1] == $x) { push(@bondarray, $tmparray[$b]); splice(@tmparray, $b, 1); $ntmp--; $b--; } } if (scalar(@bondarray) > 0) # some atoms will only appear in $aj, not $ai { my $nbondarray = scalar(@bondarray); if ($nbondarray > 1) { # loop over all bonds, find the one with lowest $aj # and then print it for (my $y=0; $y<$nbondarray; $y++) { my @tmp2 = split(" ", $bondarray[$y]); my $tmpatom = $tmp[2]; my $lowindex = 0; if ($tmp2[2] < $tmpatom) { $lowindex = $y; } my $keep = splice(@bondarray, $lowindex, 1); $y--; $nbondarray--; my @sorted = split(" ", $keep); $nbond++; printf OUT $bondfmt, $nbond, $sorted[1], $sorted[2], $sorted[3]; } } else { $nbond++; my @tmp2 = split(" ", $bondarray[0]); printf OUT $bondfmt, $nbond, $tmp2[1], $tmp2[2], $tmp2[3]; } } } close(OUT); exit;
Write, Run & Share Perl code online using OneCompiler's Perl online compiler for free. It's one of the robust, feature-rich online compilers for Perl language, running on the latest version 5.22.1. Getting started with the OneCompiler's Perl compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as Perl
and start coding.
OneCompiler's Perl online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample Perl program which takes name as input and prints hello message with your name.
my $name = <STDIN>;
print "Hello $name.\n";
Perl(Practical Extraction and Report Language) is especially desined for text processing by Larry Wall.
There is no need to specify the type of the data in Perl as it is loosely typed language.
Type | Description | Usage |
---|---|---|
Scalar | Scalar is either a number or a string or an address of a variable(reference) | $var |
Arrays | Array is an ordered list of scalars, you can access arrays with indexes which starts from 0 | @arr = (1,2,3) |
Hash | Hash is an unordered set of key/value pairs | %ul = (1,'foo', 2, 'bar) |
In Perl, there is no need to explicitly declare variables to reserve memory space. When you assign a value to a variable, declaration happens automatically.
$var-name =value; #scalar-variable
@arr-name = (values); #Array-variables
%hashes = (key-value pairs); # Hash-variables
If, If-else, Nested-Ifs are used when you want to perform a certain set of operations based on conditional expressions.
if(conditional-expression){
//code
}
if(conditional-expression){
//code if condition is true
}else{
//code if condition is false
}
if(condition-expression1){
//code if above condition is true
}else if(condition-expression2){
//code if above condition is true
}
else if(condition-expression3){
//code if above condition is true
}
...
else{
//code if all the conditions are false
}
There is no case or switch in perl, instead we use given and when to check the code for multiple conditions.
given(expr){
when (value1)
{//code if above value is matched;}
when (value2)
{//code if above value is matched;}
when (value3)
{//code if above value is matched;}
default
{//code if all the above cases are not matched.}
}
For loop is used to iterate a set of statements based on a condition.
for(Initialization; Condition; Increment/decrement){
// code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while(condition) {
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition);
Sub-routines are similar to functions which contains set of statements. Usually sub-routines are written when multiple calls are required to same set of statements which increases re-usuability and modularity.
sub subroutine_name
{
# set of Statements
}
subroutine_name();
subroutine_name(arguments-list); // if arguments are present