[gmx-users] other output format of Matrix Data than xpm format

Tsjerk Wassenaar tsjerkw at gmail.com
Thu Apr 5 10:17:21 CEST 2007


/* C proof e-mail ---

Hi Florian,

Sure. Here's an example for writing a matrix (real **mat) to an ascii
file or as a .pgm grayscale file with 1 (up to 256 levels) or 2 bytes
(up to 65536 levels). This file format is not very condensed, but is
very easy to handle. To process in python, you could have a look at
the module struct ( struct.unpack('B',...) and struct.unpack('H',...)

*/

/* Most simple is to just write a raw ascii file: */
void write_mat( char *fnm, int nx, int ny, real **mat )
{
    FILE *fp;
    int i,j;

    fp = fopen( fnm, "w" );
    for ( i=0; i<nx*DIM; i++ )
    {
        for ( j=0; j<ny*DIM; j++ )
            fprintf( fp, " %10.5f", mat[i][j] );
        fprintf( fp, "\n" );
    }
    fclose( fp );
}

/* For writing a .pgm you need some additional functionality */
/* First two helper functions to extract the minimum and maximum value
from an array for scaling */
real get_min( real **array, int n1, int n2 )
{
  real minval=1e12;
  int  i,j;
  for ( i=0; i<n1; i++ )
    for ( j=0; j<n2; j++ )
      if ( array[i][j] < minval )
        minval = array[i][j];
  return minval;
}

real get_max( real **array, int n1, int n2 )
{
  real maxval=-1e12;
  int  i,j;
  for ( i=0; i<n1; i++ )
    for ( j=0; j<n2; j++ )
      if ( array[i][j] > maxval )
        maxval = array[i];
  return maxval;
}

/* Then the pgm writing routine */
/* n1 = number of rows, n2 = number of columns */
/* depth sets the number of levels and should not exceed 65535 */
void write_pgm( char *filename, real **array, int n1, int n2, int depth )
{
    real minim,maxim;
    int i,j;
    FILE *pgm;
    int tmp;

    minim = get_min( array, n1*n2 );   /* Maps to     0 */
    maxim = get_max( array, n1*n2 ); /* Maps to depth */

    pgm = ffopen( filename, "w" );
    fprintf( pgm, "P5\n%d %d\n", n1, n2 );
    fprintf( pgm, "# Comments go before color depth of the image and
marked by #" );
    fprintf( pgm, "# Minimum value corresponds to %f \n", minim );
    fprintf( pgm, "# Maximum value corresponds to %f \n", maxim );
    fprintf( pgm, "%d\n", depth );
    for ( i=0; i<n1; i++ )
      for ( j=0; j<n2; j++ )
      {
        tmp = round(((array[i][j]-minim)/(maxim-minim))*depth);
        if ( depth > 255 )
        {
            fputc( tmp & 0xff, pgm );
            fputc( tmp >> 8, pgm );
        }
        else
            fputc( tmp, pgm );
      }
    fclose( pgm );
}

/* ==NOTES===================================== */
/* To use these in a program, add a line to t_filenm fnm[] such as:
  t_filenm fnm[] = {
    { efTRX, "-f",   NULL,   ffRDMULT},
    { efTPS, NULL,   NULL,   ffREAD  },
    { efNDX, NULL,   NULL,   ffOPTRD },
    { efPGM, "-o",  "matrix",  ffWRITE },          <<=== .pgm
    { efDAT, "-d",  "matrix",  ffWRITE }           <<=== .dat
  };
*/

/* Also, make sure that in include/types/filenm.h you add the new
filetype before efNR:
enum {
  efMDP, efGCT,
  efTRX, efTRN, efTRR, efTRJ, efXTC, efG87,
  efENX, efEDR, efENE,
  efSTX, efSTO, efGRO, efG96, efPDB, efBRK, efENT, efESP, efPQR,
  efLOG, efXVG, efOUT,
  efNDX,
  efTOP, efITP,
  efTPX, efTPS, efTPR, efTPA, efTPB,
#ifdef HAVE_LIBXML2
  efXML,
#endif
  efTEX, efRTP, efATP, efHDB,
  efDAT, efDLG,
  efMAP, efEPS, efMAT, efM2P,
  efMTX,
  efEDI, efEDO,
  efPPA, efPDO,
  efHAT,
  efXPM,
  efPGM,          <<===
  efNR
};
*/

/* Likewise, the corresponding list in src/gmxlib/filenm.c has to be modified:
static t_deffile deffile[efNR] = {
  { eftASC, ".mdp", "grompp", "-f", "grompp input file with MD parameters"   },
  { eftASC, ".gct", "gct",    "-f", "General coupling stuff"                 },
  ...SNIP...
  { eftASC, ".xpm", "root",   NULL, "X PixMap compatible matrix file"        },
  { eftBIN, ".pgm", "image",  NULL, "PGM image format (Grayscale)"
      }, <====
};
*/

/*
Of course it would be more helpful to put a wrapper around and
determine the file type from the extension. That shouldn't be a great
deal of work even...

Hope it helps... Best, */

/* Tsjerk */

/*
On 4/5/07, Florian Haberl <Florian.Haberl at chemie.uni-erlangen.de> wrote:
> Dear Tsjerk,
>
> On Thursday, 5. April 2007 06:50, Tsjerk Wassenaar wrote:
> > Hi Florian, Ran,
> >
> > Isabelles script deals with the inproduct matrices from g_anaeig.
> > Though it doesn't directly write out a matrix in ascii format, it
> > could be made to do so. You could use convert though to convert the
> > .xpm to (human readable) .ppm, which is easily further processed by a
> > script. Alternatively, it is very simple to add a bit of code to make
> > the tools write ascii matrices, or .ppm/.pgm for that matter, which
> > are also very simple to handle. To do so, you may have to add the new
> > file types in $GMXSRC/include/types/filenames.h and
> > $GMXSRC/src/gmxlib/filenames.c (follow the examples, and make sure to
> > insert the file types in the same place). For generic matrices I use
> > efDAT usually, which is already present. If you want more of an
> > example, just let me know.
>
> can you please send me more?
>
> Thanks and Greetings,
>
> Florian
>
> >
> > Best,
> >
> > Tsjerk
> >
> > On 4/4/07, Ran Friedman <r.friedman at bioc.unizh.ch> wrote:
> > > IIRC there's a script from Isabella Daidone to deal with the xpm of one
> > > of these programs. I don't remember more, but you can check the archives
> > > and try to work it out for other xpms.
> > >
> > > Ran.
> > >
> > > Berk Hess wrote:
> > > >> From: Florian Haberl <Florian.Haberl at chemie.uni-erlangen.de>
> > > >> Reply-To: Discussion list for GROMACS users <gmx-users at gromacs.org>
> > > >> To: Discussion list for GROMACS users <gmx-users at gromacs.org>
> > > >> Subject: [gmx-users] other output format of Matrix Data than xpm
> > > >> format Date: Wed, 4 Apr 2007 16:14:57 +0100
> > > >>
> > > >> Hi,
> > > >>
> > > >> is there a build in option or an easy way to change the output not to
> > > >> be xpm
> > > >> files? I only want a matrix of the numbers and not plotted as xpm
> > > >> file.
> > > >>
> > > >> I only found
> > > >>
> > > >> http://www.gromacs.org/pipermail/gmx-developers/2004-April/000866.html
> > > >>
> > > >> Affected tools like
> > > >>
> > > >> g_rms
> > > >> g_anaeig
> > > >
> > > > Currently not...
> > > > We want to add an option to all tools that write xpm to
> > > > optionally write an ascii (or binary) matrix.
> > > >
> > > > Berk.
> > > >
> > > > _________________________________________________________________
> > > > Live Search, for accurate results! http://www.live.nl
> > > >
> > > > _______________________________________________
> > > > gmx-users mailing list    gmx-users at gromacs.org
> > > > http://www.gromacs.org/mailman/listinfo/gmx-users
> > > > Please search the archive at http://www.gromacs.org/search before
> > > > posting!
> > > > Please don't post (un)subscribe requests to the list. Use the www
> > > > interface or send it to gmx-users-request at gromacs.org.
> > > > Can't post? Read http://www.gromacs.org/mailing_lists/users.php
> > >
> > > --
> > > ------------------------------------------------------
> > > Ran Friedman
> > > Postdoctoral Fellow
> > > Computational Structural Biology Group (A. Caflisch)
> > > Department of Biochemistry
> > > University of Zurich
> > > Winterthurerstrasse 190
> > > CH-8057 Zurich, Switzerland
> > > Tel. +41-44-6355593
> > > Email: r.friedman at bioc.unizh.ch
> > > Skype: ran.friedman
> > > ------------------------------------------------------
> > >
> > > _______________________________________________
> > > gmx-users mailing list    gmx-users at gromacs.org
> > > http://www.gromacs.org/mailman/listinfo/gmx-users
> > > Please search the archive at http://www.gromacs.org/search before
> > > posting! Please don't post (un)subscribe requests to the list. Use the
> > > www interface or send it to gmx-users-request at gromacs.org.
> > > Can't post? Read http://www.gromacs.org/mailing_lists/users.php
>
> --
> -------------------------------------------------------------------------------
>  Florian Haberl
>  Computer-Chemie-Centrum
>  Universitaet Erlangen/ Nuernberg
>  Naegelsbachstr 25
>  D-91052 Erlangen
>  Telephone:     +49(0) − 9131 − 85 26581
>  Mailto: florian.haberl AT chemie.uni-erlangen.de
> -------------------------------------------------------------------------------
>


-- 
Tsjerk A. Wassenaar, Ph.D.
Junior UD (post-doc)
Biomolecular NMR, Bijvoet Center
Utrecht University
Padualaan 8
3584 CH Utrecht
The Netherlands
P: +31-30-2539931
F: +31-30-2537623

*/



More information about the gromacs.org_gmx-users mailing list