src/main/java/org/ncgr/gwas/SNPLifter.java
public static void main(String[] args) throws FileNotFoundException, IOException {

	if (args.length!=2) {
	    System.err.println("SNPLifter <VCF file> <snpBatch1,snpBatch2,...>");
	    System.exit(0);
	}

	String vcfFilename = args[0];
	String[] snpBatchFilenames = args[1].split(",");

	// load the snpBatch files into the rs#->chr_pos map
	Map<String,Long> snpPositionMap = new HashMap<>();
	for (String snpBatchFilename : snpBatchFilenames) {
	    BufferedReader snpBatchReader = new BufferedReader(new FileReader(snpBatchFilename));
	    String line = null;
	    while ((line=snpBatchReader.readLine())!=null) {
		if (line.startsWith("#") || line.trim().length()==0) continue; // comment
		String[] fields = line.split("\t");
		if (fields.length!=13) {
		    System.err.println("Line does not have 13 fields:");
		    System.err.println(line);
		    System.exit(1);
		}
		try {
		    snpPositionMap.put(fields[4], Long.parseLong(fields[7]));
		} catch (NumberFormatException e) {
		    // position not determined
		}
	    }
	}

	// Now spin through the VCF file spitting out replaced lines if SNP present in map.
	// 0    1               2               3       4       5       6       7       8       9        ...
	// 1	194967674	rs380390	G	C	.	.	PR	GT	0/1      ...
	// Since the order of positions can change, we need to sort the output VCF data before printing it out
	// for each chromosome.
	TreeMap<String,TreeMap<Long,String>> chrMap = new TreeMap<String,TreeMap<Long,String>>(); // keyed by chromosome
	BufferedReader vcfReader = new BufferedReader(new FileReader(vcfFilename));
	String line = null;
	while ((line=vcfReader.readLine())!=null) {
	    if (line.startsWith("#")) {
		// comments come first so dump them
		System.out.println(line);
		continue;
	    }
	    String[] fields = line.split("\t");
	    String chr = fields[0];
	    if (chr.equals("23")) chr = "X"; // ridiculous
	    if (chr.equals("24")) chr = "Y"; // more ridiculous
	    String rs = fields[2];
	    TreeMap<Long,String> posMap = chrMap.get(chr);
	    if (posMap==null) {
		posMap = new TreeMap<Long,String>(); // keyed by pos on this chr
		chrMap.put(chr, posMap);
	    }
	    if (snpPositionMap.containsKey(rs)) {
		long pos = snpPositionMap.get(rs); // the new position
		String vcfLine = chr+"\t"+pos+"\t"+rs;
		for (int i=3; i<fields.length; i++) vcfLine += "\t"+fields[i];
		posMap.put(pos, vcfLine);
	    } else {
		// rs missing, nothing we can do
	    }
	}
	// Dump out the VCF contents by increasing position per chr
	for (String chr : chrMap.keySet()) {
	    TreeMap<Long,String> posMap = chrMap.get(chr);
	    for (long pos : posMap.keySet()) {
		System.out.println(posMap.get(pos));
	    }
	}
    }
/**
     * Main class outputs a VCF with the new SNP positions.
     */
Main class outputs a VCF with the new SNP positions.
Main class outputs a VCF with the new SNP positions .
public static void main(String[] args) throws FileNotFoundException, IOException {
	if (args.length!=2) {
	    System.err.println("SNPLifter <VCF file> <snpBatch1,snpBatch2,...>");
	    System.exit(0);
	}
	String vcfFilename = args[0];
	String[] snpBatchFilenames = args[1].split(",");
	Map<String,Long> snpPositionMap = new HashMap<>();
	for (String snpBatchFilename : snpBatchFilenames) {
	    BufferedReader snpBatchReader = new BufferedReader(new FileReader(snpBatchFilename));
	    String line = null;
	    while ((line=snpBatchReader.readLine())!=null) {
		if (line.startsWith("#") || line.trim().length()==0) continue; 
		String[] fields = line.split("\t");
		if (fields.length!=13) {
		    System.err.println("Line does not have 13 fields:");
		    System.err.println(line);
		    System.exit(1);
		}
		try {
		    snpPositionMap.put(fields[4], Long.parseLong(fields[7]));
		} catch (NumberFormatException e) {
		}
	    }
	}
	TreeMap<String,TreeMap<Long,String>> chrMap = new TreeMap<String,TreeMap<Long,String>>(); 
	BufferedReader vcfReader = new BufferedReader(new FileReader(vcfFilename));
	String line = null;
	while ((line=vcfReader.readLine())!=null) {
	    if (line.startsWith("#")) {
		s come first so dump them
		System.out.println(line);
		continue;
	    }
	    String[] fields = line.split("\t");
	    String chr = fields[0];
	    if (chr.equals("23")) chr = "X"; 
	    if (chr.equals("24")) chr = "Y"; 
	    String rs = fields[2];
	    TreeMap<Long,String> posMap = chrMap.get(chr);
	    if (posMap==null) {
		posMap = new TreeMap<Long,String>(); 
		chrMap.put(chr, posMap);
	    }
	    if (snpPositionMap.containsKey(rs)) {
		long pos = snpPositionMap.get(rs); 
		String vcfLine = chr+"\t"+pos+"\t"+rs;
		for (int i=3; i<fields.length; i++) vcfLine += "\t"+fields[i];
		posMap.put(pos, vcfLine);
	    } else {
	    }
	}
	for (String chr : chrMap.keySet()) {
	    TreeMap<Long,String> posMap = chrMap.get(chr);
	    for (long pos : posMap.keySet()) {
		System.out.println(posMap.get(pos));
	    }
	}
    }
public static void main ( String [ ] args ) throws FileNotFoundException , IOException { if ( args . length != 2 ) { System . err . println ( "SNPLifter <VCF file> <snpBatch1,snpBatch2,...>" ) ; System . exit ( 0 ) ; } String vcfFilename = args [ 0 ] ; String [ ] snpBatchFilenames = args [ 1 ] . split ( "," ) ; Map < String , Long > snpPositionMap = new HashMap < > ( ) ; for ( String snpBatchFilename : snpBatchFilenames ) { BufferedReader snpBatchReader = new BufferedReader ( new FileReader ( snpBatchFilename ) ) ; String line = null ; while ( ( line = snpBatchReader . readLine ( ) ) != null ) { if ( line . startsWith ( "#" ) || line . trim ( ) . length ( ) == 0 ) continue ; String [ ] fields = line . split ( "\t" ) ; if ( fields . length != 13 ) { System . err . println ( "Line does not have 13 fields:" ) ; System . err . println ( line ) ; System . exit ( 1 ) ; } try { snpPositionMap . put ( fields [ 4 ] , Long . parseLong ( fields [ 7 ] ) ) ; } catch ( NumberFormatException e ) { } } } TreeMap < String , TreeMap < Long , String > > chrMap = new TreeMap < String , TreeMap < Long , String > > ( ) ; BufferedReader vcfReader = new BufferedReader ( new FileReader ( vcfFilename ) ) ; String line = null ; while ( ( line = vcfReader . readLine ( ) ) != null ) { if ( line . startsWith ( "#" ) ) { System . out . println ( line ) ; continue ; } String [ ] fields = line . split ( "\t" ) ; String chr = fields [ 0 ] ; if ( chr . equals ( "23" ) ) chr = "X" ; if ( chr . equals ( "24" ) ) chr = "Y" ; String rs = fields [ 2 ] ; TreeMap < Long , String > posMap = chrMap . get ( chr ) ; if ( posMap == null ) { posMap = new TreeMap < Long , String > ( ) ; chrMap . put ( chr , posMap ) ; } if ( snpPositionMap . containsKey ( rs ) ) { long pos = snpPositionMap . get ( rs ) ; String vcfLine = chr + "\t" + pos + "\t" + rs ; for ( int i = 3 ; i < fields . length ; i ++ ) vcfLine += "\t" + fields [ i ] ; posMap . put ( pos , vcfLine ) ; } else { } } for ( String chr : chrMap . keySet ( ) ) { TreeMap < Long , String > posMap = chrMap . get ( chr ) ; for ( long pos : posMap . keySet ( ) ) { System . out . println ( posMap . get ( pos ) ) ; } } }
Main class outputs a VCF with the new SNP positions.
Main class outputs a VCF with the new SNP positions .
// load the snpBatch files into the rs#->chr_pos map // comment // position not determined // Now spin through the VCF file spitting out replaced lines if SNP present in map. // 0    1               2               3       4       5       6       7       8       9        ... // 1	194967674	rs380390	G	C	.	.	PR	GT	0/1      ... // Since the order of positions can change, we need to sort the output VCF data before printing it out // for each chromosome. // keyed by chromosome // comments come first so dump them // ridiculous // more ridiculous // keyed by pos on this chr // the new position // rs missing, nothing we can do // Dump out the VCF contents by increasing position per chr