Skip to content

Commit d278917

Browse files
Converted all core.sequence.io print and printStackTrace to slf4j biojava#155
1 parent 081d7a9 commit d278917

7 files changed

Lines changed: 83 additions & 53 deletions

File tree

biojava3-core/src/main/java/org/biojava3/core/sequence/io/CasePreservingProteinSequenceCreator.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
import org.biojava3.core.sequence.template.CompoundSet;
1313
import org.biojava3.core.sequence.template.ProxySequenceReader;
1414

15+
import org.slf4j.Logger;
16+
import org.slf4j.LoggerFactory;
17+
1518
/**
1619
* A sequence creator which preserves the case of its input string in
1720
* the user collection of the returned ProteinSequence.
@@ -30,6 +33,8 @@
3033
*/
3134
public class CasePreservingProteinSequenceCreator extends ProteinSequenceCreator {
3235

36+
private final static Logger logger = LoggerFactory.getLogger(CasePreservingProteinSequenceCreator.class);
37+
3338
public CasePreservingProteinSequenceCreator(
3439
CompoundSet<AminoAcidCompound> compoundSet) {
3540
super(compoundSet);
@@ -94,8 +99,8 @@ private static List<Object> getStringCase(String str) {
9499
public static void main(String[] args) {
95100
CasePreservingProteinSequenceCreator creator = new CasePreservingProteinSequenceCreator(AminoAcidCompoundSet.getAminoAcidCompoundSet());
96101
AbstractSequence<AminoAcidCompound> seq = creator.getSequence("aaAA",0);
97-
System.out.println(seq.getSequenceAsString()); //"AAAA"
98-
System.out.println(seq.getUserCollection()); //"[false, false, true, true]"
102+
logger.info("Sequence: {}", seq.getSequenceAsString()); //"AAAA"
103+
logger.info("User Collection: {}", seq.getUserCollection()); //"[false, false, true, true]"
99104
}
100105

101106
/**

biojava3-core/src/main/java/org/biojava3/core/sequence/io/FastaGeneWriter.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@
3434
import org.biojava3.core.sequence.compound.NucleotideCompound;
3535
import org.biojava3.core.sequence.io.template.FastaHeaderFormatInterface;
3636

37+
import org.slf4j.Logger;
38+
import org.slf4j.LoggerFactory;
39+
3740
/**
3841
* A Gene sequence has a Positive or Negative Strand where we want to write out to a stream the 5 to 3 prime version.
3942
* It is also an option to write out the gene sequence where the exon regions are upper case
@@ -42,6 +45,8 @@
4245
*/
4346
public class FastaGeneWriter {
4447

48+
private final static Logger logger = LoggerFactory.getLogger(FastaGeneWriter.class);
49+
4550
boolean showExonUppercase = false;
4651
OutputStream os;
4752
Collection<GeneSequence> sequences;
@@ -105,7 +110,7 @@ public void process() throws Exception {
105110
featureBioEnd = geneBioEnd - exonSequence.getBioBegin();
106111
}
107112
if (featureBioBegin < 0 || featureBioEnd < 0 || featureBioEnd > sb.length() || featureBioBegin > sb.length()) {
108-
System.out.println("Bad Feature " + sequence.getAccession().toString() + " " + sequence.getStrand() + " " + geneBioBegin + " " + geneBioEnd + " " + exonSequence.getBioBegin() + " " + exonSequence.getBioEnd());
113+
logger.warn("Bad Feature, Accession: {}, Sequence Strand: {}, Gene Begin: {}, Gene End: {}, Exon Begin: {}, Exon End: {}", sequence.getAccession().toString(), sequence.getStrand(), geneBioBegin, geneBioEnd, exonSequence.getBioBegin(), exonSequence.getBioEnd());
109114
} else {
110115
for (int i = featureBioBegin; i <= featureBioEnd; i++) {
111116
char ch = sb.charAt(i);
@@ -175,7 +180,7 @@ public static void main(String[] args) {
175180

176181

177182
} catch (Exception e) {
178-
e.printStackTrace();
183+
logger.warn("Exception: ", e);
179184
}
180185
}
181186
}

biojava3-core/src/main/java/org/biojava3/core/sequence/io/FastaReader.java

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
import org.biojava3.core.sequence.io.template.SequenceCreatorInterface;
3838
import org.biojava3.core.sequence.template.Compound;
3939
import org.biojava3.core.sequence.template.Sequence;
40+
import org.slf4j.Logger;
41+
import org.slf4j.LoggerFactory;
4042

4143
/**
4244
* Use FastaReaderHelper as an example of how to use this class where FastaReaderHelper should be the
@@ -45,6 +47,8 @@
4547
*/
4648
public class FastaReader<S extends Sequence<?>, C extends Compound> {
4749

50+
private final static Logger logger = LoggerFactory.getLogger(FastaReader.class);
51+
4852
SequenceCreatorInterface<C> sequenceCreator;
4953
SequenceHeaderParserInterface<S,C> headerParser;
5054
BufferedReaderBytesRead br;
@@ -151,7 +155,7 @@ public LinkedHashMap<String,S> process(int max) throws IOException {
151155
if (line.length() != 0) {
152156
if (line.startsWith(">")) {//start of new fasta record
153157
if (sb.length() > 0) {//i.e. if there is already a sequence before
154-
// System.out.println("Sequence index=" + sequenceIndex);
158+
// logger.debug("Sequence index=" + sequenceIndex);
155159
@SuppressWarnings("unchecked")
156160
S sequence = (S)sequenceCreator.getSequence(sb.toString(), sequenceIndex);
157161
headerParser.parseHeader(header, sequence);
@@ -176,14 +180,14 @@ public LinkedHashMap<String,S> process(int max) throws IOException {
176180
fileIndex = br.getBytesRead();
177181
line = br.readLine();
178182
if (line == null) {//i.e. EOF
179-
@SuppressWarnings("unchecked")
180183
String seq = sb.toString();
181184
if ( seq.length() == 0) {
182-
System.err.println("Can't parse sequence " + sequenceIndex + ". Got sequence of length 0!");
183-
System.err.println("header: " + header);
185+
logger.warn("Can't parse sequence {}. Got sequence of length 0!", sequenceIndex);
186+
logger.warn("header: {}", header);
184187
}
185-
// System.out.println("Sequence index=" + sequenceIndex + " " + fileIndex );
186-
S sequence = (S)sequenceCreator.getSequence(seq, sequenceIndex);
188+
// logger.debug("Sequence index=" + sequenceIndex + " " + fileIndex );
189+
@SuppressWarnings("unchecked")
190+
S sequence = (S)sequenceCreator.getSequence(seq, sequenceIndex);
187191
headerParser.parseHeader(header, sequence);
188192
sequences.put(sequence.getAccession().getID(),sequence);
189193
processedSequences++;
@@ -218,7 +222,7 @@ public static void main(String[] args) {
218222
is.close();
219223

220224

221-
System.out.println(proteinSequences);
225+
logger.info("Protein Sequences: {}", proteinSequences);
222226

223227
File file = new File(inputFile);
224228
FastaReader<ProteinSequence,AminoAcidCompound> fastaProxyReader =
@@ -235,16 +239,16 @@ public static void main(String[] args) {
235239

236240
for(String key : proteinProxySequences.keySet()){
237241
ProteinSequence proteinSequence = proteinProxySequences.get(key);
238-
System.out.println(key);
242+
logger.info("Protein Proxy Sequence Key: {}", key);
239243
// if(key.equals("Q98SJ1_CHICK/15-61")){
240244
// int dummy = 1;
241245
// }
242-
System.out.println(proteinSequence.toString());
246+
logger.info("Protein Sequence: {}", proteinSequence.toString());
243247

244248
}
245249

246250
} catch (Exception e) {
247-
e.printStackTrace();
251+
logger.warn("Exception: ", e);
248252
}
249253
}
250254
}

biojava3-core/src/main/java/org/biojava3/core/sequence/io/FastaWriter.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
import org.biojava3.core.sequence.io.template.FastaHeaderFormatInterface;
3535
import org.biojava3.core.sequence.template.Compound;
3636
import org.biojava3.core.sequence.template.Sequence;
37+
import org.slf4j.Logger;
38+
import org.slf4j.LoggerFactory;
3739

3840
/**
3941
* The FastaWriter writes a collection of sequences to an outputStream. FastaWriterHelper should be
@@ -45,6 +47,8 @@
4547
*/
4648
public class FastaWriter<S extends Sequence<?>, C extends Compound> {
4749

50+
private final static Logger logger = LoggerFactory.getLogger(FastaWriter.class);
51+
4852
OutputStream os;
4953
Collection<S> sequences;
5054
FastaHeaderFormatInterface<S, C> headerFormat;
@@ -133,7 +137,7 @@ public static void main(String[] args) {
133137
is.close();
134138

135139

136-
// System.out.println(proteinSequences);
140+
// logger.debug(proteinSequences);
137141

138142
FileOutputStream fileOutputStream = new FileOutputStream("/Users/Scooter/scripps/dyadic/c1-454Scaffolds_temp.faa");
139143

@@ -143,13 +147,13 @@ public static void main(String[] args) {
143147
fastaWriter.process();
144148
bo.close();
145149
long end = System.currentTimeMillis();
146-
System.out.println("Took " + (end - start) + " seconds");
150+
logger.info("Took {} seconds", (end - start));
147151

148152
fileOutputStream.close();
149153

150154

151155
} catch (Exception e) {
152-
e.printStackTrace();
156+
logger.warn("Exception: ", e);
153157
}
154158
}
155159

biojava3-core/src/main/java/org/biojava3/core/sequence/io/GenbankReader.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444
import org.biojava3.core.sequence.io.template.SequenceHeaderParserInterface;
4545
import org.biojava3.core.sequence.template.AbstractSequence;
4646
import org.biojava3.core.sequence.template.Compound;
47+
import org.slf4j.Logger;
48+
import org.slf4j.LoggerFactory;
4749

4850
/**
4951
* Use GenbankReaderHelper as an example of how to use this class where GenbankReaderHelper should be the
@@ -52,6 +54,8 @@
5254
*/
5355
public class GenbankReader<S extends AbstractSequence<C>, C extends Compound> {
5456

57+
private final static Logger logger = LoggerFactory.getLogger(GenbankReader.class);
58+
5559
private SequenceCreatorInterface<C> sequenceCreator;
5660
private GenbankSequenceParser<S,C> genbankParser;
5761
private InputStream inputStream;
@@ -150,14 +154,14 @@ public static void main(String[] args) throws Exception {
150154

151155
GenbankReader<ProteinSequence, AminoAcidCompound> proteinReader = new GenbankReader<ProteinSequence, AminoAcidCompound>(is, new GenericGenbankHeaderParser<ProteinSequence,AminoAcidCompound>(), new ProteinSequenceCreator(AminoAcidCompoundSet.getAminoAcidCompoundSet()));
152156
LinkedHashMap<String,ProteinSequence> proteinSequences = proteinReader.process();
153-
System.out.println(proteinSequences);
157+
logger.info("Protein Sequences: {}", proteinSequences);
154158

155159
String inputFile = "src/test/resources/NM_000266.gb";
156160
is = new FileInputStream(inputFile);
157161
GenbankReader<DNASequence, NucleotideCompound> dnaReader = new GenbankReader<DNASequence, NucleotideCompound>(is, new GenericGenbankHeaderParser<DNASequence,NucleotideCompound>(), new DNASequenceCreator(DNACompoundSet.getDNACompoundSet()));
158162
LinkedHashMap<String,DNASequence> dnaSequences = dnaReader.process();
159163
is.close();
160-
System.out.println(dnaSequences);
164+
logger.info("DNA Sequences: {}", dnaSequences);
161165
}
162166

163167
}

biojava3-core/src/main/java/org/biojava3/core/sequence/io/GenbankReaderHelper.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,25 @@
2525
import java.io.FileInputStream;
2626
import java.io.InputStream;
2727
import java.util.LinkedHashMap;
28+
2829
import org.biojava3.core.sequence.DNASequence;
2930
import org.biojava3.core.sequence.ProteinSequence;
30-
3131
import org.biojava3.core.sequence.compound.AminoAcidCompound;
3232
import org.biojava3.core.sequence.compound.AminoAcidCompoundSet;
3333
import org.biojava3.core.sequence.compound.DNACompoundSet;
3434
import org.biojava3.core.sequence.compound.NucleotideCompound;
3535
import org.biojava3.core.sequence.template.AbstractSequence;
36+
import org.slf4j.Logger;
37+
import org.slf4j.LoggerFactory;
3638

3739
/**
3840
*
3941
* @author Scooter Willis <willishf at gmail dot com>
4042
*/
4143
public class GenbankReaderHelper {
4244

45+
private final static Logger logger = LoggerFactory.getLogger(GenbankReaderHelper.class);
46+
4347
/**
4448
* Selecting lazySequenceLoad=true will parse the Genbank file and figure out the accessionid and offsets and return sequence objects
4549
* that can in the future read the sequence from the disk. This allows the loading of large Genbank files where you are only interested
@@ -161,12 +165,12 @@ public static void main(String args[]) throws Exception {
161165

162166
LinkedHashMap<String, DNASequence> dnaSequences = GenbankReaderHelper.readGenbankDNASequence(new File("src/test/resources/NM_000266.gb"), true);
163167
for (DNASequence sequence : dnaSequences.values()) {
164-
System.out.println( sequence.getRNASequence().getProteinSequence().getSequenceAsString() );
168+
logger.info("DNA Sequence: {}", sequence.getRNASequence().getProteinSequence().getSequenceAsString());
165169
}
166170

167171
LinkedHashMap<String, ProteinSequence> proteinSequences = GenbankReaderHelper.readGenbankProteinSequence(new File("src/test/resources/BondFeature.gb"), true);
168172
for (ProteinSequence sequence : proteinSequences.values()) {
169-
System.out.println( sequence.getSequenceAsString() );
173+
logger.info("Protein Sequence: {}", sequence.getSequenceAsString());
170174
}
171175
}
172176
}

0 commit comments

Comments
 (0)