---
title: 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`` mySeq = SymbolListFormatter.parseSymbolList(mySeqString);`
`Â `
` // Is it a big list? Don't want to hold it all in memory? Use an iterator instead.`
` for (Iterator`` myIterator = SymbolListFormater.parseSymbols(mySeqString);`
`Â Â Â Â Â Â myIterator.hasNext();Â )Â {`
`   Symbol sym = myIterator.next();`
`Â }`
`Â Â `
` // You can now use any List method, from Java Collections, to manipulate the list of bases.`
`Â `
` // The List returned is actually a SymbolList, you can cast it to get some bio-specific`
` // functions that work with 1-indexed positions as opposed to Java's default 0-indexed positions.`
`Â `
` SymbolList symList = (SymbolList)mySeq;  `
` Symbol symA = symList.get(0); // The first symbol, List-style.`
` Symbol symB = symList.get_bio(1) ; // The first symbol, bio-style. `
` if (symA==symB) { // Symbols are singletons, so == will work if they are identical including case.`
`Â Â Â System.out.println("Identical!");`
`Â }`
`Â `
` // Instead of using equals() or == to compare symbols, use the alphabet of your choice to`
` // compare them in multiple ways. It will return different values depending on whether one`
` // is a gap and the other isn't, whether they match exactly, or if they're the same symbol`
` // but in a different case, etc.`
` Alphabet dna = DNATools.DNA_ALPHABET;`
` SymbolMatchType matchType = dna.getSymbolMatchType(Symbol.get("A"), Symbol.get("a"));`
### Reversing and Complementing DNA
` // All methods in this section modify the list in-place.`
` List`` mySeq = SymbolListFormatter.parseSymbolList("ATCG");`
`Â `
`Â //Â Reverse.`
` // Method A.`
` Collections.reverse(mySeq); // Using Java Collections.`
` // Method B.`
`Â DNATools.reverse(mySeq);Â //Â DNATools-style.`
`Â `
`Â //Â Complement.`
`Â DNATools.complement(mySeq);`
`Â `
`Â //Â Reverse-complement.`
`Â DNATools.reverseComplement(mySeq);`
`Â Â Â `
` // Reverse only the third and fourth bases, 0-indexed list style?`
` Collections.reverse(mySeq.subList(2,4)); // Java Collections API.`
`Â Â Â `
` // Do the same, 1-indexed bio style?`
`Â Collections.reverse(((SymbolList)mySeq).subList_bio(3,5));`
### Editing the sequence
` // Delete the second and third bases.`
` List`` mySeq = SymbolListFormatter.parseSymbolList("ATCG");`
`Â mySeq.subList(1,3).clear();`
`Â `
` // Remove only 2nd base, bio-style.`
`Â ((SymbolList)mySeq).remove_bio(2);`
`Â `
` // Get another sequence and insert it after the 1st base.`
` List`` otherSeq = SymbolListFormatter.parseSymbolList("GGGG");`
` mySeq.addAll(1, otherSeq);`
A quality-scored DNA sequence
-----------------------------
### Constructing a quality-scored DNA sequence
` // Construct a default unscored DNA sequence with capacity for integer scoring.`
` List`` mySeq = SymbolListFormatter.parseSymbolList("ATCG");`
` TaggedSymbolList`` scoredSeq = new TaggedSymbolList``(mySeq);`
`Â `
` // Tag all the bases with the same score of 5.`
` scoredSeq.setTagRange(0, scoredSeq.length(), 5);`
`Â `
` // Tag just the 3rd base (0-indexed) with a score of 3.`
` scoredSeq.setTag(2, 3);`
`Â `
` // Do the same, 1-indexed.`
` scoredSeq.setTag_bio(3, 3);`
`Â `
` // Get the score at base 4, 1-indexed.`
` Integer tag = scoredSeq.getTag_bio(4);`
### Iterating over the base/score pairs
` // A 1-indexed iterator and ListIterators are also available.`
` for (Iterator``> 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`` iter = scoredSeq.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`` mySeq = SymbolListFormatter.parseSymbolList(fasta.getSequence());`
### 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();`