-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtilities.java
More file actions
60 lines (50 loc) · 1.76 KB
/
Utilities.java
File metadata and controls
60 lines (50 loc) · 1.76 KB
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
/*
* Peter Edge
* CSCI 5481 Final Project
*
* This file primarily contains a fasta scanning helper function
*/
import java.io.FileNotFoundException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.io.File;
import java.util.Scanner;
public class Utilities {
// utility for scanning fasta file format.
// simply takes a fasta filename
// returns a list of fasta objects
public static ArrayList<Fasta> fastaScan (String fastaFileName) throws FileNotFoundException{
File fasta = new File(fastaFileName);
Scanner scanner = new Scanner(fasta);
scanner.useDelimiter("\n");
String fileExtension = fastaFileName.substring(fastaFileName.lastIndexOf('.'));
// the return list
ArrayList<Fasta> seqList = new ArrayList<Fasta>();
String line = "";
String header = "";
String seq = "";
boolean readingSequence = false;
if (fileExtension.equals(".fasta")||fileExtension.equals(".fa")){
while(scanner.hasNext()){
line = scanner.nextLine();
// if a new entry
if (line.contains(">")){
if (readingSequence){
seqList.add(new Fasta(header, seq, false));
}
header = line;
seq = "";
readingSequence = true;
// else if continuing a previous entry
}else if (readingSequence){
seq += line;
}
}
}else{
System.out.println("Error: non-Fasta input");
}
// add in the last fasta
seqList.add(new Fasta(header, seq, false));
return seqList;
}
}