Greetings!

The below is an example of a text file from PDB.


ATOM 2 CA ASP E 423 -50.931 46.011 -16.062 1.00153.24 C
ATOM 3 C ASP E 423 -51.314 44.692 -15.381 1.00152.70 C
ATOM 4 O ASP E 423 -52.175 44.673 -14.497 1.00153.08 O
ATOM 5 CB ASP E 423 -51.993 46.400 -17.101 1.00153.44 C
ATOM 6 CG ASP E 423 -53.276 46.931 -16.464 1.00154.12 C
ATOM 7 OD1 ASP E 423 -53.246 48.037 -15.879 1.00156.34 O
ATOM 8 OD2 ASP E 423 -54.318 46.247 -16.557 1.00154.62 O
ATOM 9 N LEU E 424 -50.675 43.600 -15.805 1.00151.30 N
ATOM 10 CA LEU E 424 -50.864 42.271 -15.208 1.00149.56 C
ATOM 11 C LEU E 424 -50.526 42.277 -13.715 1.00147.96 C


As you can see, the highlighted numerals are the X,Y & Z coordinates. How do I store them in an array, row by row so that I can multiply them using matrix (translation/rotation). The format of the file remains the same(.pdb) which can be opened in notepad++.

Dani AI

Generated

The ATOM lines in the example from are a fixed-width format, so the most robust approach is to parse columns rather than rely on whitespace. was right to point at substring/split — prefer substring using the PDB column positions for X/Y/Z (1-based): X = cols 31–38, Y = 39–46, Z = 47–54. That avoids mistakes when occupancy/B-factor spacing gets mangled in text viewers.

A straightforward parse-and-store workflow (line-by-line):

  1. only process lines that start with "ATOM" or "HETATM";
  2. check line length >= 54;
  3. extract the three coordinate substrings, trim and Double.parseDouble them;
  4. store points as 4-component arrays [x,y,z,1.0] (homogeneous coords) so 4x4 transform matrices apply directly.

Example extraction (Java):

// assume 'line' starts with "ATOM" or "HETATM" and line.length() >= 54
double x = Double.parseDouble(line.substring(30, 38).trim());
double y = Double.parseDouble(line.substring(38, 46).trim());
double z = Double.parseDouble(line.substring(46, 54).trim());
double[] point = { x, y, z, 1.0 };   // store rows like this in a List<double[]>
points.add(point);

Apply a 4x4 transform (this assumes column-vector convention: p' = M * p):

double[] transformPoint(double[][] M, double[] p) {
  double[] r = new double[4];
  for (int i = 0; i < 4; i++) {
    r[i] = 0.0;
    for (int j = 0; j < 4; j++) r[i] += M[i][j] * p[j];
  }
  return r;
}

Notes and troubleshooting: validate line length before substringing; catch NumberFormatException for malformed values; if the file deviates from fixed-width use a regex fallback to locate three floats on the line. ’s idea of a Coordinates object is fine for clarity, but for bulk matrix math a primitive double[]/double[][] layout is simpler and faster. For larger transforms or many atoms, consider a linear-algebra library (Apache Commons Math, EJML) to avoid reimplementing matrix routines.

Recommended Answers

All 4 Replies

Read the values store/retrieve from Coordinates object

public class Coordinates {
  private double x;
  private double y;
  private double z;
  
  public Coordinates(){}
  
  public Coordinates(double x, double y, double z){
    setX(x);
    setY(y);
    setZ(z);
  }
  
  private void setX(double x){
    this.x = x;
  }
  
  public double getX(){
    return x;
  }
  
  private void setY(double y){
    this.y = y;
  }
  
  public double getY(){
    return y;
  }
  
  private void setZ(double z){
    this.z = z;
  }
  
  public double getZ(){
    return z;
  }
}

Use something more flexible then array like List, ArrayList or perhaps Vector

List<Coordinates> coordinates = new ArrayList<Coordinates>();
//read values from file
coordinates.add(new Coordinates(x,y,z));
//rest of code

Thank you, but how do I extract them and then store in the array? Should I use parse/scanner/tokenizer??

Hm, two posters from Singapore asking about the same problem set. What are the odds. Good catch, Tong.

Hazeel - what you use to solve this sort of problem depends largely on the sort of data you have and how it's defined. If there's a fixed-width format, you can read substrings based on character position (see String.substring()). If there's a consistent separator character (or a regex that describes a good separator) you can use split (String.split()) - I like that one when I can use it, because it gives me a nice array to play with. My last resort would be to use a tokenizer to get one instance at a time. I'd only use that if there's a toally free format and you need to do actual parsing, looking at each item and determining what you're doing. You don't need to do that here. (although obviously you'll need a scanner or a BufferedReader or something to get the data from the file in the first place)


Start by getting your lines from the file. Then read up on the String methods (substring and split) and pick one. (I'd suggest split, myself, in this instance) Then try playing with them and see what you get.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.