Skip to content

Commit 0501615

Browse files
committed
Merge branch 'bugfixes-4.2'
2 parents 68687a0 + 99fda9d commit 0501615

File tree

21 files changed

+804
-370
lines changed

21 files changed

+804
-370
lines changed

biojava-core/src/main/java/org/biojava/nbio/core/alignment/template/AlignedSequence.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,4 +158,20 @@ enum Step { COMPOUND, GAP }
158158
*/
159159
boolean isGap(int alignmentIndex);
160160

161+
/**
162+
* Returns number of gap positions (gap openings and extensions) in the sequence. This could be determined from the {@link Location}
163+
* information or from gap {@link Compound}s, which may not necessarily result in the same number.
164+
*
165+
* @return number of gap positions in the sequence
166+
*/
167+
int getNumGapPositions();
168+
169+
/**
170+
* Returns the coverage, as a fraction between 0 and 1, of this {@link AlignedSequence} with respect to the original sequence.
171+
* This is equivalent to ({@link #getLength()} - {@link #getNumGapPositions()}) / getOriginalSequence().getLength().
172+
*
173+
* @return coverage of the original sequence by the aligned sequence
174+
*/
175+
double getCoverage();
176+
161177
}

biojava-core/src/main/java/org/biojava/nbio/core/sequence/loader/UniprotProxySequenceReader.java

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,61 @@ private void writeCache(StringBuilder sb, String accession) throws IOException {
495495
fw.write(sb.toString());
496496
fw.close();
497497
}
498+
499+
/**
500+
* Open a URL connection.
501+
*
502+
* Follows redirects.
503+
* @param url
504+
* @throws IOException
505+
*/
506+
private static HttpURLConnection openURLConnection(URL url) throws IOException {
507+
// This method should be moved to a utility class in BioJava 5.0
508+
509+
final int timeout = 5000;
510+
final String useragent = "BioJava";
511+
512+
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
513+
conn.setRequestProperty("User-Agent", useragent);
514+
conn.setInstanceFollowRedirects(true);
515+
conn.setConnectTimeout(timeout);
516+
conn.setReadTimeout(timeout);
517+
518+
int status = conn.getResponseCode();
519+
while (status == HttpURLConnection.HTTP_MOVED_TEMP
520+
|| status == HttpURLConnection.HTTP_MOVED_PERM
521+
|| status == HttpURLConnection.HTTP_SEE_OTHER) {
522+
// Redirect!
523+
String newUrl = conn.getHeaderField("Location");
524+
525+
if(newUrl.equals(url.toString())) {
526+
throw new IOException("Cyclic redirect detected at "+newUrl);
527+
}
528+
529+
// Preserve cookies
530+
String cookies = conn.getHeaderField("Set-Cookie");
531+
532+
// open the new connection again
533+
url = new URL(newUrl);
534+
conn.disconnect();
535+
conn = (HttpURLConnection) url.openConnection();
536+
if(cookies != null) {
537+
conn.setRequestProperty("Cookie", cookies);
538+
}
539+
conn.addRequestProperty("User-Agent", useragent);
540+
conn.setInstanceFollowRedirects(true);
541+
conn.setConnectTimeout(timeout);
542+
conn.setReadTimeout(timeout);
543+
conn.connect();
544+
545+
status = conn.getResponseCode();
546+
547+
logger.info("Redirecting from {} to {}", url, newUrl);
548+
}
549+
conn.connect();
550+
551+
return conn;
552+
}
498553

499554
private StringBuilder fetchUniprotXML(String uniprotURL)
500555
throws IOException, CompoundNotFoundException {
@@ -504,11 +559,9 @@ private StringBuilder fetchUniprotXML(String uniprotURL)
504559
int attempt = 5;
505560
List<String> errorCodes = new ArrayList<String>();
506561
while(attempt > 0) {
507-
HttpURLConnection uniprotConnection = (HttpURLConnection) uniprot.openConnection();
508-
uniprotConnection.setRequestProperty("User-Agent", "BioJava");
509-
uniprotConnection.connect();
562+
HttpURLConnection uniprotConnection = openURLConnection(uniprot);
510563
int statusCode = uniprotConnection.getResponseCode();
511-
if (statusCode == 200) {
564+
if (statusCode == HttpURLConnection.HTTP_OK) {
512565
BufferedReader in = new BufferedReader(
513566
new InputStreamReader(
514567
uniprotConnection.getInputStream()));

biojava-core/src/main/java/org/biojava/nbio/core/util/FileDownloadUtils.java

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@
2121
*/
2222
package org.biojava.nbio.core.util;
2323

24-
import org.slf4j.Logger;
25-
import org.slf4j.LoggerFactory;
26-
2724
import java.io.File;
2825
import java.io.FileInputStream;
2926
import java.io.FileOutputStream;
@@ -36,6 +33,15 @@
3633
import java.nio.channels.Channels;
3734
import java.nio.channels.FileChannel;
3835
import java.nio.channels.ReadableByteChannel;
36+
import java.nio.file.FileVisitResult;
37+
import java.nio.file.Files;
38+
import java.nio.file.Path;
39+
import java.nio.file.Paths;
40+
import java.nio.file.SimpleFileVisitor;
41+
import java.nio.file.attribute.BasicFileAttributes;
42+
43+
import org.slf4j.Logger;
44+
import org.slf4j.LoggerFactory;
3945

4046
public class FileDownloadUtils {
4147

@@ -240,6 +246,41 @@ public static URLConnection prepareURLConnection(String url, int timeout) throws
240246
connection.setConnectTimeout(timeout);
241247
return connection;
242248
}
249+
250+
/**
251+
* Recursively delete a folder & contents
252+
*
253+
* @param dir directory to delete
254+
*/
255+
public static void deleteDirectory(Path dir) throws IOException {
256+
if(dir == null || !Files.exists(dir))
257+
return;
258+
Files.walkFileTree(dir, new SimpleFileVisitor<Path>() {
259+
@Override
260+
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
261+
Files.delete(file);
262+
return FileVisitResult.CONTINUE;
263+
}
264+
265+
@Override
266+
public FileVisitResult postVisitDirectory(Path dir, IOException e) throws IOException {
267+
if (e != null) {
268+
throw e;
269+
}
270+
Files.delete(dir);
271+
return FileVisitResult.CONTINUE;
272+
}
273+
});
274+
}
275+
/**
276+
* Recursively delete a folder & contents
277+
*
278+
* @param dir directory to delete
279+
*/
280+
public static void deleteDirectory(String dir) throws IOException {
281+
deleteDirectory(Paths.get(dir));
282+
}
283+
243284

244285
public static void main(String[] args) {
245286
String url;

biojava-genome/src/main/java/org/biojava/nbio/genome/util/ChromosomeMappingTools.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ public static ChromPos getChromPosForward(int cdsPos, List<Integer> exonStarts,
417417
int codingLength = 0;
418418

419419
@SuppressWarnings("unused")
420-
int lengthExons = 0;
420+
int lengthExons = 0;
421421
// map forward
422422
for (int i = 0; i < exonStarts.size(); i++) {
423423

@@ -565,7 +565,7 @@ public static int getCDSLengthForward(List<Integer> exonStarts, List<Integer> ex
565565

566566
codingLength += (end - start + 1);
567567
}
568-
return codingLength - 3;
568+
return codingLength-3 ;
569569
}
570570

571571
/**
@@ -791,19 +791,19 @@ public static int getCDSPosForChromosomeCoordinate(int coordinate, GeneChromosom
791791
chromosomePosition.getCdsStart(),
792792
chromosomePosition.getCdsEnd());
793793
}
794-
795-
/**
796-
* Converts the genetic coordinate to the position of the nucleotide on the mRNA sequence for a gene
794+
795+
/**
796+
* Converts the genetic coordinate to the position of the nucleotide on the mRNA sequence for a gene
797797
* living on the forward DNA strand.
798-
*
799-
* @param chromPos The genetic coordinate on a chromosome
800-
* @param exonStarts The list holding the genetic coordinates pointing to the start positions of the exons (including UTR regions)
798+
*
799+
* @param chromPos The genetic coordinate on a chromosome
800+
* @param exonStarts The list holding the genetic coordinates pointing to the start positions of the exons (including UTR regions)
801801
* @param exonEnds The list holding the genetic coordinates pointing to the end positions of the exons (including UTR regions)
802802
* @param cdsStart The start position of a coding region
803803
* @param cdsEnd The end position of a coding region
804-
*
804+
*
805805
* @return the position of the nucleotide base on the mRNA sequence corresponding to the input genetic coordinate (base 1)
806-
*
806+
*
807807
* @author Yana Valasatava
808808
*/
809809
public static int getCDSPosForward(int chromPos, List<Integer> exonStarts, List<Integer> exonEnds,

0 commit comments

Comments
 (0)