Newer
Older
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
/* IOUtils
*
* created: 2010
*
* This file is part of Artemis
*
* Copyright(C) 2010 Genome Research Limited
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or(at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*/
package uk.ac.sanger.artemis.components.variant;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.List;
import javax.swing.JOptionPane;
import uk.ac.sanger.artemis.EntryGroup;
import uk.ac.sanger.artemis.Feature;
import uk.ac.sanger.artemis.FeatureEnumeration;
import uk.ac.sanger.artemis.FeatureKeyQualifierPredicate;
import uk.ac.sanger.artemis.FeatureVector;
import uk.ac.sanger.artemis.components.MessageDialog;
import uk.ac.sanger.artemis.io.Key;
import net.sf.samtools.util.BlockCompressedInputStream;
class IOUtils
/**
* Write filtered uncompressed VCF. Uses the filter in VCFview to
* determine if variants are written.
* @param vcfFileName
* @param writer
* @param vcfView
* @param features
*/
protected static void writeVCF(final String vcfFileName,
final Writer writer,
final VCFview vcfView,
final FeatureVector features)
{
try
{
TabixReader tr = new TabixReader(vcfFileName);
String line;
while ((line = tr.readLine()) != null)
{
if(line.startsWith("#"))
String parts[] = VCFview.tabPattern.split(line, 0);
int basePosition = Integer.parseInt(parts[1]) + vcfView.getSequenceOffset(parts[0]);
if( !vcfView.showVariant(parts[3], parts[4], features, basePosition, parts[5]) )
continue;
writer.write(line+'\n');
}
writer.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
/**
* Export as a VCF based on the filtering applied in the VCFview.
* @param entryGroup
* @param vcfFiles
* @param vcfView
*/
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
protected static void export(final EntryGroup entryGroup,
final List<String> vcfFiles,
final VCFview vcfView)
{
// select all CDS features that do not have the /pseudo qualifier
final FeatureKeyQualifierPredicate predicate =
new FeatureKeyQualifierPredicate(Key.CDS, "pseudo", false);
final FeatureVector features = new FeatureVector ();
final FeatureEnumeration feature_enum = entryGroup.features ();
while (feature_enum.hasMoreFeatures ())
{
final Feature current_feature = feature_enum.nextFeature ();
if (predicate.testPredicate (current_feature))
features.add (current_feature);
}
try
{
String filterFiles = "";
for(int i=0; i<vcfFiles.size(); i++)
{
FileWriter writer = new FileWriter(vcfFiles.get(i)+".filter");
IOUtils.writeVCF(vcfFiles.get(i), writer, vcfView, features);
filterFiles += vcfFiles.get(i)+".filter\n";
}
new MessageDialog (null, "Saved Files", filterFiles, false);
}
catch (IOException e1)
{
JOptionPane.showMessageDialog(vcfView, e1.getMessage(),
"Error", JOptionPane.ERROR_MESSAGE);
}
}
/**
* Test if this is a BCF file.
* @param fileName
* @return
* @throws IOException
*/
protected static boolean isBCF(String fileName) throws IOException
{
FileInputStream fis = new FileInputStream(fileName);
BlockCompressedInputStream is = new BlockCompressedInputStream(fis);
byte[] magic = new byte[4];
is.read(magic);
fis.close();
is.close();
String line = new String(magic);
if(line.equals("BCF\4"))
return true;
return false;
}