BioJava3:HowTo

This page is a work-in-progress, describing each of the key areas in which you might want to work with the new BioJava3 code. It is structured in the form of use-cases and is not a comprehensive resource. Sections will be added and updated as new modules are added and existing ones developed in more detail.

Symbols and Alphabets

A DNA sequence

All the examples in this section require the biojava-dna module.

Construction and basic manipulation

 String mySeqString = "ATCGatcgATCG"; // Note that you can use mixed-case strings.
 List

Reversing and Complementing DNA

 // All methods in this section modify the list in-place.
 List

Editing the sequence

 // Delete the second and third bases.
 List

A quality-scored DNA sequence

Constructing a quality-scored DNA sequence

 // Construct a default unscored DNA sequence with capacity for integer scoring.
 List

Iterating over the base/score pairs

 // A 1-indexed iterator and ListIterators are also available.
 for (Iterator<TaggedSymbol`> iter = scoredSeq.taggedSymbolIterator();` `      iter.hasNext(); ) {` `   TaggedSymbol`` taggedSym = iter.next();` `   Symbol sym = taggedSym.getSymbol();` `   Integer score = taggedSym.getTag();` `   // Change the score whilst we're at it.` `   taggedSym.setTag(6); // Updates the score to 6 in the original set of tagged scores.` ` }`

Iterating over the bases only

 // Use the default iterator.
 // A ListIterator is also available, as are 1-indexed iterators.
 Iterator

Iterating over the scores only

 // A ListIterator is also available, as are 1-indexed iterators.
 for (Iterator` iter = scoredSeq.tagIterator(); iter.hasNext(); ) {` `   Integer score = iter.next();` ` }`

File parsing and converting

FASTA

The examples in this section require the biojava-fasta module. The examples that deal with converting to/from DNA sequences also require the biojava-dna module.

Convenience wrapper classes are provided to make the parsing process simpler for the most common use-cases.

Parsing a FASTA file (the easy way)

 for (ThingParser` parser = ThingParserFactory.` `        getReadParser(FASTA.format, new File("/path/to/my/fasta.fa"));` `      parser.hasNext(); ) {` `   FASTA fasta = parser.next(); ` `   // fasta contains a complete FASTA record.` ` }` ` parser.close();`

Parsing a FASTA file (the hard way)

 FASTAReader reader = new FASTAFileReader(new File("/path/to/my/fasta.fa"));
 FASTABuilder builder = new FASTABuilder();
 for (ThingParser` parser = new ThingParser``(reader, builder);` `      parser.hasNext(); ) {` `   FASTA fasta = parser.next(); ` `   // fasta contains a complete FASTA record.` ` }` ` parser.close();`

Converting the FASTA sequence into DNA sequence

 List

Converting a DNA sequence back into FASTA

 FASTA fasta = new FASTA();
 fasta.setDescription("My Description Line");
 fasta.setSequence(SymbolListFormatter.formatSymbols(mySeq));

Writing a FASTA file (the easy way)

 ThingParser` parser = ThingParserFactory.` `   getWriteParser(FASTA.format, new File("/path/to/my/fasta.fa"), fasta);` ` parser.parseAll();` ` parser.close();`

Writing a FASTA file (the hard way)

 FASTAEmitter emitter = new FASTAEmitter(fasta);
 FASTAWriter writer = new FASTAFileWriter(new File("/path/to/new/fasta.fa"));
 ThingParser` parser = new ThingParser``(emitter, writer);` ` parser.parseAll();` ` parser.close();`