src/org/gel/mauve/backbone/BackboneListBuilder.java

Go to the documentation of this file.
00001 package org.gel.mauve.backbone;
00002 
00003 import java.io.BufferedReader;
00004 import java.io.File;
00005 import java.io.IOException;
00006 import java.util.Arrays;
00007 import java.util.Properties;
00008 import java.util.StringTokenizer;
00009 import java.util.Vector;
00010 
00011 import org.gel.mauve.Genome;
00012 import org.gel.mauve.XMFAAlignment;
00013 import org.gel.mauve.XmfaViewerModel;
00014 import org.gel.mauve.format.FileFinder;
00015 
00016 public class BackboneListBuilder {
00017         public static File getFileByKey (XmfaViewerModel model, XMFAAlignment xmfa, String key) {
00018                 Properties meta = xmfa.metadata;
00019                 // Find the backbone data
00020                 String bb_fname;
00021                 if (meta.containsKey (key)) {
00022                         bb_fname = meta.getProperty (key);
00023                 } else {
00024                         return null; // no backbone information
00025                 }
00026         bb_fname = FileFinder.findFile(model, bb_fname);
00027                 File src = new File (bb_fname);
00028                 if (!src.canRead ()) {
00029                         return null; // can't read the backbone file
00030                 }
00031                 return src;
00032         }
00033 
00034         public static BackboneList build (XmfaViewerModel model, XMFAAlignment xmfa)
00035                         throws IOException {
00036                 int sequenceCount = model.getSequenceCount ();
00037                 File src = getFileByKey (model, xmfa, "BackboneFile");
00038                 if (src == null)
00039                         return null;
00040                 Vector bbvect = new Vector ();
00041                 BufferedReader br = new BufferedReader (new java.io.FileReader (src));
00042                 String cur_line;
00043                 StringTokenizer stoke = null;
00044                 while (br.ready ()) {
00045                         cur_line = br.readLine ();
00046                         stoke = new StringTokenizer (cur_line);
00047                         String tok = stoke.nextToken ();
00048                         int lcb_id = Integer.parseInt (tok);
00049                         tok = stoke.nextToken ();
00050                         long left_col = Long.parseLong (tok) - 1; // left col starts at 1
00051                         // in the file
00052                         tok = stoke.nextToken ();
00053                         long length = Long.parseLong (tok);
00054                         boolean seqs[] = new boolean [sequenceCount];
00055                         for (int sI = 0; sI < sequenceCount; ++sI)
00056                                 seqs[sI] = false;
00057                         while (stoke.hasMoreTokens ()) {
00058                                 tok = stoke.nextToken ();
00059                                 int seq = Integer.parseInt (tok);
00060                                 seqs[seq] = true;
00061                         }
00062                         Backbone bb = new Backbone ();
00063                         bb.setLcbIndex (lcb_id);
00064                         bb.setLeftColumn (left_col);
00065                         bb.setLength (length);
00066                         bb.setSeqs (seqs);
00067                         long lends[] = new long [seqs.length];
00068                         boolean lend_gaps[] = new boolean [seqs.length];
00069                         xmfa.getColumnCoordinates (model, lcb_id, left_col, lends,
00070                                         lend_gaps);
00071                         long rends[] = new long [seqs.length];
00072                         boolean rend_gaps[] = new boolean [seqs.length];
00073                         xmfa.getColumnCoordinates (model, lcb_id, left_col + length - 1,
00074                                         rends, rend_gaps);
00075                         for (int sI = 0; sI < seqs.length; ++sI) {
00076                                 if (!seqs[sI]) {
00077                                         lends[sI] = 0;
00078                                         rends[sI] = 0;
00079                                 } else {
00080                                         if (rends[sI] < lends[sI]) {
00081                                                 long tmp = lends[sI];
00082                                                 lends[sI] = rends[sI];
00083                                                 rends[sI] = tmp;
00084                                         }
00085                                 }
00086                         }
00087                         bb.setLeftEnd (lends);
00088                         bb.setRightEnd (rends);
00089 
00090                         bbvect.addElement (bb);
00091                 }
00092                 Backbone [] bb_array = new Backbone [bbvect.size ()];
00093                 bb_array = (Backbone []) bbvect.toArray (bb_array);
00094 
00095                 // now, for each genome create subarrays and sort on lcb id
00096                 Vector seq_bb_vect = new Vector ();
00097                 for (int gI = 0; gI < model.getSequenceCount (); ++gI) {
00098                         int bb_count = 0;
00099                         for (int bbI = 0; bbI < bb_array.length; ++bbI) {
00100                                 if (bb_array[bbI].getSeqs ()[gI])
00101                                         bb_count++;
00102                         }
00103                         Backbone [] seq_bb = new Backbone [bb_count];
00104                         bb_count = 0;
00105                         for (int bbI = 0; bbI < bb_array.length; ++bbI) {
00106                                 if (bb_array[bbI].getSeqs ()[gI])
00107                                         seq_bb[bb_count++] = bb_array[bbI];
00108                         }
00109                         // now sort on LCB id
00110                         Genome g = model.getGenomeBySourceIndex (gI);
00111                         BackboneList.BbLeftEndComparator comp = new BackboneList.BbLeftEndComparator (g);
00112                         Arrays.sort (seq_bb, comp);
00113                         seq_bb_vect.addElement (seq_bb);
00114                 }
00115                 BackboneList bb_list = new BackboneList ();
00116                 bb_list.setXmfa (xmfa);
00117                 bb_list.setBackbone (bb_array);
00118                 bb_list.setSeqBackbone (seq_bb_vect);
00119                 return bb_list;
00120         }
00121 }

Generated on Mon Aug 19 06:03:40 2013 for Mauve by doxygen 1.3.6