For Unix based OS (no guarantee under Windows).
I downloaded portions of ZINC database in mol2 format. Each part contains 20000 ligands. I would like to split each one into 100 ligands chunks. I know that with openbabel one can do
babel -i zinc_part.mol2 a.mol2 -m
but the chunks are always individual files. Later one could of course concatenate them, but this can be a mess. I wonder if there are other babel options or another approaches to divided a large mol2 files into chunks of N ligands each.
3 answers
One way could be to take this Perl script to split a file, and add this to the top:
$/ = "@<TRIPOS>MOLECULE";
This would instruct Perl to treat each record as one "line". But you may have to fiddle around with this to make sure your output is valid.
Tobias Kind gave 20 solutions on the Blue Obelisk eXchange website.
I just fell on the thread and wanted to share my PERL script splitting multi-molecular ZINC mol2 files into single files named ZINCxxxxxxxx.mol2. Command line : perl separe_ligands.pl -f <filename>
#!/usr/bin/perl
use strict;
use warnings;
use Cwd;
my $file="";
my @ligands=();
my $mol2="";
my $workdir=getcwd;
for my $i (0..@ARGV-1)
{
if ($ARGV[$i] eq "-f") {++$i;$file = $ARGV[$i];}
if ($ARGV[$i] eq "-w") {++$i;$workdir .= $ARGV[$i];}
}
open IN, "$file";
while (<IN>)
{
my $line = $_;
chomp($line);
if ($line =~ m/@<TRIPOS>MOLECULE/ && $mol2 eq "")
{
$mol2.=$line."\n";
}
elsif ($line =~ m/@<TRIPOS>MOLECULE/ && $mol2 ne "" )
{
my $outfile.=pop(@ligands).".mol2";
open OUT, ">$outfile";
print OUT "$mol2";
close OUT;
$mol2=$line."\n";
}
else
{
if($line =~ m/ZINC/) {push(@ligands,$line);}
$mol2.=$line."\n";
chomp($line);
}
}
close IN;
exit;
Log in to answer this question.