1616
1717package org .labkey .mgap ;
1818
19+ import au .com .bytecode .opencsv .CSVReader ;
1920import htsjdk .samtools .util .CloseableIterator ;
2021import htsjdk .variant .variantcontext .VariantContext ;
2122import htsjdk .variant .vcf .VCFFileReader ;
5152import org .labkey .api .query .BatchValidationException ;
5253import org .labkey .api .query .DetailsURL ;
5354import org .labkey .api .query .FieldKey ;
55+ import org .labkey .api .query .QueryAction ;
5456import org .labkey .api .query .QueryService ;
5557import org .labkey .api .query .QueryUpdateService ;
5658import org .labkey .api .query .UserSchema ;
59+ import org .labkey .api .reader .Readers ;
5760import org .labkey .api .security .AuthenticationManager ;
5861import org .labkey .api .security .Group ;
5962import org .labkey .api .security .GroupManager ;
99102import java .io .File ;
100103import java .io .FileInputStream ;
101104import java .io .InputStream ;
105+ import java .net .URL ;
102106import java .util .ArrayList ;
103107import java .util .Arrays ;
104108import java .util .Collection ;
@@ -1045,4 +1049,78 @@ public Collection<String> getTracks(Container target, String jbrowseSession, Str
10451049 return ret ;
10461050 }
10471051 }
1052+
1053+ @ RequiresPermission (AdminPermission .class )
1054+ public static class UpdateAnnotationsAction extends ConfirmAction <Object >
1055+ {
1056+ @ Override
1057+ public ModelAndView getConfirmView (Object o , BindException errors ) throws Exception
1058+ {
1059+ setTitle ("Update Annotation Table" );
1060+
1061+ HtmlView view = new HtmlView ("This will update the annotation table using the VariantAnnotation github repo. Do you want to continue?" );
1062+ return view ;
1063+ }
1064+
1065+ @ Override
1066+ public boolean handlePost (Object o , BindException errors ) throws Exception
1067+ {
1068+ List <Map <String , Object >> toAdd = new ArrayList <>();
1069+
1070+ final URL url = new URL ("https://raw.githubusercontent.com/bimberlabinternal/VariantAnnotation/master/fieldConfig.txt" );
1071+ try (CSVReader reader = new CSVReader (Readers .getReader (url .openStream ()), '\t' ))
1072+ {
1073+ String [] line ;
1074+ List <String > header = null ;
1075+ int idx = 0 ;
1076+ while ((line = reader .readNext ()) != null )
1077+ {
1078+ idx ++;
1079+ if (idx == 1 )
1080+ {
1081+ header = Arrays .asList (line );
1082+ continue ;
1083+ }
1084+
1085+ Map <String , Object > row = new CaseInsensitiveHashMap <>();
1086+
1087+ row .put ("category" , line [header .indexOf ("Category" )]);
1088+ row .put ("label" , line [header .indexOf ("Label" )]);
1089+ row .put ("dataSource" , line [header .indexOf ("DataSource" )]);
1090+ row .put ("infoKey" , line [header .indexOf ("ID" )]);
1091+ row .put ("dataType" , line [header .indexOf ("Type" )]);
1092+ row .put ("dataNumber" , line [header .indexOf ("Number" )]);
1093+ row .put ("description" , line [header .indexOf ("Description" )]);
1094+ row .put ("url" , line [header .indexOf ("URL" )]);
1095+
1096+ toAdd .add (row );
1097+ }
1098+ }
1099+
1100+ UserSchema us = QueryService .get ().getUserSchema (getUser (), getContainer (), mGAPSchema .NAME );
1101+ TableInfo ti = us .getTable (mGAPSchema .TABLE_VARIANT_ANNOTATIONS );
1102+ ti .getUpdateService ().truncateRows (getUser (), getContainer (), null , null );
1103+
1104+ BatchValidationException bve = new BatchValidationException ();
1105+ ti .getUpdateService ().insertRows (getUser (), getContainer (), toAdd , bve , null , null );
1106+ if (bve .hasErrors ())
1107+ {
1108+ throw bve ;
1109+ }
1110+
1111+ return true ;
1112+ }
1113+
1114+ @ Override
1115+ public void validateCommand (Object o , Errors errors )
1116+ {
1117+
1118+ }
1119+
1120+ @ Override
1121+ public @ NotNull URLHelper getSuccessURL (Object o )
1122+ {
1123+ return QueryService .get ().urlFor (getUser (), getContainer (), QueryAction .executeQuery , mGAPSchema .NAME , mGAPSchema .TABLE_VARIANT_ANNOTATIONS );
1124+ }
1125+ }
10481126}
0 commit comments