1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
|
/*
* This file is part of ALTER.
*
* ALTER is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* ALTER is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with ALTER. If not, see <http://www.gnu.org/licenses/>.
*/
package writer;
import java.util.LinkedHashSet;
import java.util.logging.Level;
import java.util.logging.Logger;
import types.DNA;
import types.Lengthable;
import types.MSA;
import types.Nucleotide;
import types.Protein;
import types.RNA;
import types.Sequence;
import types.Taxable;
import types.Type;
import types.Typeable;
/**
* Implements interface Writer for the NEXUS format.
* @author Daniel Gomez Blanco
* @version 1.1
*/
public class NexusWriter implements Writer
{
/**
* New line characters according to the given output OS.
*/
String nl;
/**
* Lowercase output.
*/
boolean lowerCase;
/**
* Sequential output.
*/
boolean sequential;
/**
* Output match characters.
*/
boolean match;
/**
* Logger to register information messages.
*/
Logger logger;
/**
* Class constructor.
* @param os Output operating system.
* @param lowerCase Lowercase output.
* @param sequential Sequential output.
* @param match Output match characters.
* @param logger Logger name.
*/
public NexusWriter(String os, boolean lowerCase, boolean sequential, boolean match, String logger)
{
if (os.equals("macos"))
nl = "\r";
else if (os.equals("linux"))
nl = "\n";
else
nl = "\r\n";
this.lowerCase = lowerCase;
this.sequential = sequential;
this.match = match;
this.logger = Logger.getLogger(logger);
}
/**
* Writes a MSA in NEXUS format.
* @param msa Input MSA.
* @return MSA in NEXUS format.
*/
public String write(MSA msa)
{
StringBuffer outb = new StringBuffer(STRING_BUFFER_MSALENGTH);
//Copy sequences and find longest ID
String[] data = new String[msa.getSeqs().size()];
LinkedHashSet<String> id = new LinkedHashSet<String>(msa.getSeqs().size());
int longestId = 0;
for (int i = 0; i < msa.getSeqs().size(); i++)
{
Sequence seq = (Sequence)msa.getSeqs().elementAt(i);
//Copy data
data[i] = getData(seq, (Sequence) msa.getSeqs().firstElement());
//Copy ID
String uid = getId(seq,id);
id.add(uid);
//Update longest ID
if (uid.length() > longestId)
longestId = uid.length();
}
//Intantiate number of sequences, length and type
int taxa = getTaxa(msa);
int length = getLength(msa);
String type = getType(msa);
//Write header
outb.append(writeHeader(taxa, length, type));
if (sequential)
outb.append(sequential(id, longestId, data));
else
outb.append(interleaved(id, longestId, data));
outb.append(writeFooter());
logger.log(Level.INFO, "MSA successfully converted to NEXUS format!");
return outb.toString();
}
/**
* Writes sequences of a MSA in sequential NEXUS.
* @param id Sequences identifiers.
* @param longestId Longest identifier length (needed to align).
* @param data Sequences data.
* @return Sequences in sequential NEXUS.
*/
private String sequential (LinkedHashSet<String> id, int longestId, String[] data)
{
StringBuffer outb = new StringBuffer(STRING_BUFFER_MSALENGTH);
//Write sequences
int i= 0;
for(String uid:id)
{
outb.append(writeSequence(uid, longestId, data[i]));
i++;
}
return outb.toString();
}
/**
* Writes sequences of a MSA in interleaved NEXUS.
* @param id Sequences identifiers.
* @param longestId Longest identifier length (needed to align).
* @param data Sequences data.
* @return Sequences in interleaved NEXUS.
*/
private String interleaved (LinkedHashSet<String> id, int longestId, String[] data)
{
StringBuffer outb = new StringBuffer(STRING_BUFFER_MSALENGTH);
//Write sequences
while (!data[0].isEmpty())
{
int i=0;
for(String uid:id)
{
outb.append(writeLine(uid, longestId, data, i));
i++;
}
outb.append(nl);
}
return outb.toString();
}
/**
* Returns the sequence identifier. In case the current identifier is
* repeated, it will be renamed.
* @param seq Sequence containing the identifier.
* @param ids Sequences copied so far.
* @return Unique sequence identifier.
*/
protected String getId(Sequence seq, LinkedHashSet<String> ids)
{
//Copiar identificador
String id = seq.getId();
//Si contiene espacios
if (id.contains(" ") || id.contains("\t"))
{
logger.log(Level.WARNING,"ID for sequence \"" + id
+ "\" contains blanks. Blanks replaced by \"_\"");
id = id.replaceAll("[\t ]","_");
}
//Si contiene caracteres destinados a comentarios
if (id.contains("[") || id.contains("]"))
{
logger.log(Level.WARNING,"ID for sequence \"" + id
+ "\" contains \"[\" or \"]\". Characters replaced by \"_\"");
id = id.replaceAll("\\[\\]","_");
}
return WriterUtils.getUniqueId(ids, id, logger.getName());
}
/**
* Returns the sequence data, using lowercase or match characters if
* necessary.
* @param seq Sequence containing the data.
* @param first First sequence in the MSA.
* @return Sequence data.
*/
protected String getData(Sequence seq, Sequence first)
{
String data;
if (match)
data = WriterUtils.writeMatch(seq, first);
else
data = seq.getData();
if (lowerCase)
return data.toLowerCase();
else
return data;
}
/**
* Returns the number of sequences in the MSA.
* @param msa MSA to get the number of sequences of.
* @return Number of sequences.
*/
protected int getTaxa(MSA msa)
{
if (msa instanceof Taxable)
return ((Taxable) msa).getTaxa();
else
return msa.getSeqs().size();
}
/**
* Returns the length of the sequences in the MSA.
* @param msa MSA to get the length of.
* @return Length of the sequences in the MSA.
*/
protected int getLength(MSA msa)
{
if (msa instanceof Lengthable)
return ((Lengthable) msa).getLength();
else
return ((Sequence) msa.getSeqs().firstElement()).getData().length();
}
/**
* Returns a string with the MSA type.
* @param msa MSA to get the type of.
* @return MSA type.
*/
protected String getType(MSA msa)
{
Type type = null;
if (msa instanceof Typeable)
type = ((Typeable) msa).getType();
if (type == null)
{
type = WriterUtils.inferType(msa);
if (type instanceof Nucleotide)
logger.log(Level.INFO,"Nucleotide MSA type inferred.");
else
logger.log(Level.INFO, "Protein MSA type inferred.");
}
if (type instanceof Protein)
return "PROTEIN";
else if (type instanceof Nucleotide)
if (type instanceof DNA)
return "DNA";
else if (type instanceof RNA)
return "RNA";
else
return "NUCLEOTIDE";
else
return "UNKNOWN";
}
/**
* Writes the NEXUS header.
* @param taxa Number of sequences.
* @param length Sequences length.
* @param type MSA type.
* @return NEXUS header.
*/
protected String writeHeader(int taxa, int length, String type)
{
String out = "#NEXUS" + nl;
out += "BEGIN DATA;" + nl;
out += "dimensions ntax=" + taxa + " nchar=" + length + ";" + nl;
out += "format missing=?" + nl;
out += "symbols=\"ABCDEFGHIKLMNOPQRSTUVWXYZ\"" + nl;
if (!sequential)
out += "interleave ";
out += "datatype=" + type + " gap=- match=.;" + nl + nl;
out += "matrix" + nl;
return out;
}
/**
* Writes NEXUS footer.
* @return NEXUS footer.
*/
protected String writeFooter()
{
return ";" + nl + "end;" + nl;
}
/**
* Writes a complete sequence.
* @param id Sequence identifier.
* @param longestId Longest identifier length.
* @param data Sequence data.
* @return Formatted sequence.
*/
protected String writeSequence(String id, int longestId, String data)
{
StringBuffer outb = new StringBuffer(STRING_BUFFER_SEQLENGTH);
outb.append(id + WriterUtils.align(id.length(),longestId)
+ WriterUtils.align(longestId, 10) + " ");
outb.append(data + nl);
return outb.toString();
}
/**
* Writes a line corresponding to a sequence of the MSA.
* @param id Sequence identifier.
* @param longestId Longest identifier length (needed to align).
* @param data Sequences data.
* @param index Index of the current sequence in the array (needed to
* update data).
* @return Line in NEXUS format.
*/
protected String writeLine(String id, int longestId, String[] data, int index)
{
StringBuffer outb = new StringBuffer(STRING_BUFFER_SEQLENGTH);
outb.append(id + WriterUtils.align(id.length(),longestId)
+ WriterUtils.align(longestId, 10) + " ");
//Escribir una linea si es posible
if (data[index].length() > 50)
{
outb.append(data[index].substring(0, 50));
data[index] = data[index].substring(50);
}
else
{
outb.append(data[index]);
data[index] = "";
}
outb.append(nl);
return outb.toString();
}
}
|