src/org/gel/mauve/contigs/FastAContigChangeWriter.java

Go to the documentation of this file.
00001 package org.gel.mauve.contigs;
00002 
00003 import java.io.File;
00004 import java.io.FileNotFoundException;
00005 import java.io.FileOutputStream;
00006 import java.io.IOException;
00007 import java.io.PrintStream;
00008 import java.util.ArrayList;
00009 import java.util.Collections;
00010 import java.util.HashSet;
00011 import java.util.Hashtable;
00012 import java.util.Iterator;
00013 import java.util.LinkedList;
00014 
00015 import org.biojava.bio.seq.ComponentFeature;
00016 import org.biojava.bio.seq.Sequence;
00017 import org.biojava.bio.seq.SequenceTools;
00018 import org.biojava.bio.seq.io.FastaFormat;
00019 import org.biojava.bio.seq.io.SeqIOTools;
00020 import org.biojava.bio.seq.io.StreamWriter;
00021 import org.biojava.bio.symbol.IllegalAlphabetException;
00022 import org.gel.mauve.Chromosome;
00023 import org.gel.mauve.Genome;
00024 import org.gel.mauve.LcbViewerModel;
00025 import org.gel.mauve.MauveConstants;
00026 import org.gel.mauve.MauveHelperFunctions;
00027 
00028 public class FastAContigChangeWriter implements MauveConstants {
00029         
00030         protected Genome genome;
00031         protected Hashtable inverters;
00032         protected Sequence seq;
00033         protected PrintStream out;
00034         protected LinkedList ordered;
00035         protected Hashtable conflicts;
00036         protected Hashtable all_contigs;
00037         protected Hashtable nexts;
00038         protected PrintStream out2;
00039         protected FastaFormat format;
00040         protected StreamWriter writer;
00041         protected StreamWriter writer2;
00042         
00043         public FastAContigChangeWriter (ContigReorderer central) {
00044                 genome = central.fix;
00045                 inverters = central.inverters;
00046                 ordered = central.ordered;
00047                 conflicts = central.conflicts;
00048                 nexts = central.nexts;
00049                 File dir = central.directory;
00050                 if (central instanceof ContigRenamer) {
00051                         format = new ChangedFastaFormat (((ContigRenamer) central).names);
00052                 }
00053                 else
00054                         format = new FastaFormat ();
00055                 boolean print_extra = false;
00056                 try {
00057                         String file = MauveHelperFunctions.genomeNameToFasta (genome);
00058                         out = new PrintStream (new FileOutputStream (new File (
00059                                         dir, file).getAbsolutePath ()));
00060                         if (print_extra) {
00061                                 file = "extra_" +file;
00062                                 out2 = new PrintStream (new FileOutputStream (new File (
00063                                                 dir, file).getAbsolutePath ()));
00064                                 writer2 = new StreamWriter (out2, format);
00065                         }
00066                         writer = new StreamWriter (out, format);
00067                 } catch (FileNotFoundException e1) {
00068                         e1.printStackTrace();
00069                 }
00070                 try {
00071                         all_contigs = getContigFeatures (genome);
00072                         writeContigs (ordered.iterator (), false);
00073                         System.out.println ("done with ordered. " + all_contigs.size());
00074                         ArrayList list = new ArrayList (conflicts.keySet ());
00075                         Collections.sort (list);
00076                         writeContigs (list.iterator (), true);
00077                         System.out.println ("done with conflicted. " + all_contigs.size());
00078                         list = new ArrayList (all_contigs.keySet ());
00079                         Collections.sort (list);
00080                         for (int i = 0; i < list.size(); i++)
00081                                 ordered.add(genome.getChromosomeAt(((ComponentFeature)
00082                                                 all_contigs.get(list.get(i))).getLocation().getMin())); 
00083                         writeContigs (list.iterator (), true, print_extra);
00084                         System.out.println ("done with unordered. " + all_contigs.size());
00085                 } catch (Exception e) {
00086                         e.printStackTrace ();
00087                 }
00088                 out.flush ();
00089                 out.close ();
00090                 if (print_extra) {
00091                         out2.flush ();
00092                         out2.close ();
00093                 }
00094         }
00095         
00096         public void writeContigs (Iterator itty, boolean key) {
00097                 writeContigs (itty, key, false);
00098         }
00099         public void writeContigs (Iterator itty, boolean key, boolean print_extra) {
00100                 if (!itty.hasNext())
00101                         return;
00102                 Long start = new Long (-1);
00103                 Object obj = null;
00104                 ListSequenceIterator list = new ListSequenceIterator ();
00105                 ListSequenceIterator list2 = new ListSequenceIterator ();
00106                 long total = 0;
00107                 try {
00108                         while (true) {
00109                                 obj = nexts.remove(start);
00110                                 if (obj != null)
00111                                         System.out.println ("got from table: " + obj);
00112                                 if (obj == null && itty.hasNext())
00113                                         obj = itty.next();
00114                                 if (obj == null)
00115                                         break;
00116                                 if (obj instanceof Chromosome)
00117                                         start = new Long (((Chromosome) obj).getStart ());
00118                                 else
00119                                         start = (Long) obj;
00120                                 //System.out.println ("start: " + start);
00121                                 ComponentFeature feat = (ComponentFeature) all_contigs.remove (start);
00122                                 if (feat == null) {
00123                                         continue;
00124                                 }
00125                                 Chromosome chrom = (Chromosome) inverters.get (start);
00126                                 Sequence seq = null;
00127                                 if (chrom != null) {
00128                                         seq = SequenceTools.reverseComplement (
00129                                                         feat.getComponentSequence ());
00130                                 }
00131                                 else {
00132                                         seq = feat.getComponentSequence ();
00133                                         if (print_extra) {
00134                                                 total += feat.getLocation ().getMax () - feat.getLocation ().getMin ();
00135                                                 //SeqIOTools.writeFasta (out2, feat.getComponentSequence ());
00136                                                 list2.add (feat.getComponentSequence ());
00137                                         }
00138                                 }
00139                                 //SeqIOTools.writeFasta (out, seq);
00140                                 list.add (seq);
00141                         }
00142                         writer.writeStream (list);
00143                         if (out2 != null)
00144                                 writer2.writeStream (list2);
00145                 } catch (IllegalAlphabetException e) {
00146                         e.printStackTrace();
00147                 } catch (IOException e) {
00148                         e.printStackTrace();
00149                 }
00150                 System.out.println ("unmatched length: " + total);
00151         }
00152 
00153 /*System.out.println ("a: " + feat.getComponentSequence ().seqString ());
00154 System.out.println ("b: " + two.seqString ());
00155 ComponentFeature.Template template = (ComponentFeature.Template) 
00156 feat.makeTemplate ();
00157 template.componentSequence = two;
00158 seq.removeFeature (feat);
00159 seq.createFeature (template);*/
00160         
00161         public static Hashtable getContigFeatures (Genome genome) {
00162                 Sequence seq = genome.getAnnotationSequence();
00163                 Iterator itty = seq.features();
00164                 Hashtable all_contigs = new Hashtable (seq.countFeatures());
00165                 while (itty.hasNext()) {
00166                         Object obj = itty.next ();
00167                         if (obj instanceof ComponentFeature) {
00168                                 ComponentFeature feat = (ComponentFeature) obj;
00169                                 all_contigs.put(new Long (feat.getLocation().getMin()), feat);
00170                         }
00171                 }
00172                 return all_contigs;
00173         }
00174 
00175 }

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